使用 bash 脚本将内容写入文件

Write content into a file with bash script

我正在尝试创建一个 bash script 以便 daemonized celeryd task。在我的 bash 脚本中,我需要创建一些文件并添加内容。此内容有一个名为 $app_name 的用户给出的变量,内容中有 read method 和其他一些变量。

问题:

当我将 bash 脚本中的内容复制到给定路径时,它不会复制内部已经存在的变量。

示例:

在我的 bash 脚本中我有:

########################
# Get application name #
########################

read -p "Define the application name (lowercase and without spaces): " app_name
echo "You defined the application name: $app_name"

############################################################
# Create service file /usr/local/etc/rc.d/celeryd_app_name #
############################################################

cat > /usr/local/etc/rc.d/celeryd_$app_name << EOF
#!/bin/sh
# =====================================================
#  celeryd_$app_name - Starts the Celery worker daemon.
# =====================================================
#
# :Usage: /etc/init.d/celeryd_$app_name {start|stop|force-reload|restart|try-restart|status}
# :Configuration file: /etc/default/celeryd_$app_name

### BEGIN INIT INFO
# Provides:          celeryd_$app_name
# Required-Start:    $network $local_fs $remote_fs
# Required-Stop:     $network $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
### END INIT INFO
EOF

但是如果我打开创建的文件,我会得到:

### BEGIN INIT INFO
# Provides:          celeryd_$app_name
# Required-Start:    
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
### END INIT INFO

它不会复制内容中存在的 $network $local_fs $remote_fs

还有另一种方法吗?

谢谢!

发生的事情是 here document 正在扩展变量,因为它们没有声明,所以你得到的是空值,来自 wiki:

By default, behavior is largely identical to the contents of double quotes: variable names are replaced by their values, commands within backticks are evaluated, etc

$ cat << EOF
> $ Working dir "$PWD" `pwd`
> EOF
$ Working dir "/home/user" /home/user

This can be disabled by quoting any part of the label, which is then ended by the unquoted value; the behavior is essentially identical to that if the contents were enclosed in single quotes. Thus for example by setting it in single quotes:

$ cat << 'EOF'
> $ Working dir "$PWD" `pwd`
> EOF
$ Working dir "$PWD" `pwd`

因此,根据您的示例,您可以将脚本修改为:

#!/bin/sh

cat << 'EOF' > /usr/local/etc/rc.d/celeryd_$app_name
#!/bin/sh
# =====================================================
#  celeryd_$app_name - Starts the Celery worker daemon.
# =====================================================
#
# :Usage: /etc/init.d/celeryd_$app_name {start|stop|force-reload|restart|try-restart|status}
# :Configuration file: /etc/default/celeryd_$app_name
### BEGIN INIT INFO
# Provides:          celeryd_$app_name
# Required-Start:    $network $local_fs $remote_fs
# Required-Stop:     $network $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
### END INIT INFO
EOF

然后将产生输出:

#!/bin/sh
# =====================================================
#  celeryd_$app_name - Starts the Celery worker daemon.
# =====================================================
#
# :Usage: /etc/init.d/celeryd_$app_name {start|stop|force-reload|restart|try-restart|status}
# :Configuration file: /etc/default/celeryd_$app_name
### BEGIN INIT INFO
# Provides:          celeryd_$app_name
# Required-Start:    $network $local_fs $remote_fs
# Required-Stop:     $network $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
### END INIT INFO