Linux Blog

Asterisk AGI IP Address lookup

Filed under: General Linux — TheLinuxBlog.com at 12:01 am on Sunday, September 19, 2010

While not exactly a shell script, I figured it would be worthy to post something rather than nothing and technically it is a script none-the-less.
I was experimenting with Asterisk AGI scripts and needed a project. I decided that an IP address lookup would be a good one. Sometimes my dynamic IP changes but my dynamic DNS doesn’t update. With this script, I should be able to dial into my Asterisk machine and get it to tell me the IP address. At least, that’s the plan.

(Read on …)

Searching for multiple strings with grep

Filed under: Shell Script Sundays — TheLinuxBlog.com at 9:36 pm on Sunday, August 23, 2009

Sometimes when using grep it’s nice to be able to search for more than one string in a file. It’s actually pretty easy to do, if you can remember the simple syntax. Basically, you pipe the terms together in double quotes and escape the pipe.

grep "gnome\|kde" install.log

The above example will search for gnome or KDE. It works for more more than one term, and also works with the invert match -v option, to exclude lines that include gnome or kde.

Todays article extra short, and late, but better late and short than none at all I guess.

Bash Scripting Techniques

Filed under: Shell Script Sundays — TheLinuxBlog.com at 10:20 pm on Sunday, May 18, 2008

Here are some techniques that you can use in your bash scripts for finding and searching through files. Combined with other shell scripting techniques these can be very powerful.

Find all files in the current directory and print them:

find . -iname ".jpg"

Find all files that you have access to read with matching patern:

find / -iname "pattern"

Normally with grep text is matched and is case sensitive. Heres how to do a case insensitive search with grep:

cat [filename] | grep -i [match]

Finding and replacing text is easily done in bash with sed. This find and replace puts the contents into a new file:

 cat [filename] | sed 's/FIND/REPLACE/' > [new filename]

Finding the line number that a particular line of text is on is sometimes useful. Here is how to do it:

 cat [filename] | grep -n [match]

Looping over a file in bash and echoing the output is sometimes useful for the processing of text files. Heres how to do it:

cat [filename] | while read i; do echo $i; done

Thats about all the bash scripting techniques that I can currently think of for finding in files. I know there are a ton more that I use but its hard to write them all down at once. As I come up with them or solve a problem I’ll add them here. If you have any of your own, please leave them in the comments.

Perl Regular Expression Cheat Sheet

Filed under: Shell Script Sundays — Owen at 10:15 pm on Saturday, October 6, 2007

Regular Expressions can be tricky, that’s why it is a good idea to keep a quick “cheat sheet” handy when working with them, here’s a concise cheat sheet to get you started:

.  	Match any character
\a  	Match alarm
\d 	Match digit character
\D  	Match non-digit character
\e  	Match escape
\f  	Match form-feed
\n  	Match newline
\r  	Match return
\s  	Match whitespace character
\S  	Match non-whitespace character
\t  	Match tab
\w 	Match "word" character (alphanumeric and "_")
\W  	Match non-word character
\022 Match octal char (i.e. 22 octal)
\xff  	Match hex char (i.e. ff in hex)
 
*	Match 0 or more times
+      	Match 1 or more times
?      	Match 1 or 0 times
 
{n}    	Match exactly n times
{n,}   	Match at least n times
{n,m}	  Match at least n but not more than m times
 
^	Match if at beginning
$ 	Match if at end
 
Examples:
\d{2}-\d{2}-\d{2}		# match date in dd-mm-yy format
^[ \t]+ 			#match leading whitespace
[ \t]+$			#match trailing whitespace
^[ \t]+|[ \t]+$ 		#match leading or trailing whitespace
 
$string =~ m/text/;		#returns true if $string contains text, case sensitive
$string =~ m/text$/i;		#returns true if $string contains text
$string =~ s/text1/text/;	#replace text1 with text2 in $string
$string !~ m/text/;		#returns false if $string contains text, case sensitive
$string !~ m/text/i;		#returns false if $string contains text

I find it useful to print it out and have it handy whenever I wade into the murky waters of regular expressions.

Decision making in Bash If / Then / Else statements and Cases

Filed under: Shell Script Sundays — TheLinuxBlog.com at 3:44 pm on Sunday, August 5, 2007

There comes a time in every shell script where a decision has to be made.

To make a decision in bash the following if then else syntax can be used:

if [ condition ]
then

statements

[ elif [ condition ]

then

statements ]

[ else

statements ]

fi

Anything in non bold [ brackets ] is optional, and they should not be typed but the bold in them is required. Anything in bold must be typed. Statements are generally other commands or flow control statements.

To give an example of how to use bash if then else statements in the real world take the following scenario:

A system administrator has a strict habit of firing people that have too many .png files. He checks the systems regularly and makes sure that nobody has too many. The following script will display a message depending on the number of .png’s that are in the directory.

#!/bin/bash
gif_count=$(ls | grep .png | wc -l)
echo “Number of PNG’s: $gif_count”
if [ $gif_count -lt 10 ]
then
echo “He will be happy, you have less than 10 files”
elif [ $gif_count -gt 10 ] && [ $gif_count -lt 20 ]
then
echo “Consider deleting some files”
else
echo “you have too many files, DELETE SOME BEFORE HE FINDS OUT!”;
fi

Using Cases.

Cases are similar to if statements in that they match a expression and perform a task if the pattern is matched.

bash case syntax:

case expression in

pattern1 ) statements ;;

pattern2 ) statements ;;

esac

This is fairly simple and some people find this easier than doing if statements for simple logic. Take the following real world example:

The system administrator has recently gone on a bigger power trip than before. Since people got wise about using png’s and started saving images in other file formats he is now monitoring png’s gif’s and jpg’s. To combat the problem, you can use a case to count how many files you have of each type. (This is intended as an example, there are many ways to accomplish this task, this is just to demonstrate how cases work)

#!/bin/bash
#set all variables to 0
png=0
gif=0
jpg=0

# start loop
for wc in $(ls); do

case “$wc” in
*png ) let png=$png+1 ;;
*gif ) let gif=$gif+1 ;;
*jpg ) let jpg=$jpg+1 ;;
esac

# end loop
done

echo “Png’s $png”;
echo “gif’s $gif”;
echo “jpg’s $jpg”;

There you have it, two ways to make basic decisions in bash. Just figure out what you want to do then use an if then else, or a case statement to do the logic. I myself prefer if statements over cases as they make more sense to me and I find it easier to perform logic within ifs.

Programs used in this post
ls, echo, grep, wc