gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:46:31 25 4
gpt4 key购买 nike

我以 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com