In my script there's:
#!/bin/bash
# Take sudo password from the first argument if present
SUDO="sudo -u postgres"
if [ "$1" != "" ]; then
SUDO="echo $1 | sudo -S -u postgres"
fi
${SUDO} psql -c "create database foo;"
... several other similar commands here ...
When I run the script with myPassword
I get:
myPassword | sudo -S -u postgres psql -c create database foo;
So it just echoes the line. Obviously what I want is to literally run that command, but so that $1
gets expanded:
$ echo myPassword | sudo -S -u postgres psql -c create database foo;
How can I do that?