LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-08-2009, 07:22 AM   #1
Vilmerok
LQ Newbie
 
Registered: Feb 2009
Posts: 6

Rep: Reputation: 0
Question bash script: how to check stderr after command "find"


I use command "find" in my bash script: if the filename exist command find work quiet, and if the filename not exist I see the message "find: /tmp/filename: No such file or directory".


My problem is following, i want to have in my script something like this:

find "/tmp/filename" -type f -delete | "if no_any_errors execute command1" , if file_not_found execute command2"

Does it possible?
 
Old 07-08-2009, 07:35 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
The error message is due to the fact that you use the name of the file you want to look for as the search path of the find command. Usually find is in the form
Code:
find /tmp -name filename
this looks recursively inside the /tmp directory for a file called filename. Output is null if any file is found, otherwise the file names are printed out. In both cases the exit code of the command is 0. Anyway, using the file name in place of the search path, as in your example, the exit code is 1 due to the error and 0 otherwise.

However if you do
Code:
find "/tmp/filename" -type f
and a directory called /tmp/filename/ exists, the find command looks for any file inside that directory and the exit status is 0.

What I am trying to tell is that the find command is not suitable for the task you want to achieve, because the exit code of find is not strictly related to the existence of the file you're searching. If you're looking for the existence of a particular file, just use test -f and act accordingly to the result. For example:
Code:
if [ -f /tmp/filename ]
then
  echo file found
  command1
else
  echo file not found
  command2
fi
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 07:42 AM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 02:32 PM
Bash Script: Problem running variable command containing "" Paasan Programming 2 01-21-2004 01:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration