Linux Blog

Sequences with seq

Filed under: Quick Linux Tutorials,Shell Script Sundays — TheLinuxBlog.com at 9:28 pm on Sunday, July 19, 2009

I’m going to keep this one short since time I’ve had a serious lack of time recently. If you need to free up some time by putting repetitive commands into loops, you can use the command seq.

Basically, you can use any generic loop such as the while or for loop. Here are two examples:

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

seq 1 10 | while read i; do echo $i; done;

These both count and echo from 1 to ten. Replace the echo $i; with whatever it is you want to do. You can use sleep, to sleep for a number of seconds, and any other logic you wish here.

Screen Script, Part Deux

Filed under: Shell Script Sundays — TheLinuxBlog.com at 6:09 pm on Sunday, May 3, 2009

So, the last script for handling the screen didn’t quite work out as I had liked so I have stopped using it. Basically if you try to SCP something, it would flip out because there was no terminal. Also, if you were trying to do anything in X after logging in with SSH you would have to detach your screen session first, which could get annoying. I’ve came up with this snippet, its rather ugly but is intended to be run after you log in if you want to start a screen session. I’d like a solution that logs me in with this sort of dialog selection but also allows X and scp transfers to work if a selection is not made. Perhaps it could be done on a timer. Anyhow, here is the script:

screen -r $(dialog –stdout –menu Test 20 50 10 `echo $(screen -ls | grep \( | sed -e ‘s/\t/,/g’ | cut -d , -f 2- | awk -F ‘,’ ‘{print $1,$2}’ | awk -F ‘.’ ‘{print $1,$3}’ | cut -d \  -f 1,3)`)

It errors if no selection was made but that can be changed pretty easily by adding some if logic. Perhaps I’ll tweak it and repost, or perhaps I’ll find a better solution. Who knows.

APC Access Temperature Query and Conversion. (2 of 2)

Filed under: Shell Script Sundays — TheLinuxBlog.com at 6:55 pm on Sunday, March 29, 2009

This second part of the script APC Access Temperature Query Script and its been a long time coming. Basically this script is the part that runs as a cron and will e-mail me if the temperature goes over a certain threshold. Once it returns to normal it e-mails me again. It has the option to send a text message to me via my SMS gateway, but it is commented out.

#!/bin/bash

temp=$(/home/linux/bin/temp f)
threshold=76

if [ "`echo \"$temp > $threshold\" | bc`" == 1 ]; then
echo $(date +%s) $temp >> /home/linux/thermal-over.log
echo “High Temp”;

if [ "$(cat temp.txt)" == "norm" ]; then
echo “Sending E-Mail, High Temp”;
echo “Current Temperature Is: $(/home/linux/bin/temp f)” | mail -s “Thermal Overload” owen@linuxblog                #echo “Current Temp Is: $(/home/linux/bin/temp f)” | mail -s “Thermal Overload” mynumber@cingularme.com
echo “high” > temp.txt
fi
elif [ "`echo \"$temp < $threshold\" | bc`" == 1 ]; then
echo “Low Temp”;

if [ "$(cat temp.txt)" == "high" ]; then
echo “Temp Resumed, Sending E-Mail”;
echo $(date +%s) Resumed at: $temp | mail -s “Thermal Normal” owen@linuxblog
echo “norm” > temp.txt
fi

fi

echo $(date +%s) $temp >> /home/linux/thermal.log

When I first wrote the script, I did not do any temperature checking. I found out that I needed to when I came back one morning with a bunch of emails that I needed to delete. Its pretty simple to figure out, temp.txt holds a value that is either norm or high. It gets switched when the temperature changes, this will in turn stop it from e-mailing me repeatedly. Once the temperature drops it flips it back. It will still e-mail if your temperature fluctuates between 75 and 77 which can be annoying, but you can adjust the threshold with the variable and set it to what you need. Thankfully our chiller has been fixed and I no longer have to worry about the temperature, but it still runs on a cron just in case.

Logging Sessions to Twitter

Filed under: Linux Software — TheLinuxBlog.com at 12:01 am on Wednesday, August 20, 2008

If you follow me on Twitter you would know I asked everyone if there was anything that they wanted me to write about. @Ben_Marvin responded and asked about logging the commands you type to Twitter.At first, I thought that this could be done with history, which it probably can be, but does everything you type really have to be echo’ed to Twitter? I don’t think that you’d have many friends, Twitter would probably hate you and you’d most likely hit your maximum requests per hour pretty quickly.The Script command is another option, and this works quite well for this purpose. Read the script man page to find out more about this program. It basically (when ran) takes the I/O from your terminal and logs it to a file. Its a very handy utility.So, how do we get this data into Twitter? First of all, Twitter doesn’t allow very long posts so echoing out the data that the commands you type is not really practical. The best way to do it is to use script to log the session, exit the session and grep for the “]0\;” string for stuff you typed & not the responses.Here is the code:

script; grep ]0\; typescript 

You can then copy and paste it to your favorite Twitter application or pipe it to a scrubbing script to remove the formatting and do the correct HTML stuff, then post it via the http interface. Either way, it can be done even though I don’t think it really should.

Automated scanning with the shell

Filed under: Shell Script Sundays — TheLinuxBlog.com at 9:25 am on Sunday, July 27, 2008

I recently needed to scan a lot of images on my desktop PC. Unfortunately I am not the owner of an automatic document feed printer, and if I were it wouldn’t have helped this time because the documents I needed to scan were not feed able. XSANE is a great way to scan documents visually in Linux. Its not the easiest to use, but it has plenty of options. Part of the SANE package is scanimage, scanimage can be used from the shell.

The first thing that I did was a few test images with scanimage. I quickly found out that scanimage outputs in pnm format, and at a high resolution if the correct options are used. Once I found out the good options for my scanner (scanimage –resolution 400 > file.pnm) I wrote a quick shell script to scan up to 1000 times or until I don’t give the script any input. To do this, I used a combination of snippets that can be found in this blog column.

Here is a direct link to the script, and the shell script source below

#!/bin/bash
for i in `seq 1 1000`; do

#get input line
read inputline;

if [ $inputline ]; then

#Process Scanned Image in BG
echo Scanning Pg$i;
scanimage –resolution 400 > Pg$i.pnm;
echo Next;
else
exit
fi

done;

To use it all I do is execute the script, and I get to scan up to 1000 documents providing I type something after it prompts “Next”, and then hit enter. Once I was done scanning, I just hit enter to stop the script execution and then moved on to manipulating the images with the shell.

Hope this shell script scanning script is useful, if it is then drop me a comment, or if you have any suggestions or it was not at all helpful still drop me a comment.

Adding a service in Fedora

Filed under: Shell Script Sundays — TheLinuxBlog.com at 2:08 pm on Sunday, July 6, 2008

This week on Shell Script Sundays I’ll show you how to add a service to Fedora. This is very useful if you don’t happen to use yum for every service you want to run, and xinetd doesn’t really work for you.

Firstly there are three main parts to a Fedora service script. Start, Stop and Restart. They are pretty much self explanatory, but you don’t have to worry about the restart action since all it does is stop’s and then starts the service.

Without further ado here is the script:

#!/bin/bash
#
# Fedora-Service Update notification daemon
#
# Author:       TheLinuxBlog.com
#
# chkconfig:    1000 50 50
#
# description:  This is a test Fedora Service \
#               Second line of the fedora service template.
# processname:  FedoraTemplate
#
RETVAL=0;

start() {
echo “Starting Fedora-Service”
}

stop() {
echo “Stopping Fedora-Service”
}

restart() {
stop
start
}

case “$1″ in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $”Usage: $0 {start|stop|restart}”
exit 1
esac

exit $RETVAL

Now that you have a template for the script, you will want to modify it for your service. You need to keep the header at the top. This is how the Fedora Knows about your service. The three numbers indicate what order the scripts should start up and shut down in. The first seems to be a identification number and the other two are the startup and shutdown order. These can be adjusted depending on when you want the service to start up.Once you are done modifying the script put the script in /etc/init.d/

To make sure it works you can call it with service using the following actions:

service start
service stop
service restart

If all of the actions work, you are ready to add the service to the system. If you use the setup command as root it seems to do this step for you, but if you just want to add the service quickly without bothering to scramble through configuration menu’s you can do the following:

chkconfig –add <script name>

If you want the service to start automatically at boot up you can use ntsysv. For more information read my post on Managing Services on Fedora

Using wc and How To Count Table Rows

Filed under: Shell Script Sundays — TheLinuxBlog.com at 1:07 pm on Sunday, March 9, 2008

I made this little script to check how many packages were available on the web from the Cygwin Package Repository located at http://www.cygwin.com/packages

Its a one liner but it does its job well.

CYGLIST=$(curl http://www.cygwin.com/packages/ | grep \<tr | grep ball | wc -l); echo $CYGLIST;

All the above is doing is creating a variable called CYGLIST that is the result of grabbing the cygwin.com/packages/ page, grepping all of the TR’s that also have the word “ball” in it (for the image) and then using the wc -l (L) command to count how many results are found. Then the list is echoed out.

wc is a very useful command for printing newline, word and byte counts. This is a good example of how to use wc to count lines in a shell script. wc can also be used to print all of these values in one line of a file.  The syntax is below:

bash-3.1# wc file.txt
9  20 184 file.txt

The above shows the number of lines in the file.txt, it shows how many words are in the file and also how many bytes. In my first example wc uses the -l switch to display the number of lines. This script can also be used with a little bit of bash math to calculate how many items are in an HTML list. I’m working on a script that automatically does this, when its finished I will be sure to post it here on The Linux Blog.

Shell Script to get user input

Filed under: Shell Script Sundays — TheLinuxBlog.com at 2:06 am on Sunday, January 27, 2008

Creating a shell script to get input is rather easy. Shell scripts prompting input are generally more user friendly too. In this article I’ll show you how to read input from the bash shell. Take the example below:

#!/bin/bash

echo "Shell Script To Get User Input";

while read inputline
do
what="$inputline"
echo $what;

if [ -z "${what}" ];
then
exit
fi

done

All it does is echo’s the introduction “Shell Script To Get User Input” and then goes right to the bash read input loop. The next line makes a variable so that we can echo it out and also check if its empty with the if [ -z part. If the script is empty we exit, if not we loop around another time.

This is a very basic example but it can easily be modified so that you can use bash to grab user input. If you have any trouble with this script drop me a comment and I’ll be happy to help you out.