LINES in shell programming

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"

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

Make sure you include spaces around square brackets and "=" in if statement.

1 Like

I meant in command line I type MATCH APPLE :smiley: sorry....

---------- Post updated at 04:22 PM ---------- Previous update was at 04:17 PM ----------

thank you so much for helping me :slight_smile:

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
$

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.