Hi there. I'm trying to teach myself UNIX but the book I bought is a bit confusing. I'm trying out this exercise and I think I'm on the right track, but I'd appreciate any suggestions on how to improve what I have so far. Also, I'm not clear on how to use the read command to utilize the user's input (#'s 4-7). I appreciate your help!
#!/bin/bash
while:
do
clear
# Display menu
echo
echo "*************************************************************"
echo "Please choose from the following options; type the"
echo "option number and hit the <Enter> key."
echo
echo " 1) To list names of the files in the current DIR"
echo " 2) Display today's date and time"
echo " 3) Display a sorted list of people currently logged on"
echo " 4) Display whether a file is a file or a DIR"
echo " 5) Create a backup for a file"
echo " 6) Find a user by First of Last name in /etc/passwd file"
echo " 7) Find the manual pages for a specific command"
echo " 8) Exit"
echo
echo "*************************************************************"
read option
case "$option" in
1) echo "The files in the current DIR are: "
ls -l
echo "Hit <Enter> to continue."
read ignore
;;
2) echo "The current date is: "
date
unset date
echo "Hit <Enter> to continue."
read ignore
;;
3) echo "The following is a list of the academic scholars"
echo "currently logged in:"
who | cut -d " " -f1 | sort
echo "Hit <Enter> to continue."
read ignore
;;
4) echo "Enter file to determine whether a "simple" file or a"
echo "directory:"
5) echo "Enter the file you would like to create a backup for:"
6) echo "Enter user's first or last name:"
7) echo "Enter command for which you would like the manual:"
8) echo "Thank You for using Skynet"
echo "Remember: Skynet is the Future"
sleep 1.5
clear
exit 0
;;
*) echo "Invalid option; try running the program again."
exit 1
;;
esac
done.
exit 0
Are you sure its not homework??:)
hmmm anyways
read <varname>
the <varname> will store whatever entered from Standard I/O
you can use $<varname> where ever you want
Not homework I swear! I'm 32 years old and only got my first computer ever last year. Before that the most I had ever done with one is clock in and out of my old warehouse job.
You show a prompt to the user if they choose 4 through 7 from the menu. You will need to capture the response of that prompt again, and run the relevant command - "cp" for backup, "grep" for /etc/passwd lookup, "man" for display of manual page etc. Also, you can perform a couple of file tests to determine if the input value in #4 is a regular file or a directory or something else.
In the command pipeline of #3, you may want to use "sort -u" to display unique users, since the same user can be displayed more than once in the output of "who" command.
I've tweaked your script a bit, incorporating these suggestions -
$
$ cat menu.sh
#!/bin/bash
#set -vx
while :
do
clear
# Display menu
echo
echo "*************************************************************"
echo "Please choose from the following options; type the"
echo "option number and hit the <Enter> key."
echo
echo " 1) To list names of the files in the current DIR"
echo " 2) Display today's date and time"
echo " 3) Display a sorted list of people currently logged on"
echo " 4) Display whether a file is a file or a DIR"
echo " 5) Create a backup for a file"
echo " 6) Find a user by First of Last name in /etc/passwd file"
echo " 7) Find the manual pages for a specific command"
echo " 8) Exit"
echo
echo "*************************************************************"
read option
case "$option" in
1) echo "The files in the current DIR are: "
ls -l
echo "Hit <Enter> to continue."
read ignore
;;
2) echo "The current date is: "
date
unset date
echo "Hit <Enter> to continue."
read ignore
;;
3) echo "The following is a list of the academic scholars"
echo "currently logged in:"
who | cut -d " " -f1 | sort -u
echo "Hit <Enter> to continue."
read ignore
;;
4) echo "Enter file to determine whether a simple file or a directory:"
read fdname
if [ ! -e $fdname ]; then
echo "$fdname does not exist."
elif [ -d $fdname ]; then
echo "$fdname is a directory."
elif [ -f $fdname ]; then
echo "$fdname is a regular file."
else
echo "$fdname is something else."
fi
echo "Hit <Enter> to continue."
read ignore
;;
5) echo "Enter the file you would like to create a backup for:"
read fname
if [ ! -e $fname ]; then
echo "$fname does not exist."
elif [ ! -f $fname ]; then
echo "$fname is not a regular file."
else
cp "$fname" "$fname.bkp"
echo "Created backup $fname.bkp for $fname"
fi
echo "Hit <Enter> to continue."
read ignore
;;
6) echo "Enter user's first or last name:"
read flname
grep $flname /etc/passwd
if [ $? -ne 0 ]; then
echo "Sorry, $flname was not found in /etc/passwd"
fi
echo "Hit <Enter> to continue."
read ignore
;;
7) echo "Enter command for which you would like the manual:"
read cmd
man $cmd
echo "Hit <Enter> to continue."
read ignore
;;
8) echo "Thank You for using Skynet"
echo "Remember: Skynet is the Future"
sleep 1.5
break
;;
*) echo "Invalid option; valid ones are from 1 to 8"
echo "Hit <Enter> to continue."
read ignore
;;
esac
done
$
$
Hope that helps,
tyler_durden