Redirect Standard Error to /dev/null is not working.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Redirect Standard Error to /dev/null is not working.
# 1  
Old 02-19-2013
Redirect Standard Error to /dev/null is not working.

Hello.

When I run a .ksh that contains the command below, and there is no file available in the source location the "FILE_NAME_*.CSV not found" error is still being displayed.

Code:
FILEN=$(ssh ${SOURCE_SERV} "cd ${SOURCE_LOCATION} ;ls ${FILES}") 2> /dev/null

.

This is interfering with the rest of the script, as it is not exiting as required, and is continuing to the next command (See below).

Code:
echo "- Listing available file(s) on ${SOURCE_SERV}${SOURCE_LOCATION}:\n"
		FILEN=$(ssh ${SOURCE_SERV} "cd ${SOURCE_LOCATION} ;ls ${FILES}") 2> /dev/null
		print ${FILEN}
	if [ $? -eq 0 ]; then
	echo "\n- End of Source of files list."
else
        echo "\t\t#####################################\n\t\t#        NO FILES AVAILABLE.        #\n\t\t#####################################\n\n\n- Exiting script"
	exit 0
fi

Can anyone advise what I am doing wrong with the redirection? Basically If there are no files available, I need to echo the warning and exit the script successfully.

Thanks in advance.

---------- Post updated at 02:36 PM ---------- Previous update was at 12:07 PM ----------

Ignore me,

I have fixed it with the following:

Code:
echo "- Listing available file(s) on ${SOURCE_SERV}${SOURCE_LOCATION}:\n"
		FileExists=`ssh ${SOURCE_SERV} "test -f ${SOURCE_LOCATION}/${FILES} && echo 1 || echo 0"`
	if [ ${FileExists} = 0 ]; then
	echo "\t\t#####################################\n\t\t#        NO FILES AVAILABLE.        #\n\t\t#####################################\n\n\n- Exiting script"
	exit 0
else	
	FILEN=$(ssh ${SOURCE_SERV} "cd ${SOURCE_LOCATION} ;ls ${FILES}")
	print ${FILEN}
	echo "\n- End of Source of files list."

Thanks for looking though!
# 2  
Old 02-20-2013
Good you found a fix by yourself! Still, for e.g. later reference, these were the errors in your first script, which you did not correct but circumvent:
a) put the redirection inside the command substitution.
b) save the exit status for later use before printing $FILEN (which will overwrite status)
This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-20-2013
Quote:
Originally Posted by RudiC
b) save the exit status for later use before printing $FILEN (which will overwrite status)
This is correct, but even

Code:
var=$(command; command)
echo $?

Will show only the exit status of the last command, which is a variable assignment. The exit code will be that of var=someting, not that of any of the commands.

Btw., you might want to put quotes around the subshell, otherwise the script might break if the result of the subshell contains whitespace. For example:

Code:
var=$(echo "abc def")

Will first assign "abc" to "$var", then try to execute "def", which will fail. Use:

Code:
var="$(echo "abc def")"

instead.

I hope this helps.

bakunin

PS:
Moderator's Comments:
Mod Comment changed thread title to solved
This User Gave Thanks to bakunin For This Post:
# 4  
Old 02-20-2013
Quote:
Originally Posted by bakunin
. . .
Code:
var=$(command; command)
echo $?

Will show only the exit status of the last command, which is a variable assignment. The exit code will be that of var=someting, not that of any of the commands.
. . .
Hmmm - sounds reasonable, but (linux, bash):
Code:
$ X=$(ls xyyxyx 2>/dev/null)
$ echo $?, ">"$X"<"
2, ><

while
Code:
$ X=""
$ echo $?, ">"$X"<"
0, ><

So the 2 would be the ls' exit status, wouldn't it?
This User Gave Thanks to RudiC For This Post:
# 5  
Old 02-20-2013
Thanks Guys, I appreciate the help.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

redirect the audio output to /dev/null

I'm using an text-to-speech synthesis in a script, and I need to redirect it's output to /dev/null how can I do that ? And how to redirect the stream to his normal output then (sound card ) ? thankx (2 Replies)
Discussion started by: firelink
2 Replies

2. Shell Programming and Scripting

Redirecting standard out to /dev/null goes to file "/dev/null" instead

I apologize if this question has been answered else where or is too elementary. I ran across a KSH script (long unimportant story) that does this: if ; then CAS_SRC_LOG="/var/log/cas_src.log 2>&1" else CAS_SRC_LOG="/dev/null 2>&1" fithen does this: /usr/bin/echo "heartbeat:... (5 Replies)
Discussion started by: jbmorrisonjr
5 Replies

3. HP-UX

/dev/Null error

Hi Guru's, I am trying to test the network speed or load by this command. but getting error " Not Connected ". Could you guys please help. ftp> put "|dd if=/dev/zero bs=8k count=1000000" /dev/null Not connected. Please use code tags! (9 Replies)
Discussion started by: sris.sun
9 Replies

4. UNIX for Dummies Questions & Answers

/dev/null 2>&1 Versus /dev/null 2>1

How are these two different? They both prevent output and error from being displayed. I don't see the use of the "&" echo "hello" > /dev/null 2>&1 echo "hello" > /dev/null 2>1 (3 Replies)
Discussion started by: glev2005
3 Replies

5. UNIX for Dummies Questions & Answers

Redirect Standard output and standard error into spreadsheet

Hey, I'm completely new at this and I was wondering if there is a way that I would be able to redirect the log files in a directories standard output and standard error into and excel spreadsheet in anyway? Please remember don't use too advanced of terminology as I just started using shell... (6 Replies)
Discussion started by: killaram
6 Replies

6. Shell Programming and Scripting

How to redirect the import log to /dev/null?

Hi i am running oracle database import through a script and script scans the import log to see if there are any errors. Now the porblem is that when i run the script the import log appears on the screen even if i direct the output of import to /dev/null. imp "'/ as sysdba'"... (5 Replies)
Discussion started by: vinoo128
5 Replies

7. Solaris

What is /dev/tty /dev/null and /dev/console

Hi, Anyone can help My solaris 8 system has the following /dev/null , /dev/tty and /dev/console All permission are lrwxrwxrwx Can this be change to a non-world write ?? any impact ?? (12 Replies)
Discussion started by: civic2005
12 Replies

8. UNIX for Dummies Questions & Answers

redirect stderr and/or stdout to /dev/null from command line

Is it possible to redirect errors at the command line when you run the script such as bash scriptname & 2>/dev/null? (1 Reply)
Discussion started by: knc9233
1 Replies

9. Shell Programming and Scripting

redirect stderr to dev/null in bash env.

Working in a bash environment, in the following example, how do I direct the error message that putting in an invalid flag (-j for example) would normally produce to dev/null? while getopts "abcd" opt do case "$opt" in i) a etc ;; r) b etc ;; f) c etc ;; v) d... (7 Replies)
Discussion started by: Sniper Pixie
7 Replies

10. Shell Programming and Scripting

error piping nohup to /dev/null

Hi, I have a script as follows: #!/bin/sh nohup ./my_program >& /dev/null & However, i get a "Generated or received a file descriptor number that is not valid" whenever I run it. running the command up in prompt is ok though. if i change the first line to #!/bin/csh i get a then:... (4 Replies)
Discussion started by: mochi
4 Replies
Login or Register to Ask a Question