The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Checking for existence of a flat file in UNIX ! Ariean Shell Programming and Scripting 4 04-25-2008 11:58 AM
checking for existence of table in oracle kjs SUN Solaris 0 10-19-2007 12:21 AM
checking file existence DILEEP410 Shell Programming and Scripting 3 01-24-2007 08:43 AM
File existence problem anormal UNIX for Dummies Questions & Answers 2 05-15-2006 09:54 AM
File existence mpang_ Shell Programming and Scripting 2 03-27-2006 08:27 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-12-2007
Registered User
 

Join Date: May 2006
Location: Hyderabad, India
Posts: 12
Checking the existence of a file..

Hi,

I am trying to check for the existence of a file using the 'test' and the file existence options.
When trying to check for a file with a space in between e.g 'Team List', it gives the following error.

learn1: line 3: test: `Team: binary operator expected

I am pasting my code below as well:

echo "Please enter a file name"
read fname
if test -f $fname
then
echo "$fname exists"
else
echo "$fname doesnot exist"
fi


Can someone help me with this...

Regards,
Indra
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-12-2007
Registered User
 

Join Date: Jun 2007
Location: Minnesota
Posts: 2
use double quotes around variable

Try using double quotes around your variable that holds the file name.

Code:
»if test -f "$filename"; then
©:echo "success"
©:else
©:echo "failure"
©:fi
success
Reply With Quote
  #3 (permalink)  
Old 06-12-2007
Registered User
 

Join Date: May 2006
Location: Hyderabad, India
Posts: 12
Hi Gabe,

can you also explain how would the double quotes make the difference.

Regards,
Indra
Reply With Quote
  #4 (permalink)  
Old 06-12-2007
Shell_Life's Avatar
Unix/Informix/4GL/SQL
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Indra,
Try using " (double quote) before and after $fname:
Code:
echo "Please enter a file name"
read fname
if test -f "$fname""
then
  echo "$fname exists"
else
  echo "$fname doesnot exist"
fi
Reply With Quote
  #5 (permalink)  
Old 06-12-2007
solfreak's Avatar
Registered User
 

Join Date: Jun 2007
Posts: 35
Space is one of the default input field seperators.
So, if you just use $fname then that's 2 arguments for test and hence doesn't work.
"$fname" is just 1 argument.
Reply With Quote
  #6 (permalink)  
Old 06-12-2007
Registered User
 

Join Date: Jun 2007
Location: Minnesota
Posts: 2
solfreak is on the money with this one.

If you consider that the test command is looking for a single argument, and then you provide it with a list, then it will only test the first item in that list.

So, I created a test script named "test.ksh". Here it is:
Code:
#! /usr/bin/ksh

echo "enter a file name: \c"
read filename

if test -f "${filename}"; then
   echo "Success: ${filename}"
   else
     echo "Failure: ${filename}"
fi
I also created a file in the same directory named "one two.txt".

Code:
»ls
one two.txt  test.ksh
»test.ksh 
enter a file name: one two.txt
Success: one two.txt
Then I changed test.ksh to remove the double quotes around the variable "filename":
Code:
#! /usr/bin/ksh

echo "enter a file name: \c"
read filename

if test -f ${filename}; then
   echo "Success: ${filename}"
   else
     echo "Failure: ${filename}"
fi
I ran this in an identical manner as before:

Code:
»test.ksh
enter a file name: one two.txt
Failure: one two.txt
I believe that this is due to the way that the test function works. When presented with a list of words separated by spaces, it tests the first item in that list (in this case, part of a filename), but if that list of words is surrounded by double quotes, then it is viewed as a single argument, and the test is then successful.

hth,
gabe
Reply With Quote
  #7 (permalink)  
Old 06-13-2007
Registered User
 

Join Date: May 2006
Location: Hyderabad, India
Posts: 12
Thumbs up

Thanks everyone for helping me with this.

Thanks & regards,
Indra
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:27 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0