home

Quickies, by Andrea Olivato

text

Delete .svn folders from trunk with find

Quick code to remove all .svn files and folder from a trunk.

find . -name "*svn*" -exec rm -rf '{}' \;

2 years ago

July 13, 2009
Comments (View)
text

Create a Zenity progress bar using wget output

I found this simple script to redirect wget progress bar to a nicer zenity window.

Sed is used to parse the output and send to Zenity only the correct progress. An infinite bash cycle is used to check that zenity is still running (if the user closes the window probably you want wget to be killed)

# Start wget | zenity
# Note the & at the end of the pipe, this allows the script to continue with wget running in the background
wget http://filename 2>&1 | sed -u 's/.*\ \([0-9]\+%\)\ \+\([0-9.]\+\ [KMB\/s]\+\)$/\1\n# Downloading \2/' | zenity --progress --title="Downloading File..." &

#Start a loop testing if zenity is running, and if not kill wget
RUNNING=0
while [ $RUNNING -eq 0 ]
do
if [ -z "$(pidof zenity)" ]
then
  pkill wget
  RUNNING=1
fi
done

2 years ago

July 9, 2009
Comments (View)
text

Zenity dialog. Get the answer from Bash.

This shows how to create a Zenity dialog window and how to get the answer given by user into a Bash variable.

#!/bin/bash

# Creates a Zenity question dialog, 
# similar to JS confirm().
# Put the answer code on a variable.

ANSWER=$(zenity --entry --text "What's your name?" ); echo $szAnswer

# Creates a Zenity popup (information dialog)
# and put there user previous answer

zenity --info --text "Hello ${ANSWER}";

# On the questions dialog you can even set a
# text to be shown on the text field.
# This is like HTML value="" proprierty

ANSWER=$(zenity --entry --text "What's your name?" --entry-text "Type your name"); echo $szAnswer

2 years ago

July 7, 2009
Comments (View)
text

Zenity questions

Quick examples on how to launch Zenity questions from Bash and use the returned values.

#!/bin/bash

# Creates a Zenity question dialog, 
# similar to JS confirm().
# Put the answer code on a variable.

ANSWER=`zenity --question --text "Do you like me?"; echo $?`;

# Check what the user pressed
if [[ $ANSWER -eq 1 ]]
then
	# 1 => pressed cancel
	echo "No, you don't";
else
	# 0 => pressed ok
	echo "Oh yes, you do";
fi;

2 years ago

July 6, 2009
Comments (View)
text

Cycle all folder’s file and get each name in #bash

This is pretty useful if you need to do some operations on certain files in a folder. In the example I’m cycling all files in the apache log folder, selecting only the .log files and then get the basename (no path) of the log.

DIR="/var/log/apache2";
for FILE in ${DIR}/*.log
do
        FILE=`basename $FILE`;
        echo "Let's do some op on $FILE";
done

2 years ago

July 6, 2009
Comments (View)
text

Using Sqlite3 in Bash

After my previous post about working with sqlite3 on #perl, here’s an other quick script to explore how to use this simple DB in #bash.

#!/bin/bash

# Defining my databse first table
STRUCTURE="CREATE TABLE data (id INTEGER PRIMARY KEY,name TEXT,value TEXT);";

# Creating an Empty db file and filling it with my structure
cat /dev/null > dbname.db
echo $STRUCTURE > /tmp/tmpstructure
sqlite3 dbname.db < /tmp/tmpstructure;
rm -f /tmp/tmpstructure;

# Inserting some data into my structure
sqlite3 dbname.db "INSERT INTO data (name,value) VALUES ('MyName','MyValue')";
sqlite3 dbname.db "INSERT INTO data (name,value) VALUES ('MyOtherName','MyOtherValue')";

# Getting my data
LIST=`sqlite3 dbname.db "SELECT * FROM data WHERE 1"`;

# For each row
for ROW in $LIST; do

	# Parsing data (sqlite3 returns a pipe separated string)
	Id=`echo $ROW | awk '{split($0,a,"|"); print a[1]}'`
	Name=`echo $ROW | awk '{split($0,a,"|"); print a[2]}'`
	Value=`echo $ROW | awk '{split($0,a,"|"); print a[3]}'`
	
	# Printing my data
	echo -e "\e[4m$Id\e[m) "$Name" -> "$Value;
	
done

2 years ago

July 1, 2009
Comments (View)
text

String to upper or lower in #Bash

This shows how to convert a string to lower or upper using bash and tr utility.

STRING="myFAncyString";

# String to lower
echo $STRING | tr "[:upper:]" "[:lower:]";

# String to upper
echo $STRING | tr "[:lower:]" "[:upper:]";

Via spikesource

2 years ago

June 30, 2009
Comments (View)
text

Get your system load

How to get the system load only using uptime ouptut, filtered using awk.

uptime | awk -F'load average:' '{ print "Load: "$2 }'

On twitter @markkolich suggested me a quicker way to obtain the same result

cat /proc/loadavg

2 years ago

June 24, 2009
Comments (View)
text

memtop: order top by %Mem using expect

Top is one of the commands I use the most while working on linux servers. It’s terribly useful and configurable, got a clean interface and is simple to modify its output using shortcuts.

A lot of times I need to order top by memory percentage instead of cpu percentage. This can be easily achieved by pressing ‘M’ while looking at top.

As I’m really (terribly) lazy and I wanted to test my ‘expect’ discovery, I decided to create a simple script (can be also used as an alias I believe) to run top ordered by %mem directly from bash.

Please note that I know about existing python/perl script that creates mem orderer output from ps command, but that’s not what I wanted…

This is one of my firsts expect scripts, more will come as I really love it… here’s what came out

#!/bin/bash
# This script depends on expect tcl utility. 
# Please install it before running this code.

expect -c "
    spawn top       
    expect order                    
    send \"M\"                                      
    expect eof                                                      
"

2 years ago

June 23, 2009
Comments (View)
text

How to check if a string contains an other in #bash

Quick trick using #grep in #bash for checking if a string is contained in an other one

STRING="someUnknownString";
NAIL="know";

res=`echo $STRING | grep $NAIL`;
if [[ ${#res} > 0 ]];
then
	echo "Yes I found it.";
else
	echo "No way, there isn't."
fi

If you want you can make the whole thing case insensitive just using grep -i

STRING="someUnknownString";
NAIL="Unkn";

res=`echo $STRING | grep -i $NAIL`;
if [[ ${#res} > 0 ]];
then
	echo "Yes I found it.";
else
	echo "No way, there isn't."
fi

Lorenzo Sfarra on IdentiCa suggested me a quicker way to obtain the same result with less code. You can find this nicer way below…

STRING="someUnknownString";
NAIL="Unkn";
echo $STRING | grep -qi $NAIL && echo "Yes, I found it." || echo "no way, there isn't."

2 years ago

June 22, 2009
Comments (View)