lzma command not found when executing shell script only under sudo - linux

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.

Related

Cannot run .sh script under sudo in linux

I have a script foo.sh located in /home/pi/Documents/Python directory. Purpose of this shell script is to run python script which needs root priviledges as it must reset usb device.
The script is as follows:
#!/bin/sh
export PATH="$PATH:/home/pi/.local/lib/python3.7"
python3 /home/pi/Documents/Python/foo.py
When I run the foo.py from Midnight Commander (setting a cursor on the file and pressing enter) it works, it exports the path correctly and the python script fails as it does not have enough priviledges to reset usb device.
I have actually made this script to run python script under root, but the root needs set a path to used module first.
However when I run
sudo foo.sh
I receive an answer:
sudo: foo.sh: command not found
I have checked the permissions and the foo.sh file has -rwxr-xr-x
sudo python3
typed in terminal also works correctly and opens python interpreter.
What is the problem that causes wrong behaviour under sudo?
I might be mistaken (I don't have a Linux Machine at hand atm, so I cannot verify), but if I recall correctly the user_home is part of the PATH variable exported for that user.
When you use the command sudo you are acting on the behalf of root which has got a different user_home than yours (== the current user), therefore your script is not found in any of the directories listed in the active PATH (the one of root because you are using the sudo command).
However, it should be possible to run successfully the following command:
$ sudo ./foo.sh
I hope this might shed some light.
Unless foo.sh is in a directory shown referenced by the PATH environmental variable, the environment will not recognise the command and hence the error
If you are in the directory with the foo.sh script, execute it with:
sudo ./foo.sh
If you are in a different directory, execute with:
sudo /pathtosh/foo.sh

How do I bind a terminal command to the execution of a shell script?

Say I have a programme that's run from a shell script:
cd /path/to/file/
./programme
How would I bind that to a single command so I only have to type one thing? ie for most installed programmes I can just type the name of the programme and it's running
programme
you must add your script path in $PATH variable in ~/.bashrc file ,like this:
export PATH=$PATH:/path/to/file
or put your script in linux binary directory like /usr/local/bin , /usr/bin
For this you need to create a script with a name and give execute permission to it. Then Copy that script to /usr/bin directory . Now you can run your scrpt as a command in terminal. For details please refer the link https://devopsmanual.in/2018/04/17/create-our-own-script-in-linux/

"cannot execute binary file" when trying to run a shell script on linux

I am very new to linux and shell scriprting.
I am trying to run a shellscript from secure shell (ssh) on linux using following commands:
chmod +x path/to/mynewshell.sh
sh path/to/mynewshell.sh
I get this error:
path/to/mynewshell.sh: path/to/mynewshell.sh: cannot execute binary file.
Tried using this command:
bash path/to/mynewshell.sh
I get the same error.
Tried with this command: su - myusername sh path/to/mynewshell.sh
It is asking for my password and giving me this error: no such file or directory.
1.The result of cat -v path/to/mynewshell.sh is:
^#^#^#^#^#^#^#^#Rscript "$dir"/diver_script.R
done
2.When tried 'less path/to/mynewshell.sh' i got this on my terminal:
#!/bin/bash/Rscript^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#
^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#
for dir in /path/to/* ; do
^#^#^#^#^#^#^#^#Rscript "$dir"/myRscript.R
done
3.When i ran file path/to/mynewshell.sh : i got this "Bourne-Again shell script text executable"
Please give any advice on how I can try executing the shellscript.
chmod -x removes execution permission from a file. Do this:
chmod +x path/to/mynewshell.sh
And run it with
/path/to/mynewshell.sh
As the error report says, you script is not actually a script, it's a binary file.
I was getting the same error running my shell script through a bash interpreter in PowerShell. I ran dos2unix myscript.sh on the shell script, and now it runs ok.
From a proposed duplicate:
run_me.sh.xz: run_me.sh.xz: cannot execute binary file
This is because the file is compressed, as indicated by the .xz extension. You need to remove the compression before the file can be used.
xz -d ./run_me.sh.xz
chmod +x ./run_me.sh # probably not necessary if you already did that before
./run_me.sh
Other compression schemes like gzip (.gz extension), bzip2 (.bz2 extension) etc behave similarly; you just have to know the name of the command to uncompress it, which is of course usually easy to google.
To anyone else having the problem i had.
i was trying to run a 16 bit unicode text file converted to a shell script, this doesn't work as all 16 bit unicode text files have a 0xFFFE marker at the start making mac os not like the file and this gives the “cannot execute binary file” error.
open the text file click on "Format" at the top, go down to "Make Plain Text" click it.
open your terminal type chmod 777 /path/to/file.sh
put in terminal: /path/to/file.sh to run it
That script is simply not a shell script.
A shell script is usually readable and contains shell code.
The output your cat command shows looks indeed like it's a binary of some sort.
As some note, it might be because of a file conversion issue when copying but it looks more like an actual binary to me.
You can check what it is identified as with the file command so:
file path/to/mynewshell.sh
Just start with a clean script and rewrite the code, it looks like you just want to run some R scripts in a directory?
Make sure the R scripts point to the right R script executioner.
In my case I had a bash script that would not execute. The file was originally generated from a find ... -print0 command. Leaving a \0 character the script, removing that character solved my problem.

Linux: Run script in bin with sudo

So I'm sure this is written somewhere, and I'm just wording my searches wrong, but I haven't been able to find it.
I write shell scripts. Many of those scripts require superuser permissions. Rather than putting the sudo command inside the script, I want to invoke the entire script from the start with sudo.
So instead of:
#!/bin/bash
# Sample Script 1
sudo mkdir /usr/tempDir
It would be:
#!/bin/bash
# Sample Script 2
mkdir /usr/tempDir
And I would just invoke it like this: sudo ./sampleScript.sh
My problem is that when I write a script, I simlink it to my bin, so I don't need to worry about the path to the script when I execute it.
If I have the file simlinked to my bin, and I run it as just: sampleScript.sh, it is recognized (and will fail). However, if I do: sudo sampleScript.sh, I get a "sudo: sampleScript.sh: command not found" message.
This happens if it's in both /home/myName/bin and /usr/local/bin.
I know there has to be a trick to making this work that I'm missing. And I also know I'm probably just not wording my google searches properly. Just looking for some guidance to figure this part out.
Thanks.
When you call sudo the PATH changes ie it's not using yours. Look here
sudo changes PATH - why?
It does this because it's now running as root, not as your user.

How can I make executables in my `PATH` available to GNU Make if they need to be run with sudo?

Consider the following Makefile.
install:
sudo rpi-install.py /dev/ttyUSB0 foo.bin
Note that I have deliberately not hardcoded a path to rpi-install.py because it is not in the same location on other people's machines, but I expect it to be in the PATH of everyone who uses my code.
Unfortunately, when I type make install, I get the following output.
sudo rpi-install.py /dev/ttyUSB0 larson.bin
sudo: rpi-install.py: command not found
make: *** [install] Error 1
When I type the exact same command on my shell, it works exactly as expected.
Additionally, when I remove the sudo from the Makefile, it successfully finds the binary and gets a permission denied error due to lack of root privileges.
How can I allow make to discover the programs that are in my PATH when they must be run with sudo?
For the sake of reproducibility, assume that the following contents are in rpi-install.py, and that it lives in the directory $HOME/bin. Additionally assume that PATH includes $HOME/bin.
#!/usr/bin/env python
print "Hello World!"
There was a combination of two fixes that resolved this problem.
I needed to set PATH in .profile instead of .bashrc because the default shell /bin/sh used by make did not pick up the correct path from .bashrc.
I needed to set the environment for the sudo command inside the makefile explicitly to be the external PATH, based on this answer to this question.
sudo env "PATH=$(PATH)" rpi-install.py /dev/ttyUSB0 larson.bin

Resources