Command not found error in bash script

I wrote a bash script. It basicaly gets three paths based on input parameters, and then gets the filename / filename in the path.

Sort of:
I provide:

AA=/home/user 

He then uses the find command to get / home / user / dir 2 / images / dir / tellmeimage1fun.bin

Finally, I should get tellmeimage1fun.bin as output.

Script:

 #!/bin/bash echo "arg0 n/k/d" AA=$1 CC=$3 PATH1="`find $AA/dir2/images/dir/ -name *image1*.bin`" PATH2="`find $AA/dir2/images/dir/ -name *bimage2*.bin`" PATH3="`find $AA/dir2/images/dir/ -name *cimage3*.bin`" if [ $CC = "n" ] ; then PATH=$PATH1 elif [ $CC = "k" ] ; then PATH=$PATH2 else PATH=$PATH3 fi #Getting filename name from path: IMG="`ls $PATH | cut -d "/" -f6`" OUTPUT: /users/prasapat/bin/sl5: line 22: ls: command not found /users/prasapat/bin/sl5: line 22: cut: command not found 

If I give full paths to ls and cut, they work. But I do not want to do this for all the commands in the script. If I delete the last line and repeat the PATH variable, it is completely perfect. Only after adding the last command I see a traffic jam.
Please help and let me know if I made an obvious mistake.

+4
source share
6 answers

The problem is that you are overriding the PATH variable, where bash is looking for binaries if you are not using the full path when called.

You should change the PATH in your bash script to MYPATH or something like that so that it does not bind to environmental variables.

If you don’t know what the PATH variable is for, you can see the wikipedia article

+10
source

The $PATH variable is a special environment variable containing a list of directories in which your shell (in this case, bash) should look like when you type a command (for example, find and ls ). ) Just try echo $PATH in a script or shell to get an idea of ​​what it looks like (usually you will specify /bin , /usr/bin and /usr/local/bin , possibly more.)

Since you really do not need to override this variable in this particular script, you should use a different name than $PATH .

+2
source

$PATH is a predefined variable that gives directories a search when searching for executable files. Choose a different variable name for the script and everything will be fine.

+1
source

Use a different variable name than PATH . $PATH is an environment variable that tells your shell where to look for executable files (for example, you can run ls instead of /bin/ls ).

0
source

You use PATH , which is special and is used to search for commands, and therefore ls cannot be resolved. Use any name except PATH

 if [ $CC = "n" ] ; then MY_PATH=$PATH1 elif [ $CC = "k" ] ; then MY_PATH=$PATH2 else MY_PATH=$PATH3 fi export MY_PATH IMG="`ls $MY_PATH | cut -d "/" -f6`" 
0
source

I had this problem, it turns out that editing the bash script using Notepad ++ consisted in adding DOS line endings instead of UNIX line endings. Running the script on Linux caused a "command not found" error.

Helped diagnose the problem by running my script as follows:

 bash -x testscript.sh 

Which will output any compiler output. An error message is displayed:

 bash -x testscript.sh + $'\r' : command not found 2: 'estscript.sh: line 3: syntax error near unexpected token `{ 

I fixed the problem by changing the formatting of line endings in Notepad ++ to be UNIX and not DOS by choosing Edit -> EOL Conversion -> UNIX.

0
source

Source: https://habr.com/ru/post/1347821/


All Articles