LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-12-2012, 03:33 PM   #1
amonamarth
Member
 
Registered: Dec 2009
Location: Los Angeles
Distribution: Fedora ,CentOS, RHEL
Posts: 59

Rep: Reputation: 17
LS command exit code is "0" even tough file is not there.


In bash script, I need `ls` to exit >0 if the file I'm trying to list is not found, but `ls` will still exit 0

[root@sirvjames01 tmp]# cat tailjames-smtp
#! /bin/sh
#
# JAMES script to tail latest SMTP logs
#
# description: This is NOT the JAMES STDOUT dump, for that just 'tail -f /var/log/james/james'

# Variables
DATE=`date +%F`
LATEST_SMTP_LOG_FILE=`ls -lta /home/james/current/apps/james/logs/smtpserver-$DATE-* | awk '{ print $9 }' | head -1` 2>&1
echo $?
[root@sirvjames01 tmp]#
[root@sirvjames01 tmp]# ll /home/james/current/apps/james/logs/smtpserver-2012-10-12-34-56.log
-rw-r--r--. 1 root root 0 Oct 12 13:02 /home/james/current/apps/james/logs/smtpserver-2012-10-12-34-56.log
[root@sirvjames01 tmp]#
[root@sirvjames01 tmp]# sh tailjames-smtp
0
[root@sirvjames01 tmp]#

... but when I remove the file, LS still exits "0"

[root@sirvjames01 tmp]# rm -rf /home/james/current/apps/james/logs/smtpserver-2012-10-12-34-56.log
[root@sirvjames01 tmp]# sh tailjames-smtp
ls: cannot access /home/james/current/apps/james/logs/smtpserver-2012-10-12-*: No such file or directory
0
[root@sirvjames01 tmp]#
 
Old 10-12-2012, 03:44 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
$? is not referencing the exit code of ls, since ls was not the last command that was run. The last command that was run was head, so that's the exit code you're checking.

I would store the output of ls into a temporary variable, then check the exit code, then do the awk and head processing as necessary.

Also, why are you doing a long listing with ls and then using awk to just pull off the filename? Drop the -l off of your ls command and skip the awk altogether.

Last edited by suicidaleggroll; 10-12-2012 at 03:47 PM.
 
1 members found this post helpful.
Old 10-12-2012, 04:28 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Instead of checking the exit status of the command, you can check if the variable LATEST_SMTP_LOG_FILE is empty or not, e.g.
Code:
LATEST_SMTP_LOG_FILE=`ls -t /home/james/current/apps/james/logs/smtpserver-$DATE-* 2>/dev/null | head -1`

[ -n "$LATEST_SMTP_LOG_FILE" ] && echo log found || echo log not found
In alternative you can try the find command, so that you have not to deal with the redirection of the standard error:
Code:
LATEST_SMTP_LOG_FILE=`find /home/james/current/apps/james/logs -name smtpserver-$DATE-\* -exec ls -t {} \+ | head -1`
 
2 members found this post helpful.
Old 10-12-2012, 05:58 PM   #4
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
Bash has a variable PIPESTATUS which holds the status of every command in a pipe.
In your case you should test $PIPESTATUS[0] which is the return status of the first command in the pipe, "ls".
 
1 members found this post helpful.
Old 10-18-2012, 05:11 PM   #5
amonamarth
Member
 
Registered: Dec 2009
Location: Los Angeles
Distribution: Fedora ,CentOS, RHEL
Posts: 59

Original Poster
Rep: Reputation: 17
Thank you all
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] HELP ME! URGENT - Entered "exit" before previous command had completed run dajobi Linux - Newbie 3 03-18-2012 08:12 PM
[SOLVED] Errors executing shell script: "command not found" and "no such file or directory" eko000 Linux - Newbie 1 01-14-2011 07:54 AM
Command "mail" returns "panic: temporary file seek" kenneho Linux - Software 5 12-23-2008 03:27 AM
How to exit and save (or just to exit) inside a "make config" running command? scrat75 Linux - Kernel 6 09-05-2008 04:02 PM
ps2pdf problems "Unrecoverable error, exit code 1" sohmc Linux - Software 1 11-14-2005 08:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:07 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
Open Source Consulting | Domain Registration