使用sudo执行2(类似)命令,给出相同的输出

时间:2021-07-06 02:41:05

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

这是我的shell脚本,我正在尝试以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?

我仍然获取当前目录的内容,而不是获取/ opt的内容。这是导致htis的ls之后的空间吗?我该如何解决?

1 个解决方案

#1


1  

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

问题是你的脚本只接受--cmd之后的一个参数。这意味着您的命令只能包含一个令牌。

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.

我建议您将所有位置参数作为运行命令,并使用“$ @”将其传递给sudo。为了实现这一点,您需要避免像现在这样转移所有非选项参数。

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:

然后,对于脚本的其余部分,将$ CMD_OR_SCRIPT的所有实例替换为“$ @”(需要双引号)。另外,运行脚本时不要指定--cmd。因此,对于您的样本用法,您应该使用:

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

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

根据OP的要求,这是整个脚本的编辑版本:

#!/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

#1


1  

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

问题是你的脚本只接受--cmd之后的一个参数。这意味着您的命令只能包含一个令牌。

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.

我建议您将所有位置参数作为运行命令,并使用“$ @”将其传递给sudo。为了实现这一点,您需要避免像现在这样转移所有非选项参数。

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:

然后,对于脚本的其余部分,将$ CMD_OR_SCRIPT的所有实例替换为“$ @”(需要双引号)。另外,运行脚本时不要指定--cmd。因此,对于您的样本用法,您应该使用:

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

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

根据OP的要求,这是整个脚本的编辑版本:

#!/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