为什么可以';我不能在UNIX while循环中正确读取用户输入吗?

为什么可以';我不能在UNIX while循环中正确读取用户输入吗?,unix,shell,Unix,Shell,我在UNIX中使用bourne shell,遇到以下问题: #!/bin/sh while read line do echo $line if [ $x = "true" ] then echo "something" read choice echo $choice else echo "something" fi done <file.txt #/垃圾箱/垃圾箱 读行时 做 回音$线

我在UNIX中使用bourne shell,遇到以下问题:

#!/bin/sh


while read line
   do

  echo $line

  if [ $x = "true" ]
  then
        echo "something"
        read choice
        echo $choice
  else
        echo "something"
  fi

done <file.txt
#/垃圾箱/垃圾箱
读行时
做
回音$线
如果[$x=“true”]
然后
呼应“某物”
阅读选择
echo$choice
其他的
呼应“某物”
fi

已完成我不太会编写shell脚本,但我认为“read choice”应该是“read$choice”

我不太会编写shell脚本,但我认为“read choice”应该是“read$choice”

这是因为您要求程序从文件
file.txt中读取:

done <file.txt

应该是
“$line”

还要注意
,如果从文件中读取的单词中有空格,没有它们,程序将中断。

这是因为您要求程序从文件
file.txt
中读取:

done <file.txt

这应该是
“$line”

也请注意
,如果从文件中读取的单词有空格,没有它们,程序将中断。

通过
重定向标准输入通过
重定向标准输入
exec 3<&0
#!/bin/sh

x=true  # to make the example work

exec 3<&0

while read line
   do

  echo $line

  if [ $x = "true" ]
  then
        echo "something"
        read choice <&3
  else
        echo "something"
  fi

done <file.txt