1

I want to compare what the user enters to this text file, to determine if what they entered is in the text file or not, and then tell them.

file1: color_readme.txt in this file is:

red
red
orange
blue

code:

echo Please enter a color:
cat color_readme.txt (printing the colors to screen)
read userinput (read what the user enters)
variable1 = grep $userinput (a random variable = what is found in the file according to what the user typed)
if userinput = variable1 (SAY THIS)
else (SAY THIS)

What is the best way to do this for a beginner? My teacher only wants me using the basic if ifelse and else conditions.

Ben
  • 2,122
  • 2
  • 28
  • 48

3 Answers3

4
echo "Pick one of the colours below:"
cat colour_readme.txt
read i
if x=$(grep "$i" colour_readme.txt)
then echo "You picked $i and it appears in colour_readme.txt as $x"
else echo "You picked $i but it does not appear in colour_readme.txt"
fi

You can test the status of a command without using the test (or [ or [[) operators; they're just special commands that return an exit status that can be tested by if.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Thank you very much Jonathan! The only thing I am curious about is why "$i" needs to be in quotations? – Ben Oct 14 '12 at 22:11
  • In this case, I'm `$i` doesn't need to be in quotation marks. It is a good habit to get into, however, should you never need to use special characters in your `grep`, such as a `()* ?{}` and the like. (Note there is a space in there too. If you had `light red` as a color, then the quotes would be required.) Because of this, many shell scripters automatically put around variables rather than try to decide if they are needed on a case-by-case basis. – Jeremy J Starcher Oct 15 '12 at 00:04
  • @JeremyJStarcher: What if the user types 'sky blue pink' in answer to the `read i` prompt? – Jonathan Leffler Oct 15 '12 at 00:06
  • Try it. At the `bash` prompt type `read my_input; echo $my_input` The `read` command works line-by-line. – Jeremy J Starcher Oct 15 '12 at 00:12
  • 1
    Just a heads up, I just realized that if someone enters "ang" as a color, it will match 'orange' since all of the grep commands are doing just partial matches. Read this for how to match the entire line: http://stackoverflow.com/questions/4709912/how-to-grep-the-exact-match – Jeremy J Starcher Oct 15 '12 at 00:17
  • @JeremyJStarcher: So the value in `i` after the user types `sky blue pink` will be...`sky blue pink`...and if the `grep` does not enclose `$i` in quotes, the `grep` command will go off looking for `sky` in files `blue` and `pink` as well as the colours README file, which is not what's wanted. As to the partial match, that's an obvious issue; type just `r` and `orange` and `red` (twice) will be picked; enter `e` and they'll all be picked. If you want to match the exact line, use `grep "^$i$" colour_readme.txt`... – Jonathan Leffler Oct 15 '12 at 00:28
  • Couldn't have said it better myself. – Jeremy J Starcher Oct 15 '12 at 00:34
2
answer="Wrong"
realcolour=`cat ./color_readme.txt`

Until [ $answer = "Correct" ] ; Do
echo -n "Please enter a colour:"
read usercolour

If [ $usercolour = $realcolour ] ; Then
     answer="Correct"
Else
     answer="Wrong"
Fi

echo $answer
Done

edit: ...above was written before OP clarified multiple colours in text file...

  • Using `less` in place of `cat` or even just `realcolour=$(<./color_readme.txt)` is not sensible. `less` is designed for interactive use; it reduces to `cat` when used where the output is not going to a terminal, as in this example where you're trapping the input in a shell variable. – Jonathan Leffler Oct 14 '12 at 22:00
  • not accounting for spelling mistakes, random acts of caPitAliSatIoN, etc, etc, bust the above is a basic version of what I think you're asking..? Give more detail... – Richard Thomas Oct 14 '12 at 22:00
  • Thanks a lot Richard, i never knew bash had an until command! I only have experience with windows cmd / batch file stuff. – Ben Oct 14 '12 at 22:12
2
echo "name a color"
read i
grep -q $i color.txt
if [ $? == 0 ]
then
echo "$i is in the file"
else
echo "$i is not in the file"
fi

"if [ $? == 0 ]" tests the exit status of the previous command, which in this case was grep. If grep finds something it will have an exit status of 0, and if not, an exit status of 1.

ricksebak
  • 63
  • 1
  • 6