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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Redirecting standard out to /dev/null goes to file "/dev/null" instead
# 1  
Old 02-13-2012
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:

Code:
if [ blahblah ] ; then
   CAS_SRC_LOG="/var/log/cas_src.log 2>&1"
else
   CAS_SRC_LOG="/dev/null 2>&1"
fi

then does this:

Code:
    /usr/bin/echo "heartbeat: `/usr/bin/date` " >> $CAS_SRC_LOG

In other words, based on some condition, the standard output destination is set to a either a file or to null. That was the intent.

In actuality, instead of going to null, the output goes to a file /dev/"null 2>&1"


Code:
-rw-r--r--    1 root     system     76774847 Feb 13 17:07 null 2>&1

My question is, what are the substitution and command line parsing rules? Why didn't the built command line string get parsed as expected?





Thanks
John Morrison

Last edited by methyl; 06-07-2012 at 07:40 AM.. Reason: please use code tags
# 2  
Old 02-13-2012
Because the shell only substitutes once. after it's done substituting in the text from the string, it won't go back and see that it was actually meant as a redirection. To do that kind of double-think, you'd have to feed it into eval.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 02-13-2012
This should do the job

Code:
if [ blahblah ] ; then
  CAS_SRC_LOG="/var/log/cas_src.log"
else
  CAS_SRC_LOG="/dev/null"
fi

/usr/bin/echo "heartbeat: `/usr/bin/date` " >> $CAS_SRC_LOG 2>&1

This User Gave Thanks to chihung For This Post:
# 4  
Old 02-14-2012
Thank you chihung for the fix.

So let me see if I understand this. I need to play tokenizer in my head to see this.

The parser sees the redirect token and sees that it is followed by a substitution string instead of something else like a directory path. Tokenizing on white-space (with parphrasing) it sees
... <stdout-redirect-append> <substitution-string>
instead of
... <stdout-redirect-append> <file-path> <stderr-redirect> <to-whereever-stdout-goes>

the <substitution-string> is internally processed as a <file-path> and since most characters, including spaces, can be in file names, the file name is "null 2>&1".

Subtle. A second parsing, after all string substitution would have reworked it, hence "eval".

Did I kinda get this right?

Thanks
John Morrison
# 5  
Old 02-14-2012
It's not subtle, it's so unsubtle the human brain doesn't expect it Smilie

It processes everything at the same time, in one whack. It has to get things out of the variable to consider them as shell statements -- and by the time it's done that, it's already done, so doesn't bother. Removing text from a variable is a processing step. To reprocess it again and consider it a shell statement would be another step, and it only does one step.

This is also a security feature, preventing someone from causing havoc just by inputting the raw text `rm -Rf /` and having it executed whenever any substitution happens.

This is also what makes eval so dangerous -- it will execute commands from arbitrary text. Use it with caution. Or better yet, don't use it at all.
# 6  
Old 06-06-2012
Just FYI -

This is a known bug in certain versions of IBM Systems Director (aka CAS AGENT). Apparently it gets automatically installed on AIX 6.1 TL7. Here's a link to a blog (not mine) about how they fixed the issue (pretty much the same as suggested by chihung).

edit - I can't post URLs yet, but if you google "CAS AGENT BUG DEV NULL" it should be the first link.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

/dev/null file issue

Hi Experts, I Have a query. In one of my server I just came to know that there was /dev/null file which is a not a character file. Its just a normal file. I know the command to create the character file (/dev/null) but what is the procedure. Like should i delete /dev/null and create or... (7 Replies)
Discussion started by: jayadeava
7 Replies

2. UNIX for Dummies Questions & Answers

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. FILEN=$(ssh ${SOURCE_SERV} "cd ${SOURCE_LOCATION} ;ls ${FILES}") 2> /dev/null. This is interfering with the rest... (4 Replies)
Discussion started by: jimbojames
4 Replies

3. UNIX for Dummies Questions & Answers

/dev/null a file using xargs

Hi, I'm currently using the following command to wipe clean a log file which can't be straight out RM'd: cat /dev/null > server.log I'm building this into a script and I'm current working on a command to run on each machine to do this automatically however I have multiple files so I need... (11 Replies)
Discussion started by: Deehem
11 Replies

4. Shell Programming and Scripting

find error?? find / -name "something.txt" 2>/dev/null

why is this giving me errors? i type this in: find / -name "something.txt" 2>/dev/null i get the following error messages: find: bad option 2 find: path-list predicate-list :confused: (5 Replies)
Discussion started by: magiling
5 Replies

5. 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

6. Shell Programming and Scripting

Difference between ">/dev/null 2>&1" and "2>&1 >/dev/null"

Does >/dev/null 2>&1 and 2>&1 >/dev/null mean the same? (4 Replies)
Discussion started by: proactiveaditya
4 Replies

7. Shell Programming and Scripting

Meaning of "> /dev/null 2>&1"

Hi, I am new into UNIX shell scripting and I am wondering what is the meaning of the below text which appears at the end of each line in the ".sh" file: > /dev/null 2>&1 For example, the line below: sh $HOME/stats/Rep777/Act_777.sh omc omc > /dev/null 2>&1 I know for sure what "sh... (10 Replies)
Discussion started by: salanalani
10 Replies

8. 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

9. UNIX for Advanced & Expert Users

/dev/NULL can't open this file ??

when i write the following two statements : cp /dev/NULL /clocal/mqbrkrs/user/mqsiadm/sanjay/dspmq_temp cat /dev/NULL > /clocal/mqbrkrs/user/mqsiadm/sanjay/dspmq_temp its gives me errors like : cp: /dev/NULL: A file or directory in the path name does not exist. cat : /dev/NULL can't open... (2 Replies)
Discussion started by: varungupta
2 Replies

10. UNIX for Dummies Questions & Answers

Creating a file / /dev/null

First of all, hello there cOmMuNity ! :cool: Well, I've got two basic questions: 1) In how many ways it's possible to create a file ? I know one manner using "touch", other typing something like echo "" > file ... The fact is that I need to overwrite the file if it exists, and touch... (8 Replies)
Discussion started by: 435 Gavea
8 Replies
Login or Register to Ask a Question