linux - Bash 编程 — 如何将字符串添加到文本文件中

标签 linux bash shell command-line linux-mint

touch log.txt
while [ 1 ]; do 
 echo "insert data: "
 read data
 if [ $data = "finish" ]; then
  break
 fi
 echo "$data" >> log.txt

当我运行该程序并在“hello im martin”之类的输入中输入一个词时,它给我一个错误提示,指出意外的运算符。如何解决这个问题?

最佳答案

在表示 while 循环结束的 echo 语句后,您缺少 done 关键字。

此外,您应该将第 5 行的 $data 放在引号中,否则 hello im martin 输入将导致该行被解释为:

if [ hello im martin = "finish" ]; then

关于linux - Bash 编程 — 如何将字符串添加到文本文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25946098/

相关文章:

linux - 通过 ssh session 在 Linux 中安装 Flash Player

linux - 使用 grep 在子目录中递归搜索特定文件名中的特定关键字

regex - golang exec 查找命令正则表达式

linux - shell脚本不打印值

Linux printf 将十进制转换为二进制?

linux - 如何在 linux 中重新添加 unicode 字节顺序标记?

string - 如何在 bash 中处理长字符串并仍然获得格式良好的代码?

bash -/bin/bash : Command not found in alpine docker

linux - bash 脚本中的 sed 输出在 CLI 中有效,在 cron 中输出不同

linux - 如何使变量成为 bash 中文件夹中的文件名序列?并在for循环中使用两个变量?