I'm not Batman
I'm not Batman

Reputation: 1

Bash scripts errors

I'm trying to run those scripts but I keep receiving errors messages:

1-

#!/bin/bash

filename=$1


if [ -f $filename ]
then
    owner=`stat -c %U $filename`
    grep $owner /etc/passwd
    if [ $? -eq 0 ]; then
            perm=`stat -c %a $filename | head -c 1`
            if [ $perm -gt 3 ]; then
                cat $filename | grep NOTE
            fi
        fi
fi

the error message is :

stat: missing operand Try `stat --help' for more information.


2-

#!/bin/bash

NoSum=$1
sum=0
echo "Please enter $NoSum values one at a time"
for (( i=1; i<=$NoSum; i++ ))
do
    echo "Next Value?"
    read num
    let "a = $sum + $num"
    sum=$a
done

echo "The sum is : $sum"

the error message is:

Please enter values one at a time ./scr3: line 6: ((: i<=: syntax error: operand expected (error token is "<=") The sum is : 0


3-

#!/bin/bash

dir=$1

if [ -d $dir ]
    then
    perm=`stat -c %a $dir | head -c 1`
    if [ $perm -gt 5 ]; then
        cd $dir
        for file in $dir/*
        do
            if ! [ -x "$file" ]
            then
                echo "$file"
            fi
        done
    fi
fi

the error message is:

stat: missing operand Try `stat --help' for more information. ./scr4: line 8: [: -gt: unary operator expected


any idea how to fix them ?

Upvotes: 0

Views: 1290

Answers (2)

rjv
rjv

Reputation: 6776

Nothing is wrong about the programs.You are not supplying the command line arguments.You must run it as

1 and 3:

./script.sh <filename>

2:

./script.sh <number>

$1 stands for the first command line argument

Upvotes: 1

Reinstate Monica Please
Reinstate Monica Please

Reputation: 11613

You need to quote variables in bash to prevent word-splitting issues, both in test brackets [] and most of the time in other use.

So your first script would be

#!/bin/bash

filename="$1"


if [ -f "$filename" ]
then
    owner="`stat -c %U "$filename"`"
    grep "$owner" /etc/passwd
    if [ $? -eq 0 ]; then
            perm="`stat -c %a "$filename" | head -c 1`"
            if [ "$perm" -gt 3 ]; then
                cat "$filename" | grep NOTE
            fi
    fi
fi

The others have similar erros

Upvotes: 0

Related Questions