Linux Blog

Bringing The Internet Up After Failure

Filed under: Shell Script Sundays — TheLinuxBlog.com at 9:58 pm on Sunday, September 9, 2007

This Shell Script Sunday is a short one but don’t let that fool you to the power of the shell. This script I wrote earlier in the week due to power spikes at the office. All of our equipment would stay powered on due to UPS’s but unfortunately something with the ISP was not staying on. Once the brownout occurred our router box would still have an IP and seem to be working but it wouldn’t. We had our suspicions about what piece of equipment it was but had no power to fix it. I would renew the IP from the ISP bring the public interface down by using eth0 down and then eth0 up but this was not successful. To fix it from the router I had to actually reset the network. This worked, but we have some services running at the office that I like to access from home. So to fix the problem I wrote a one liner to reset the network if the connection goes down.

ping -c 1 OurISP.com 2> /dev/null > /dev/null && echo > /dev/null || sudo /etc/rc.d/network restart

The techniques in this script are covered in Shell Scripting 101. All this does is ping OurISP.com one time and output the error & standard output to /dev/null. If the ping was successful it does nothing and if the ping failed then it restarts the network. To get it to repeat at an interval I just set it up as a cron job. This did the trick and I now do not have to worry about brownouts.

For, While and Until Loops in Bash

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

Normally in a shell script it is desirable to do something repetitive.
I have touched on some loops in other posts but now would like to go over them in a bit more detail. Each of the examples in this post are intended to give an introduction to looping in bash.

For Loops
For loops allow you to repeat a section of code a number of times. Its very similar to other languages syntax but works a little differently. The for loop in bash only allows you to give a fixed list of values to loop over. A good way to remember how a for loop works is “For each of the dishes: clean and dry.”
For Syntax:

1
2
3
4
for i [in list]
do
statements [use $i]
done

For Example:

1
2
3
4
5
6
for x in 1 2 3
do
echo "Number: $x"
done
 
echo "Finished!"

This is a very simple script that just counts to 3 and then prints “Finished!”

While and until Loops
In essence while and until are the same in bash. The titles are pretty much self explanatory. A while loop would be explained in real life as “While the sink is still full: wash dishes” and a until loop would be “Until the sink is empty: Wash dishes.”
While and Until Syntax:

1
2
3
until/while [condition] do
statements
done

Example of a While loop:

1
2
3
4
5
6
count=1
while [ $count -lt 10  ]; do
echo $count
let count=$count+1
done
echo "Finished!"

Basically this loop will loop over the code while the count variable is less than 10. If we didn’t put the let statement in the script it would get stuck in the loop causing the user to press CTRL+C to end the script.

Doing the same thing can be done in a until loop except the condition has to be modified to get the same result.
Until example:

1
2
3
4
5
6
count=1
until [ $count -gt 9 ]; do
echo $count
let count=$count+1
done
echo "Finished!"

Now that you’ve figured out how to loop over something its probably a good idea to know how to stop the loop.
All that needs to be done to stop a loop is:

break

Break Example:

1
2
3
4
5
6
7
8
for x in 1 2 3 4 5
do
if [ $x = 3 ]; then
echo "Number is 3. Quitting"
break;
fi
echo "Number: $x"
done

This is a very easy to follow example. Its the same as the basic for loop except that if x is 3 it will stop the loop. This example has no real practical purpose. Since its a for loop the number 3 could just have been omitted.

Real World For Loop Example
Looping over all files in /etc and printing all of those that match “grep conf” and putting them in quotes.
The code to do this in a loop is:

1
2
3
4
for x in $(ls /etc -1|grep conf);
do
echo "$x"
done

The situation for many bash scripts is that there is normally a shorter way to do something. Take the Real World For Loop Example in this tutorial the same results can be achieved with:

x=$(ls /etc |grep conf); echo "$x"\n

This will get the job done but a loop may be better for esthetic purposes or for additional logic.

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

Recent changes to The Linux Blog.

Filed under: The Linux Blog News — TheLinuxBlog.com at 10:39 pm on Sunday, July 29, 2007

This article is mostly just updates on the site.

Server Changes
I have recently moved the site to a new server. This was a fairly simple task which took longer than it should because of DNS issues.

Now that it is on the new Linux based web host all should be dandy. The fact that I have more flexibility over the old host is an added bonus.

Bugs

Some problems that surfaced after I moved the site have just been resolved in a matter of minutes ago.

The biggest problem the site had was that the detailed/archive WordPress URL’s were not getting processed correctly. I was aware of the problem last night but was too tired to fix it. The fix was simple. I had to place the .htaccess file in the directory. The problem was that when I used lftp to mirror the old code, it didn’t download the hidden .htaccess file. I should have checked this before I updated the DNS but I guess something will always go wrong and I’m glad it was a simple fix. When I figure out how to mirror a directory in lftp including hidden files I will be sure to let everyone know. After scanning the help for the mirror command it didn’t jump out at me, but maybe thats just because I’m tired.
The URL’s that were affected by the bug were:

http://www.thelinuxblog.com/2007/07/29/shell-script-sundays/

http://www.thelinuxblog.com/2007/07/28/phones-meet-linux/

New Category Created
Not only did I fix this little problem, I also created the Shell Script Sundays section and moved the related posts to that category. The When Photoshop Fails article was posted on a Monday but I did most of the writing on Sunday, so I believe that since its mostly a shell scripting article it is worthy of this category.

After writing a paragraph in the description section of WordPress, I realized that it doesn’t actually show up anywhere on the site. I’m unsure if it shows up in the RSS feeds or not but I’m going to post it here anyway for the web browsers.

Shell Script Sundays Description
This section of the site is dedicated to Linux shell scripts. Twice a month I will post a nifty shell script that will perform a certain task. Most of the scripts will be written in Bash or the Korn shell and occasionally a CLI PHP or Perl script may surface. Some scripts will be more advanced than others and some will require additional software to be installed. This section will show how powerful scripting can be and I hope it educates people on how to shell script with Linux.

More to come!

Expect a Linux related post within the next few days. I have hundreds of topics to choose from, but I am always willing to take peoples suggestions on what to blog about. So if there is a particular topic that interests you, just contact us and we’ll do our best to cater to your needs.

Ciao,

- Owen.

Shell Script Sundays

Filed under: The Linux Blog News — TheLinuxBlog.com at 12:06 am on Sunday, July 29, 2007

Well, I have been planning content for this blog for a while and I now have a rather healthy long list of Linux related subjects that I can get crunching on.

One of the new sections I am going to add is going to be on shell scripts. It will be called Shell Script Sundays. Expect useful and handy shell scripts that anyone can use to do specific tasks.

This is going to be a good way to show off how powerful the shell is and what can be achieved with one. I hope to educate and help people along the way. So tune in next week for the first ever edition of Shell Script Sundays and feel the power of the almighty shells.

« Previous Page