User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 456,425 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,593 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 2207 | Replies: 4
Reply
Join Date: Apr 2007
Posts: 5
Reputation: fresher is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
fresher fresher is offline Offline
Newbie Poster

case statement (meaning of this block of code)

  #1  
Apr 18th, 2007
Hi, im in the process of developing an iptables firewall for personal use...Being a newbie in shell scripting im trying to adapt a code i found here:http://homelansecurity.sourceforge.net/script.php. thanks to [J. Howard.] but dont quite understand this block of code below especially the content of '$1' variable as related to the case statement found at the bottom of the script: ie

case '$1' in
'up')
firewall_up
;;.......

##########################
//to show the $1 i dont understand
if [ $EXTINT != "DISABLE" ]; then
EXTIP=$( ifconfig $EXTINT | grep 'inet[^6]' | sed 's/[a-zA-Z:]//g' | awk '{print $1}' )
WANBCAST=$( ifconfig $EXTINT | grep 'inet[^6]' | sed 's/[a-zA-Z:]//g' | awk '{print $2}' )
WANMASK=$( ifconfig $EXTINT | grep 'inet[^6]' | sed 's/[a-zA-Z:]//g' | awk '{print $3}' )
WAN=$EXTIP/$WANMASK
fi

many thanks in advance for any help
Last edited by fresher : Apr 18th, 2007 at 6:55 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Posts: 1,509
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is offline Offline
Posting Virtuoso

Re: case statement (meaning of this block of code)

  #2  
Apr 18th, 2007
$1 is the first command line argument givben when the script is called (i.e. if the command is scriptname hello then $1 is hello).
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Apr 2007
Posts: 5
Reputation: fresher is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
fresher fresher is offline Offline
Newbie Poster

Re: case statement (meaning of this block of code)

  #3  
Apr 18th, 2007
Originally Posted by masijade View Post
$1 is the first command line argument givben when the script is called (i.e. if the command is scriptname hello then $1 is hello).


thanks for the post..but i dont quite get it. how do you mean if the 'command is scriptname hello'?what would be the output assigned to EXTIP when ' EXTIP=$( ifconfig $EXTINT | grep 'inet[^6]' | sed 's/[a-zA-Z:]//g' | awk '{print $1}' )' is executed..many thanks

Ps: EXTIP = IP address of eth0 (EXTINT)
Last edited by fresher : Apr 18th, 2007 at 7:45 am.
Reply With Quote  
Join Date: Feb 2006
Posts: 1,509
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is offline Offline
Posting Virtuoso

Re: case statement (meaning of this block of code)

  #4  
Apr 18th, 2007
I simply used scriptname as a stand in for the name of the script. But lets use a more commonly known script for this, i.e. the inetd script from /etc/init.d

That script you run as
/etc/init.d/inetd start
to start inetd and as
/etc/init.d/inetd start
to stop inetd

in the first case $1 is "start" and in the second case $1 is "stop" That is the $1 when used in the case statement. $1, $2, ..... $NF take on a different meaning when used inside awk. An awk command reads input (in this case standard input) line by line and separates the line into "fields" based on consecutive whitespace. i.e. the line "hello world" will result in $1 being hello and $2 being world.

To understand that complete command perform the following steps (eth0 is the most common network interface in linux. If you have something other than linux you will determine for yourself what the interface is. Use ifconfig to find one.)

1) type ifconfig eth0 (this is the first part of the command)

2) then find the first line that starts with inet, but not inet6 (this is the grep part)

3) then remove all alphabetic characters from that line (this is the sed part)

4) Then take the first "word" from that line, i.e. all characters up until the first "whitespace" character, disregarding leading whitespace (this is awk part when $1 is used)
if $2 is used then take the second word, i.e. the first grouping of characters after the first whitespace (disregarding leading whitespace) but before the second whitespace, etc. etc.
Last edited by masijade : Apr 18th, 2007 at 1:54 pm. Reason: formatting changes
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Apr 2007
Posts: 5
Reputation: fresher is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
fresher fresher is offline Offline
Newbie Poster

Re: case statement (meaning of this block of code)

  #5  
Apr 18th, 2007
Originally Posted by masijade View Post
I simply used scriptname as a stand in for the name of the script. But lets use a more commonly known script for this, i.e. the inetd script from /etc/init.d

That script you run as
/etc/init.d/inetd start
to start inetd and as
/etc/init.d/inetd start
to stop inetd

in the first case $1 is "start" and in the second case $1 is "stop" That is the $1 when used in the case statement. $1, $2, ..... $NF take on a different meaning when used inside awk. An awk command reads input (in this case standard input) line by line and separates the line into "fields" based on consecutive whitespace. i.e. the line "hello world" will result in $1 being hello and $2 being world.

To understand that complete command perform the following steps (eth0 is the most common network interface in linux. If you have something other than linux you will determine for yourself what the interface is. Use ifconfig to find one.)

1) type ifconfig eth0 (this is the first part of the command)

2) then find the first line that starts with inet, but not inet6 (this is the grep part)

3) then remove all alphabetic characters from that line (this is the sed part)

4) Then take the first "word" from that line, i.e. all characters up until the first "whitespace" character, disregarding leading whitespace (this is awk part when $1 is used)
if $2 is used then take the second word, i.e. the first grouping of characters after the first whitespace (disregarding leading whitespace) but before the second whitespace, etc. etc.


Masijade, thank you very much. I absolutely understand it now...much obliged!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 1:14 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC