简体   繁体   English

无法执行二进制文件错误

[英]Cannot execute binary file error

I've got a shell script which I am trying to run as a specific user. 我有一个shell脚本,试图以特定用户身份运行。 My command looks like this: 我的命令如下所示:

su - jetty sh ./runProgram.sh

When I attempt to run this command through the console I get an error saying: 当我尝试通过控制台运行此命令时,出现错误消息:

/bin/sh: /bin/sh: cannot execute binary file

I also tried: 我也尝试过:

 su - jetty sh runProgram.sh

And I still get the same error.. 而且我仍然遇到相同的错误。

It DOES work if I do this: 如果执行此操作,它将起作用:

sh runProgram.sh

But this shell script is meant to be run by a specific user. 但是,此Shell脚本应由特定用户运行。 Any advice on how to get this working?? 关于如何使它工作的任何建议?

尝试

su - jetty -c sh runProgram.sh

According to su 's documentation ( info coreutils 'su invocation' ), it will by default execute a shell, and the arguments to su are passed as arguments to the shell. 根据su的文档( info coreutils 'su invocation' ),它将默认执行一个shell,并将su的参数作为参数传递给该shell。

The implication is simply this: su is doing in essence: /bin/sh *arguments_to_su* but it does it as another user (the "effective user id")... that's all... So 含义很简单: su实际上是在做: /bin/sh *arguments_to_su*但是它是作为另一个用户(“有效用户ID”)来完成的……就这么简单了。

su - jetty sh ./runprogram.sh

is akin to 类似于

(become the user jetty via login or su)
/bin/sh sh ./runprogram.sh

...and the shell will report an error, because the first /bin/sh , called by su , is trying to run the program sh as a shell script, with ./runprogram.sh as its argument. ...并且外壳程序将报告错误,因为第一个/bin/sh (由su调用)正试图将程序sh作为外壳程序脚本运行,并以./runprogram.sh作为其参数。 But sh itself is not a shell script, it is a binary (whose job it is is to run shell scripts). 但是sh本身不是shell脚本,它是二进制文件(它的工作是运行shell脚本)。

if you were to simply do this: 如果您只是要这样做:

su - jetty ./runprogram.sh

Then the su command will call /bin/sh with the program ./runprogram.sh as its argument, and jetty as the effective user id, and all should be well. 然后, su命令将使用程序./runprogram.sh作为其参数,并使用jetty作为有效的用户ID来调用/bin/sh ,一切都应该很好。 ...SHOULD be well, because since you are doing an su - you are making the shell a login shell and changing to the user's home directory. ...应该很好,因为既然您正在执行su -您就将Shell设为登录Shell并更改为用户的主目录。 If runprogram.sh is not in the home directory, you will get an error. 如果runprogram.sh不在主目录中,则会出现错误。

This is why, also, you cannot run for example run a cp command by simply: 这也是为什么您也不能通过以下简单方式运行例如运行cp命令的原因:

su - jetty cp file1 file2

...because, again, after su changes effective user id to jetty it will try this: ...因为再次,在su将有效用户ID更改为jetty后,它将尝试以下操作:

/bin/sh cp file1 file2

...and cp is not a shell script. ...而且cp不是shell脚本。 But the -c option works in this case; 但是-c选项在这种情况下有效; because you are telling su that you want to run /bin/sh with the -c option to the shell: 因为您告诉su您想使用/bin/sh c /bin/sh shell选项运行/bin/sh

su - jetty -c "cp file1 file2"

does the job. 做这份工作。 Note that you must quote the command, because the entire string is passed to the shell and (I believe) any following arguments are ignored. 请注意,您必须引用该命令,因为整个字符串都将传递给外壳程序,并且(我相信)以下任何参数都将被忽略。

Finally, the previous poster's answer doesn't work for me on Linux, it requires the entire command string to be quoted. 最后,上一个发布者的答案在Linux上对我不起作用,它要求使用整个命令字符串加引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM