[SOLVED] LS command exit code is "0" even tough file is not there.
Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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'
$? 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.
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`
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".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.