TonyD
TonyD

Reputation: 15

Linux - Loop User input in CSH

I need to Loop user input for up to 10 times or until the user presses "n" and then echo out the list of words entered. When I run this I get the message: Directory stack not that deep.

#!/bin/csh

echo "Enter Word"
set buffer = ("" "" "" "" "" "" "" "" "" "")
set count = 0
set argument = $<

while ($count <=10 && $argument != "n")
set buffer[$count] = $argument
@ count++
if ($argument = "n")
break
endif
set buffer [$count] = $argument
@ count++`enter code here`
end
echo $buffer

Upvotes: 1

Views: 1290

Answers (1)

F. Hauri  - Give Up GitHub
F. Hauri - Give Up GitHub

Reputation: 70947

1st try

That's my 1st script:

#!/bin/csh

set buffer = ('' '' '' '' '' '' '' '' '' '')
set count = 1
echo -n "Enter 1st Word: "
set argument = $<

while ($count <= 10 && $argument != "n")
    set buffer[$count] = $argument
    @ count++
    if ($count <= 10) then
    echo -n "Enter Word #"$count": "
    set argument = $<
    endif
end

set repl = 1
while ($repl < $count)
    printf " %2d  %s\n" $repl $buffer[$repl]
    @ repl++
end

echo $buffer
echo "$buffer"

Play:

Enter 1st Word: Hello
Enter Word #2: good
Enter Word #3: world!
Enter Word #4: 
Enter Word #5: End
Enter Word #6: now.
Enter Word #7: n
  1  Hello
  2  good
  3  world!
  4  
  5  End
  6  now.
Hello good world! End now.
Hello good world!  End now.

Or to iterate over arguments on same input

#!/bin/csh

set buffer = ('' '' '' '' '' '' '' '' '' '')
set count = 1
echo -n "Enter words: "
set argument = $<
set array = ($argument)

while ($count <= 10 && $array[1] != "n" && $#array > 0)
    set buffer[$count] = $array[1]
    @ count++
    if ($count <= 10 && $#array > 1) then
    shift array
    else
    break
    endif
end

set repl = 1
while ($repl < $count)
    printf " %2d  %s\n" $repl $buffer[$repl]
    @ repl++
end

echo $buffer
echo "$buffer"

Run:

Enter words: Hello good world. n Trick test
  1  Hello
  2  good
  3  world.
Hello good world.
Hello good world.

Upvotes: 2

Related Questions