LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   bash scripting problem with nested if statements in while loop error (http://www.linuxquestions.org/questions/programming-9/bash-scripting-problem-with-nested-if-statements-in-while-loop-error-725108/)

xskycamefalling 05-11-2009 01:28 AM

bash scripting problem with nested if statements in while loop error
 
ok so im getting a syntax error on line 74 the 2nd to last fi the code is as follows

Code:

#!/bin/sh

echo "enter the sentance or word you would like to have translated"
read "line"
y="y"
n="n"
echo "Would you like to have this sentance translated to pig laton?"
echo "y or n"
read "answer"
if [ "$answer" = "$y" ]
then
    pig=`echo "$line" | pig`
    echo "$pig"
else
    if [ "$answer" = "$n" ]
    then
        echo "would you like to have this sentance translated to morse code?"
        echo "y or n"
        read "answer"
            if [ "$answer" = "$y" ]
            then
                morse=`echo "$line" | morse -s`
                echo "$morse"
            else
                if [ "$answer" = "$n" ]
                then
                    echo "fine then"
                fi

            fi
    fi
fi

while [ "$answer1" != "$n" ]
do
    echo "is there anything else you would like to have translated?"
    echo "y or n"
    read "answer1"
    if [ "$answer1" = "$y" ]
    then
        echo "enter the sentance or word you would like to have translated"
        read "line"
    else
        if [ "$answer1" = "$n" ]
        then
            echo "FINE THEN GOODBYE"
        fi
    fi
echo "would you like to have this sentance translated to pig laton?"
echo " y or n"
read "answer"
if [ "$answer" = "$y" ]
then
    pig=`echo "$line" | pig`
    echo "$pig"
else
    if [ "$answer" = "$n" ]
        echo "Would you like to have this sentance translated to morse code?"
        echo "y or n"
        read "answer"
        if [ "$answer" = "$y" ]
        then
            morse=`echo "$line" | morse -s`
            echo "$morse"
        else
            if [ "$answer" = "$n" ]
            then
                echo "ok"
            else
            sleep 5

            fi
        fi
    fi
fi
done

any help is appreciated

Dralnu 05-11-2009 01:59 AM

First, I would change /bin/sh to /bin/bash. This way you should be sure that everything will work as expected incase this script it run on a diffrent system that has /bin/sh linked to another shell, or actually contains the old Bourne Shell.

Secondly, you're missing a then after line 57

H_TeXMeX_H 05-11-2009 04:08 AM

Quote:

Originally Posted by Dralnu (Post 3536679)
First, I would change /bin/sh to /bin/bash. This way you should be sure that everything will work as expected incase this script it run on a diffrent system that has /bin/sh linked to another shell, or actually contains the old Bourne Shell.

I disagree. /bin/sh must be linked to bash-compatible shell, and in any system in the past decade it is definitely NOT the old Bourne shell.

I think it is correct to use /bin/sh instead of /bin/bash as TODAY it is more likely that bash may not be installed and instead /bin/sh is linked to say ksh or other bash-compatible. So it's better to use /bin/sh IMO.

Dralnu 05-11-2009 02:02 PM

Quote:

Originally Posted by H_TeXMeX_H (Post 3536761)
I disagree. /bin/sh must be linked to bash-compatible shell, and in any system in the past decade it is definitely NOT the old Bourne shell.

I think it is correct to use /bin/sh instead of /bin/bash as TODAY it is more likely that bash may not be installed and instead /bin/sh is linked to say ksh or other bash-compatible. So it's better to use /bin/sh IMO.

Do you mean POSIX-compatable shell?

H_TeXMeX_H 05-11-2009 04:14 PM

Do you know of any distro today that still uses the old Bourne shell ? I don't. But I know some distros where bash is not necessary and is replaced by ksh, but the symlink from /bin/sh is still there.


All times are GMT -5. The time now is 06:54 PM.