Unix/Lunix Programming Exercise 5 Read the following script interactive_mode and
ID: 3906745 • Letter: U
Question
Unix/Lunix Programming
Exercise 5
Read the following script interactive_mode and answer the question.
# interactive mode
if [[ -n $interactive ]]; then
while true; do
read -p "Enter name of output file: " filename
if [[ -e $filename ]]; then
read -p "'$filename' exists. Overwrite? [y/n/q] > "
if [[ "$REPLY" == "y" ]]; then
break
elif [[ "$REPLY" == "q" ]]; then
echo "Program terminated."
exit
fi
elif [[ -z $filename ]]; then
continue
else
break
fi
done
fi
Assume that interactive is not empty.
What are the next five commands that will be executed if user provides an empty filename?
What is the next three commands being executed if user supplies a non-existing filename?
What is the next two commands being executed if user supplies an existing filename?
If user then provides ‘n’, what are the next three commands being executed?
Explanation / Answer
Solution:
Question ) What are the next five commands that will be executed if user provides an empty filename?
Answer ) condition if [[ -e $filename ]]; is false as the file name is blank; it comes to elif [[ -z $filename ]]; where again the lenght of the string (i.e. filename) is zero, hence the condition is false and it goes to else where break is executed and it breaks out.
Question ) What is the next three commands being executed if user supplies a non-existing filename?
Answer )
condition if [[ -e $filename ]]; is false as the file is non existent, t comes to elif [[ -z $filename ]]; where again the lenght of the string (i.e. filename) is zero, hence the condition is false and it goes to else where break is executed and it breaks out.
Question ) What is the next two commands being executed if user supplies an existing filename?
Answer )
condition if [[ -e $filename ]]; is true as the file is existent, it comes to read -p "'$filename' exists. Overwrite? [y/n/q] > " where it asks for user input and depending on user choice, either if [[ "$REPLY" == "y" ]]; then break or elif [[ "$REPLY" == "q" ]]; then echo "Program terminated."
Question ) If user then provides ‘n’, what are the next three commands being executed?
Answer )
As there is no matching condition when the user enters n ; it exits the inner if-elif and comes to read -p "Enter name of output file: " filename and asks for user input as there is a while true; loop above, and then comes to if [[ -e $filename ]]; then
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.