The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 02:58 PM
checking for existence of table in oracle kjs SUN Solaris 0 10-19-2007 03:21 AM
checking file existence DILEEP410 Shell Programming and Scripting 3 01-24-2007 12:43 PM
File existence problem anormal UNIX for Dummies Questions & Answers 2 05-15-2006 12:54 PM
File existence mpang_ Shell Programming and Scripting 2 03-27-2006 11:27 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-12-2007
igandu igandu is offline
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
  #2 (permalink)  
Old 06-12-2007
gabe gabe is offline
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
  #3 (permalink)  
Old 06-12-2007
igandu igandu is offline
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
  #4 (permalink)  
Old 06-12-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

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
  #5 (permalink)  
Old 06-12-2007
solfreak's Avatar
solfreak solfreak is offline
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.
  #6 (permalink)  
Old 06-12-2007
gabe gabe is offline
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
  #7 (permalink)  
Old 06-13-2007
igandu igandu is offline
Registered User
  
 

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

Thanks everyone for helping me with this.

Thanks & regards,
Indra
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:01 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0