如何在myconfig.conf
使用BASH 调用的文件中编写多行?
#!/bin/bash
kernel="2.6.39";
distro="xyz";
echo <<< EOL
line 1, ${kernel}
line 2,
line 3, ${distro}
line 4
line ...
EOL >> /etc/myconfig.conf;
cat /etc/myconfig.conf;
Run Code Online (Sandbox Code Playgroud)
ktf*_*ktf 442
语法(<<<
)和使用的命令(echo
)是错误的.
正确的是:
#!/bin/bash
kernel="2.6.39"
distro="xyz"
cat >/etc/myconfig.conf <<EOL
line 1, ${kernel}
line 2,
line 3, ${distro}
line 4 line
...
EOL
cat /etc/myconfig.conf
Run Code Online (Sandbox Code Playgroud)
Ken*_*ent 73
#!/bin/bash
kernel="2.6.39";
distro="xyz";
cat > /etc/myconfig.conf << EOL
line 1, ${kernel}
line 2,
line 3, ${distro}
line 4
line ...
EOL
Run Code Online (Sandbox Code Playgroud)
这样做你想要的.
Tk4*_*421 31
如果您不想更换变量,则需要用单引号括起EOL.
cat >/tmp/myconfig.conf <<'EOL'
line 1, ${kernel}
line 2,
line 3, ${distro}
line 4 line
...
EOL
Run Code Online (Sandbox Code Playgroud)
上一个例子:
$ cat /tmp/myconfig.conf
line 1, ${kernel}
line 2,
line 3, ${distro}
line 4 line
...
Run Code Online (Sandbox Code Playgroud)
Wil*_*ell 14
heredoc解决方案当然是最常用的方法.其他常见解决方案是:
echo 'line 1, '"${kernel}"' line 2, line 3, '"${distro}"' line 4' > /etc/myconfig.conf
和
exec 3>&1 # Save current stdout exec > /etc/myconfig.conf echo line 1, ${kernel} echo line 2, echo line 3, ${distro} ... exec 1>&3 # Restore stdout
Pra*_*tik 10
我正在使用Mac OS并在SH 脚本中编写多行,以下代码对我有用
#! /bin/bash
FILE_NAME="SomeRandomFile"
touch $FILE_NAME
echo """I wrote all
the
stuff
here.
And to access a variable we can use
$FILE_NAME
""" >> $FILE_NAME
cat $FILE_NAME
Run Code Online (Sandbox Code Playgroud)
请不要忘记根据需要为脚本文件分配 chmod。我用过
chmod u+x myScriptFile.sh
Run Code Online (Sandbox Code Playgroud)