Test for FileName
|
Original Message
|
Name: NewbieKsh
Date: January 12, 2007 at 16:17:13 Pacific
Subject: Test for FileNameOS: Win XPCPU/Ram: 512Model/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
|
Use following form to reply to current message: