1

I made this bash function to detect if the user running is actually logged in as root user and not using sudo by checking the uid + home directory of the user executing the command:

#!/bin/bash

set -x
function check_root(){
  home=`sh -c 'cd ~/ && pwd'`
  if [ "$home" != "/root" ] || [ "$(id -u)" != "0" ]; then
    echo -e "This script can only be executed by the root user, not with sudo  elevation"
  exit 1
  fi
}

check_root

When i run it as a regular user (uid 1000) it works as expected:

++ check_root
+++ sh -c 'cd ~/ && pwd'
++ home=/home/jake
++ '[' /home/jake '!=' /root ']'
++ echo -e 'This script can only be executed by the root user, not with sudo    elevation'
This script can only be executed by the root user, not with sudo elevation
++ exit 1

When i run it as root it also works as expected:

++ check_root
+++ sh -c 'cd ~/ && pwd'
++ home=/root
++ '[' /root '!=' /root ']'
+++ id -u
++ '[' 0 '!=' 0 ']'

But when i run it as regular user (uid 1000) with sudo elevation i get this:

./check_root.sh: 4: ./check_root.sh: Syntax error: "(" unexpected

System info:

Linux jake 3.11.0-26-generic #45~precise1-Ubuntu SMP Tue Jul 15 04:02:35 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

bash --version GNU bash, versie 4.2.25(1)-release (x86_64-pc-linux-gnu)

8
  • Does the error disappear if you write function check_root () { instead? Does it appear if you run it as normal user with env - ./check_root.sh? Commented Nov 14, 2014 at 17:47
  • 2
    The error message indicates that the script is not running under bash. On many systems, for example, dash is the default shell and it would give that exact error message. The details of how you invoke the script would be important here.
    – John1024
    Commented Nov 14, 2014 at 17:53
  • 1
    Does it help to use standard POSIX shell function syntax instead of the bash extension? Leave out the function keyword at the beginning.
    – Barmar
    Commented Nov 14, 2014 at 18:51
  • 1
    Why are you checking $home rather than $HOME or, better, the output of $(whoami)?
    – DopeGhoti
    Commented Nov 14, 2014 at 20:20
  • 2
    Exactly what command are you using to run the script with sudo? What is the output of <check_root.sh head -n 1 | od -t x1? Commented Nov 14, 2014 at 22:42

1 Answer 1

0

I had solved this in the mean time but did not yet post the solution - turned out to be syntax-related:

Working solutions:

function check_root {
 stuff..
}

and

function check_root () {
 stuff..
}

Eg removing the () from the function declaration or ensuring it is sepaerated by spaces at both sides fixes it.

Here's what i found in the bash manpage that made me spot the mistake :

Shell Function Definitions
       A shell function is an object that is called like a simple command and executes a compound command with a new set of positional parameters.  Shell functions are declared as follows:

       name () compound-command [redirection]
       function name [()] compound-command [redirection]
              This defines a function named name.  The reserved word function is optional.  If the function reserved word is supplied, the parentheses are optional

But.. i have no clue what exactly makes bash behave this way in this case.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .