Error output of cat to /dev/null


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Error output of cat to /dev/null
# 1  
Old 02-04-2011
Error output of cat to /dev/null

Hello,

I'm trying to send the error output of a 'cat' operation to /dev/null like this:

Code:
cat /dirA/dirB/temp*.log  > /dirA/dirB/final.log 2>/dev/null

This works perfectly in a terminal, but not when placed in a script.

If there are no files matching temp*.log the script outputs an error saying: /dirA/dirB/final.log: No such file or directory.

The same problem occurs on the next line in the script where I want to remove all temp*.log files
Code:
rm /dirA/dirB/temp*.log 2>/dev/null


Sending all errors of the script to /dev/null is not an option as I need error information from other parts of the script.

Anybody an idea as to how to solve this
# 2  
Old 02-04-2011
What Operating System and version?
What Shell?
How many files?
Was the script created on the unix server with a proper unix editor such as "vi" ? If not, what was used.
Code:
uname -a
echo $SHELL
find /dirA/dirB/ -name temp\*.log -print | wc -l
sed -n l scriptname

# 3  
Old 02-04-2011
I'm writing/testing the script on
- Linux pclinux 2.6.15-27-386 #1 PREEMPT Sat Sep 16 01:51:59 UTC 2006 i686 GNU/Linux (ubuntu dapper drake)
- /bin/bash
But eventually it needs to run on
- Linux embLinux 2.6.12 #184 Wed Dec 9 08:35:30 CET 2009 armv4tl unknown
- /bin/sh
The results are the same on both systems

- anything between 0 and 100 files
- the files are created using gedit
Code:
`cat /dirA/dirB/temp*.log > /dirA/dirB/final.log 2>/dev/null`$
`rm /dirA/dirB/temp*.log 2>/dev/null`$

# 4  
Old 02-04-2011
Quote:
`cat /dirA/dirB/temp*.log > /dirA/dirB/final.log 2>/dev/null`$
`rm /dirA/dirB/temp*.log 2>/dev/null`$
Just for interest the $ sign (only) at the end of the line when viewed with "sed" tells me that this is a normal unix text file.

The problem with the script is the backticks. Not required in this context and will cause the error you are seeing. It will try to execute each file in the directory list and fail on the first one because it is not in $PATH !

Try this:
Code:
cat /dirA/dirB/temp*.log > /dirA/dirB/final.log 2>/dev/null
rm /dirA/dirB/temp*.log 2>/dev/null

# 5  
Old 02-04-2011
Removving the backticks is no solution, the result is the same (I had already played with this).

It is not first file on which it gives an error, it gives an error that the final file does not exist. It looks as though the system has difficulties with writing "nothing" to a file that does not exist.
# 6  
Old 02-04-2011
you could try and send the error of the cat to null
Code:
cat /dirA/dirB/temp*.log 2>/dev/null > /dirA/dirB/final.log 2>/dev/null

or you could trap all standard error out to null
Code:
MY_ERR="/dev/null"
exec 2>$MY_ERR

cat /dirA/dirB/temp*.log > /dirA/dirB/final.log
rm /dirA/dirB/temp*.log


Last edited by Scott; 02-05-2011 at 08:30 AM.. Reason: Code tags
# 7  
Old 02-04-2011
I stand corrected. You get the issue I mentioned with `ls` (don't try it!).

Any space characters or non-printing characters in the filenames?
Is the directory automounted from another computer?

We can check that the filename globbing is working and check that the Shell generates the complete line.
Code:
echo cat /dirA/dirB/temp*.log


Btw. I hate open-ended lists on command lines. This construct more robust:
Code:
>/dirA/dirB/final.log
ls -1 /dirA/dirB/temp*.log 2>/dev/null | while read filename
do
         cat "${filename}" >>/dirA/dirB/final.log ; REPLY=$?
         if [ ${REPLY} -eq 0 ]
         then
               rm "${filename}"
         fi
done

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

tar not reading if output directed to /dev/null

I stumbled across a somewhat strange behavior of tar and find no explanation for it: i was testing a DVD for read errors and thought to simply tar the content and direct the output to /dev/null: tar -cvf - /my/mountpoint/*ts > /dev/null This way i expected the system to read the complete... (4 Replies)
Discussion started by: bakunin
4 Replies

6. UNIX for Dummies Questions & Answers

cp output /dev/null results in not a directory

Hello, I am working on a script to measure the read performance of a busybox environment. The logical choice is to use a command line like: (time cp * /dev/null) 2> /tmp/howlong.txt Ah, the rub is cp or /dev/null will only accept a single file at a time. The result in the txt file is and... (1 Reply)
Discussion started by: stevesmo
1 Replies

7. UNIX for Dummies Questions & Answers

cat /dev/null

Hi, Excuse my ignorance here - I'm a networks man and my knowledge of all things unix is somewhat limited. We have a very large file (/var/tmp/mond.log) that we need to zero - does the "cat /dev/null > /var/tmp/mond.log" command achieve this? (4 Replies)
Discussion started by: freakydancer
4 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. Solaris

URGENT - setup port and dump all output to /dev/null or a file

Please help urgently. I need to setup up some sort of service on a solaris server on a port. I dont need it do anything special, anything that is sent to this port from an external server should be dump to /dev/null or a flat file.. Can you help urgently? (1 Reply)
Discussion started by: frustrated1
1 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