DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 30th November 2008
chavez243 chavez243 is offline
Fdisk Soldier
 
Join Date: May 2008
Location: Leamington, ON
Posts: 50
Thanked 2 Times in 2 Posts
Default Shell Scripting with BSD

Anyone know of a good resource to read up on shell scripting?

I'm working on a script that take a single argument and checks various files for the presence of that expression:

eg. ./script.sh filename


so far I have:

Code:
#!/bin/sh
check=$1
result=`cat /path/to/file/file1 | grep $1`
if ($result !="")
   then 
   echo "$check found"
   exit
fi
echo "$check not found"
it's not doing exactly what I want though, I've googled, but haven't come across anything definitive... any help appreciated

TIA
Reply With Quote
  #2   (View Single Post)  
Old 30th November 2008
J65nko J65nko is online now
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 3,158
Thanked 182 Times in 149 Posts
Default

Surround the variable you want to test with quotation marks and use square brackets for testing:
Code:
#!/bin/sh

check="$1"
FILE=testfile
result=$( grep "$1" ${FILE} )

echo Fixed original version

if [ "$result" != "" ] ; then
    echo "$check found"
else
    echo "$check not found"
fi
A sample run with the -vx options
Code:
$ cat testfile         
the quick brown fox jumps over the lazy dog

$ sh -vx greptest "brown fox" 

#!/bin/sh

check="$1"
+ check=brown fox
FILE=testfile
+ FILE=testfile
result=$( grep "$1" ${FILE} )
+ grep brown fox testfile
+ result=the quick brown fox jumps over the lazy dog

echo Fixed original version
+ echo Fixed original version
Fixed original version

if [ "$result" != "" ] ; then
    echo "$check found"
else
    echo "$check not found"
fi
+ [ the quick brown fox jumps over the lazy dog !=  ]
+ echo brown fox found
brown fox found
The same but now without the quotation marks
Code:
if [ $result != "" ] ; then
    echo "$check found"
else
    echo "$check not found"
fi
+ [ the quick brown fox jumps over the lazy dog !=  ]
greptest[13]: [: quick: unexpected operator/operand
+ echo brown fox not found
brown fox not found
The shell complains about "quick" being an unexpected operator or operand. The shell uses spaces, tabs or newlines as field separator. It sees "the" and then "quick". But "quick" is not one of the operators defined in test(1). By embedding the variable within quotes you tell the shell not to separate the value of the variable but to treat it as one single unit.
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
  #3   (View Single Post)  
Old 30th November 2008
J65nko J65nko is online now
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 3,158
Thanked 182 Times in 149 Posts
Default

A simple demonstration how quoting a variable prevents the shell from breaking up a variable value containing whitespaces
Code:
$ FILE="name with spaces"
$ cat $FILE
cat: name: No such file or directory
cat: with: No such file or directory
cat: spaces: No such file or directory
Here the shell breaks up the value of the FILE variable into 3, and passes them to the cat utility. Because cat accepts multiple files, it first tries to display file "name", then file "with" and file "spaces". Because these files don't exist, we see cat issueing three error messages.

The same command but now using a quoted FILE variable
Code:
$ cat "$FILE"
cat: name with spaces: No such file or directory
Now cat only gives one single message: there is no file "name with spaces".
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
  #4   (View Single Post)  
Old 30th November 2008
chavez243 chavez243 is offline
Fdisk Soldier
 
Join Date: May 2008
Location: Leamington, ON
Posts: 50
Thanked 2 Times in 2 Posts
Default

thanks, I give that a shot.
Reply With Quote
  #5   (View Single Post)  
Old 30th November 2008
vermaden's Avatar
vermaden vermaden is offline
Administrator
 
Join Date: Apr 2008
Location: pl_PL.lodz
Posts: 1,052
Thanked 118 Times in 93 Posts
Default

Quote:
Originally Posted by chavez243 View Post
Anyone know of a good resource to read up on shell scripting?
This one is pretty good (Apple's Shell Scripting Primer):
http://developer.apple.com/documenta...hellScripting/

The Advanced Bash Scripting Guide is also pretty goood:
http://tldp.org/LDP/abs/html/

... but remember to always use $! /bin/sh (avoid bash) because that make scripts bash dependant while good scripts are POSIX sh compilant and really cross platform. Also bash only options are not good enought to stick to them, all of this can be done in POSIX sh in very similar way.
__________________
religions, worst damnation of mankind
"If 386BSD had been available when I started on Linux, Linux would probably never had happened." Linus Torvalds

Linux is not UNIX! Face it! It is not an insult. It is fact: GNU is a recursive acronym for “GNU's Not UNIX”.
vermaden's: links resources deviantart spreadbsd
Reply With Quote
  #6   (View Single Post)  
Old 1st December 2008
J65nko J65nko is online now
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 3,158
Thanked 182 Times in 149 Posts
Default

  1. Alternative 1
    Code:
    if [ ! -z "$result" ] ; then
        echo "$check found"
    else
        echo "$check not found"
    fi
  2. Alternative 2, which is used heavily in the OpenBSD rc scripts
    Code:
    if [ "X${result}" != "X" ] ; then
        echo "$check found"
    else
        echo "$check not found"
    fi
  3. Alternative 3
    Code:
    if test grep "$check" "$FILE" >/dev/null 2>&1 ; then
        echo "$check found"
    else
        echo "$check not found"
    fi
    This third alternative is the one I would use

Re: Resources

Don't forget examples like the startup/boot scripts in FreeBSD and NetBSD. By studying them I learned a lot about shell scripting.
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
  #7   (View Single Post)  
Old 1st December 2008
rliegh rliegh is offline
New User
 
Join Date: Nov 2008
Posts: 2
Thanked 0 Times in 0 Posts
Default

I think Unix shell scripting with sh/ksh is a good starting point.
Reply With Quote
  #8   (View Single Post)  
Old 1st December 2008
lvlamb's Avatar
lvlamb lvlamb is offline
Real Name: Louis V. Lambrecht
Spam Deminer
 
Join Date: May 2008
Location: .be
Posts: 221
Thanked 25 Times in 24 Posts
Default

BIG caveat: a default shell can be many.
On my OpenBSD system:
Code:
ls -al /bin | grep sh
-r-xr-xr-x   1 root  bin    355024 Oct 29 21:23 csh
-r-xr-xr-x   3 root  bin    403536 Oct 29 21:24 ksh
-r-xr-xr-x   3 root  bin    403536 Oct 29 21:24 rksh
-r-xr-xr-x   3 root  bin    403536 Oct 29 21:24 sh
-r-xr-xr-x   5 root  bin    138768 Oct 29 21:24 sha1
So, same code for the three sh, ksh, rksh
As, formerly in Linux sh/bash.

Now, scripts lin Linux starting with #!/bin/sh usually invoked the bash interpreter with the POSIX flag set.
This is changing on Debians (Lenny, Ubuntu) as the #!/bin/sh now invokes the dash (Debian Almquist shell).
http://packages.debian.org/search?ar...0&keywords=ash

I *assume* (assumptions are the mother of all f*ck-ups) that an OpenBSD rksh would (with some flags set in the code) invoke ksh (which is Public Domain ksh, not the 1993 AT&T ksh as on FreeBSD's ksh) with the restricted shell:
Quote:
The rksh or ksh -r command opens the Restricted Korn Shell. The behavior of these commands is identical to those of the ksh command, except that the following actions are not allowed:

* Change the current working directory
* Set the value of the SHELL, ENV, or PATH variables
* Specify the pathname of a command containing a / (slash)
* Redirect output of a command with > (right caret), >| (right caret, pipe symbol), <> (left caret, right caret), or >> (two right carets).
http://publib.boulder.ibm.com/infoce...cmds4/rksh.htm
Not excatly knowing what involing /bin/sh in OpenBSD would result in.


So, consider that under *any* operating system, the base shell is *undefined* at best, unless you know all the switches of your local installed variant.

Amazes me as, as for the fdisk, these are basic pieces of programing which makes your system work or break. And the less pieces which are takem into account, or at the least plainly *understood*.
__________________
da more I know I know I know nuttin'
Reply With Quote
  #9   (View Single Post)  
Old 2nd December 2008
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

If you assume that there is a /bin/sh and it supports a moderate portion of Bourne syntax, you shouldn't have much problems. ash, dash, bash, ksh, zsh; all support a common level of traditional syntax.


shells/v7sh is my favorite tool for testing scripts. I know, if v7sh understands it, almost any systems /bin/sh should, sort of systems with the old Thompson or PWB shells around... in which case, the script is probably screwed anyway.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
Old 3rd December 2008
JMJ_coder JMJ_coder is offline
VPN Cryptographer
 
Join Date: May 2008
Posts: 464
Thanked 8 Times in 8 Posts
Default

Kochan and Wood wrote a good primer called Unix Shell Programming.
__________________
And the WORD was made flesh, and dwelt among us. (John 1:14)
Reply With Quote
Reply

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell scripting resources J65nko Programming 0 23rd December 2008 09:57 PM
ports config and makefile scripting boincv FreeBSD Ports and Packages 6 1st October 2008 07:57 AM
Good Bash/Scripting book? cwhitmore Programming 7 15th August 2008 01:54 AM
Color shell? giga FreeBSD General 3 14th August 2008 12:07 AM
Shell Script. bsdnewbie999 Programming 21 15th July 2008 07:54 AM


All times are GMT. The time now is 06:39 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick