LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Other *NIX
User Name
Password
Other *NIX This forum is for the discussion of any UNIX platform that does not have its own forum. Examples would include HP-UX, IRIX, Darwin, Tru64 and OS X.

Notices

Reply
 
LinkBack Search this Thread
Old 11-07-2009, 01:14 AM   #1
Bone11409
LQ Newbie
 
Registered: Jan 2009
Posts: 19

Rep: Reputation: 0
Looking for a command that is just like the goto but for os x bash


Hey I need a command like goto but for the os x leopard using bash I have look on the net but have not found any thing.
 
Old 11-07-2009, 03:32 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 7,510
Blog Entries: 1

Rep: Reputation: 1138Reputation: 1138Reputation: 1138Reputation: 1138Reputation: 1138Reputation: 1138Reputation: 1138Reputation: 1138Reputation: 1138
Hi,

Bash does not have a goto statement.

Depending on what you want to do, which you do not mention, you need to use some other construction.

Post the problem (as detailed as possible) and we are probably able to help you with a solution.

Hope this helps.
 
Old 11-07-2009, 09:32 AM   #3
Bone11409
LQ Newbie
 
Registered: Jan 2009
Posts: 19

Original Poster
Rep: Reputation: 0
Ok I am trying to compare this variable gateway=$(netstat -r | grep default | awk '{print $2}' to a list of 5 different gateway address's and then when it cycles threw and finds the one it match's to then it will skip the comparing of the rest and give me another variable to show that gateway's name. Here's kind of what I had in mind.

#!/bin/bash
gateway=$(netstat -r | grep default | awk '{print $2}'
if [ "$gateway" = "10.184.16.1"]; then
name=$1floor
goto line1
else
if [ "$gateway" = "10.184.32.1"]; then
name=$2floor
goto line1
else
line1
exit
fi
fi
'

Last edited by Bone11409; 11-07-2009 at 09:34 AM.
 
Old 11-07-2009, 09:54 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.37, Debian Squeeze
Posts: 7,964
Blog Entries: 25

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Code:
#!/bin/bash
gateway=$(netstat -r | grep default | awk '{print $2}'
case "$gateway" in
    '10.184.16.1' )
        name=${1}floor
        ;;
    '10.184.32.1' )
        name=${2}floor
        ;;
    * )
        <error commands>
esac

<line1 commands>
exit

Last edited by catkin; 11-07-2009 at 09:55 AM. Reason: Added error code and {}
 
Old 11-07-2009, 10:20 AM   #5
Bone11409
LQ Newbie
 
Registered: Jan 2009
Posts: 19

Original Poster
Rep: Reputation: 0
Hey I just try your script here on my laptop and it did not work for me. Also could you walk me threw that command I have not seen it before.


command substitution: line 6: syntax error near unexpected token `newline'
/Users/bone/Desktop/fixodtest: command substitution: line 6: ` '10.184.16.1' '
/Users/bone/Desktop/fixodtest: line 6: syntax error near unexpected token `;;'
/Users/bone/Desktop/fixodtest: line 6: ` ;;'
 
Old 11-07-2009, 10:43 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.37, Debian Squeeze
Posts: 7,964
Blog Entries: 25

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Quote:
Originally Posted by Bone11409 View Post
Hey I just try your script here on my laptop and it did not work for me. Also could you walk me threw that command I have not seen it before.


command substitution: line 6: syntax error near unexpected token `newline'
/Users/bone/Desktop/fixodtest: command substitution: line 6: ` '10.184.16.1' '
/Users/bone/Desktop/fixodtest: line 6: syntax error near unexpected token `;;'
/Users/bone/Desktop/fixodtest: line 6: ` ;;'
The GNU Bash Reference Manual and Advanced Bash-Scripting Guide describe the case-esac command.

As a matter of good practice I changed the double quotes around the IP addresses to single quotes (it would have worked with the double quotes but it is prudent to use single quotes unless double quotes are necessary).

I changed $1floor to ${1}floor presuming you wanted the value of $1 suffixed with the string 'floor' -- the original usage would have referenced a variable named 1floor.

Regards your error, try changing
Code:
gateway=$(netstat -r | grep default | awk '{print $2}'
to
Code:
gateway="$(netstat -r | grep default | awk '{print $2})')"
Sorry -- I did not notice the missing ')' in your original post. Adding the double quotes is good practice in case the string returned by the netstat command has embedded whitespace; without the double quotes bash would try to run anything after the whitespace as a command.
 
Old 11-07-2009, 04:55 PM   #7
Bone11409
LQ Newbie
 
Registered: Jan 2009
Posts: 19

Original Poster
Rep: Reputation: 0
Here's what I have come up with it's simple and it work's

#!/bin/bash
gateway=$(netstat -r | grep default | awk '{print $2}')
if [ "$gateway" = "10.184.16.1" ]; then
name=$"1floor"
fi
if [ "$gateway" = "10.184.32.1" ]; then
name=$"2floor"
fi
echo "$name"
 
Old 11-08-2009, 12:33 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.37, Debian Squeeze
Posts: 7,964
Blog Entries: 25

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Quote:
Originally Posted by Bone11409 View Post
Here's what I have come up with it's simple and it work's

#!/bin/bash
gateway=$(netstat -r | grep default | awk '{print $2}')
if [ "$gateway" = "10.184.16.1" ]; then
name=$"1floor"
fi
if [ "$gateway" = "10.184.32.1" ]; then
name=$"2floor"
fi
echo "$name"
Glad it works for you. Why are you using ANSI-C quoted strings in name=$"1floor" when you have no backslash-escape sequences rather than the simpler name=1floor?
 
Old 11-08-2009, 10:15 AM   #9
Bone11409
LQ Newbie
 
Registered: Jan 2009
Posts: 19

Original Poster
Rep: Reputation: 0
No reason just over looked it
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH goto dkrysak Programming 27 12-22-2010 08:30 AM
goto/label command for scripting in bash shell terry.trent Linux - Software 3 07-09-2010 10:15 AM
can you use goto in bash script sabliny Programming 3 10-07-2005 05:54 PM
Goto command? batfoot Linux - General 4 08-26-2003 07:17 PM
GOTO function in bash? sobchak Programming 1 07-22-2002 05:10 AM


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

Main Menu
 
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
Open Source Consulting | Domain Registration