LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 05-11-2009, 01:28 AM   #1
xskycamefalling
Member
 
Registered: May 2009
Posts: 43

Rep: Reputation: 15
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
 
Old 05-11-2009, 01:59 AM   #2
Dralnu
Member
 
Registered: Jul 2005
Distribution: Arch Linux, Gentoo Linux
Posts: 335

Rep: Reputation: Disabled
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
 
Old 05-11-2009, 04:08 AM   #3
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Quote:
Originally Posted by Dralnu View Post
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.
 
Old 05-11-2009, 02:02 PM   #4
Dralnu
Member
 
Registered: Jul 2005
Distribution: Arch Linux, Gentoo Linux
Posts: 335

Rep: Reputation: Disabled
Quote:
Originally Posted by H_TeXMeX_H View Post
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?
 
Old 05-11-2009, 04:14 PM   #5
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
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.
 
  


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
[bash] if statements in a loop MarkGM Programming 3 04-02-2009 09:12 PM
bash scripting: loop over a file, replacing two decimal numbers frankie_DJ Programming 2 04-30-2007 05:04 PM
nested loop in Perl help needed Grafbak Programming 9 12-19-2006 11:28 AM
Nested-double loop error Harry Seldon Programming 3 05-06-2006 06:15 PM
Do/while loop and if statements not functioning properly w/ array... ohfaney Programming 5 05-02-2006 03:22 AM


All times are GMT -5. The time now is 09:40 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration