LinuxQuestions.org
Social Bookmarking all things Linux and Open Source
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
 
Thread Tools
Old 11-07-2009, 02:14 AM   #1
Bone11409
LQ Newbie
 
Registered: Jan 2009
Posts: 16
Thanked: 0
Looking for a command that is just like the goto but for os x bash


[Log in to get rid of this advertisement]
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.
macos Bone11409 is offline     Reply With Quote
Old 11-07-2009, 04:32 AM   #2
druuna
Senior Member
 
Registered: Sep 2003
Location: netherlands
Distribution: lfs
Posts: 3,269
Thanked: 78
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.
druuna is offline     Reply With Quote
Old 11-07-2009, 10:32 AM   #3
Bone11409
LQ Newbie
 
Registered: Jan 2009
Posts: 16
Thanked: 0

Original Poster
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 10:34 AM..
macos Bone11409 is offline     Reply With Quote
Old 11-07-2009, 10:54 AM   #4
catkin
Senior Member
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.0
Posts: 1,835
Blog Entries: 6
Thanked: 225
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 10:55 AM.. Reason: Added error code and {}
linux catkin is offline     Reply With Quote
Old 11-07-2009, 11:20 AM   #5
Bone11409
LQ Newbie
 
Registered: Jan 2009
Posts: 16
Thanked: 0

Original Poster
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: ` ;;'
macos Bone11409 is offline     Reply With Quote
Old 11-07-2009, 11:43 AM   #6
catkin
Senior Member
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.0
Posts: 1,835
Blog Entries: 6
Thanked: 225
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.
linux catkin is offline     Reply With Quote
Old 11-07-2009, 05:55 PM   #7
Bone11409
LQ Newbie
 
Registered: Jan 2009
Posts: 16
Thanked: 0

Original Poster
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"
macos Bone11409 is offline     Reply With Quote
Old 11-08-2009, 01:33 AM   #8
catkin
Senior Member
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.0
Posts: 1,835
Blog Entries: 6
Thanked: 225
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?
linux catkin is offline     Reply With Quote
Old 11-08-2009, 11:15 AM   #9
Bone11409
LQ Newbie
 
Registered: Jan 2009
Posts: 16
Thanked: 0

Original Poster
No reason just over looked it
macos Bone11409 is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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 goto dkrysak Programming 24 10-06-2009 07:01 AM
can you use goto in bash script sabliny Programming 3 10-07-2005 06:54 PM
goto/label command for scripting in bash shell terry.trent Linux - Software 2 04-04-2004 05:37 PM
Goto command? batfoot Linux - General 4 08-26-2003 08:17 PM
GOTO function in bash? sobchak Programming 1 07-22-2002 06:10 AM


All times are GMT -5. The time now is 05:05 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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration