Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello Everyone,
I am a total beginner in UNIX and have to write this huge shell script in bash shell. The script is to create a spellingchecking program. First, script needs to check if file entered by user exists in parent or current directory (both). I have an idea but not sure if it is right: Can someone please help:
if [ -f $filename ]
echo "File exists"Can someone please tell me how do I check it in both current directory and parent directory? I will really appreciate any or all input. Thanks.

This works if the HOME shell variable is set:
#!/bin/bashfilename=myfile
if [[ -f "$filename" ]]
then
echo "File exists"
fiif [[ -f "$HOME/$filename" ]]
then
echo "File exists in HOME directory"
fi

Thank you so much for your help. I was coming up with something, would you advice if this would be correct in Bash shell.
if [ -f ./$filename ] || [ -f ../$filename ]; then
echo "file exists.
else
echo"Input correct name"
fiThanks again.
What I need to add here is a for loop that will keep on asking the user to input the filename until it is correct...say 5 times. Could someone tell me how to do it or look at what i did and correct it:for ((x=1; x<=5; x++)
do
if [ -f ./$filename ] || [ -f ../$filename ]; then
echo "file exists.
else
echo"Input correct name"
fi
doneAny help here will be appreciated. Thanks.

Here is a way using a while loop:
#!/bin/bashfilename=init
cnt=0
while true
doif [[ -f "$filename" || -f ../"$filename" ]]
then
echo "File exists"
break
else
echo "Input file name "
fi
read filename((cnt+=1))
if [[ $cnt -eq 5 ]]
then
echo "exceeded 5 tries. quitting"
exit
fi
doneecho "continue"

Thank you again for your response. It helps to have a direction. However, when I tried it, it does not seem to work correctly. Once I input wrong filename, get the error message and start to enter correct filename, but loop still continues on for five times. Inother words, even if I input correct filename, it is not taking that input but simply displaying "Input file name " five times and then quits. Any thoughts or input will be appreciated.
Thanks.

I've checked the code I posted and it works fine. I'm running the bash shell on a Solaris 9 system with no problem.
Based on your description it looks like the read command is being ignored. Tonight, I'll try it on my Ubuntu Linux system and see what happens.
What OS are you running?

Hello, I am using bash shell on solaris 10.
Just in case I may have confused you, it does let me input the file name but does not do anything, simply types the statement "input filename" - repeats process three times and exits. Please help. Thank you very much.

Yes, I'm thoroughly confused. I placed a read command inside of a while loop. When you see "Input file name ", the user is expected to enter a file name, and if the file is found, it breaks out of the loop.
You get a chance to enter 4 file names before the script terminates.
Frankly, I really don't know what you expect.

My bad..and I really apologize for that. Your suggestion works just fine, I made an error by typing read $filename rather than read filename. I am really sorry, I should have caught that.
However, It was also due to with the first code section where file is supposed to check in the parent directory. It appears the program is able to see if the file exists in the parent directory but unable to read the permissions. Let's say I am in one of the directories and try to search for a file in my home directory, program is able to locate the file, but not able to read the permissions (it prompts file is not readable/writeable, when actually it is). It is able to do so when I am in the parent directory itself. For the record, I am using similar way to check for file permissions:if [ -r $filename -a -w $filename ]; then
echo "file is readable and writeable";
else
echo "file is not readable and writeable";
fiAny thoughts as to what I may be doing wrong. I will appreciate it. Thanks again for your help.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |