Simple array usage in Bash

#!/bin/bash

array=(one two three four [5]=five)

echo "Array size: ${#array[*]}"

echo "Array items:"
for item in ${array[*]}
do
printf " %s\n" $item
done

echo "Array indexes:"
for index in ${!array[*]}
do
printf " %d\n" $index
done

echo "Array items and indexes:"
for index in ${!array[*]}
do
printf "%4d: %s\n" $index ${array[$index]}
done

Fifth basic front end with dialog/Xdialog - Building a Gauge

A gauge based on dialog can be used to indicate progress of your program. Building a gauge is slightly tricky. Look at the following example:

#!/bin/sh
DIALOG=${DIALOG=dialog}

COUNT=10
(
while test $COUNT != 110
do
echo $COUNT
echo "XXX"
echo "The new\n\message ($COUNT percent)"
echo "XXX"
COUNT=`expr $COUNT + 10`
sleep 1
done
) |
$DIALOG --title "My Gauge" --gauge "Hi, this is a gauge widget" 20 70 0

Here the dialog program gets its input from the code shown within the parentheses. This code emits the number to be used for gauge and the message to be shown. The message to be shown in the gauge box must be surrounded by echo "XXX". The screen-shot of a gauge is shown below.

Fourth basic front end with dialog/Xdialog - Radiolist and Checklist

Radiolists and checklists can be programmed just like menus. A simple radio list example is given below.
#! /bin/sh
DIALOG=${DIALOG=dialog}
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$
trap "rm -f $tempfile" 0 1 2 5 15

$DIALOG --backtitle "Select your favorite singer" \
--title "My favorite singer" --clear \
--radiolist "Hi, you can select your favorite singer here " 20 61 5 \
"Rafi" "Mohammed Rafi" off \
"Lata" "Lata Mangeshkar" ON \
"Hemant" "Hemant Kumar" off \
"Dey" "MannaDey" off \
"Kishore" "Kishore Kumar" off \
"Yesudas" "K. J. Yesudas" off 2> $tempfile

retval=$?

choice=`cat $tempfile`
case $retval in
0)
echo "'$choice' is your favorite singer";;
1)
echo "Cancel pressed.";;
255)
echo "ESC pressed.";;
esac

A screen shot is shown below.

Third basic front end with dialog/Xdialog - building menu

Try the following program both in console and X (after changing dialog to Xdialog as before):
#!/bin/sh
DIALOG=${DIALOG=dialog}
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$
trap "rm -f $tempfile" 0 1 2 5 15

$DIALOG --clear --title "My favorite HINDI singer" \
--menu "Hi, Choose your favorite HINDI singer:" 20 51 4 \
"Rafi" "Mohammed Rafi" \
"Mukesh" "Mukesh" \
"Kishore" "Kishore Kumar" \
"Saigal" "K L Saigal" \
"Lata" "Lata Mangeshkar" \
"Yesudas" "K J Yesudas" 2> $tempfile

retval=$?

choice=`cat $tempfile`

case $retval in
0)
echo "'$choice' is your favorite hindi singer";;
1)
echo "Cancel pressed.";;
255)
echo "ESC pressed.";;
esac

The results are as below

Second basic front end with dialog/Xdialog

The following script reads a string you input and prints it back.

#!/bin/sh
DIALOG=${DIALOG=dialog}
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$
trap "rm -f $tempfile" 0 1 2 5 15

$DIALOG --title "My input box" --clear \
--inputbox "Hi, this is a sample input box\n
Try entering your name below:" 16 51 2> $tempfile

retval=$?

case $retval in
0)
echo "Input string is `cat $tempfile`";;
1)
echo "Cancel pressed.";;
255)
if test -s $tempfile ; then
cat $tempfile
else
echo "ESC pressed."
fi
;;
esac

Try running the program under console and under X ( after changing dialog to Xdialog as above)

Basic front end with dialog/Xdialog

This script displays a simple YES/NO box.

