Logical Error in Shell script.Please help (UNIX)

安稳与你 提交于 2019-12-12 02:43:09

问题


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

回答1:


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



回答2:


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




回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/3962155/logical-error-in-shell-script-please-help-unix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!