Generate script in bash and save it to location requiring sudo

后端 未结 3 1814
孤街浪徒
孤街浪徒 2021-02-02 08:23

In bash I can create a script with a here-doc like so as per this site: http://tldp.org/LDP/abs/html/abs-guide.html#GENERATESCRIPT

(
cat <<\'EOF\'
#!/bin         


        
3条回答
  •  借酒劲吻你
    2021-02-02 09:22

    Just putting sudo before cat doesn't work because >$OUTFILE attempts to open $OUTFILE in the current shell process, which is not running as root. You need the opening of that file to happen in a sudo-ed subprocess.

    Here's one way to accomplish this:

    sudo bash -c "cat >$OUTFILE" <<'EOF'
    #!/bin/bash
    #? [ ] / \ = + < > : ; " , * | 
    #/ ? < > \ : * | ”
    #Filename="z:"${$winFn//\//\\}
    echo "This is a generated shell script."
    App='eval wine "C:\Program Files\foxit\Foxit Reader.exe" "'$winFn'"'
    $App
    EOF
    

    This starts a sub-shell under sudo, and opens $OUTFILE from that more privileged subprocess, and runs cat (as yet another privileged subprocess). Meanwhile, the (less privileged) parent process pipes the here-document to the sudo subprocess.

提交回复
热议问题