Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone please help with this Linux script? So i figured out my last assignment, but our teacher wants us to modify this script using

Can someone please help with this Linux script?

So i figured out my last assignment, but our teacher wants us to modify this script using the following perameters.

1. Add a function to validate the username in /etc/passwd.

2. Use a case statement instead of "if" for the program name.

3. Use the "select" syntax to prompt for a continue value.

Here's the code that needs modifying: Help please!

#!/bin/bash

MYPGM=$0

NUMARGS=$#

echo "You called me as $MYPGM"

echo "You passed in $NUMARGS arguments."

if [ $NUMARGS -eq 4 ]; then

ARG1=$1

ARG2=$2

ARG3=$3

ARG4=$4

#Argument 1 and 2

if [ "$ARG1" == "uid" ] || [ "$ARG1" == "user" ]; then

FINDOPT=$ARG1

FINDVAL=$ARG2

else

echo "Usage: $0 [uid | user] [user id | username] [directory] [find option]"

echo "Invalid 1st Argument! Exit Code is: 5"

exit 5

fi

#Argument 3

if [ -d "$ARG3" ]

then

FINDDIR=$ARG3

echo "$ARG3 is a directory"

else

echo "$ARG3 is not a directory"

echo "Usage: $0 [uid | user] [user id | username] [directory] [find option]"

echo "Invalid 3rd Argument! Exit Code is: 6"

exit 6

fi

if [ -w "$ARG3" ]

then

echo "$ARG3 is writable"

else

echo "$ARG3 is not writable"

echo "Usage: $0 [uid | user] [user id | username] [directory] [find option]"

echo "Invalid 3rd Argument! Exit Code is: 6"

exit 6

fi

if [ -x "$ARG3" ]

then

echo "$ARG3 is executable"

else

echo "Usage: $0 [uid | user] [user id | username] [directory] [find option]"

echo "Invalid 3rd Argument! Exit Code is: 6"

exit 6

echo "$ARG3 is not executable"

fi

#Argument 4

if [ "$ARG4" == "ls" ] || [ "$ARG4" == "print" ] || [ "$ARG4" == "delete" ]

then

FINDCMD=$ARG4

else

echo "Usage: $0 [uid | user] [user id | username] [directory] [find option]"

echo "Invalid 4th Argument! Exit Code is: 7"

exit 7

fi

else

echo "Usage: $0 [uid | user] [user id | username] [directory] [find option]"

echo "Invalid number of Arguments! Exit Code is: 8"

exit 8

fi

if [ "$MYPGM" == "./tmp_look.sh" ]; then

echo "Looking in $FINDDIR for files owned by $FINDOPT $FINDVAL"

find "$FINDDIR" -maxdepth 1 -type f -"$FINDOPT" "$FINDVAL" -ls

RC=$?

elif [ "$MYPGM" == "./tmp_clean.sh" ]; then

echo "cleaning $FINDDIR for user id $FINDVAL"

find /tmp -maxdepth 1 -type f -"$FINDOPT" "$FINDVAL" -ls

echo "Do you wish to continue? Enter Y/y to proceed, or N/n to exit"

read OK

if [ "$OK" == "Y" ] || [ $OK == 'y' ]; then

echo "OK then, deleting..."

find "$FINDDIR" -maxdepth 1 -type f -"$FINDOPT" "$FINDVAL" -delete

RC="$?"

elif [ "$OK" == "N" ] || [ $OK == 'n' ]; then

echo "OK then, clean exit..."

RC=0

else

echo "Sorry, confused..."

RC=9

fi

fi

echo "Done! Exit Code is: $RC"

exit $RC

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Identify compounds A through D in the following equations:

Answered: 1 week ago

Question

3. Identify professional nursing organizations and languages.

Answered: 1 week ago

Question

3. How has Starbucks changed since its early days?

Answered: 1 week ago