Error "expr: command not found" in bourne script

4.9k views Asked by At

I am building a bourne script for a class, and am encountering an issue with this loop. Basically I am building a loop to go through all the files in a specified directory and determine how many files and directories there are as well as how many files are readable, writeable, and/or executable. The issue is, when I run the script it returns the following errors:

assignment4.sh: line 61: expr: command not found
assignment4.sh: line 61: expr: command not found
assignment4.sh: line 36: expr: command not found
assignment4.sh: line 41: expr: command not found
assignment4.sh: line 48: expr: command not found
assignment4.sh: line 55: expr: command not found
assignment4.sh: line 36: expr: command not found
assignment4.sh: line 41: expr: command not found
assignment4.sh: line 48: expr: command not found
assignment4.sh: line 36: expr: command not found
assignment4.sh: line 41: expr: command not found
assignment4.sh: line 48: expr: command not found

the section of code causing this error is as follows:

for filename in "$PATH"/"$1"/*
do
    if [ -f "$filename" ]
    then
        files=`expr $files + 1`

        if [ -r "$filename" ]
        then 
        readable=`expr $readable + 1`
        fi

        if [ -w "$filename" ]
        then    
            writeable=`expr $writeable + 1`
        fi

        if [ -x "$filename" ]
        then 
            executable=`expr $executable + 1`
        fi
    elif [ -d "$filename" ] 
    then
        directories=`expr $directories + 1`
    fi
done

I know that expr is on the machine I'm running because I used it earlier and it was working, though I did have to change the format of my statement utilizing it as before they were formatted

var=`expr $var+1`

and were not updating the variable's value the correct way.

2

There are 2 answers

3
Andras On BEST ANSWER

what's the $PATH of the script when it runs? expr is always present (traditionally in /bin, but in /usr/bin on linux)

but you can't iterate on $PATH/$1/*, because PATH is like /bin:/usr/bin:/usr/local/bin which does not match any directory. I think you want for filename in $(echo -n $PATH | tr : ' ')/$1/* or some such

0
Charles Duffy On

If this is not Bourne but POSIX sh (that is, a shell compatible with behavior standardized in the early 90s rather than one direct from the 70s), you can use:

readable=$((readable + 1))

...rather than relying on expr.