LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 11-11-2008, 01:13 PM   #1
kaplan71
Member
 
Registered: Nov 2003
Posts: 810

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:

Code:
# 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, 01:24 PM   #2
sfjoe
Member
 
Registered: Jun 2007
Location: San Francisco
Distribution: Centos 4, Debian
Posts: 55

Rep: Reputation: 16
if [ "$read" = "y" ]; then
# handle true case
else
# handle false case
fi
 
Old 11-11-2008, 01:44 PM   #3
kaplan71
Member
 
Registered: Nov 2003
Posts: 810

Original Poster
Rep: Reputation: 39
Hi there --

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

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

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

Quote:
Please enter the full path of the directory:
/tmp
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.
 
  


Reply


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 07:37 AM
proper syntax for 'more' command bourne Linux - General 6 09-14-2007 10:26 AM
Tar for backups ? Proper syntax ? Reefcrazed Linux - Newbie 8 02-21-2006 06:18 PM
C syntax problem with if statements illiniguy3043 Programming 3 12-06-2004 03:06 AM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 03:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:29 AM.

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