我以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
答案 0 :(得分:0)
为什么不使用命令标志-c
来表示
> foo=bar; su marshell -c "./suc $foo"
bar
其中脚本只打印传递给它的所有参数。 在您的具体情况下,它应该类似于
> 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.
答案 1 :(得分:0)
您需要引用应逐字传递的部分。如果您的命令行参数是foo
,bar
,baz
和quux
,并且您希望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
遗憾的是,不正确引用任何包含空格的参数。
更好的方法实际上是
sudo -u marshell java -cp /home/marshell/sanity_test_scripts/ JavaRunCommand "$@"