views:

40

answers:

3

I need to run this command

psql -c "create database $var with encoding 'unicode';" -U edumate template1

from the script under different user. The su syntax is su postre -c 'some command' so there are another quotes required. Also please note that the psql command has $var variable inside.

so 'some command' = psql -c "create database $var with encoding 'unicode';" -U edumate template1

and 'some command' must be enclosed in quotes too (I guess)

+1  A: 

You may try sudo instead of su e.g.

sudo psql -c "create database $var with encoding 'unicode';" -U edumate template1

(Note: well I havent tested exactly this command as I dont have psql with me right now)

Gopi
@Gopi: I updated my question ...
Radek
A: 

I would enter this into a script file (enter it into an editor such as vi and save it to a file). Then, chmod +x and then do your su -c '<filename>'

If you don't want to save it to a file, then you'll have to escape your quotation marks with \" or \' inside your command depending upon which quote you decide to use with su -c

umop
don't forget the variable $var that is set during the run of the script. Not sure if the file would work in that case ...
Radek
+2  A: 

There's a trick you can use anytime you need to have the shell accept as a single argument something that has both single quotes and double quotes. Because the shell won't let you escape quotes, you need to turn it into multiple abutting quoted strings, switching between single and double quotes as needed to protect what's inside. It's ugly, but it works. For example, to have

   He said "It's done"

be a single argument, you could abut the three strings:

   'He said "It'   - protect this substring with single quotes
   "'"             - protect this substring with double quotes
   's done"'       - protect this substring with single quotes

to get:

   'He said "It'"'"'s done"'

In your case that would give a very ugly:

su postre -c 'psql -c "create database '"$var with encoding 'unicode';"'" -U edumate template1'
rettops
it looks promising ... your example should look like `echo 'He said "It'"'s"' done"'`
Radek
what about the variable? I need the value not the variable name to be passed. Can I use `'` or `"` for protection?
Radek
The variable will be evaluated as long as it's within double quotes. That's why I switched to double quotes just before the $var.
rettops
I added the missing 's' in the example. I put it in the final single-quote delimited substring to match the three strings above, rather than in the middle substring as you suggested. Either placement would work, since it's not a special character.
rettops
@rettops: actually the shell *will* let you escape quotes between double quotes. But you have to remember which characters to prefix with a backslash (`"`, backslash, `$` and `\`` only), so your advice is good.
Gilles
@rettops: it works. Thank you. I had troubles with the space in front of `-U`. The space must be there otherwise the whole command doesn't work
Radek
@Gilles: maybe Bash allows escaping quotes, but tcsh does not:<br>99> test: echo "testing \" quote escaping"<br>Unmatched ".
rettops
@rettops: it's not just bash, it's any Bourne-style shell (which is what most people mean by shell in a unix context).
Gilles
@Gilles: let's not talk religion.
rettops