【问题标题】:I want to write a single line in file using shell script linux我想使用shell脚本linux在文件中写一行
【发布时间】:2012-03-11 15:18:25
【问题描述】:

我有一些输入可以说“somespacehere .file somemorespace”sample.cpp“”。 上面的行我想使用 shell 脚本写在一个文件中。 我应该如何编写 shell 脚本? 请帮我。 谢谢。

我写了一个代码

#!/bin/bash
filename= "./xyz.txt"
file = open(filename,'w')
echo "  .file    \"sample.cpp\"" > file
file .close()

它给了我错误

语法错误:“(”意外 在最后一行。

【问题讨论】:

  • 不清楚你在这里问什么。您是否希望将多个命令连接成一行?您是否希望将一个命令的输出作为参数传递给另一个?
  • 我只想将字符串 startshere somespace .file somemorespace "sample.cpp" stringendshere 写入文件。

标签: linux shell file-io


【解决方案1】:

以 shebang 开头,然后将您想要的行回显到文件中?例如,

#!/bin/bash

filename="./xyz.txt"
echo "  .file    \"sample.cpp\"" > $filename

【讨论】:

  • 你会想要转义那些内部引号。
  • 感谢凯文,感谢您的反馈,我已经通过转义内部引用更新了答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-22
  • 2013-05-08
  • 2017-06-23
  • 2013-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多