Shell Scripts |
Information Center |
As an IT professional, I regularly use shell scripts as part of my job. In addition, I frequently use them in my website design and maintenance.
Most of my shell scripts employ the Bourne Shell.
Shown below are some commands that I have found useful in writing shell scripts.
It is extremely important to learn how to use the UNIX stream editor sed. Below are actual sed commands I've imbedded in shell scripts. Frequently, I will use sed commands between pipes in the process of manipulating a stream of data.
| sed -e 's/.$//' | Removes the last character from every line |
| sed -e 's/ [ ]*/ /g' | Replaces multiple blank spaces with one blank space |
| sed -e 's/200[0-9,-]*x//' | Removes a string such as '2005-23-13x' from each line |
Another utility to learn is bc, particularly if you need to work with non-integer numbers in a shell script. This utility is an arbitrary precision calculator.
Here is an example of how I numerically compare two values in a shell script:
cat bc-ex1.sh
#!/bin/sh
if test `echo "if ( $1 <= $2 ) 0 ; if ( $1 > $2 ) 1" | bc` = 0
then
echo $1 is less than or equal to $2
else
echo $1 is greater than $2
fi
./bc-ex1.sh 6.233245 6.23323
6.233245 is greater than 6.23323
./bc-ex1.sh 6.233 6.23323
6.233 is less than or equal to 6.23323
|
| DAResler.net | Information Center |
Updated on October 12, 2007 |
Displayed on July 2, 2009
Copyright © 2006-2007 D. A. Resler. All rights reserved. |