从bash脚本生成bash脚本

And*_*amp 2 linux bash shell

我需要从脚本中生成一个脚本但是遇到问题,因为正在解释进入新脚本的一些命令而不是写入新文件.例如,我想在其中创建一个名为start.sh的文件,我想将变量设置为当前的IP地址:

echo "localip=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1  -d'/')" > /start.sh
Run Code Online (Sandbox Code Playgroud)

写入文件的内容是:

localip=192.168.1.78
Run Code Online (Sandbox Code Playgroud)

但我想要的是新文件中的以下文字:

localip=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1  -d'/')"
Run Code Online (Sandbox Code Playgroud)

以便在运行生成的脚本时确定IP.

我究竟做错了什么 ?

Cha*_*ffy 7

你这不必要了.使用带有引号的heredoc来传递文字内容,而不进行任何扩展:

cat >/start.sh <<'EOF'
localip=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1  -d'/')
EOF
Run Code Online (Sandbox Code Playgroud)

使用<<'EOF'<<\EOF不仅仅是<<EOF必要的; 后者将像原始代码一样执行扩展.


如果您要写的任何内容start.sh需要基于当前变量,那么请务必使用它printf %q来安全地转义其内容.例如,要设置当前$1,$2等期间积极start.sh执行:

# open start.sh for output on FD 3
exec 3>/start.sh

# build a shell-escaped version of your argument list
printf -v argv_str '%q ' "$@"

# add to the file we previously opened a command to set the current arguments to that list
printf 'set -- %s\n' "$argv_str" >&3

# pass another variable through safely, just to be sure we demonstrate how:
printf 'foo=%q\n' "$foo" >&3

# ...go ahead and add your other contents...
cat >&3 <<'EOF'
# ...put constant parts of start.sh here, which can use $1, $2, etc.
EOF

# close the file
exec 3>&-
Run Code Online (Sandbox Code Playgroud)

这比>>/start.sh在需要追加的每一行上使用效率要高得多:使用exec 3>file然后>&3只打开文件一次,而不是每生成一次输出的命令打开一次.