31 August 2011

Bash script: Common tasks

Wrting a shell script some comman tasks

1. Getting running script name
Suppose script name is script.sh

#!/bin/bash
ME="$(basename $0)"
echo "My name: $ME"
ME=${ME%.*}
echo "My name without extension: $ME"


Output will be:
My name: script.sh
My name without extension: script


ME="$(basename $0)" Will return the scipt name
ME=${ME%.*} Will remove the last extension from the script

2. Getting PID of the running script
#!/bin/bash
echo "My PID: $$"

$$ gives the PID of the running shell script.

3. Getting command like options using getopts
#!/bin/bash
ME="$(basename $0)"
VERSION=1.0
usage()
{
echo "usage:$ME [options]"
echo "-h, --help print the help for this program"
}

args=`getopt -o hv -l help,version -- $@`;
errcode=$?;

# Check if any argument value is missing
if [ $errcode -ne 0 ]; then
usage;
exit 1;
fi;
set -- $args
argsarr=($args)

# Get the value of the arguments
for i; do
argval=$2
shift
case "$i" in
-h|'--help') usage; exit 0;;
-v|'--version') echo $VERSION; exit 0;;
--) ;;
esac
done


"getopts" provides the facility to take named arguments. Arguments -o will specify which small arguments will be allowed and -l provides the long listing arguments.

We can run above script as-
$ ./script.sh -h or $./script --help
will print the help

4. Getting disk space for a particular partition
#!/bin/bash
partition="/var"
availdisk="$(df -kP ${partition}| awk '{ print $4 }' | tail -1)"
echo "Available disk space: $availdisk"


5. Getting System architecture
#!/bin/bash
arch="$(uname -a)"
echo "System architecture : $arch"


6. Getting system load average
#!/bin/bash
loadavg="$(uptime | awk -F "load average:" '{ print $2 }' | cut -d, -f1)"
echo "Load average for last 1 Minute: $loadavg"
loadavg="$(uptime | awk -F "load average:" '{ print $2 }' | cut -d, -f2)"
echo "Load average for last 5 Minute: $loadavg"
loadavg="$(uptime | awk -F "load average:" '{ print $2 }' | cut -d, -f3)"
echo "Load average for last 10 Minute: $loadavg"


7. Getting current time in specific format
#!/bin/bash
ts="$(date +%F-%H.%M.%S.0000)"
echo $ts


8. Returning values other than integer from shell function
Shell allow only intger to be used in return statement of the function. To return value other than integer from function use echo command.

#! /bin/bash
concat()
{
echo "$1" "$2"
# return "$1" "$2" # not allowed
}
str="$(concat 'Hi' 'There')"
echo "$str"


9. Comparing two floating number
Only integer number can be compaired using lt gt. To comapre the floating
point number we can bc.

#! /bin/bash
compareNumber()
{
compare_result=`echo "$1 > $2" | bc 2>/dev/null`
if [ $compare_result ]; then
return 1
fi
return 0
}
if ! compare_result 10.1 20.5; then
echo "First one is less than second"
else
echo "First one is grater than second"
fi


10. Stripping out filename from full path
#!/bin/bash

getFileName()
{
local fullpath="$1"
local filename="${fullpath##*/}" # Strip longest match of */ from start
echo $filename
}
filename="$(getFileName $1)"

echo "File name from path : $filename"


11. Stripping directory path from full path
#!/bin/bash

getDirPath()
{
local fullpath="$1"
local filename="${fullpath##*/}" # Strip longest match of */ from start
local dir="${fullpath:0:${#fullpath} - ${#filename}}"
echo "$dir"
}

path="$(getDirPath $1)"
echo "Direcotry path: $path"

No comments:

Post a Comment