AlwaysALearner
AlwaysALearner

Reputation: 6690

Executing 2 (similar) commands using sudo giving same exact output

Here is my shell script where I'm trying to execute a command as sudo

#! /bin/sh

# Initialize values of arguments
SUDO_USER=""
SUDO_PWD=""
CMD_OR_SCRIPT=""

if ! options=$(getopt -u -o : -l sudoUser:,sudoPwd:,cmd: -- $@)
then
    echo "Wrong option given :: $1"
    exit 1
fi

set -- $options

while [ $# -gt 0 ]
do
    case $1 in
        --sudoUser)
                SUDO_USER="$2"
                shift
                ;;

        --sudoPwd)
                SUDO_PWD="$2"
                shift
                ;;

        --cmd)
                CMD_OR_SCRIPT="$2"
                shift
                ;;

    esac
    shift
done

if [ "$CMD_OR_SCRIPT" != "" ]; then
    if [ "$SUDO_USER" != "" ]; then
        if [ "$SUDO_PWD" != "" ]; then
            echo $SUDO_PWD | sudo -k -S -u $SUDO_USER $CMD_OR_SCRIPT
        else
            sudo -u $SUDO_USER -k -S $CMD_OR_SCRIPT
        fi
    else
        if [ "$SUDO_PWD" != "" ]; then
            echo $SUDO_PWD | sudo -k -S $CMD_OR_SCRIPT
        else
            sudo $CMD_OR_SCRIPT
        fi
    fi
fi

when i run the command as-

./execSudoScript.sh --sudoUser root --sudoPwd pass --cmd ls

I get the contents of current directory as output as expected. But if I run the command as-

./execSudoScript.sh --sudoUser root --sudoPwd pass --cmd ls /opt

I still get the contents of current directory, instead of getting the contents of /opt. Is it the space after ls that is causing htis? How do I fix this?

Upvotes: 0

Views: 139

Answers (1)

C. K. Young
C. K. Young

Reputation: 223133

The problem is that your script only accepts one argument after --cmd. That means that your command can only have one token in it.

I suggest that you take all positional arguments as the command to run, and pass that to sudo using "$@". For that to work, you will need to avoid shifting off all the non-option arguments like you're currently doing.

For example, you might do this:

while [ "$#" -gt 0 ]; do
    case "$1" in
    --sudoUser)
        SUDO_USER="$2"
        shift 2;;
    --sudoPwd)
        SUDO_PWD="$2"
        shift 2;;
    *)
        break;;
    esac
done

then, for the rest of your script, replace all instances of $CMD_OR_SCRIPT with "$@" (the double-quotes are required). Also, do not specify --cmd when running your script. Thus, for your sample usage, you should use:

./execSudoScript.sh --sudoUser root --sudoPwd pass ls /opt

At the OP's request, here's an edited version of the whole script:

#!/bin/sh

# Initialize values of arguments
SUDO_USER=""
SUDO_PWD=""

while [ "$#" -gt 0 ]; do
  case "$1" in
  --sudoUser)
    SUDO_USER="$2"
    shift 2;;
  --sudoPwd)
    SUDO_PWD="$2"
    shift 2;;
  *)
    break;;
  esac
done

if [ -n "$SUDO_USER" ]; then
  if [ -n "$SUDO_PWD" ]; then
    echo "$SUDO_PWD" | sudo -k -S -u "$SUDO_USER" "$@"
  else
    sudo -u "$SUDO_USER" "$@"
  fi
else
  if [ -n "$SUDO_PWD" ]; then
    echo "$SUDO_PWD" | sudo -k -S "$@"
  else
    sudo "$@"
  fi
fi

Upvotes: 1

Related Questions