linux - 如何以不同用户身份运行 bash 命令?

标签 linux bash su

我以 root 身份登录但是,我尝试使用名为 marshell 的不同用户从 bash 脚本运行 java 程序。我收到以下错误,不确定我做错了什么?

#!/bin/bash
sudo su marshell <<'EOF'
CP=/home/marshell/sanity_test_scripts/ # The classpath to use
java -cp $CP JavaRunCommand $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}
EOF

更新:

#!/bin/bash
su marshell <<EOF
CP=/home/marshell/sanity_test_scripts/US_SOUTH/YP/DataWorks/free
java -cp "$CP" JavaRunCommand "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}"
EOF

错误:

root@devopsops:bash runbash_marshell.sh https://api.xyz.net user@gmail.com pass AllServices test Data free data-monitor Y00 UKLONDON

runbash_marshell.sh: line 5: warning: here-document at line 3 delimited by end-of-file (wanted `EOF')
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
        at JavaRunCommand.main(JavaRunCommand.java:308)
bash: line 2: EOF: command not found

最佳答案

为什么不使用命令标志 -c 来执行 su 之类的

> foo=bar; su marshell -c "./suc $foo" 栏

其中脚本仅打印传递给它的所有参数。 在您的具体情况下,它应该看起来像

> su marshell -c "CP=/home/marshell/sanity_test_scripts/; java -cp $CP JavaRunCommand $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}"

> sudo -u marshell -i "CP=/home/marshell/sanity_test_scripts/; java -cp $CP JavaRunCommand $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}"

对于 sudo,您可以使用类似参数 -u-i 的内容。

-u user     The -u (user) option causes sudo to run the specified
               command as a user other than root.  To specify a uid
               instead of a user name, use #uid.  When running commands as
               a uid, many shells require that the '#' be escaped with a
               backslash ('\').  Security policies may restrict uids to
               those listed in the password database.  The sudoers policy
               allows uids that are not in the password database as long
               as the targetpw option is not set.  Other security policies
               may not support this.

-i [command]   The -i (simulate initial login) option runs the shell
               specified by the password database entry of the target user
               as a login shell.  This means that login-specific resource
               files such as .profile or .login will be read by the shell.
               If a command is specified, it is passed to the shell for
               execution via the shell's -c option.  If no command is
               specified, an interactive shell is executed.  sudo attempts
               to change to that user's home directory before running the
               shell.  The security policy shall initialize the
               environment to a minimal set of variables, similar to what
               is present when a user logs in.  The Command Environment
               section in the sudoers(5) manual documents how the -i
               option affects the environment in which a command is run
               when the sudoers policy is in use.

关于linux - 如何以不同用户身份运行 bash 命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28017530/

相关文章:

c++ - 从 Linux 到 Windows 交叉编译时应该如何处理字符编码?

c - 原始套接字 linux

bash脚本条件检查语句抛出错误

bash - 退出管道中的 BASH 无限循环

linux - 如何使用/dev/null 进行 bash 命令

linux - 树莓派 3 上的 ftrace + addr2line

linux - 从 Linux 命令行查找 PDF 中的颜色空间

java - 在java程序中执行bash命令

python - 通过 Python 脚本更改用户 (su) (pexpect, popen...)

ssh - 在远程服务器上发出命令时如何允许输入密码?