[sf-lug] Need help with an Linux assignment script

Timothy Wang timothyjwang at yahoo.com
Mon Apr 28 20:07:53 PDT 2008


I am doing an assignment in City College CS160b and was having difficulty 
understanding why it is not working. The assignment is posted on 

http://fog.ccsf.cc.ca.us/~gboyd/

under cs 160b asmt02

twang03 at hills:~ $ vi chk

#!/bin/bash
#1st argument if filepath exists, output in readable sentences




if [ $# -lt 2 ]; then
    echo "chk: need -f or -u file or user" >&2
    exit 1
fi

if [ ! "$1" = -f ] && [ ! "$1" = -u ]; then
    echo "chk need -f or -u plus file or user" >&2
    exit 1
fi
# =====    ============================
if [ "$1" = -f ]; then

    if [ -h "$fname" ]; then
            echo "fname" is a path to a symbolic link

    elif [ -d "$fname" ]; then
        echo "$fname" is a path to a directory

    elif [ -f "$fname" ]; then
        echo "fname" is a path to a regular file

    elif [ ! -d " $fname" ] && [ ! -f "$fname" ] && [ ! -h "$fname" ];then
        echo "$fname" is a path to an unknown filetype.
    fi

    if [ -r "$fname" ] && [ ! -w "$fname" ] && [ ! -x "$fname" ]; then
        echo "$fname" is readable.
    else
        echo "$fname" is not readable.
    fi

    if [ -w "$fname" ] && [ ! -w "$fname" ] && [ ! -x "$fname" ]; then
        echo "$fname" is writable
    else
        echo "$fname" is not writable
    fi

    if [ -x "$fname" ] && [ ! -w "$fname" ] && [ ! -x "$fname" ]; then
        echo "$fname" is executable
    else
        echo "$fname" is not executable
    fi
fi
# ==================================================

if [ "$1" = -u ]; then
    if $(! cat /etc/passwd | grep "^$2:" > /dev/null)
    then
        echo error found
fi
    login="$2"
    name="$(finger -m "$login" | grep 'real life' | cut -d':' -f3 | tail -n1)"

    logrec="$(finger -m "$login" | grep 'Last login' | cut -d' ' -f3)"
    whom="$(who | grep "$login")"
    hmdir="$(finger -m "$login" | grep 'Directory: ' | cut -d' ' -f2)"

    if "$("name" = "???")"; then
        echo "$login doesn\'t exist on this system"
    else
        if [ -n "$who" ]; then
            echo "$login is currently logged into the system"
        else
            echo "$login"\'s last login was "$logrec"
        fi

        echo "$login"\'s real name is "$name"
        echo "$login"\'s home directory is located at "$hmdir"
    fi
fi

                                                                                                                                           77,0-1        96%


      ____________________________________________________________________________________
Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ




More information about the sf-lug mailing list