I have been trying to make a shell script in bash that will display the following:
You are the super user (When I run the script as root). You are the user: "user" (When I run the script as a user).
#!/bin/bash/
if { whoami | grep "root" }; then
echo $USER1
else
echo $USER2
fi
I keep recieving these syntax error messages:
script.sh: line 2: syntax error near unexpected token `then'
script.sh: line 2: `if { whoami | grep "root" }; then'
Could someone help me out?
;before the}. As the comment on that answer says, you don't need{...}at all. And once you get that fixed, you'll probably want to usegrep -qto hide the output, since all you care about is success or failure. Also, a simplegreplike this will give you a false positive if your user name is, for example,notroot.[ $(id -u) = 0]is probably the most reliable method. Grepping for the stringrootcan give you false positives, and$USERcan be changed by the user.