#!/bin/bash
DIALOG=${DIALOG=dialog}

$DIALOG --title " My first dialog" --clear \
--yesno "Hello , this is my first dialog program" 10 30

case $? in
0)
echo "Yes chosen.";;
1)
echo "No chosen.";;
255)
echo "ESC pressed.";;
esac

A screen-shot of the above program is given below.

Backup reminder script

#!/bin/bash
DIALOG=Xdialog
BACKUP_SCRIPT=backup_script
BACKUP_TIME="2pm"
BACKUP_DAYS=1
SNOOZE_MINS=5


$DIALOG --display=:0.0 --wrap --title "Backup Reminder" \
--ok-label Continue \
--cancel-label Snooze \
--yesno "You need to do a backup now. Please attach \
your external backup drive and click 'Continue' to continue \
with the backup or 'Snooze' to postpone the backup for a few \
minutes or close the window to skip this scheduled backup \
and reschedule the backup for next time." 15 60 &> /dev/null

return_value=$?

# if the command succeeded (the user clicked continue)
# proceed with the backup
if [ $return_value = 0 ]; then
$BACKUP_SCRIPT
success=$?

# if the backup script succeeded then send mail to
# the user and schedule the next backup
if [ $success = 0 ]; then
echo "backup succeeded. scheduling next backup for"
echo $0 | at $BACKUP_TIME + $BACKUP_DAYS days
# the backup script failed so stop the backup loop
# and mail the user the failed message
else
echo "backup failed! stopping backup loop! please check your backup script and re-run this script!"
fi
# the backup has been skipped (user closed the window)
# so reschedule the backup for the next scheduled time
elif [ $return_value = 255 ]; then
echo "backup skipped. scheduling next backup for"
echo $0 | at $BACKUP_TIME + $BACKUP_DAYS days
# either the dialog command failed because X wasn't available
# or the user snoozed the backup, so reschedule the backup
else
echo $0 | at now + $SNOOZE_MINS minutes &> /dev/null
fi

Check low space

#!/bin/bash

PATHS="/export/home /home"
AWK=/usr/bin/awk
DU="/usr/bin/du -ks"
GREP=/usr/bin/grep
DF="/usr/bin/df -k"
TR=/usr/bin/tr
SED=/usr/bin/sed
CAT=/usr/bin/cat
MAILFILE=/tmp/mailviews$$
MAILER=/bin/mailx
mailto="all@company.com"
for path in $PATHS

do
DISK_AVAIL=`$DF $path | $GREP -v "Filesystem" | $AWK '{print $5}'|$SED 's/%//g'`
if [ $DISK_AVAIL -gt 90 ];then
echo "Please clean up your stuff\n\n" > $MAILFILE
$CAT $MAILFILE | $MAILER -s "Clean up stuff" $mailto
fi
done

Remove comments and blank lines


sed '/ *#/d; /^ *$/d' file

Bash sockets using /dev/tcp

If you dont`t have telnet client or netcat you can do this:

#!/bin/bash
exec 3<>/dev/tcp/linuxscripting.blogspot.com/80
echo -e "GET / HTTP/1.1\nHost: kinqpinz.info;\nConnection: close\n\n">&3
cat <&3

Remove empty directories


find . -type d -empty -print0 | xargs -0 rmdir

Magento installation script

#!/bin/bash

clear

stty erase '^?'

echo "To install Magento, you will need a blank database ready with a user assigned to it."

echo

echo -n "Do you have all of your database information? (y/n) "

read dbinfo

if [ "$dbinfo" == "y" ] ; then

echo

echo -n "Database Host (usually localhost): "
read dbhost


echo -n "Database Name: "
read dbname


echo -n "Database User: "
read dbuser

echo -n "Database Password: "

read dbpass

echo -n "Store URL: "
read url


echo -n "Admin Username: "
read adminuser


echo -n "Admin Password: "
read adminpass

echo -n "Admin First Name: "

read adminfname

echo -n "Admin Last Name: "
read adminlname


echo -n "Admin Email Address: "
read adminemail


echo -n "Include Sample Data? (y/n) "
read sample

if [ "$sample" = "y" ] ; then

echo
echo "Now installing Magento with sample data..."


echo
echo "Downloading packages..."
echo


wget http://www.magentocommerce.com/downloads/assets/1.2.0.1 /magento-1.2.0.1.tar.gz

wget http://www.magentocommerce.com/downloads/assets/1.2.0/magento-sample-data-1.2.0.tar.gz


echo
echo "Extracting data..."
echo


tar -zxvf magento-1.2.0.1.tar.gz
tar -zxvf magento-sample-data-1.2 .0 .tar.gz


echo
echo "Moving files..."

echo

mv magento-sample-data-1.2.0/media/* magento/media/

mv magento-sample-data-1.2.0 /magento_sample_data_for_1.2.0.sql magento/data.sql
mv magento/* magento/.htaccess .


echo
echo "Setting permissions..."
echo

chmod o+w var var/.htaccess app/etc
chmod -R o+w media

echo
echo "Importing sample products..."
echo


mysql -h $dbhost -u $dbuser -p$dbpass $dbname < data.sql


echo
echo "Initializing PEAR registry..."

echo

./pear mage-setup .


echo
echo "Downloading packages..."

echo

./pear install magento-core/Mage_All_Latest


echo
echo "Cleaning up files..."

echo

rm -rf downloader/pearlib/cache/* downloader/pearlib/download/*

rm -rf magento/ magento-sample-data-1.2.0 /
rm -rf magento-1.2.0.1.tar.gz magento-sample-data-1.2.0.tar.gz

rm -rf index.php.sample .htaccess.sample php.ini.sample LICENSE.txt STATUS.txt data.sql

echo

echo "Installing Magento..."
echo


php-cli -f install.php --
--license_agreement_accepted "yes"
--locale "en_US"

--timezone "America/Los_Angeles"
--default_currency "USD"
--db_host "$dbhost"

--db_name "$dbname"
--db_user "$dbuser"
--db_pass "$dbpass"

--url "$url"
--use_rewrites "yes"
--use_secure "no"

--secure_base_url ""
--use_secure_admin "no"
--admin_firstname "$adminfname"

--admin_lastname "$adminlname"
--admin_email "$adminemail"
--admin_username "$adminuser"

--admin_password "$adminpass"

echo

echo "Finished installing Magento"
echo


exit
else
echo "Now installing Magento without sample data..."


echo
echo "Downloading packages..."

echo

wget http://www.magentocommerce.com/downloads/assets/1.2.0.1 /magento-1.2.0.1.tar.gz


echo
echo "Extracting data..."

echo

tar -zxvf magento-1.2.0.1.tar.gz


echo
echo "Moving files..."

echo

mv magento/* magento/.htaccess .


echo
echo "Setting permissions..."

echo

chmod o+w var var/.htaccess app/etc

chmod -R o+w media

echo

echo "Initializing PEAR registry..."
echo


./pear mage-setup .

echo

echo "Downloading packages..."
echo


./pear install magento-core/Mage_All_Latest

echo

echo "Cleaning up files..."
echo


rm -rf downloader/pearlib/cache/* downloader/pearlib/download/*
rm -rf magento/ magento-1.2.0.1.tar.gz
rm -rf index.php.sample .htaccess.sample php.ini.sample LICENSE.txt STATUS.txt


echo
echo "Installing Magento..."

echo

php-cli -f install.php --

--license_agreement_accepted "yes"
--locale "en_US"
--timezone "America/Los_Angeles"

--default_currency "USD"
--db_host "$dbhost"
--db_name "$dbname"

--db_user "$dbuser"
--db_pass "$dbpass"
--url "$url"

--use_rewrites "yes"
--use_secure "no"
--secure_base_url ""

--use_secure_admin "no"
--admin_firstname "$adminfname"
--admin_lastname "$adminlname"

--admin_email "$adminemail"
--admin_username "$adminuser"
--admin_password "$adminpass"


echo
echo "Finished installing Magento"


exit
fi
else

echo
echo "Please setup a database first. Don't forget to assign a database user!"

exit

fi

Kill process by name


ps axco pid,command | grep | awk '{ print $1; }' | xargs kill -9

Locate running gnome-session and send a notification

#!/bin/bash
user=`whoami`
pids=`pgrep -u $user gnome-session`
title=$1
text=$2
timeout=$3

if [ -z "$title" ]; then
echo You need to give me a title >&2
exit 1
fi
if [ -z "$text" ]; then
text=$title
fi
if [ -z "$timeout" ]; then
timeout=60000
fi

for pid in $pids; do
# find DBUS session bus for this session
DBUS_SESSION_BUS_ADDRESS=`grep -z DBUS_SESSION_BUS_ADDRESS \
/proc/$pid/environ | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//'`
# use it
DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS \
notify-send -u low -t $timeout "$title" "$text"
done


