Generate script in bash and save it to location requiring sudo

D W picture D W · Dec 10, 2010 · Viewed 7.9k times · Source

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

If my $OUTFILE is a directory requiring sudo privileges where do I put the sudo command or what else can I do to make it work?

Answer

Dennis Williamson picture Dennis Williamson · Dec 11, 2010

This is how I would do it:

sudo tee "$OUTFILE" > /dev/null <<'EOF'
foo
bar
EOF