LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [shell script] execute command and parse output (https://www.linuxquestions.org/questions/programming-9/%5Bshell-script%5D-execute-command-and-parse-output-699838/)

stoiss 01-25-2009 04:42 PM

[shell script] execute command and parse output
 
Hi,

I have been trawling the WWW for some time now, trying to find an answer to my problem.

I'm writing a shell script (bourne) intended for backing up a database on a remote phpbb bulletin site (ftp access only) and moving the backup to a local machine.

Right now I can generate a DB dump on the remote site and ftp it to my local machine using ncftpget.

The problem is when I try to parse the output from ncftpget in order to determine how the transfer went.

The command is executed like this from within the script :

Code:

PS=`/usr/bin/ncftpget -u UserName -p SomePassWord -V www.somesite.com . /store/auto_backup/*.sql.gz`
echo "Echo : $PS"

In the case of an error (wrong pass) the command will output this if run directly from the shell:
Code:

/usr/bin/ncftpget -u SomeUser -p WrongPass -V www.somesite.com . /store/auto_backup/*.sql.gz
ncftpget: cannot open www.somesite.com: username and/or password was not accepted for login.

The error is never stored in the $PS variable and thus cannot be evaluated.
I have written other scripts that did not have this problem...

I know this may seem like a trivial problem to some, but I have been scratching my head quite a bit trying to figure this one out.

Any comments/feedback appreciated!

bgeddy 01-25-2009 04:53 PM

Quote:

The error is never stored in the $PS variable and thus cannot be evaluated.
Possibly the error is being written to sdterr rather than stdout. Try this to test :

Code:

PS=$(/usr/bin/ncftpget -u UserName -p SomePassWord -V www.somesite.com . /store/auto_backup/*.sql.gz 2>&1)
echo "Echo : $PS"

This simply redirects stderr to stdout.

stoiss 01-26-2009 01:49 AM

That did the trick! Thank you :)


All times are GMT -5. The time now is 09:04 AM.