Linux 如何以不同的用户身份运行bash命令?
我以root用户身份登录,但是,我正在尝试使用另一个名为marshall的用户从bash脚本运行java程序。我得到以下错误不确定我做错了什么Linux 如何以不同的用户身份运行bash命令?,linux,bash,su,Linux,Bash,Su,我以root用户身份登录,但是,我正在尝试使用另一个名为marshall的用户从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 为什么不使用s
#!/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
为什么不使用su-like的命令标志
-c
>foo=bar;苏马歇尔-c.“/suc$foo”
条形码
脚本只打印传递给它的所有参数。
在您的特定情况下,它应该看起来像
>su marshall-c“CP=/home/marshall/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.
你需要逐字逐句地引用应该传递的部分。如果命令行参数是foo
、bar
、baz
和qux
,并且希望Java执行
java -cp "$CP" JavaRunCommand foo bar baz quux
然后,您需要插入命令行参数,但要引用(美元登录)$CP
,因此当前shell不会插入它
#!/bin/bash
sudo su marshell <<EOF
CP=/home/marshell/sanity_test_scripts/ # The classpath to use
java -cp "\$CP" JavaRunCommand $@
EOF
虽然我找不到实际错误,但如果需要展开位置参数,则不能引用EOF
。无论如何,这是一个更好的方法:java-cp$cp JavaRunCommand“$@”
并且不要忘记在您的情况下取消引用EOF
:sudo su marshall如果您已经以root用户身份登录,为什么要使用sudo
?runbash_marshall.sh:第5行:警告:此处文档位于第2行,由文件结尾分隔(通缉'EOF')) ................ bash:line 3:EOF:command not found@j.a.我确实按照建议更新了我的代码,它确实起了作用,但我仍然看到上面的警告,不知道为什么?为什么要使用sudo su
而不是sudo-u
?我在问题中添加了更新后的脚本
java -cp "$CP" JavaRunCommand foo bar baz quux
#!/bin/bash
sudo su marshell <<EOF
CP=/home/marshell/sanity_test_scripts/ # The classpath to use
java -cp "\$CP" JavaRunCommand $@
EOF
sudo -u marshell java -cp /home/marshell/sanity_test_scripts/ JavaRunCommand "$@"