0

My apologies if my issue has already been asked in other posts, but I wasn't able to find any. I am writing a little shell script which requires me to write a nested if statement. I am not really sure I am doing it right. I get no errors, but the program isn't functioning as I expect it. What I want is: If the file is already in MasterFile.txt, then the user has the option to either take another set of data by saying y or Y, or say n or N to terminate the program. Problem: Script does not terminate. If the file does not exist in MasterFile.txt, then take the data set. Here's the code:

if grep -q "dicounter_${string1}_from_${string2}" MasterFile.txt;
then {
   echo "dicounter_${string1}_from_${string2} already exists in the MasterFile. Would you like to proceed?"
   read string3
   if "${string3}" = 'Y' || "${string3}" = 'y'; then {
      screen -S trans -L /dev/ttyACM0
      screen -S trans -X stuff 's'$(echo -ne '\015')
      sleep 8s
      screen -S trans -X quit
   }
   else{return}
   fi
else{
#opening screen & begin analysis
screen -S trans -L /dev/ttyACM0
screen -S trans -X stuff 's'$(echo -ne '\015')
sleep 8s
screen -S trans -X quit
}
fi
9
  • Um... what shell is this? sh-type shells don't use { ... } for blocks.
    – Kusalananda
    Jun 1, 2017 at 16:43
  • I believe its bash. The program worked just fine with just one if statement. I don't think {...} is the problem. There must be something wrong with the way I implemented the nested if
    – Ptheguy
    Jun 1, 2017 at 16:46
  • Now I get an error that says there is something wrong with the last else statement
    – Ptheguy
    Jun 1, 2017 at 16:51
  • @Kusalananda, well, you could use them for command grouping there as well as elsewhere ...
    – ilkkachu
    Jun 1, 2017 at 17:19
  • @Ptheguy if "${string3}" = 'Y' takes the contents of string3 as the name of a command, and runs it with two arguments = and Y...
    – ilkkachu
    Jun 1, 2017 at 17:20

1 Answer 1

3

A quick rewrite with more conventional syntax:

if grep -q "dicounter_${string1}_from_${string2}" MasterFile.txt; then 
   echo "dicounter_${string1}_from_${string2} already exists in the MasterFile. Would you like to proceed?"
   read string3
   if [[ "${string3^}" == 'Y' ]]; then
      screen -S trans -L /dev/ttyACM0
      screen -S trans -X stuff 's'$(echo -ne '\015')
      sleep 8s
      screen -S trans -X quit
   else
       exit 0
   fi
else
    #opening screen & begin analysis
    screen -S trans -L /dev/ttyACM0
    screen -S trans -X stuff 's'$(echo -ne '\015')
    sleep 8s
    screen -S trans -X quit
fi

You don't appear to have any functions declared here, so return is not valid for this use-case. I have presumed that a non-affirmative answer to your prompted question is meant to abort the entire script, hence the use of exit.

The construct ${variable^} will force the first character of $variable, if a letter, to be upper-case, which simplifies the response check.

7
  • So two things: one if I enter y it quits, and regardless of what I enter, i get the following error ./test/sh: line19: [[variable: command not found
    – Ptheguy
    Jun 1, 2017 at 17:20
  • ensure that the first line of the script is #!/bin/bash, and not #!/bin/sh. [[ [...] ]] constructs are a bashism.
    – DopeGhoti
    Jun 1, 2017 at 17:22
  • yes, the header is correct
    – Ptheguy
    Jun 1, 2017 at 17:24
  • And you have a space between [[ and "${string3^}"?
    – DopeGhoti
    Jun 1, 2017 at 17:25
  • That was it!! Thank you once more. If you have the time, do you mind teaching me what [[ means and why a space can be so important?
    – Ptheguy
    Jun 1, 2017 at 17:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .