# Bash script newbie help



## fullauto (Jan 2, 2013)

I wrote a couple scripts a few months ago. pfdown - Which asked how many minutes, and then executed a line to do just that bringing the firewall back up when the minutes had run out. And pfup - which was used to locate the pid of the sleeping pfdown process and kill -HUP the pid and allow the rest of the pfdown script to bring the firewall back up.

Both work great! 

However, I would really like to cut my teeth on some better bash scripts. But, I have to relay on you nice people as the particular knowlege I need is not mentioned on any reference that I found online.


```
ARG1=$1
ARG2=$2
echo $ARG1
echo $ARG2

if [ -z $ARG1 ] || [ $ARG1 != "-u" ] || [ $ARG1 != "-d" ]; then
  printf "%s\n" "Usage: firewall -u|-d [mins]"
  exit 0
```
I cannot seem to figure out how to have the script test if:
#1 [ -z $ARG1 ] or
#2 [ $ARG2 != "-u" ] or 
#3 [ $ARG2 != "-d" ] then
run the printf.

Basically, the script will test wether the first argument passed is empty, or not one of the intended switches. Then, will run the printf:usage if any of those tests come back true.

I know this is terribly newbie of me. But, any help would be greatly appreciated.


----------



## chatwizrd (Jan 2, 2013)

I like to use getopts. It is very easy to use.

http://www.mkssoftware.com/docs/man1/getopts.1.asp


----------



## shitson (Jan 3, 2013)

+1 on the above comment


----------



## kpa (Jan 3, 2013)

You gain nothing by using shells/bash for shell scripting. The plain old sh(1) does everything you need and when you finally hit a ceiling on its capabilities, switch to some other scripted language that offers more capabilities, for example lang/perl5.14.

I'm not trying to sound like a broken record here


----------



## UNIXgod (Jan 5, 2013)

kpa said:
			
		

> You gain nothing by using shells/bash for shell scripting. The plain old sh(1) does everything you need and when you finally hit a ceiling on its capabilities, switch to some other scripted language that offers more capabilities, for example lang/perl5.14.
> 
> I'm not trying to sound like a broken record here



getopts() is actually posix =) In fact the ops post doesn't use bashisms from what I see:

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/getopts.html

I'm not old enough to know if this is one of the Korn extensions or the original Bourne shell tools.

I agree with your statement. Though I prefer Ruby as a high level sh over perl. Either way you can't lose. Almquist's Shell, Perl and Ruby all fill the same semantics which in turn put them into a family of languages. On slim jails or services which don't have external languages to act as a dependency FreeBSD's sh (ash) provides everything a user needs to create their automations and programs.

Finally as for the parsing question. OP you code looks good. Consider a case statement with regular expression shell pattern to test against valid input. Using short circuits( i.e. || &&) are best in creating a script which rely on functions to be called on failing signals. ARG is redundant, consider just using $n direct or parsing $@.


```
#!/bin/sh
usage(){
        printf "%s\n" "Usage: firewall -u|-d [mins]"
        exit 0
}
case $@ in
        -[ud]|'')
                usage
                exit 0
                ;;
        *) 
                echo "With help from UNIXgod"
                exit 127
                ;;
esac
```


----------

