Bash Script is not working [on hold]
Bash Script is not working [on hold]
When i am writing the script like this
#!/bin/sh
pidof MX-CM48 | xargs kill
if [ -f /home/root/MX-CM48NEW ]; then
mv /home/root/MX-CM48NEW /home/root/MX-CM48
chmod 777 /home/root/MX-CM48
fi
cd /home/root
./MX-CM48 &
the script is working.
but when i am trying to write it like this:
#!/bin/sh
NEW_FILE="/home/root/MX-CM48NEW"
OLD_FILE="/home/root/MX-CM48"
PATH="/home/root"
APP_NAME="MX-CM48"
pidof $APP_NAME | xargs kill
if [ -f $NEW_FILE ]; then
mv $NEW_FILE $OLD_FILE
chmod 777 $OLD_FILE
fi
cd $PATH
./$APP_NAME &
the pidof and the if are not working.
This question appears to be off-topic. The users who voted to close gave this specific reason:
Possible duplicate of When to use xargs when piping?, How to set a variable to the output of a command in Bash?
– jww
Jul 1 at 10:02
Find out what the PATH variable means and why it makes sense to use lowercase letters for your own variables.
– Cyrus
Jul 1 at 10:30
2 Answers
2
Unless you copied all commands to /home/root
, it is pretty clear what is wrong.
/home/root
PATH="/home/root"
should (probably) be:
PATH="$PATH:/home/root"
or use full path-names, like in:
/usr/bin/pidof $APP_NAME | /usr/bin/xargs /bin/kill
This is not the right answer -- the
$PATH
variable has a special meaning, and you should never try to use it for something else.– Gordon Davisson
Jul 1 at 17:32
$PATH
I assumed, that there are some executable in
/home/root
that are called from the script and that, therefore, it had to be appended to the existing PATH; therefore the "probably". I agree that your explanation is more elaborate and perhaps therefore more helpfull for understanding the meaning of PATH.– Ljm Dullaart
Jul 2 at 16:46
/home/root
The PATH
environment variable defines where the system looks for the executables corresponding to commands. It's a colon-delimited list of directories that contain executable files, something like /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
. So when you use a command like mv
, the system looks for a file named "mv" in /usr/local/bin, then in /usr/bin, then in /bin etc.
PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
mv
When you set PATH
to "/home/root" in the script, that then becomes the only place the system will look for command executables. I presume that's not what you wanted at all.
PATH
The solution is simple: use a different name for your variable. Actually, it's best to use lower- or mixed-case variable names for your things, because there are a lot of all-caps variable names that have some sort of special meaning and it's hard to remember & avoid all of them.
BTW, you should (almost) always double-quote variable references, e.g. pidof "$APP_NAME"
to avoid unexpected weirdness from the way the shell parses unquoted variables. Finally, when you use cd
in a script, you should always check for an error; otherwise if the cd
command fails, the rest of the script will blindly continue executing in the wrong place.
pidof "$APP_NAME"
cd
cd
cd "$path" || {
echo "Error moving to $path directory; giving up" >&2
exit 1
}
"./$app_name" &
Also see How to use Shellcheck, How to debug a bash script? (U&L.SE), How to debug a bash script? (SO), How to debug bash script? (AskU), Debugging Bash scripts, etc.
– jww
Jul 1 at 10:02