Computing.Net > Forums > Unix > Test for FileName

Test for FileName

Reply to Message Icon

Original Message
Name: NewbieKsh
Date: January 12, 2007 at 16:17:13 Pacific
Subject: Test for FileName
OS: Win XP
CPU/Ram: 512
Model/Manufacturer: Toshiba
Comment:

I am working on a script that will test for a pattern. I have one line that checks to see if the argument entered is the file. I am unsure of how you test for file name. This is my script so far.

if [ $# -gt 2 ] #test if arguments are greater than 2
then
echo "Please enter only 2 arguments"
elif [ $# -lt 2 ]
then
echo "Please enter at least 2 arguments"
elif [ -f $3 ] #test for file name
then
echo "$PWD/$2/$1" #print dir/file/pattern
OUTPUT=egrep -in "$1 $2" else
echo "error: second argument must be a file"
echo "usage: $0 search-pattern file"
fi


I don't think that I am correct in this line:
elif [ -f $3 ]

thanks in advance for your help
Tina



Report Offensive Message For Removal


Response Number 1
Name: salem
Date: January 12, 2007 at 19:41:24 Pacific
Subject: Test for FileName
Reply: (edit)

You should use the $2 to pass the file name as you already using it in the egrep string. However, you are missing the single quote for the egrep

if [ $# -ne 2 ] #test if arguments are greater than 2
then
echo "Please enter only 2 arguments"
elif [ $# -lt 2 ]
then
echo "Please enter at least 2 arguments"
elif [ -f $2 ] #test for file name
then
echo "$PWD/$2/$1" #print dir/file/pattern
OUTPUT=`egrep -in $1 $2`
echo $OUTPUT
else
echo "error: second argument must be a file"
echo "usage: $0 search-pattern file"
fi

Short version of your script will be as following:

if [ $# -ne 2 ] ; then
echo "Please enter at least 2 arguments"
elif [[ ! -f $2 ]] ; then
echo "error: second argument must be a file"
echo "usage: $0 search-pattern file"
else
test -f $2 && egrep -in $1 $2
fi



Report Offensive Follow Up For Removal

Response Number 2
Name: NewbieKsh
Date: January 14, 2007 at 20:34:41 Pacific
Subject: Test for FileName
Reply: (edit)

thanks, those typos will be the end of me yet!


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Test for FileName

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge