-2

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.

Ron
  • 33
  • 7
  • 1
    Also see [How to use Shellcheck](https://github.com/koalaman/shellcheck), [How to debug a bash script?](https://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](https://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](https://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Jul 01 '18 at 10:02
  • 1
    Possible duplicate of [When to use xargs when piping?](https://stackoverflow.com/q/35589179/608639), [How to set a variable to the output of a command in Bash?](https://stackoverflow.com/q/4651437/608639) – jww Jul 01 '18 at 10:02
  • 4
    Find out what the PATH variable means and why it makes sense to use lowercase letters for your own variables. – Cyrus Jul 01 '18 at 10:30

2 Answers2

4

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.

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.

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.

cd "$path" || {
    echo "Error moving to $path directory; giving up" >&2
    exit 1
}
"./$app_name" &
Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
0

Unless you copied all commands to /home/root, it is pretty clear what is wrong.

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
Ljm Dullaart
  • 4,273
  • 2
  • 14
  • 31
  • 4
    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 01 '18 at 17:32
  • 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 02 '18 at 16:46