如何在Linux中通过shell脚本自动将数据写入文本文件?
我打开了文件。但是,我不知道如何写入数据。
如何在Linux中通过shell脚本自动将数据写入文本文件?
我打开了文件。但是,我不知道如何写入数据。
当前回答
移动我的评论作为一个答案,由@lycono要求
如果你需要用root权限来做这件事,这样做:
sudo sh -c 'echo "some data for the file" >> fileName'
其他回答
移动我的评论作为一个答案,由@lycono要求
如果你需要用root权限来做这件事,这样做:
sudo sh -c 'echo "some data for the file" >> fileName'
这种方法是有效的,也是最好的
cat > (filename) <<EOF
Text1...
Text2...
EOF
基本上,文本将搜索关键字“EOF”,直到它终止写入/追加文件
#!/bin/bash
cat > FILE.txt <<EOF
info code info
info code info
info code info
EOF
#!/bin/sh
FILE="/path/to/file"
/bin/cat <<EOM >$FILE
text1
text2 # This comment will be inside of the file.
The keyword EOM can be any text, but it must start the line and be alone.
EOM # This will be also inside of the file, see the space in front of EOM.
EOM # No comments and spaces around here, or it will not work.
text4
EOM
也可以使用这里的文档和vi,下面的脚本生成一个FILE.txt 3行和变量插值
VAR=Test
vi FILE.txt <<EOFXX
i
#This is my var in text file
var = $VAR
#Thats end of text file
^[
ZZ
EOFXX
然后文件将有如下3行。“i”是启动vi插入模式,类似地用Esc和ZZ关闭文件。
#This is my var in text file
var = Test
#Thats end of text file