Computing.Net > Forums > Unix > Bash Shell Script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Bash Shell Script

Reply to Message Icon

Name: learnpro
Date: November 23, 2008 at 17:48:32 Pacific
OS: Windows XP
CPU/Ram: 2Gb
Product: Dell
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: November 24, 2008 at 09:51:53 Pacific
Reply:

This works if the HOME shell variable is set:


#!/bin/bash

filename=myfile
if [[ -f "$filename" ]]
then
echo "File exists"
fi

if [[ -f "$HOME/$filename" ]]
then
echo "File exists in HOME directory"
fi



0

Response Number 2
Name: learnpro
Date: November 24, 2008 at 13:18:10 Pacific
Reply:

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"
fi

Thanks 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
done

Any help here will be appreciated. Thanks.


0

Response Number 3
Name: nails
Date: November 24, 2008 at 23:05:11 Pacific
Reply:

Here is a way using a while loop:


#!/bin/bash

filename=init
cnt=0
while true
do

if [[ -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
done

echo "continue"



0

Response Number 4
Name: learnpro
Date: November 25, 2008 at 00:19:56 Pacific
Reply:

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.


0

Response Number 5
Name: nails
Date: November 25, 2008 at 11:46:35 Pacific
Reply:

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?


0

Related Posts

See More



Response Number 6
Name: learnpro
Date: November 25, 2008 at 19:57:56 Pacific
Reply:

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.


0

Response Number 7
Name: nails
Date: November 28, 2008 at 18:33:20 Pacific
Reply:

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.



0

Response Number 8
Name: learnpro
Date: November 29, 2008 at 08:49:52 Pacific
Reply:

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";
fi

Any thoughts as to what I may be doing wrong. I will appreciate it. Thanks again for your help.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Bash Shell Script

Shell script for network test www.computing.net/answers/unix/shell-script-for-network-test/7976.html

Bash Script Question www.computing.net/answers/unix/bash-script-question/6086.html

backup a file with SHELL SCRIPT www.computing.net/answers/unix/backup-a-file-with-shell-script/4522.html