简体   繁体   中英

lzma command not found when executing shell script only under sudo

I am building project source code in a SUSE server. The project build.sh called "lzma" command to compress kernel. The project build.sh need "sudo" to get access to some system command. But I has tried to execute "sudo ./build.sh", and the shell always report error: "lzma: command not found."

I could execute "lzma" in shell with my user account. It works fine. I also write a test shell script named "test.sh" which calls "lzma" command. I found that it fails with same error message if I excute "test.sh" with "sudo" . But if I execute "test.sh" without "sudo", it works fine. Why ?

"Command not found" within sudo is almost invariably the result of an environment variable such as PATH, LD_LIBRARY_PATH (if what's missing is not the executable but a shared library it requires) or the like being altered.

You can pass working values through your environment variables through explicitly:

sudo PATH="$PATH" ./test.sh

Sudo uses a different Path then your user account.

EDIT (see comments)

Try and execute:

type lzma

Say the output reads something like '/usr/bin/lzma', then just copy that output into your sudo command like (for example):

sudo /usr/bin/lzma

That should do the trick. You should also write the full path of lzma into your shell script if you are to run it as root.

EDIT 2:

Or, as Charles Duffy mentioned in his answer, you could leave all things as is and simply use PATH="$PATH" in your command if you are trying to execute your file as SUDO or as a different user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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