PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 29, 2022

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

 October 29, 2022     bash, cat, eof, su, sudo     No comments   

Issue

##!/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

Above script is used to create autorestartnftables.sh,expect result as below:

#!/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 after run 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

Where is the problem?


Solution

Do not nest, nest, nest. Instead use declare -f and functions to transfer work to unrelated context.

##!/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"

In this case, you could check if the user running your script is the user you want and then restart your script with that user:

##!/bin/bash
set -e
backup_dir='/home/my/backup'
user='my'
if [[ "$USER" != "$user" ]]; then
   # restart yourself as that user
   exec sudo -u "$user" "$0" "$@"
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

Check your scripts with shellcheck.



Answered By - KamilCuk
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing