You are not logged in.

#1 2012-03-24 12:39:31

awayand
Member
Registered: 2009-09-25
Posts: 398

[SOLVED] scripting sudo

I want to run the following script called "backup":

#!/bin/bash

sudo -u postgres "pg_dump -Fc davical >davical.pgdump"

But when I run it, the error appears:

sudo: pg_dump -Fc davical >davical.pgdump: command not found

Anyone know what I am doing wrong?

Thanks!

Last edited by awayand (2012-03-24 12:53:21)

Offline

#2 2012-03-24 12:47:24

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: [SOLVED] scripting sudo

I'm not sure you can have redirection inside a command string like that.

I suspect you could put the command string in it's own script, or perhaps in a function within the same script, then use sudo to call the other script or function.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2012-03-24 12:53:00

awayand
Member
Registered: 2009-09-25
Posts: 398

Re: [SOLVED] scripting sudo

Thanks for the tip! Now it works:

sudo -u postgres bash -c "pg_dump -Fc davical >davical.pgdump"

Offline

#4 2012-03-24 17:04:00

foppe
Member
Registered: 2011-04-02
Posts: 47

Re: [SOLVED] scripting sudo

# Check for admin rights. If user is not an admin user, exit the script
if [ $UID != 0 ]
then
   echo "You need to be root to run this script! Exiting..."
   exit
fi

This snippet checks if you've sudo-ed it (or ran as root). All subsequent commands can be coded without sudo.

Last edited by foppe (2012-03-24 17:07:30)

Offline

Board footer

Powered by FluxBB