PROGRAM DESCRIPTION: Write a bash program (or script) that accepts a mandatory s
ID: 3667456 • Letter: P
Question
PROGRAM DESCRIPTION:
Write a bash program (or script) that accepts a mandatory single name and optional login ID [EUID] as command-line arguments to the bash program and outputs a menu of options for the user to select. If the input matches one of the three non-exit options, an appropriate message and command result should be printed. Following one of these options, the menu will continue to be displayed until the user exits the program. If the user selects the option to exit the program, or any invalid option, the program will display a thank you message and exit the program as described below.
• If the mandatory single name, such as “Mark”, is included on the command line, your script should politely greet that person by name, such as “Good day, Mark! Nice to meet you!” If no parameters or more than two parameters are supplied on the command line, you are to display a “usage statement”, such as usage: minor3 name [euid], and exit the program.
• Your program should then display a menu of the following options that can be selected by the user in a loop:
1) List and count all non-hidden files in the current directory.
o Your program will display the total number of non-hidden files in the current directory, and then list all the non-hidden files in the current directory.
2) Check if given user (default = current user) is logged in, then list all active processes for that user.
o If the user supplied a second optional login ID (i.e., EUID) parameter, your program will use this login ID as the “given user”. If no second option login parameter was supplied, your program will use the user name associated with the current effective user ID as the “given user”.
o Your program will display whether or not the “given user” is logged in, and then list all active processes for that user. To make things easier, you may list all processes associated with the “given user”, as may be found when using the grep command.
3) List the sizes and names of the 10 largest files and directories in the current directory.
o Your program will display the size and name of the 10 largest files and directories in the current directory. As a hint, you may want to look at the du command pipelined with other commands.
4) Exit this shell program.
o Your program will display a thank you message to the user by name, such as “Thanks, Mark! Have a great day!” and then exit the program.
o Additionally, your program should exit the program if the user enters any option other than 1 – 3 indicated above. Please see the SAMPLE OUTPUT for several examples.
REQUIREMENTS:
• You may assume that the mandatory single name is given as a single string without whitespace, and that the optional login ID, when given, is valid.
• Your program should be named “minor3.sh”, without the quotes.
• The program has to run on a LINUX machine
Attached picture is sample output of how the program should run
(Please excuse the picture format- I wanted to include all of the sample output on one picture)
SAMPLE OUTPUT (user input shown in bold green): mat02998cse04-Icsce3600/sp16/minor3.sh usage: ninor3 name [euid] mat02998cse04:-Icsce3600/sp16/minor3.sh Hark Good day, Mark! Nice to meet you Enter one of the following options: 1) List and count all non-hidden files in the current directory 2) Check if given user (default -current user) is logged in, then list all active processes for that user 3) st the sizes and names of the 10 largest files and directories in the current directory 4) Exit this shell progran Total number of files: 16 a.out mat2 file minorl.c nyfile nyfile2 sample sec001 test filel mat file minor3.sh nyfilel newFile samplel sysbuf.c t file Enter one of the following options: 1) List and count all non-hidden files in the current directory 2) Check if given user (default -current user) is logged in, then list all active proc sses for that us@ r 3) List the sizes and names of the 10 largest files and directories I in the current directory 4) Exit this shell progran > Active processes for nat0299 (logged in) mat0299 3752 24033 0 09:38 7 mat0299 3767 3752 0 09:38 pts/5 00:00:00 -bash mat0299 13442 32414 0 11:28 pts/5 00:00:00 ps -ef mat0299 13443 32414 0 11:28 pts/5 00:00:00 grep at0299 00:00:00 sshd: mat02998pts/s 24033 1551 0 09:38 ? 00:00:00 sshd: at0299 [priv] mat0299 32414 3767 0 11:27 pts/5 00:00:00 bin/bash/ninor3.sh Mark Enter one of the following options: ) List and count all non-hidden files in the current directory 2) Check if given user (default = current user) is logged in, then list all active processes for that user 3) List the sizes and names of the 10 largest files and directories I in the current directory 4) Exit this shell progran > size and Name of 10 largest files and directories: myfile newFile sample samplei sec001 1304 1364 total Enter one of the ollowing options: 1) List and count all non-hidden files in the current directory 2) Check if given user (default current user) is logged in, then I list al1 active processes for that user 3) List the sizes and names of the 10 largest files and directoriesI in the current directory 4) Exit this shell progran Thanks, Mark! Have a great day! mat02998ese04: /esce3600/sp16/minor3.sh Mark phs0004 Good day, Mark! Nice to meet you! Enter one of the following options: List and count all non-hidden files in the current directory 2) Check if given user (default = current user) is logged in, then 3) List the sizes and names of the 10 largest files and directories I 4) Exit this shell progran ist all active processes for that user in the current directoryExplanation / Answer
#!/bin/bash # UseGetOpt.sh # Author: Peggy Russell UseGetOpt () { declare inputOptions declare -r E_OPTERR=85 declare -r ScriptName=${0##*/} declare -r ShortOpts="adf:hlt" declare -r LongOpts="aoption,debug,file:,help,log,test" DoSomething () { echo "The function name is '${FUNCNAME}'" # Recall that $FUNCNAME is an internal variable #+ holding the name of the function it is in. } inputOptions=$(getopt -o "${ShortOpts}" --long "${LongOpts}" --name "${ScriptName}" -- "${@}") if [[ ($? -ne 0) || ($# -eq 0) ]]; then echo "Usage: ${ScriptName} [-dhlt] {OPTION...}" exit $E_OPTERR fi eval set -- "${inputOptions}" # Only for educational purposes. Can be removed. #----------------------------------------------- echo "++ Test: Number of arguments: [$#]" echo '++ Test: Looping through "$@"' for a in "$@"; do echo " ++ [$a]" done #----------------------------------------------- while true; do case "${1}" in --aoption | -a) # Argument found. echo "Option [$1]" ;; --debug | -d) # Enable informational messages. echo "Option [$1] Debugging enabled" ;; --file | -f) # Check for optional argument. case "$2" in #+ Double colon is optional argument. "") # Not there. echo "Option [$1] Use default" shift ;; *) # Got it echo "Option [$1] Using input [$2]" shift ;; esac DoSomething ;; --log | -l) # Enable Logging. echo "Option [$1] Logging enabled" ;; --test | -t) # Enable testing. echo "Option [$1] Testing enabled" ;; --help | -h) echo "Option [$1] Display help" break ;; --) # Done! $# is argument number for "--", $@ is "--" echo "Option [$1] Dash Dash" break ;; *) echo "Major internal error!" exit 8 ;; esac echo "Number of arguments: [$#]" shift done shift # Only for educational purposes. Can be removed. #---------------------------------------------------------------------- echo "++ Test: Number of arguments after "--" is [$#] They are: [$@]" echo '++ Test: Looping through "$@"' for a in "$@"; do echo " ++ [$a]" done #---------------------------------------------------------------------- } ################################### M A I N ######################## # If you remove "function UseGetOpt () {" and corresponding "}", #+ you can uncomment the "exit 0" line below, and invoke this script #+ with the various options from the command-line. #------------------------------------------------------------------- # exit 0 echo "Test 1" UseGetOpt -f myfile one "two three" four echo;echo "Test 2" UseGetOpt -h echo;echo "Test 3 - Short Options" UseGetOpt -adltf myfile anotherfile echo;echo "Test 4 - Long Options" UseGetOpt --aoption --debug --log --test --file myfile anotherfile exitRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.