Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 11-11-2008, 12:13 PM   #1
Registered: Nov 2003
Posts: 788

Rep: Reputation: 39
Proper Syntax for If...Then..Else statements with a script

Hi there --

I am writing a script the prompts the user to enter a directory path, and then prompts the user to provide a hardware device file. Listed below is the part of the script that is involved with this issue:

# Prompt the user for the directory in question
echo "Please enter the full path of the directory:"
read path

# Confirm the directory that was entered is the correct one
echo "Is this the correct directory? [y/n]"

# Prompt the user for the full pathname of the destination tape drive
echo "Please enter the full devicefile name of the tape drive: "
read devicefile

# Confirm the devicefile that was entered is the correct one
echo "Is this the correct device filename? [y/n]"
After the user enters the appropriate information after each prompt, the script will ask for confirmation. What I need help with is the correct syntax to use in each case so that if the answer is "y" the script will continue onward, if it is "n" the user will be prompted to re-enter the information.
Old 11-11-2008, 12:24 PM   #2
Registered: Jun 2007
Location: San Francisco
Distribution: Centos 4, Debian
Posts: 55

Rep: Reputation: 16
if [ "$read" = "y" ]; then
# handle true case
# handle false case
Old 11-11-2008, 12:44 PM   #3
Registered: Nov 2003
Posts: 788

Original Poster
Rep: Reputation: 39
Hi there --

Thanks for your reply. I inserted the following if...else statement in the script:

# Confirm the directory that was entered is the correct one
echo "Is this the correct directory? [y/n]"

        if ["$read" = "y"]; then
        cd $path
        exit 1;
When I run the script, I am prompted for the directory, but after I insert the data, I get the following error message:

Please enter the full path of the directory:
Is this the correct directory? [y/n]
./encrypted_tar_backup[24]: test: A ] character is missing.
I didn't mention this earlier, but I am currently using the sh shell within the script.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP/MySQL - Proper syntax for using alphabetical variables in a query? win32sux Programming 3 03-10-2008 06:37 AM
proper syntax for 'more' command bourne Linux - General 6 09-14-2007 09:26 AM
Tar for backups ? Proper syntax ? Reefcrazed Linux - Newbie 8 02-21-2006 05:18 PM
C syntax problem with if statements illiniguy3043 Programming 3 12-06-2004 02:06 AM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 02:41 AM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:42 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration