Write a bash program that accepts a file name as a command line argument. Then c
ID: 669649 • Letter: W
Question
Write a bash program that accepts a file name as a command line argument. Then check to see if the file is included in the current working directory. If the file is NOT in the current working directory, add the file to the current working directory. (Hint: Look at the UNIX “touch” command). Now that we “know” that the file is in the current directory, prompt the user to say whether they want the file deleted. A “simple” yes or no should suffice. However, for yes accept any of:
Yes
yes
y
and for no, accept any of:
No
no
n
If your program receives an acceptable input (for yes or no) delete the file if appropriate. Write a message to indicate whether the file is deleted or not. However, if your program does NOT get a “valid” yes or no, write a message indicating that and ask the user to try again (again with an appropriate prompt). Your program should be able to try THREE times to get an acceptable yes or no but quit after the third incorrect input, again with an appropriate message, of course.
should work in UNIX
Explanation / Answer
#!/bin/bash
file="./file"
if[-e $file]; then
echo"file exists"
else
echo "file does not exists"
fi
#!/bin/bash
PS3='choose one option:'
#print to stdout
echo "1.keep"
echo"2.Delete"
echo -n "please choose a word[1 or 2]?"
#read userinput
read choice
#bash nested ifelse
if[$choice -eq 1];then
echo "you have choosen keep"
#print to stdout
echo "1.Yes"
echo "2.yes"
echo "3.y"
echo -n "please choose a word[1,2,or 3]?"
#read user input
read choice
#bash nested ifelse
if[$choice -eq1],then
echo "you have choosen Yes"
else
if[$choic -eq2], then
echo "you have choosen yes"
else
echo "you choosen y"
fi
fi
fi
else
echo "you have choosen Delete"
#print to stdout
echo "a.No"
echo "b.no"
echo "c.n"
echo -n "please choose a word[a,b,or c]?"
#read user input
read choice
#bash nested ifelse
if[$choice -eq a],then
echo "you have choosen No"
else
if[$choic -eq b], then
echo "you have choosen no"
else
echo "you choosen n"
fi
fi
fi
#!/bin/bash
Count=0
#bash until loop
until[$Count -gt2];do
if[$Choice -eq1=$Choice -eq2=$Choice -eq3]then
echo "Your file is with you"
else
echo "your file is deleted"
let Count=Count+1;
fi
#!/bin/bash
Count=0
#bash until loop
until[$Count -gt2];do
if[$Choice -eqa=$Choice -eqb=$Choice -eqc]then
echo "Your file is Deleted"
else
echo "your file is with you"
let Count=Count+1;
fi
done
Assumed output:
linuxconfig.org:~$touch file
linuxconfig.org:~$ls
file filestring.sh
linuxconfig.org:~$./filestring.sh
file exists
linuxconfig.org:~$
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.