0

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?

2
  • As Ignacio's answer says, you need to have ; 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 use grep -q to hide the output, since all you care about is success or failure. Also, a simple grep like this will give you a false positive if your user name is, for example, notroot. Commented Mar 11, 2017 at 0:05
  • [ $(id -u) = 0] is probably the most reliable method. Grepping for the string root can give you false positives, and $USER can be changed by the user. Commented Mar 11, 2017 at 0:15

3 Answers 3

2

If braces are used to chain commands then the last command must have a command separator after it.

{ foo ; bar ; }
Sign up to request clarification or add additional context in comments.

1 Comment

And note that in this case, the braces aren't necessary: just do if whoami | grep root; then....ignoring the more obvious solution using id instead of whoami
1
 userType="$(whoami)"
 if [ "$userType" = "root" ]; then
    echo "$USER1"
 else
    echo "$USER2"
 fi

2 Comments

That's an awful lot of complication just to prevent checking the result code of grep...
true, don't think you need grep at all
0

pay attention with your first line, the correct syntax for she-bang is:

#!/bin/bash

everything you put there, is the interpreter of your script, you can also put something like #!/usr/bin/python for python scripts, but your question is about the if statement, so you can do this in two ways in shell script using

if [ test ] ; then doSomething(); fi

or

if (( test )) ; then doSomething(); fi

so to answer your question basically you need to do this

#!/bin/bash

if [ `id -u` -eq 0 ] ; then
        echo "you are root sir";
else
        echo "you are a normal user"
fi

if (( "$USER" = "root" )); then
        echo "you are root sir";
else
        echo "you are a normal user"
fi

note that you could use a command using `cmd` or $(cmd) and compare using -eq (equal) or = (same), hope this help you :-)

2 Comments

if tests the success or failure of a command. [ ... ] is just a special case of that (yes, [ is actually a command, not part of the shell's syntax, though it's typically a built-in command). Since the OP specifically wants to test the success or failure of the whoami | grep "root" command, there's no need for [ ... ] or (( ... )).
But yes, if [ $(id -u) -eq 0 ] ; then is probably the cleanest solution. Note that testing $USER can be unreliable, since any user can change the value of the $USER environment variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.