0
#!/bin/sh
#datafile
#script

while echo enter name
read -r code
do
if [ "$code" = 'STOP' ];
then
   break
else
grep "$code" datafile > outputres
while echo ' Would you like to see B OR R'
read -r answer
do
if [[ "$answer" = 'B'  && "$code" = datafile ]];
then
   echo show B.
   break
elif [[ "$answer" = 'R'  &&  "$code" = datafile ]];
then
   echo show R
   break
elif [[ "$answer" = 'B'  &&  "$code" != datafile ]];
then
   echo No such THING.
   break
elif [[ "$answer" = 'R' && "$code" != datafile ]];
then
   echo No such THING.
   break
else :
   echo Enter only B or R.
fi
done

fi
done
echo Goodbye!

Hello, I hope someone can help. but I'm having some issues when I execute my program I receive "no such thing" when it should display "show r".

UPDATE: I changed code to a number found in the data and it works, so I need a better way to say ("$code" = filename) number is in data or not in the data.

btw I have also checked if i have any errors and I dont

  • 2
    You're missing a space at the end of every `[ $code = filename]` test, which makes the condition invalid and probably explains your problem – Aaron Mar 07 '22 at 12:53
  • Also, you need to quote `"$answer"` and `"$code"` – Charles Duffy Mar 07 '22 at 12:53
  • 3
    Run your code through http://shellcheck.net/ and fix all the warnings it emits. – Charles Duffy Mar 07 '22 at 12:53
  • 3
    Beyond that -- we need a [mre] to contain enough information to have the same _expected_ output you state, and the same failure mode you state. Right now `$code` is never set, and the value `answer` contains is subject to change as user input changes. Hardcode the input so it generates the same output every time -- then, if the output is still wrong, there are fewer undefined / subject-to-change variables. – Charles Duffy Mar 07 '22 at 12:55
  • i have added the full script, hopefully it makes it clearer – violet roxy Mar 07 '22 at 15:20
  • What are you entering for the `code` and `answer` values? – Gordon Davisson Mar 07 '22 at 18:36
  • @GordonDavisson for my program it will prompt me to input a number (6000) ... btw 6000 is in my data file and then for my answer I enter (B) however, I will receive "no such thing" when I should receive "show B" – violet roxy Mar 07 '22 at 18:45
  • "6000" is not the same string as "datafile", so `[[ 6000 = datafile ]]` will be false. If you're trying to find out if a value is in a file (which is completely different from string comparison), you'd generally use `grep -q` (see [this Q&A](https://stackoverflow.com/questions/56170215/what-is-the-point-of-grep-q)). – Gordon Davisson Mar 07 '22 at 19:29

0 Answers0