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

时间:2019-03-18 09:21:38

标签: bash unix freebsd

我正在尝试创建一个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

还有另一种方法吗?

谢谢!

1 个答案:

答案 0 :(得分:4)

正在发生的事情是here document正在扩展变量,并且由于未声明变量,因此您从Wiki获取空值:

  

默认情况下,行为与双引号的内容基本相同:变量名被其值替换,对反引号内的命令进行求值等

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

可以通过引用标签的任何部分来禁用它,然后以未引用的值结尾;如果内容用单引号引起来,则其行为本质上相同。因此,例如,将其设置为单引号:

$ 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
相关问题