How to redirect stderr for <file if file does not exist?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to redirect stderr for <file if file does not exist?
# 1  
Old 08-07-2014
How to redirect stderr for <file if file does not exist?

Code:
$ a=$(<missing)
-bash: missing: No such file or directory

$ a=$(<missing) 2> /dev/null
-bash: missing: No such file or directory

$ a=$(<missing 2> /dev/null)
-bash: missing: No such file or directory

$ a=$( { <missing 2> /dev/null; } )
-bash: missing: No such file or directory

$ a=$( (<missing 2> /dev/null) )
-bash: missing: No such file or directory

Here is the context (what I am trying to do):

I have some .csv files. I need to find some files that go with them to parse them in another directory but I don't know the exact directory (I do know that it contains the same number and is unique).

Code:
Parent_Folder/Excel Files_00001/File_1.csv
Parent_Folder/Excel Files_00001/File_2.csv
Parent_Folder/Excel Files_00001/File_3.csv
Parent_Folder/Run00001 - Unknown String1/DVars.xml
Parent_Folder/Run00001 - Unknown String1/valid.xml
Parent_Folder/Excel Files_00002/File_1.csv
Parent_Folder/Excel Files_00002/File_2.csv
Parent_Folder/Excel Files_00002/File_3.csv
Parent_FolderRun00002 - Unknown String2/DVars.xml
Parent_FolderRun00002 - Unknown String2/valid.xml

"DVars.xml" should always be there. It contains several parameters (including the Unknown Strings)
"valid" is usually not there and I don't want to see an error message if it is not.
Code:
            runNumber="${parentFolder:(-5)}" # Last 5 characters of Excel Files_xxxxx folder name is run number
            runFolder="$parentFolder""/../Run""$runNumber""*/" # traverse up one directory
            valFile="$runFolder""valid"
            xmlFile="$runFolder""DVars.xml"
            oIFS="$IFS"; IFS=''; # we need to expand the '*' but not split on whitespace
            [ DVarsXml="$(<$xmlFile)" ] || DVarsXml='<P2 K="RunComment" V="XML File Missing" />'
            [ valid="$(<$valFile)" 2>/dev/null ] || valid="V"
            IFS="$oIFS"

How can I prevent the stderr?

Mike
# 2  
Old 08-07-2014
Might be simplest to just not read the file, if it's not there.
Code:
[ -f "file" ] && var=$(<"file") || var=""

# 3  
Old 08-07-2014
Quote:
Originally Posted by Corona688
Might be simplest to just not read the file, if it's not there.
Code:
[ -f "file" ] && var=$(<"file") || var=""

Thanks. Actually the answer is close. var=$(<"$file") seems to block the expansion of the * while var="$(<$file)" like I had it works.

Mike
# 4  
Old 08-07-2014
Perhaps this (-r readable -s size above zero) :

Code:
[ -r $valFile -a -s $valFile ] && valid="$(<$valFile)" || valid="V"


Last edited by Chubler_XL; 08-07-2014 at 08:05 PM.. Reason: Quotes adjusted for IFS changes and wildcard expansion required in OP
# 5  
Old 08-07-2014
Quote:
Originally Posted by Michael Stora
Thanks. Actually the answer is close. var=$(<"$file") seems to block the expansion of the *
If the * expands to more than one file, that may cause problems.
# 6  
Old 08-07-2014
Another thought (fetches data from 1st file, you can do anything you like in the loop with each file expanded from *).

No need to change IFS as for "knows" about the expansion

Code:
runFolder="$parentFolder""/../Run""$runNumber""*/" # traverse up one directory
valFile="$runFolder""valid"
for x in $valFile
do
   [ -f "$x" ] && valid=$(<"$x") || valid="V"
   break
done

# 7  
Old 08-07-2014
Quote:
Originally Posted by Corona688
If the * expands to more than one file, that may cause problems.
Yes, but I know the folder numbers are unique. I another script I test the exit status of ls when there are multiple matches but in this script calling the non-builtin 'ls' bogs things down as there are about 10,000 of these files to parse.

Quote:
Originally Posted by Chubler_XL
Another thought (fetches data from 1st file, you can do anything you like in the loop with each file expanded from *).

