如何在 `su $user <<EOF` 中 `cat << 'EOF'`?

How to `cat << 'EOF'` within `su $user <<EOF`?

##!/bin/bash
set -e
backup_dir='/home/my/backup'
user='my'

su $user <<EOFHD
cat << 'EOF' > $backup_dir/autorestartnftables.sh
#!/bin/bash
SERVICENAME="nftables"

# return value is 0 if running
STATUS=$?
if [[ "$STATUS" -ne "0" ]]; then
        echo "Service '$SERVICENAME' is not curently running... Starting now..."
        systemctl start $SERVICENAME
fi
EOF
chmod +x $backup_dir/autorestartnftables.sh
EOFHD

以上脚本用于创建autorestartnftables.sh,预期结果如下:

#!/bin/bash
SERVICENAME="nftables"
# return value is 0 if running
STATUS=$?
if [[ "$STATUS" -ne "0" ]]; then
        echo "Service '$SERVICENAME' is not curently running... Starting now..."
        systemctl start $SERVICENAME
fi

autorestartnftables.sh 在 运行 sudo bash ./example.sh 之后:

#!/bin/bash
SERVICENAME="nftables"
# return value is 0 if running
STATUS=0
if [[ "" -ne "0" ]]; then
        echo "Service '' is not curently running... Starting now..."
        systemctl start 
fi

问题出在哪里?

不嵌套,嵌套,嵌套。而是使用 declare -f 和函数将工作转移到不相关的上下文。

##!/bin/bash
set -e
backup_dir='/home/my/backup'
user='my'
work() {
    cat << 'EOF' > $backup_dir/autorestartnftables.sh
#!/bin/bash
SERVICENAME="nftables"

# return value is 0 if running
STATUS=$?
if [[ "$STATUS" -ne "0" ]]; then
        echo "Service '$SERVICENAME' is not curently running... Starting now..."
        systemctl start $SERVICENAME
fi
EOF
    chmod +x $backup_dir/autorestartnftables.sh
}
su "$user" bash -c "$(declare -p backup_dir); $(declare -f work); work"

在这种情况下,您可以检查用户 运行 您的脚本是否是您想要的用户,然后用该用户重新启动脚本:

##!/bin/bash
set -e
backup_dir='/home/my/backup'
user='my'
if [[ "$USER" != "$user" ]]; then
   # restart yourself as that user
   exec sudo -u "$user" "[=11=]" "$@"
fi

cat << 'EOF' > $backup_dir/autorestartnftables.sh
#!/bin/bash
SERVICENAME="nftables"

# return value is 0 if running
STATUS=$?
if [[ "$STATUS" -ne "0" ]]; then
        echo "Service '$SERVICENAME' is not curently running... Starting now..."
        systemctl start $SERVICENAME
fi
EOF
chmod +x $backup_dir/autorestartnftables.sh

使用 shellcheck 检查您的脚本。