LinuxQuestions.org
Help answer threads with 0 replies.
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-02-2011, 12:50 AM   #1
mrmnemo
Member
 
Registered: Aug 2009
Distribution: linux
Posts: 527

Rep: Reputation: 51
Smile BASH function using if,then,else


Hi,
I am trying to build a function insode a script. If some one could tell me how I can do this I would appreciate it.
Code:
#!/bin/sh
#System commands and other configurables.
IPT=/sbin/iptables
IP6T=/sbin/ip6tables
IPST=/usr/sbin/ipset
MODP=/sbin/modprobe
GET=/usr/bin/wget
INT_NET=192.168.1.0/24
SSH_PRT=
PERM_BAN=blacklist
TMP_BAN=tmpbanlist
GATES=gateway_list
RLIMIT=
ISO="af cn kr"
DLROOT=http://www.ipdeny.com/ipblocks/data/countries
ZONEROOT=/etc/firewall/zones
MKD=/bin/mkdir
TEST=genblacklist
MAIL=mail


### load connection-tracking modules
$MODP ip_conntrack
$MODP iptable_nat
$MODP ip_conntrack_ftp
$MODP ip_nat_ftp
#functions

###Clean-Up Existing Rule Set
cleanup()
{
	$IPT -F
	$IPT -F -t nat
	$IPT -X
	$IPT -P INPUT DROP
	$IPT -P OUTPUT DROP
	$IPT -P FORWARD DROP
	$IP6T -F
	$IP6T -X
	$IP6T -P INPUT DROP
	$IP6T -P FORWARD DROP
	$IP6T -P OUTPUT DROP
}

###Perm Blacklist : Set up daemon for updates weekly
genblacklist()
{
	for c in $ISO
		if [-f $ZONEROOT/$c.zone]; then 
			test $(date -d "-1 week" +%s) -lt $(stat -c "%Y" $ZONEROOT/$c.zone)
			printf "Zone files are up to date. Less than 1 week old." 
     		elif
     			test $(date -d "+1 week" +%s) -lt $(stat -c "%Y" $ZONEROOT/$c.zone)
			printf "Zone files are out of date. Updating." ; then
     			$GET -Nr $ZONEROOT $DLROOT/$c.zone
		else
			$MKD -p 
			$GET -Nr $ZONEROOT $DLROOT/$c.zone
		fi
}
I can find lots of tutorials in how to use if, then, else. However, how do I define a variable inside the function?
SEE>>
Code:
for c in $ISO
Also, am I using the 'test' command correctly( -/+ week as valid test)?

Any how, thanks for any help in advance.
 
Old 05-02-2011, 01:28 AM   #2
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 987
Blog Entries: 4

Rep: Reputation: 248Reputation: 248Reputation: 248
Greetings!

You're *really* close

Code:
for c in $( echo ${ISO} )
do
  if [ -f ${ZONEROOT}/${c}.zone]; then 
      test $(date -d "-1 week" +%s) -lt $(stat -c "%Y" ${ZONEROOT}/${c}.zone)
      printf "Zone files are up to date. Less than 1 week old." 
  elif
      test $(date -d "+1 week" +%s) -lt $(stat -c "%Y" ${ZONEROOT}/${c}.zone)
      printf "Zone files are out of date. Updating." ; then
      ${GET} -Nr ${ZONEROOT} ${DLROOT}/${c}.zone
  else
      ${MKD} -p 
      ${GET} -Nr ${ZONEROOT} ${DLROOT}/${c}.zone
  fi
done
P.S: Wrap your variables in curly brackets whenever you get a chance. That cuts down on the shell's chance to get confused.

P.P.S: If this script runs as root, you might want to consider using the absolute path to any binaries you call.
Jokers tend to leave little scripts in a filesystem named "ls" that contain "rm -rf $@"

Last edited by xeleema; 05-02-2011 at 01:34 AM. Reason: Because I have a sense of humor.
 
1 members found this post helpful.
Old 05-02-2011, 02:23 AM   #3
mrmnemo
Member
 
Registered: Aug 2009
Distribution: linux
Posts: 527

Original Poster
Rep: Reputation: 51
Well that joker would be a real douche I would think.
 
Old 05-02-2011, 02:35 AM   #4
mrmnemo
Member
 
