0

i am getting an error [: missing `]' in checking the condition of a string after getting the input of a user in BASH

did i miss something?

#!/bin/bash

read -p "Do You want to Proceed The Process? (yes/no) : " input

echo "you choose : '$input'"



while [ "$input" != "no" && "$input" != "yes"  ]
 do
    echo "unknown input '$input' please only input (yes/no) \n"
    read -p "Do You want to Proceed The Process? (yes/no) : " \input
done

if [ "$input" == "no" ]
    then 
        echo "ok then bye :)"
        exit
elif [ "$input" == "yes" ]
    then
        echo "nice!, here's the nuclear code : 177013"
        # Do not search this code ^ btw
fi

exit
  
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 1
    You can't use `&&` inside `[`. `&&` separates commands. `[` _is a single command_. There's literally a `/bin/[` on your filesystem; even if the shell provides a faster built-in version, for syntax parsing reasons it acts just like any other command. That's why legacy `[` has `-a`, though good practice would be to run two separate `[` commands instead, and use `&&` to combine them (`[ "$input" != no ] && [ "$input" != "yes" ]`). – Charles Duffy May 14 '22 at 16:48
  • 1
    Also, `==` isn't a POSIX-standardized string comparison operator; you're looking for `=`. See https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html -- if you don't follow the standard your script will fail with some `/bin/sh` implementations. – Charles Duffy May 14 '22 at 16:51
  • 1
    Consider making a habit of running your code through http://shellcheck.net/ (which is also installable as a local command) before asking questions here. – Charles Duffy May 14 '22 at 16:51
  • OMG nice, thanks for the Great advice Charles :) – Noxindocraft May 14 '22 at 16:56
  • i literally have been digging out some shady old looking bash tutorial to came up with that solution lmao – Noxindocraft May 14 '22 at 16:58
  • 1
    The reference source I recommend is the [BashGuide](https://mywiki.wooledge.org/BashGuide) -- the same wiki also has a BashFAQ, BashPitfalls page, etc. Also generally very good is the [bash-hackers' wiki](https://wiki.bash-hackers.org/). – Charles Duffy May 14 '22 at 17:13

0 Answers0