Pavitar
Pavitar

Reputation: 4374

Logical Error in Shell script.Please help (UNIX)

Following is a source code which takes in only 'files',lists the file permissions of a file and prints the output by replacing

r=READ,w-WRITE,x-EXECUTABLE.

It should also echo "User".But the My problem here is that I have replaced '-' by User but then if the file has a permission of r--x,it also prints "User" @ that point.I know its not a correct way to go about it.Can anyone suggest me a better way of echoing "User".

I have also tried printing it before the loop but then it won't serve my purpose, as My program only works withe file permissions of a FILE and not any block/socket/pipe/directory/etc.

   #!/bin/bash

    if [ $# -lt 1 ];then
        echo "USAGE: $0 file-name"
        exit 1
    fi


    ls -l $1 | cut -c1-4 | tr "\012" "." > fp


    i=1

    while(($i <= 4))
    do
        p=`cat fp | cut -c$i`

        case $p in

        [dbsplc] | t) echo "not a file";
            exit 1;;
        -) echo "User";;
        r) echo "READ";;
        w) echo "WRITE";;
        x) echo "EXECUTE";;

        esac

        ((++i))
    done


    exit 0

Upvotes: 0

Views: 379

Answers (4)

Pavitar
Pavitar

Reputation: 4374

This served my purpose,I separated the first condition into a different case-statement.:

#!/bin/bash

if [ $# -lt 1 ];then
    echo "USAGE: $0 file-name"
    exit 1
fi


ls -l $1 | cut -c1-4 | tr "\012" "." > fp


i=1
while(($i == 1))
do
    p=`cat fp | cut -c$i`

    case $p in

    [dbsplc] | t) echo "not a file";
        exit 1;;
esac

     echo "User"
    ((++i))
done



while(($i <= 4))
do
    p=`cat fp | cut -c$i`

    case $p in
    r) echo "READ";;
    w) echo "WRITE";;
    x) echo "EXECUTE";;

    esac

    ((++i))
done


exit 0

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246877

Too complicated. You don't have to rely on ls at all:

#!/bin/bash

if [[ $# -lt 1 ]]; then
    echo "USAGE: $(basename "$0") filename ..."
    exit 1
fi

exit_status=0

for file in "$@"; do
    if [[ ! -f "$file" ]]; then
        echo "not a file: $file" >&2
        exit_status=$(( exit_status + 1 ))
        continue
    fi

    echo "$file:"
    echo "User"   

    [[ -r "$file" ]] && echo "READ"
    [[ -w "$file" ]] && echo "WRITE"
    [[ -x "$file" ]] && echo "EXECUTE"
done

exit $exit_status

Upvotes: 4

Arnaud F.
Arnaud F.

Reputation: 8452

an exemple using awk (easily adaptable to your program)

ll |awk '{
             rights=substr($1, 2, 3);
             sub(/r/, "READ ", rights);
             sub(/w/, "WRITE ", rights);
             sub(/x/, "EXECUTE ", rights);
             print rights $3
          }'

Explanations :

rights=substr($1, 2, 3);

$1 contains rights of your program and we only takes the 3 first rights (user one)

sub(/r/, "READ ", rights);

Substiture "r" with READ in rights (and so on).

print rights $3

Print rights (substituated) and $3 that contains the user name.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798804

I'd just use stat -c %a and process that instead.

Upvotes: 1

Related Questions