Ssh key authentication

#!/bin/bash
# This must be allowed on server end
# in /etc/sshd_config (default):
# RSAAuthentication yes
# PubkeyAuthentication yes


user='username'
host='hostname.com'

ssh-keygen -t rsa

cat >> ~/.ssh/config <<>> ~/.ssh/authorized_keys"

# done
ssh hostname.com

Find files older than 30 days

find . -type f -mtime +30 -exec ls -l {} \;

Find files and sub-directories that are older than 30 days in the working directory and list them. By changing the value for -mtime, you can adjust the time.

Display most frequently used commands


history | awk '{print $2}' | awk 'BEGIN {FS="|"}{print $1}' | sort | uniq -c | sort -n | tail | sort -nr


Get an ordered list of subdirectory sizes

Lists the size of every file and subdirectory of the current directory. Useful to find where all that space goes.


du -sk ./* | sort -n | awk 'BEGIN{ pref[1]="K"; pref[2]="M"; pref[3]="G";} { total = total + $1; x = $1; y = 1; while( x > 1024 ) { x = (x + 1023)/1024; y++; } printf("%g%s\t%s\n",int(x*10)/10,pref[y],$2); } END { y = 1; while( total > 1024 ) { total = (total + 1023)/1024; y++; } printf("Total: %g%s\n",int(total*10)/10,pref[y]); }'

or


du -ksh ./* | sort -n

Resolve all conflicts in SVN

Use caution! Resolve all your conflicted files.

svn st|awk '/^C/{ print $2; }'|xargs svn resolved

Subversion Repository Move

# Subversion Management
# create a gzipped dumpfile of a repository
svnadmin dump path-to-old-repo | gzip > ~/dumpfile.gz

# import into another repository from a gzipped dumpfile
gunzip -c ~/dumpfile.gz | svnadmin load path-to-new-repo

# relocated any existing working copies.
# See also http://svnbook.red-bean.com/en/1.5/svn.ref.svn.c.switch.html
cd path-to-working-copy-from-old-repo
svn switch --relocate file:///path/to/old-repo file:///path/to/new-repo .

Password generator

This is a simpler password generator.

< /dev/urandom tr -dc A-Za-z0-9_ | head -c8; echo

Note that the 'tr' strips out everything except characters in the ranges (alphanumeric, mixed case and underscores). This is a nice approach as piping to head means the minimum number of bytes required to generate a password of appropriate length are taken from /dev/urandom vs other methods which take more than you should need but still have a chance of not having obtained enough random data to generate a password of the required length. You can change the parameter to head to get passwords of any length.

What is shell script

A shell script is a script written for the shell, or command line interpreter, of an operating system. It is often considered a simple domain-specific programming language. Typical operations performed by shell scripts include file manipulation, program execution, and printing text.