how to get stderr


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to get stderr
# 1  
Old 06-15-2011
how to get stderr

Hello

I try to store stderr into a variable, then if this var is not empty i send an email and stop my script.

I think my problem is due of "<$dump" into my command line.

Code:
my bad command line (see samples below on this post)
if ! $returnedStr ; 
 then
     echo ERROR READING DUMP:  $returnedStr
  exit 1
 else
  echo cool ($returnedStr)
 fi

allways returns "cool" even if "my bad command line" throw an error.

Here all syntaxes i tested for "my bad command line":
Code:
exec 2>&1
returnedStr= `mysql -u theuser --password=thepassword < dump.sql`

Code:
returnedStr= `mysql -u theuser --password=thepassword < dump.sql 2>$1`

Code:
mysql -u theuser --password=thepassword < dump.sql 2>>$returnedStr

May someone give me the tips please?
# 2  
Old 06-15-2011
Well, do you have /dev/stderr on your UNIX?

If there is no stdout, it is simple:
Code:
zvar=$( your_command_and_args 2>&1 )

This User Gave Thanks to DGPickett For This Post:
# 3  
Old 06-15-2011
Thank you for answering DGPicket

But unfortunatly your syntaxe didn't works as well :
Code:
returnedStr=$( mysql -u theuser --password=thepassword < dump.sql 2>&1 )

=> ERROR: command not found
also tested :
Code:
returnedStr=$ ( mysql -u theuser --password=thepassword < dump.sql 2>&1 )

=> syntax error near unexpected token `('

And yes i see a stderr@ into /dev/ (also stdin@ and stdout@)
uname says : Linux 2.6.18-238.12.1.el5 x86_64 x86_64 x86_64 GNU/Linux

this is a ssh terminal acces that my web host allowed to me. So for security reason, some files are not into the usual places, but into my local folders.

PS : i'm a really dummy with unix Smilie just trying step by step to make my sh script with caution.

Any other idea? Thanks.
# 4  
Old 06-15-2011
Quote:
Originally Posted by giova
Thank you for answering DGPicket

But unfortunatly your syntaxe didn't works as well :
Code:
returnedStr=$( mysql -u theuser --password=thepassword < dump.sql 2>&1 )

=> ERROR: command not found
also tested :
Code:
returnedStr=$ ( mysql -u theuser --password=thepassword < dump.sql 2>&1 )

=> syntax error near unexpected token `('
Your shell doesn't have $(this syntax). Try "`backticks`"
# 5  
Old 06-15-2011
Is it sort of "Amish" to use the bourne shell, still? Smilie
This User Gave Thanks to DGPickett For This Post:
# 6  
Old 06-15-2011
Depends which...
# 7  
Old 06-15-2011
SmilieThank you So much Corona !
Now it works, i'm totally confused with that QuoteDoubleQuoteBackQuoteUnderQuote syntax, but this now just works like a charms Smilie

I really have to learn how to manage this !

Just to confirm, here the final syntax :
Code:
returnedStr= "`mysql -u theuser --password=thepassword < dump.sql 2>&1`"
if [[ -n "$returnedStr" ]] ; then
  echo ERROR READING DUMP:  $returnedStr
  exit 1
 else
  echo cool ($returnedStr)
 fi

CGPicket > well UNIX shell sounds Armish stylelife when you discover it SmilieSmilie

Thanks again people.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Doubt regarding stderr

Hi All, I am writing a shell script code. and i want the stderr to be send to a file and the stdout to be displayed in terminal. In my shell script code i use a read command to get data from user.read -r -p "Enter the type :" data and while i execute my script i use./my_script.sh 2>... (4 Replies)
Discussion started by: Vinoth R
4 Replies

2. Solaris

can't get stderr port

Hi Experts, i have a solaris 9 OS and i get the following message repeated many time in my /var/adm/messages : Oct 31 16:30:44 baobab rsh: can't get stderr port: Cannot assign requested address have you any idea how can i resolve this issue ??:confused: thanks for help (2 Replies)
Discussion started by: lid-j-one
2 Replies

3. Shell Programming and Scripting

How to redirect stderr to a file as well

Hello everyone, I'm a nooby in Linux, and I need some help. I have a shell script like this: echo "Start of script" > ../My_Log_Dir/Script_Name.log .. cp ../My_DataIn/File.txt ../My_DataOut/ 2>> ../My_Log_Dir/Script_Name.log rc=$? .. echo "End of Script" >>... (5 Replies)
Discussion started by: H.Faria
5 Replies

4. Shell Programming and Scripting

stderr/stdout

Can somebody explain to me why the diff output is not going to stderr? Yet when I issue a diff from the command line the return code is -ne 1. I am guessing diff always writes to stdout??? Is there away I can force the difff to write to stderr USING THE CURRENT template. If possible, I... (5 Replies)
Discussion started by: BeefStu
5 Replies

5. Shell Programming and Scripting

Append stderr

Hi everybody. I was used to redirect stderr to a file in this way, calling a generic script:./myScript &> output.logBut now I need something more sophisticated...Inside a bash script I launch an executable in this way:${command} >> "${globalLogFile}"So I redirect the stdout into globalLogFile.... (14 Replies)
Discussion started by: canduc17
14 Replies

6. Shell Programming and Scripting

Redirecting stderr problem

% ls -ld /usr /foo ls: /foo: No such file or directory drwxr-xr-x 14 root wheel 512 May 18 02:49 /usr % ls -ld /usr /foo 1>/dev/null/ /dev/null/: Not a directory. % ls -ld /usr /foo 2>/dev/null/ /dev/null/: Not a directory. ^^Why why why doesn't this work for me. Furthermore, where is... (7 Replies)
Discussion started by: phpfreak
7 Replies

7. BSD

Awstats stderr solution

Hello! Firts of all, sorry fo my poor english, but i hope you'll undestand me :) Ok, let's go to the problem.... I have a lot of awstats crontabs, and i have one mail,where i get daily and weekly output of all servers that i administrate, now it's problem that i get to his mail every time,... (3 Replies)
Discussion started by: flashm3
3 Replies

8. Shell Programming and Scripting

STDERR output

Hi, Need some help here on a script I'm writing. I know that STDERR is normally done is this manner: script 2>stderr.out However, if I wanted to output the stderr from a rsh command how do I do that? Example: su - username -c "rsh $hostname /opt/gilberteu/scriptname" 1>stdout... (5 Replies)
Discussion started by: gilberteu
5 Replies

9. UNIX for Dummies Questions & Answers

stderr redirection

Does anyone know away of redirecting the stderr for a bourne or korn shell script to a file. (5 Replies)
Discussion started by: blakmk
5 Replies

10. Programming

stderr

in fprint(stderr, "lkjalsdi\n"); what does stderr mean? thanks (1 Reply)
Discussion started by: dell9
1 Replies
Login or Register to Ask a Question