LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Proper Syntax for If...Then..Else statements with a script (https://www.linuxquestions.org/questions/linux-software-2/proper-syntax-for-if-then-else-statements-with-a-script-682651/)

kaplan71 11-11-2008 12:13 PM

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.

sfjoe 11-11-2008 12:24 PM

if [ "$read" = "y" ]; then
# handle true case
else
# handle false case
fi

kaplan71 11-11-2008 12:44 PM

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.


All times are GMT -5. The time now is 02:12 AM.