Hey Guys, I need help with shell scripting and I was wondering If someone could
ID: 675427 • Letter: H
Question
Hey Guys, I need help with shell scripting and I was wondering If someone could help me with that.
Write a shell script call fmgr.sh which will provide the ability to either recover a file from the user's $HOME/junk directory into the current working directory or to remove it permanently.
fmgr.sh starts by showing the contents of the junk directory and provides the user the ability to select which file he/she wishes to work with along with the option to reload the list or quit.
Remember the files in junk should have a timestamp appended to them. If the user chooses to recover a file, this will have to be removed.
Once the user has selected a file, he/she should be prompted whether to remove it permanently or attempt to recover.
If the file is to be deleted, delete it from junk.
If the file is to be recovered, get the selected file's name, remove the timestamp extension and move it back into the current directory minus the timestamp.
But do this only after making sure that another copy/version of the file doesn't already exist in the current directory.
If it does, offer the user the chance to either overwrite it anyways, move it with the timestamp still attached, or cancel this particuar action.
Pseudocde
set an answer variable to null string ""
While user wants to continue (or until he/she wants to quit)
while-start (driver loop)
Use select to show a list of choices. The list consists of the
choice relist, the contents of the junk directory, and quit.
Remember you can individually enter items and/or use the graves to run
a command to provide items for the list portion of select command.
select action body
if the users makes a valid selection
break out of the select loop.
# The choice variable you name in the select line will only be assigned
# a value if user picks a valid choice from list.
# So test for non zero length.
select-body-end
if selection is quit
break out of main loop
* Don't use exit, break should take you out of driver loop.
else if selection is to relist
do a continue,
* this should bring you directly back to the top of the driver loop.
endif
* If you have selected an actual file, the file chosen will be in the
* variable initialized by the select command.
Prune the timestamp extension off of the retrieve filename
echo out the variable, pipe it to sed, and remove the extenstion and
assign it to another variable.
# You will need the graves to do this. The variable assignment it the
# main command and the echo and sed are generating the value.
If the pruned filename does not exist in current directory.
# Use ls on the filename and rediect any output and/or errors to /dev/null
# Test the status code of ls
Move the file to current directory under the original name without
the timestamp.
* Use the name returned from the select and the new pruned name.
else
set a new response variable to ""
while the response is not o/c/m
loop-body
Print out a message warning user that a file exists and give her/him
the option to overwrite, cancel the restore, or move junked file but
leave the timestamp on.
Prompt user for a choice ( suggest o/c/m ).
# use bash read command to get response.
Use case to check response
if the case is o
then do the move as above.
else if the case is c
then ]
break out of inner loop
# this should loop around back to top of program.
else if the case is m
then
move the file from junk to current directory
# but don't rename in any way
else
# non-valid response.
do a continue.
# This will bring you back to inner loop listing the o/c/m options
endcase
end o/c/m loop
end pruned file exists if
end of main driver loop
Here is My work:
!/bin/bash
ans=""
# ans variable is defined to make a choice in the loop
until [ "$ans" = quit ]
do
select CHOICE in relist ~/junk quit
do
if [ "$CHOICE" == "" ]; then
echo -e "Invalid entry"
continue
# check the select loop condition
elif [ $CHOICE = relist ];then
echo You choose $CHOICE which is No 1
continue
elif [ $CHOICE = quit ]; then
echo You choose $CHOICE which is No 2
break
# elif [ $CHOICE = ~/junk ]; then
# echo You choose $CHOICE which is No 3
# break
fi
# echo You choose the list $CHOICE
done
done
Explanation / Answer
function empty
{
local var="$1" # declare the variables
# Return the true if:
# 1. if var is a null string ("" as empty string)
# 2. a non the set of the variable is passed
# 3. a declared the variable or array but without a value is passed
# 4. an empty the array is passed
if test -z "$var" # condition checking
then
[[ $( echo "1" ) ]]
return
# Return true if var is zero (0 as an integer or a string)
elif [ "$var" == 0 2> /dev/null ]
then
[[ $( echo "1" ) ]]
return
# Return the true if var is 0.0 (0 as a float)
elif [ "$var" == 0.0 2> /dev/null ]
then
[[ $( echo "1" ) ]]
return
fi
[[ $( echo "" ) ]]
}