X-Recipient: archive-cygwin AT delorie DOT com X-Spam-Check-By: sourceware.org Content-class: urn:content-classes:message MIME-Version: 1.0 Subject: RE: bash programming: testing for empty string Content-Type: text/plain; charset="us-ascii" Date: Wed, 5 Mar 2008 10:12:21 -0000 Message-ID: <5E25AF06EFB9EA4A87C19BC98F5C87533F04DF@core-email.int.ascribe.com> From: "Phil Betts" To: Reply-To: X-IsSubscribed: yes Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id m25ACuju006281 bootleg86 wrote on Wednesday, March 05, 2008 9:28 AM:: > Hi, > > I'm trying to find the time of a file by doing this. > > filetime=`ls -l --time-style=+%a:%H:%M /tmp/1.txt | awk '{print $6}'` > > if [ -z "$filetime"]; then > echo "File does not exist" > else > echo "Time file: $filetime" > fi > > However, when the file does not exist and filetime returns an empty > string, it does not evaluate [ -z "$filetime" ] to true > I have also tried the reverse which is > > if [ -n "$filetime"]; then > echo "Time file: $filetime" > else > echo "File does not exist" > fi > > but I still get the same results. > > What does $filetime evaluate to when the command exits with an error? WJFFM However it's not good style to trigger a foreseeable error and rely on the error handling of a command to do what you want. Apart from anything else, doing it your way will be slower due to the number of unnecessary processes invoked when the file doesn't exist. Instead, first test if the file exists, and only if so print its time: if [ -e /tmp/1.txt ];then filetime=`ls -l --time-style=+%a:%H:%M /tmp/1.txt | awk '{print $6}'` echo "Time file: $filetime" else echo "File does not exist" fi Also, if you don't actually need $filetime after you've printed it, save yourself a bit more time by: echo -n "Time file: " ls -l --time-style=+%a:%H:%M /tmp/1.txt | awk '{print $6}' -- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Problem reports: http://cygwin.com/problems.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/