0

I have written this bash script which is meant to check your input to see if it is y, n or something else (this will be part of a larger project):

#!/bin/bash

echo "Have you updated the PATH variable in your .bashrc file yet? [y/n]"
read response

        if [$response = "y"]; then
                echo "Checkpoint passed"
                set $checkpoint = "t"
        elif [$response = "n"]; then
                echo "Please set the PATH variable in your .bashrc file before running this script."
                set $checkpoint = "f"
        else            
                echo "Please only use 'y' and 'n'."
                set $checkpoint = "f"
        fi

But every time that I run it I get an error similar to this one no matter what I put in:

Have you updated the PATH variable in your .bashrc file yet? [y/n]
y
./snbjdkhome: line 6: [y: command not found
./snbjdkhome: line 9: [y: command not found
Please only use 'y' and 'n'.

So what is wrong with my code? (I am very new to shell scripting.)

1

4 Answers 4

5

This will make you clear (Check the Whitespaces):

$ var=c

$ ["$var" == "c"] && echo "OK"
[c: command not found

$ ["$var" == "c" ] && echo "OK"
[c: command not found

$ [ "$var" == "c"] && echo "OK"
bash: [: missing `]'

$ [ "$var" == "c" ] && echo "OK"
OK

So, you need to put whitespace prior to and after the condition while using test ([ ])command.

4

there is one simple error on line 6 and 9. There should be space between [ and $response in if [$response = "y"]then on line 6; . Similarly, for [ and $response on line 9`

Also you must put double qoutes around $response to avoid errors in case user enters a input with space.

3

Also, the set command is more exotic than you think.  You don’t use the set command to set variables.

You know that, when you invoke a shell script with parameters, that they get assigned to $1, $2, $3, …; right?  Well, one of the things the set command does is to set $1, $2, $3, … for the current (interactive) shell.  So, for example,

% set checkpoint="y"
% echo "$checkpoint"
                                (nothing)
% echo "arg1 = '$1', arg2 = '$2', arg3 = '$3'"
arg1 = 'checkpoint=y', arg2 = '', arg3 = ''
% set checkpoint = "y"
% echo "$checkpoint"
                                (nothing)
% echo "arg1 = '$1', arg2 = '$2', arg3 = '$3'"
arg1 = 'checkpoint', arg2 = '=', arg3 = 'y'

which is not what you want.

And you have another problem.  The correct syntax is

checkpoint="t"

If you say

$checkpoint="t"

and $checkpoint is not set, this command reduces to

="t"

which looks for a program called =t (which probably doesn’t exist).  Similarly,

$checkpoint = "t"

reduces to

= "t"

which looks for a program called =.  Even worse, if $checkpoint is already set to "t", and you say

$checkpoint="f"

then that is interpreted as

t="f"

thus setting the variable t (i.e., $t).

1

Bash interprets your $response as commands it can't find. Enclose them in double-quotes like the strings you compare them to, and it should work as expected.

You must log in to answer this question.