Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using [[ ]] and printing from line command

Status
Not open for further replies.

jalge2

Technical User
Feb 5, 2003
105
US
I'm searching for a string, and then trying to get the answers to print. My script looks like this echo "\tPlease enter the battery number you wish to search for..."
+4 read ans
+5
+6 if [[ $ans != 0 ]]
+7
+8 then
+9 cat /dso/eis/log/batlog.log.* |grep $ans
+10 fi
+11
+12 echo "\tWould you like to print this? (y/n)"
+13 read ans1
+14
+15 if [[ $ans1 = y -o Y ]]
+16
+17 then
+18 lp -dpcl@pr7
+19
+20 fi

Line 15 is coming back with an error. Saying -o is unexpected. What am I doing wrong?

 
if [ "$ans1" = "y" -o "$ans1" = "Y" ]
then


Try that. It should work.
 
jalge2,

I'm not sure that this script will do what you expect.

echo "\tPlease enter the battery number you wish to search f
or..."
+4 read ans
+5
+6 if [[ $ans != 0 ]]
+7
+8 then
+9 cat /dso/eis/log/batlog.log.* |grep $ans
+10 fi

if $ans = 0, then the next line executed is:-
echo "\tWould you like to print this? (y/n)"

is this what you want?

cat /dso/eis/log/batlog.log.* |grep $ans

will find all partial or complete matches.
ie is $ans = 20, then 200 and 120 will match.

You need to use grep -w to match a word, or change the pattern to " ${ans} ", depending upon which characters surround the battery number in the file. Some grep don't have -w switch.

What if the battery number is not found in ANY log file? You script assumes that it is.

Will the battery number only exist in 1 file?
The following command will put all filenames that contain $ans in $Files
(NB you don't need cat to do this)

Files=$(grep -lw ${ans} /dso/eis/log/batlog.log*) 2> /dev/null)

If there is more that 1 file, you need to process them thusly:-

for File in $Files
do
...
done


when you need to check a variable for case, typeset the variable to either upper or lower, and do a simple test.

typeset -u ans1

and then simply [[ $ans1 = Y ]] .....

So, the script would look something like this:-

# /bin/ksh!

typeset -u ans1

read -- ans?"Please enter the battery number you wish to search for "
[[ -z $ans || $ans = 0 ]] && #ksh compound if syntax
{
print "No battery number entered. Exiting.."
exit 1
}

Files=$(grep -lw ${ans} /dso/eis/log/batlog.log.* 2> /dev/null) ||
{
print "Can't find battery number $ans in any log file. exiting.."
exit 1
}

for File in $Files
do
print "Battery # $ans found in file $File"
print "First line of that file is:-\n"
head -1 $File
read -- ans1?"Would you like to print this? (y/n)"
[[ $ans1 = Y ]] &&
lp -dpcl@pr7 $File
done

exit 0
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top