my script.
My program is called match. Also file 1.txt contains
apple orange juice
table chair cup
#!/bin/sh
program=$1
while read a b
do
if ["$a"="$program"]
then
echo a
fi
done<1.txt
I type MATCH APPLE. It has to compare "apple" with first token of each line of file 1.txt. If they are the same than print WHOLE LINE. In my example it has to be "apple orange juice". But my code does not work properly. Not sure what is wrong. Help me please.
'type 'match apple'? Output whole line (minus any white space around field 1):
echo "$a $b"
rdrtx1
January 24, 2013, 4:12pm
3
try:
#!/bin/sh
program=$1
while read a b
do
if [ "$a" = "$program" ]
then
echo "$a" "$b"
fi
done < 1.txt
Yes, I have changed but it gives me wrong things.
./match: line 5: [apple=apple]: command not found
./match: line 5: : command not found
./match: line 5: [home=apple]: command not found
rdrtx1
January 24, 2013, 4:17pm
5
Make sure you include spaces around square brackets and "=" in if
statement.
1 Like
I meant in command line I type MATCH APPLE sorry....
---------- Post updated at 04:22 PM ---------- Previous update was at 04:17 PM ----------
thank you so much for helping me
gary_w
January 24, 2013, 4:25pm
7
I'll bite:
$ cat x
#!/bin/sh
program="$1"
while read a b
do
if [ "$a" = "$program" ]; then
echo "$a $b"
fi
done < 1.txt
exit 0
$ ./x apple
apple orange juice
$
manueldo:
Yes, I have changed but it gives me wrong things.
./match: line 5: [apple=apple]: command not found
./match: line 5: : command not found
./match: line 5: [home=apple]: command not found
i think i know what goes wrong with your code you missed the spaces
it's
if [ "$a" = "$program" ]; then echo "$a $b" fi
and not
if ["$a" = "$program"]; then echo "$a $b" fi
you better check your program
White space is good, free, use it wisely to separate and indent for structure.
if [ "$a" = "$program" ]
then
echo "$a $b"
fi
It turns out that '[' is a command file originally, a hard link to 'test', although ksh and bash have built it in to save exec()'s.