8

I have written a bash script that gets three paths based on input parameters and then then gets the imagename/filename in the path.

Something like:
I provide:

AA=/home/user

Then it uses the find command to get
/home/user/dir2/images/dir/tellmeimage1fun.bin

Finally I have to 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 complete paths to ls and cut they work. But i don't want to do that for all commands in the script. If i remove the last line and echo the PATH variable it is completely fine. Only after adding the last command, I see the problem.

What am I doing wrongly?

codeforester
  • 39,467
  • 16
  • 112
  • 140
Pkp
  • 929
  • 6
  • 19
  • 31

6 Answers6

18

The problem is that you are redefining the PATH variable where bash looks into to find the binary files if you don't use a complete path when calling.

You should change the PATH in your bash script to MYPATH or something like that, so that it doesn't mess with the already environmental variables.

If you don't know what the PATH variable is for you can look at wikipedia's article

Santiago Alessandri
  • 6,630
  • 30
  • 46
  • 5
    A better solution is to never use upper case for any private variable. Uppercase is reserved for system variables; using lower case ensures that you won't accidentally clobber some other system variable with the next variable name you try. See also https://stackoverflow.com/questions/673055/correct-bash-and-shell-script-variable-capitalization – tripleee Apr 18 '19 at 09:05
3

I had this problem, turns out editing a bash script using Notepad++ was adding DOS line endings instead of UNIX line endings. Running the script in a Linux environment was causing the 'command not found' error to be thrown.

Managed to diagnose the problem by running my script like so:

bash -x testscript.sh

Which will dump any compiler output. The error message that gets thrown is:

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

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

Tom Walsh
  • 31
  • 5
  • This is also a common FAQ, but different from the question here. For a fuller treatment, see https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings – tripleee Nov 05 '19 at 20:02
2

$PATH is a special environment variable that contains a list of directories where your shell (in this case, bash) should look in when you type a command (such as find and ls.) Just try echo $PATH in a script or in a shell to get a feeling of what it looks like (you will typically have /bin, /usr/bin and /usr/local/bin listed there, maybe more.)

As you don't really need to redefine this variable in this particular script, you should use another name than $PATH.

codeforester
  • 39,467
  • 16
  • 112
  • 140
1

$PATH is a predefined variable which gives the directories to search when looking for executables. Pick a different variable name for your script and you'll be fine.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

Use a different variable name than PATH. $PATH is the environment variable which tells your shell where to look for executables (so, e.g., you can run ls instead of /bin/ls).

entropo
  • 2,481
  • 15
  • 15
0

You are using the PATH that is special and used to locate the commands and that is why ls can't be resolved. Use any name other than 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`"
amit_g
  • 30,880
  • 8
  • 61
  • 118