|
Programming C, bash, Python, Perl, PHP, Java, you name it. |
|
Thread Tools | Display Modes |
|
|||
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" TIA |
|
|||
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 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 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
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
|||
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 The same command but now using a quoted FILE variable Code:
$ cat "$FILE" cat: name with spaces: No such file or directory
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
|||
thanks, I give that a shot.
|
|
||||
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 |
|
|||
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 |
|
|||
I think Unix shell scripting with sh/ksh is a good starting point.
|
|
||||
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 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:
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' |
|
||||
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''. |
|
|||
Kochan and Wood wrote a good primer called Unix Shell Programming.
__________________
And the WORD was made flesh, and dwelt among us. (John 1:14) |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Good Bash/Scripting book? | cwhitmore | Programming | 10 | 22nd December 2017 03:59 PM |
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 |
Color shell? | giga | FreeBSD General | 3 | 14th August 2008 12:07 AM |
Shell Script. | bsdnewbie999 | Programming | 21 | 15th July 2008 07:54 AM |