•
•
•
•
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
![]() |
•
•
Join Date: Apr 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
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
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.
$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
----------------------------------------------
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
•
•
Join Date: Apr 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
$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.
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
to start inetd and as
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
2) then find the first line that starts with
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.
That script you run as
/etc/init.d/inetd startto start inetd and as
/etc/init.d/inetd startto 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
----------------------------------------------
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
•
•
Join Date: Apr 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
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) typeifconfig eth0(this is the first part of the command)
2) then find the first line that starts withinet, but notinet6(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!
![]() |
•
•
•
•
•
•
•
•
DaniWeb Shell Scripting Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
access activation animations api blogger blogging blogs code combo dani daniweb data debugging development dreamweaver dropdownlist flash gdata google gpl html innovation interactivity. key linux microsoft module multimedia net news openbsd product programming reuse rss scripting serial source tags vista web web development wysiwyg xml
- Help with switch/case statement navigation (PHP)
- Is Select Case statement case sensitive in VB6? (Visual Basic 4 / 5 / 6)
- Switch Case Statement (Java)
- switch/case statement (C++)
Other Threads in the Shell Scripting Forum
- Previous Thread: Find the total lines in the file
- Next Thread: learning shell scripting



Linear Mode