Registered: Aug 2009
Distribution: linux
Posts: 527

Original Poster
Rep: Reputation: 51
Well the joker would be a douche wouldnt he. I mean to wipe a directory like that on someone. Any way.

So, could you tell me why the if, then, else statements are working? For some reason when conditions in if are not met, the script still runs as if they had been met. See below:
Code:
./Documents/new.set.script.sh: 73: [-f: not found
stat: cannot stat `/etc/firewall/zones/kr.zone': No such file or directory
test: 73: -lt: argument expected
Zone files are out of date. Updating./etc/firewall/zones: Scheme missing.
--2011-05-02 02:28:51--  http://www.ipdeny.com/ipblocks/data/countries/kr.zone
Resolving www.ipdeny.com... 64.85.170.117
Connecting to www.ipdeny.com|64.85.170.117|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 13909 (14K) [text/plain]
Server file no newer than local file `www.ipdeny.com/ipblocks/data/countries/kr.zone' -- not retrieving.
I thought that ifr the file had not been found at $ZONEROOT, else would take over and build the directory/file. No? Also, what does this mean:
Code:
test: 73: -lt: argument expected
Zone files are out of date. Updating./etc/firewall/zones: Scheme missing.
Thanks again.
 
Old 05-02-2011, 02:59 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,533
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by xeleema View Post
P.S: Wrap your variables in curly brackets whenever you get a chance. That cuts down on the shell's chance to get confused.

P.P.S: If this script runs as root, you might want to consider using the absolute path to any binaries you call.
Jokers tend to leave little scripts in a filesystem named "ls" that contain "rm -rf $@"
It's not the shell that gets confused -- it's the programmer! It is a matter of personal preference but I find using braces (curly brackets) makes the code more cluttered and hence harder to understand so I use them only when necessary -- that is when doing expansions and when the following character could be part of the variable name, for example echo "${animal}s are living critters"

The problem with absolute paths is they are not portable across systems that have executables in different directories. One solution is to put the names in variables (for example ls=/bin/ls) so they can be changed in one place. That's still cumbersome and requires modifying the script to work on a variety of systems. Perhaps the best solution (for security and to set up a defined shell execution environment) is for the script to set its own PATH, for example export PATH=/usr/bin:/bin
 
1 members found this post helpful.
Old 05-02-2011, 03:02 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,533
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by mrmnemo View Post
So, could you tell me why the if, then, else statements are working?
Because the [ ... ] shell builtin needs whitespace to the right of [ and to the left of ]. A space is the nicest choice.

BTW [[ ... ]] is more robust than [ ... ] for reasons explained here.
 
1 members found this post helpful.
Old 05-02-2011, 03:06 AM   #7
mrmnemo
Member
 
Registered: Aug 2009
Distribution: linux
Posts: 527

Original Poster
Rep: Reputation: 51
well all i want to do is create a folder with some files in it. Why does the script continue to elif in my script if the folder /etc/firewall does not exist? I thought thats what the starting if statement was looking for. if [-f some.file] is not found, then what ever commands are under else would be ran. No?
 
Old 05-02-2011, 03:13 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,533
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by mrmnemo View Post
Also, what does this mean:
[CODE]test: 73: -lt: argument expected
Not 100% sure but fixing the cause of stat: cannot stat `/etc/firewall/zones/kr.zone': No such file or directory will likely fix it.

BTW test, [ ... ] and [[ ... ]] are functionally similar; for clean coding you can pick one ([[ .. ]] is recommended) and use only that one.
 
1 members found this post helpful.
  


Reply

Tags
bash, scripting


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
[SOLVED] Setting function default in a bash script - Bash sixtensixone Linux - General 13 01-08-2011 01:44 PM
Bash array Add function example using indirect array reference as function argument bobywelsh Programming 10 07-05-2010 04:44 AM
Bash looping or function within a function FirmbIT Programming 2 04-24-2006 01:06 PM
Bash function pazvant Linux - Software 1 03-08-2005 07:47 AM
How do I add a function to Bash? Gag Halfrunt Linux - Newbie 5 11-25-2004 12:05 PM


All times are GMT -5. The time now is 09:14 PM.

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 Google+: linuxquestions
Open Source Consulting | Domain Registration