-2
echo "Enter Username"
read name
username = 'hagemaru'
if [ $name == $username ]
  then
    echo "Valid Username"
else
    echo "Entered Username is invalid"
    exit 1
fi
exit 0

Errors while executing:

l.sh: 3: username: not found
l.sh: 4: [: hagemaru: unexpected operator

Output should be:

Valid Username
Cyrus
  • 84,225
  • 14
  • 89
  • 153

1 Answers1

0

You need to remove the space between username and =, otherwise bash will interpret username as a command (as indicated in the error message) which doesn't exist. Doing this, you also need to remove the space between username= and 'hagemaru' as bash will interpret username= 'hagemaru' as running the command username= with a parameter of 'hagemaru' which is not valid as there is no username= command in bash.

echo "Enter Username"
read name
username='hagemaru'
if [ $name == $username ]
  then
    echo "Valid Username"
else
    echo "Entered Username is invalid"
    exit 1
fi
exit 0
Mehdi Charife
  • 722
  • 1
  • 7
  • 22
  • Still this error is coming: Login.sh: 4: [: hagemaru: unexpected operator – Rishab Parashar Mar 06 '23 at 08:47
  • Did you also remove the space between `=` and `'hagemaru'`? Try using the code in my answer. – Mehdi Charife Mar 06 '23 at 08:48
  • I tried your code still the same issue. – Rishab Parashar Mar 06 '23 at 08:53
  • I don't think so. The code works fine. Maybe you failed to save the file or something. – Mehdi Charife Mar 06 '23 at 08:56
  • Try executing the code here to see that it works. https://www.onlinegdb.com/online_bash_shell – Mehdi Charife Mar 06 '23 at 08:59
  • Error: Enter Username ': not a valid identifier`name main.bash: line 12: syntax error: unexpected end of file – Rishab Parashar Mar 06 '23 at 09:04
  • Are you sure you are copying the code in the answer? – Mehdi Charife Mar 06 '23 at 09:08
  • Yes, I also tried this in different terminals. Same issue. – Rishab Parashar Mar 06 '23 at 09:19
  • Can you tell me where you are testing this code. – Rishab Parashar Mar 06 '23 at 09:19
  • I tested the code with my bash interpreter as well as in the one available at this site that I mentioned above https://www.onlinegdb.com/online_bash_shell – Mehdi Charife Mar 06 '23 at 09:20
  • 1
    @RishabParashar The "not a valid identifier" error suggests your script has DOS/Windows line endings rather than unix; see ["': not a valid identifier"](https://stackoverflow.com/questions/29576654/not-a-valid-identifier) and ["Are shell scripts sensitive to encoding and line endings?"](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) Also, in a `[ ]` test expression, `==` is nonstandard (although some shells tollerate it); use `=` instead. – Gordon Davisson Mar 06 '23 at 10:24
  • 1
    @RishabParashar Are you sure the script is run by `bash` not `sh`? POSIX compatible shells require `=` instead of `==`. Maybe insert a line `#! /bin/bash` – Bodo Mar 06 '23 at 14:46