This is the script I need to write: Your assignment is to write a shell script n
ID: 3718848 • Letter: T
Question
This is the script I need to write: Your assignment is to write a shell script named “rpsm.sh”. The script reports and prints (to stdout) selected storage management information. Think of it as “RePort Storage Management”. Clever, eh?
The easy way to describe what the script needs to do is to look at what it should display in the case where you provide incorrect parameters, or in the case where you provide “-h” for help:
Usage: ./rpsm.sh (options) (directory)
Reports selected information about specified directory tree.
Options:
-h Print this message
-v Print script version information
-u Find and list all files with setuid set, all owners
-g Find and list all files with setgid set, all owners
-w Find and list all files that are world-writable
-b Find and list all files whose size is at least 10M
-d Report directory disk usage
-i Report information about filesystem
-a All of the above (except -h and -v)
Test the script against a directory where you won't get a bunch of permission denied messages.
Most of the options above are self-explanatory, but some are not, so let me provide examples for the latter case. The “-d” option should produce sorted output just like this (just an example):
anyuser@whatever:~$ ./rpsm.sh –d /home/anyuser/anydirectory
4 /home/anyuser/anydirectory/Perms
8 /home/anyuser/anydirectory/EnvDemo
12 /home/anyuser/anydirectory/BigFiles
24 /home/anyuser/anydirectory/SomeSetgids
24 /home/anyuser/anydirectory/SomeSetuids
76 /home/anyuser/IASC3350
The “-i” option shows me information about the filesystem holding the directory I am asking about. (Hint: look at the “df” command.) It does not give me all of the information but it should look just like this (just an example):
anyuser@whatever:~$ ./rpsm.sh –i /home/anyuser/anydirectory
Filesystem Type Use% Mounted on
prog.ist.somecollege.edu:/home nfs4 52% /home
Again, you will want to figure out how to get the information and then also how to cut it up. Here’s another example:
anyuser@whatever:~$ ./rpsm.sh –id /etc
Filesystem Type Use% Mounted on
/dev/mapper/whatever-root ext4 28% /
4 /etc/apache2/sites-enabled
4 /etc/apparmor.d/disable
4 /etc/apparmor.d/force-complain
4 /etc/apparmor.d/tunables/multiarch.d
4 /etc/apparmor/init/network-interface-security
(more...)
You’ll note that the output for “–id” versus “–di” would be different because of the order of the command line arguments. Also note that you need to handle – for instance - “–id” as the same as “–i–d”.
Explanation / Answer
##rpms.sh
input1=$1
version=1.0
argCount=$#
dirName=$2
if [ $argCount -gt 2 ]
then
echo " Invalid Arguments .."
echo " example: ./rpsm.sh (options) (directory)"
else
if [ $input1 == "-v" ]
then
echo "Version = $version"
elif [ $input1 == "-h" ]
then
echo "-h Print this message"
echo "-v Print script version information"
echo "-u Find and list all files with setuid set, all owners"
echo "-g Find and list all files with setgid set, all owners"
echo "-w Find and list all files that are world-writable"
echo "-b Find and list all files whose size is at least 10M"
echo "-d Report directory disk usage"
echo "-i Report information about filesystem"
echo "-a All of the above (except -h and -v)"
elif [ $input1 == "-u" ]
then
ls -ld $dirName
elif [ $input1 == "-d" ]
then
du -d 1 $dirName
elif [ $input1 == "-b" ]
then
find $dirName -size +10M -exec ls -lh {} ;
elif [ $input1 == "-w" ]
then
find $dirName -type f -maxdepth 1 -writable
elif [ $input1 == "-i" ]
then
df $dirName
elif [ $input1 == "-id' ]
then
df $dirName
du -d 1 $dirName
elif [ $input1 == "-u" ]
then
ls -lu
elif [ $input1 == "-g" ]
then
ls -g
elif [ $input1 == "-a" ]
then
ls -ld $dirName
du -d 1 $dirName
df $dirName
find $dirName -size +10M -exec ls -lh {} ;
find $dirName -type f -maxdepth 1 -writable
fi
fi
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.