No need to change IFS as for "knows" about the expansion

Code:
runFolder="$parentFolder""/../Run""$runNumber""*/" # traverse up one directory
valFile="$runFolder""valid"
for x in $valFile
do
   [ -f "$x" ] && valid=$(<"$x") || valid="V"
   break
done

There should only be one. If there is more than one match, I would not want to use the data. The additonal loop would slow things down. There is one predictable program writing these files so I know there is one of each type of folder for each run number.

Mike
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirect STDOUT & STDERR to file and then on screen

Dear all, redirecting STDOUT & STDERR to file is quite simple, I'm currently using: Code: exec 1>>/tmp/tmp.log; exec 2>>/tmp/tmp.log But during script execution I would like the output come back again to screen, how to do that? Thanks Luc edit by bakunin: please use CODE-tags like the... (6 Replies)
Discussion started by: tmonk1
6 Replies

2. Shell Programming and Scripting

Redirect STDOUT & STDERR to file and then on screen

Dear all, redirecting STDOUT & STDERR to file is quite simple, I'm currently using: exec 1>>/tmp/tmp.log; exec 2>>/tmp/tmp.logBut during script execution I would like the output come back again to screen, how to do that? Thanks Lucas (4 Replies)
Discussion started by: Lord Spectre
4 Replies

3. Shell Programming and Scripting

ksh- redirect stderr to file and then modify the file

I have the following: remsh $host -n 2>>syslog_issue_list.txt grep -i -e "EMS" -e "error" -e "warning" -e "excessive" /var/adm/syslog/syslog.log | awk /"$DATE1"/ | awk -vhost="$host" '!/remsh|telnetd/{print host "\n", $0 >> "syslog_issue_list.txt"}' I am creating a health script that has... (4 Replies)
Discussion started by: chipblah84
4 Replies

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

5. Shell Programming and Scripting

redirect stdout and stderr to file wrong order problem with subshell

Hello I read a lot of post related to this topic, but nothing helped me. :mad: I'm running a ksh script with subshell what processing some ldap command. I need to check output for possible errors. #!/bin/ksh ... readinput < $QCHAT_INPUT |& while read -p line do echo $line ... (3 Replies)
Discussion started by: Osim
3 Replies

6. Shell Programming and Scripting

Redirect stdout/stderr to a file globally

Hi I am not if this is possible: is it possible in bach (or another shell) to redirect GLOBALLY the stdout/stderr channels to a file. So, if I have a script script.sh cmd1 cmd2 cmd3 I want all stdout/stderr goes to a file. I know I can do: ./script.sh 1>file 2>&1 OR ... (2 Replies)
Discussion started by: islegmar
2 Replies

7. UNIX for Dummies Questions & Answers

Redirect just stderr to a file with a timestamp

I'm using below command to redirect stderr to a file but I also want to add timestamp to stderr.out to find out the date / time the error occurred. ls -ltr 2>>/tmp/stderr.out Thanks (5 Replies)
Discussion started by: mbak
5 Replies

8. Shell Programming and Scripting

How to redirect stderr and stdout to a file

Hi friends I am facing one problem while redirecting the out of the stderr and stdout to a file let example my problem with a simple example I have a file (say test.sh)in which i run 2 command in the background ps -ef & ls & and now i am run this file and redirect the output to a file... (8 Replies)
Discussion started by: sushantnirwan
8 Replies

9. Shell Programming and Scripting

how can i redirect stderr to file in Make?

Hello all im using tcsh shell on sun Solaris , using the Make utility for compilation i will like to be able to redirect the stderr to file , how can it be done ? (0 Replies)
Discussion started by: umen
0 Replies

10. Shell Programming and Scripting

Redirect stdout and stderr

How can I redirect and append stdout and stderr to a file when using cron? Here is my crontab file: */5 * * * * /dir/php /dir/process_fns.php >>& /dir/dump.txt Cron gives me an 'unexpected character found in line' when trying to add my crontab file. Regards, Zach Curtis POPULUS (8 Replies)
Discussion started by: zcurtis
8 Replies
Login or Register to Ask a Question