虽然从shell脚本sh linux中读取

Fjo*_*dor 0 linux shell sh while-loop

我正在读取sh shell脚本中的文件,如果条件成功,我想从文件中删除一行.

if [ -f "file.txt" ]; then
     while read line
     do
          if [*condition...*]; then
                *remove line from file*
     done
fi
Run Code Online (Sandbox Code Playgroud)

这是正确的做法吗?当我现在运行脚本时它没有输出(如果我在while循环中尝试使用echo)并且永远不会结束...

fed*_*qui 5

你正在使用while read line...但没有正确阅读.您可能需要更改done以下内容done < file.txt:

if [ -f "file.txt" ]; then
     while read line
     do
          if [ *condition...*]; then
                *remove line from file*
     done < file.txt  # <------------- here!
fi
Run Code Online (Sandbox Code Playgroud)