Linux Blog

Shell Script Flow Control – my most refered to articles

Filed under: Shell Script Sundays — TheLinuxBlog.com at 2:38 pm on Sunday, November 9, 2008

This week I do not have time to write a full fledged article on shell scripting, so I am going to some flow control and logic articles that I wrote that I read the most.

One script that I continuously refer to is one of the first shell scripting articles I wrote titled “When Photoshop Fails.” The reason I refer to this article is because it describes a couple of looping techniques and a loop that works with spaces in filenames. For those interested but do not want to read the whole thing here is the loop in short:

find * -iname “*” | while read i; do echo “$i”; done

The next article I’m linking to is the one I wrote on loops: http://www.thelinuxblog.com/for-while-and-until-loops-in-bash/ it outlines some of the basic looping techniques. What I fail to mention in this article is that seq can be used to generate sequences of numbers for the [in list] section. The following is how you would create a loop to loop from 1 to 10 echoing out each number:

for i in `seq 1 10`; do echo $i; done;

Select Statements in Bash is exactly what the title implies. How to implement select statements in bash scripting. Also known as switch’s and case’s in other languages many will be familiar with this sort of logic. Even though I do not refer to it as often as some of the other articles I think it is worth of a mention in this list.

Decision making using if statements is something that every shell scripter is bound to come across. While not exactly the most in depth article on bash if then else statements it is a good start for any one wanting to learn more.

I love dialog’s and it just so happens that dialog and xdialog do a great job of making dialogs for your shell scripts. Creating Dialogs with Dialog has some basic uses of dialog. While it is not exactly logic or flow control, it can be used to prompt the user for input or just to pretty things up a little. It just so happens that it has an X front end called xdialog which does a good job too.

This is not all of the articles I refer to, but its the ones I wrote. If any one else has a list of articles they refer to that are bookmarked that I wrote or not I’d be interested to see them. Just paste them in a comment.

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