Linux - CSH中的循环用户输入

时间:2016-11-06 01:16:58

标签: linux csh

我需要循环用户输入最多10次,或者直到用户按下" n"然后回显输入的单词列表。当我运行此消息时,我收到消息: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

1 个答案:

答案 0 :(得分:1)

第一次尝试

这是我的第一个脚本:

#!/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"

玩:

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.

或迭代相同输入的参数

#!/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"

执行命令

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