LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH function using if,then,else (https://www.linuxquestions.org/questions/programming-9/bash-function-using-if-then-else-878250/)

mrmnemo 05-02-2011 12:50 AM

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.

xeleema 05-02-2011 01:28 AM

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 $@"

mrmnemo 05-02-2011 02:23 AM

Well that joker would be a real douche I would think.

mrmnemo 05-02-2011 02:35 AM

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.

catkin 05-02-2011 02:59 AM

Quote:

Originally Posted by xeleema (Post 4343512)
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

catkin 05-02-2011 03:02 AM

Quote:

Originally Posted by mrmnemo (Post 4343569)
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.

mrmnemo 05-02-2011 03:06 AM

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?

catkin 05-02-2011 03:13 AM

Quote:

Originally Posted by mrmnemo (Post 4343569)
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.


All times are GMT -5. The time now is 07:38 PM.