0

I did try and find the answer online. I cant find inbuilt command to check data type of the read command input.

This is the following script I am noobie creating below. For some reason the comparison is not working when I am inputing int==1.

I have tried comparing to "1" vs 1, but both do not result in True and end up executing the else command?

echo "1. Start Session"
echo "2. Entry Here"
echo "3. Entry here"

read option1
echo "you selected" $option1

if [ $option1 == "1" ]
then    
  echo "aaaaaaaaaah!!"
else
  echo "nooooooooo!"
fi

Why do I get "noooooo!" and how can I change this to "aaaaaaaaah!"

Thank you in advance!!

  • What exactly are you entering when prompted? You write `int==1`, but is that what you're typing? – Benjamin W. Sep 29 '22 at 14:51
  • 1
    Try adding `set -x` to your script and running it, this will show you what the contents of your `$option1` variable are at the time of the comparison and might make it clearer why the comparison is failing. – tjm3772 Sep 29 '22 at 15:05
  • Note that `==` is not a valid string comparison in baseline /bin/sh; the only POSIX-standardized comparison operator is `=`. – Charles Duffy Sep 29 '22 at 15:09
  • Also if this is the whole script, add a valid shebang to the top such as `#!/bin/bash` to make sure you're not running as some other shell. – tjm3772 Sep 29 '22 at 15:09
  • 1
    (also, only tag for one or the other of bash and zsh, never both at once) – Charles Duffy Sep 29 '22 at 15:09

1 Answers1

-1

For numeric comparision change == for -eq

E.g

echo "1. Start Session"
echo "2. Entry Here"
echo "3. Entry here"

read option1
echo "you selected $option1"

if [ "$option1" -eq 1 ]
then    
  echo "aaaaaaaaaah!!"
else
  echo "nooooooooo!"
fi

Your question is answered here:

Shell equality operators (=, ==, -eq)

X T
  • 445
  • 6
  • 22
  • 1
    your answer is totally correct – X T Sep 29 '22 at 15:00
  • Should also be quoting expansions. `"$option1"`, not bare `$option1` – Charles Duffy Sep 29 '22 at 15:06
  • ...without the quotes, consider what happens if the OP types `-n foobar -o 1` at the prompt; `[ -n foobar -o 1 -eq 1 ]` is true, but you certainly don't _want_ the script to call it success. – Charles Duffy Sep 29 '22 at 15:07
  • @XT, it's correct as far as it goes, _but_ `test "1" = 1` is true because a comparison between `1` and `1` is also true when compared as strings, not _just_ when they're compared as integers. It'd make a meaningful difference if option1 contained `01` or `001` or something else that's a different string that evaluates to the same number, but we have no reason to believe that to be the case here. – Charles Duffy Sep 29 '22 at 15:08
  • 1
    @CharlesDuffy A potential benefit of `-eq` is that you'll get an error if one of the values isn't numeric. – Barmar Sep 29 '22 at 15:11
  • @Barmar, to be sure, yes. I'm not saying it's not better practice; I'm just saying it's not an adequate explanation for the OP's problem here. – Charles Duffy Sep 29 '22 at 15:12
  • @CharlesDuffy I do not agree with you – X T Sep 29 '22 at 15:36
  • @XT, don't agree with me about what? That `[ 1 = "1" ]` is true? You can try it yourself; it's not a matter of opinion but simple fact. Or do you disagree that `[ $option1 -eq 1 ]` can incorrectly return true if `option1='-n foobar -o 1'`? Because that's a demonstrably, testably true fact as well (in sh or bash, not zsh). – Charles Duffy Sep 29 '22 at 15:39
  • 1
    (I would also suggest `echo "you selected $option1"`, with the `$option1` _inside_ the double quotes; otherwise, you're prone to the bug described in [I just assigned a variable, but `echo $variable` shows something else](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else)) – Charles Duffy Sep 29 '22 at 15:43
  • 1
    @CharlesDuffy consider to upvote the answer after correct it. – X T Sep 29 '22 at 15:45
  • @peterpan, the only reason I'm not upvoting is that while the answer describes an improvement to the practices in the OP's code, it doesn't explain why the OP had a problem in the first place. `==` is indeed bad form (as a non-POSIX extension), whereas `=` is the right way to do a string comparison and `-eq` the right way to do a numeric comparison; but if the OP has the value `1` assigned to `option1`, _either_ a string comparison or a numeric comparison should work identically. – Charles Duffy Sep 29 '22 at 15:46
  • From where I stand, then, this question doesn't have enough information to let others reproduce the stated problem and thus shouldn't be answered at all. (see the _Answer Well-Asked Questions_ section of [How to Answer](https://stackoverflow.com/help/how-to-answer)). – Charles Duffy Sep 29 '22 at 15:47