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

问题描述 投票:1回答:1

我正在尝试为bash script创建一个daemonized celeryd task。在我的bash脚本中,我需要创建一些文件并添加内容。该内容具有由用户命名的$app_nameread 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

还有另一种方法吗?

谢谢 !

bash unix freebsd
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.