Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Unix/Linux Objective: Write a single shell script chk.sh to output certain kinds

ID: 3718089 • Letter: U

Question

Unix/Linux

Objective: Write a single shell script chk.sh to output certain kinds of information about two different types of Unix objects: files and users. This will require processing of command-line arguments and the use of complex, nested conditional statements (if-then-else) to control program flow.

Writing Your Script

This shell script, chk.sh, can be used to check a file if it is invoked as:

chk.sh -f filepath

or to check a user if it is invoked as

chk.sh -u user

Here is the information it should report:

1) Running chk.sh -f filepath accomplishes:

If filepath exists, output in readable setences

If it doesn't exist, output an error message. Exit and do not continue to report permissions.

Report what the filepath is: file, directory, or something else, and continue to report the permissions

Report what combination of read, write and execute access rights your program has for the data. Note that this will be dependent on who runs your program. Don't attempt to do this using filtering commands as output by ls -l. Use the test operators discussed in our labs.

If filepath does not exist (and is not a symbolic link), your program should report this in an informative error message. In this case, you should exit with an error.

2) Running chk.sh -u user accomplishes:

Note: you may not use the finger or pinky programs to get your information for chk.sh -u

If the user exists on the system, report

the path to the user's home directory

if the user is currently logged in, say so. Otherwise, report when they last logged in. (Take some care so that this is generated reliably and quickly.)

If the user doesn't exist, report an informative error message, and exit with an error.

You may not use the finger or pinky programs to get your information for chk.sh -u

Your program must, of course, check its arguments well and report any inconsistencies. If your program detects an error it should give the user sufficient information to diagnose the error and rerun the program successfully. It should also set the exit status appropriately.

Explanation / Answer

function usage()

{

#update this with usage guide

echo 'usage docs';

}

if [[ $# -eq 0 ]] ; then

echo 'Error: No arguments supplied';

exit 1;

fi

while [[ $# -gt 0 ]]

do

key="$1";

case $key in

-f)

GIVEN_PATH="$2";

shift; # past argument

shift; # past value

;;

-u)

USR="$2";

shift; # past argument

shift; # past value

;;

esac

done

##Both user&path are given

if [[ -n $GIVEN_PATH && -n $USR ]] ; then

echo 'Both path and user flags are specified.... Read the docs';

usage;

exit 1;

##user/path is not given

elif [[ -z $GIVEN_PATH && -z $USR ]] ; then

echo 'Path/user not specified.... Read the docs';

usage;

exit 1;

##only path is specified

elif [[ -n $GIVEN_PATH ]] ; then

##Checking existence

if [[ ! -f $GIVEN_PATH && ! -d $GIVEN_PATH ]] ; then

echo 'Path not found!';

exit 1;

elif [[ -f $GIVEN_PATH ]] ; then

echo 'Given path is a file';

elif [[ -d $GIVEN_PATH ]]; then

echo 'Given path is a directory';

fi

##Checking permissions

if [[ -r $GIVEN_PATH ]] ; then

echo 'Given filepath is readable(for the current user)';

else

echo 'Given filepath is not readable(for the current user)';

fi

if [[ -w $GIVEN_PATH ]] ; then

echo 'Given filepath is writeable(for the current user)';

else

echo 'Given filepath is not writeable(for the current user)';

fi

if [[ -x $GIVEN_PATH ]] ; then

echo 'Given filepath is executable(for the current user)';

else

echo 'Given filepath is not executable(for the current user)';

fi

else #only user is specified

##Check if user exist

if id -u "$USR" &>/dev/null ; then

echo 'User exist!';

else

echo "user doesn't exist";

exit 1;

fi

##Finding home directory

printf 'Home directory of given user is : ';

eval echo "~$USR"

##Check if the user is logged in

who="$(who)";

if echo "$who" | grep -q "$USR"; then # Check if $user is in $who

echo "$USR is currently logged in";

##ouput the last logged in time

else

printf 'User last loggedin on : ';

lastLogin=$(last $USR | head -1 | tr -s ' ' | cut -d ' ' -f 4,5,6,7);

echo "$lastLogin";

fi

fi

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote