Perform an operation based on the availability of a file

February 14, 2012 at 01:26:39
Specs: CentOS
I have a script that reads the filename entered by the user and performs a set of actions on that file.
However, if the filename is invalid, the user is asked to enter the filename again. If he enters a correct filename, then the function func1 is executed, whereas , if the user enters the wrong filename 3 times in succession, he should exit out of the loop.

My main menu view is something like this

1. Create new
2. Modify existing entry
3. Delete existing entry
Enter the option : 2

The entries to be modified are stored in a text file. If i enter the value 2, it will ask me for the file name where the entries are stored.

Enter the filename : newfile.txt
Entries modified

If the filename is correct, the above is displayed.
If the filename is wrong, then it should say

"Incorrect filename"
Enter the filename :

If the incorrect filename is entered 3 times in succession, then it should exit the loop and go to the main menu main menu, which is

1. Create new
2. Modify existing entry
3. Delete existing entry
Enter the option :

How exactly do i frame the loop in order to achieve this?


See More: Perform an operation based on the availability of a file

Report •

#1
February 14, 2012 at 04:42:11
I hav solved the issue by using the following code. This seems to execute without any errors, but I haven't tested it completely.
If you know an easier method, or if u spot any errors in my code, please let me know.

The code I'm using is as follows

a=0
while [ $a -lt 3 ]
do
readfile # function to read filename
if [ -a $filename ] # check if the file exists
then
doproc # function to do the process
break
else
echo "File does not exist..."
fi
a=`expr $a + 1`
done


Report •
Related Solutions


Ask Question