Shell script to format date in yyyy.mm.dd format
#!/bin/bash
year=2012
for (( c=1; c<=12; c++ ))
do
month=$(printf "%02d" $c)
for (( d=1; d<=15; d++ ))
do
day=$(printf "%02d" $d)
echo $year.$month.$day
done
done
Linux, Solaris, Scripting, Testing, QA, Database, Puzzles ...
#!/bin/bash
year=2012
for (( c=1; c<=12; c++ ))
do
month=$(printf "%02d" $c)
for (( d=1; d<=15; d++ ))
do
day=$(printf "%02d" $d)
echo $year.$month.$day
done
done
Posted by
Vishnu Agrawal
at
10:57 PM
0
comments
Labels: shell scripting
#!/bin/bash
FILENAME=test.csv
OLDSEP=$IFS
IFS=,
[ ! -f $FILENAME ] && { echo "File not found : $FILENAME"; exit 99; }
while read field1 field2 field3 field4
do
echo "$field1 : $field2 : $field3 : $field4"
done < $FILENAME
IFS=$OLDSEP
Posted by
Vishnu Agrawal
at
1:02 PM
3
comments
Labels: linux, shell scripting, solaris
#!/bin/bash
SERVERS=(SERVER1 SERVER2 SERVER3 SERVER4)
# set length of an array
arrayLength=${#SERVERS[*]}
# or
#arrayLength=${#SERVERS[@]}
#Traverse each entry
for (( i=0; i<${arrayLength}; i++ ));
do
echo ${SERVERS[$i]}
done
Posted by
Vishnu Agrawal
at
11:26 AM
0
comments
Labels: shell scripting
Following is the shell script solution for getting number of days between two dates:
#/bin/sh
date1=`date +%s -d $1`
date2=`date +%s -d $2`
diff=`expr $date2 - $date1`
diff_days=`expr $diff / \( 60 '*' 60 '*' 24 \)`
echo $diff_days
[vishnu@ shell]# ./daydiff.sh 2010-08-02 2010-09-15
44
[vishnu@ shell]#
Here is the perl solution for the same:
#!/bin/perl
use strict;
use warnings;
use Time::Local;
my $date1 = $ARGV[0];
my $date2 = $ARGV[1];
my @formatted_date1 = split(/-/, $date1);
my @formatted_date2 = split(/-/, $date2);
my $local_date1 = timelocal(0, 0, 0, $formatted_date1[2], $formatted_date1[1], $formatted_date1[0]);
my $local_date2 = timelocal(0, 0, 0, $formatted_date2[2], $formatted_date2[1], $formatted_date2[0]);
my $diffSeconds = $local_date2- $local_date1;
my $diffDays = $diffSeconds / (60 * 60 * 24);
print "Day diff :: $diffDays\n";
[vishnu@ shell]# ./daydiff.pl 2010-08-02 2010-09-15
Day diff :: 43
[vishnu@ shell]#
Here is modified version of above shell script, if you want to find out time difference in seconds
#!/bin/bash
d1=`date +%s -d "$1"`
d2=`date +%s -d "$2"`
((diff_sec=d2-d1))
echo "Diff Seconds : $diff_sec"
[vishnu@ shell]#./date.sh "2011-07-19 22:44:34" "2011-07-20 02:04:14"
Diff Seconds : 11980
[vishnu@ shell]#
Posted by
Vishnu Agrawal
at
10:31 AM
0
comments
Labels: perl script, shell scripting
Here is the automated shell script which can dump the memory and cpu usage of a process. Helpful in cases when you want to monitor a process for a long time.
#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "Usage ... [$0
exit
fi
pid=$1
# maxRunTime is used to control the script, so that it run only for specified number of seconds
maxRunTime=72000
# sleepTime is used to sleep for specified seconds, to take the memory dump again
sleepTime=120
logFile=${pid}_process.log
commandFile=${pid}_command.log
`ptree $pid >> $commandFile`;
`pargs $pid >> $commandFile`;
currentRunTime=0
while [ $currentRunTime -lt $maxRunTime ]
do
time=`date +%F::%X | tr -d '[\n]'`
line=`ps -eo pid,pcpu,vsz | grep $pid`;
echo "$time $line" >> $logFile
sleep $sleepTime
currentRunTime=`expr $currentRunTime + $sleepTime`
echo "Execution Time : ${currentRunTime} seconds [max ${maxRunTime}]"
done;
Posted by
Vishnu Agrawal
at
10:56 AM
0
comments
Labels: shell scripting
For using sed inside a shell script, The variable should be in "double quotes" and the command in 'single quotes'. Following is the example
#!/bin/bash
usage () {
echo "$0 -s < name> -r <replace name> -f <file name>"
exit
}
while getopts s:r:f: option
do
case "$option" in
s) search="$OPTARG";;
r) replace="$OPTARG";;
f) filename="$OPTARG";;
\?) usage
esac
done
sed -i 's/'"$search"'/'"$replace"'/g' "$filename"
Posted by
Vishnu Agrawal
at
10:39 PM
0
comments
Labels: sed, shell scripting
There are some variables which are set internally by the shell and which are available to the user:
$1 - $9 These variables are the positional parameters.$0 The name of the command currently being executed.$# The number of positional arguments given to this invocation of the shell.$? The exit status of the last command executed is given as a decimal string.
When a command completes successfully, it returns the exit status of 0
(zero), otherwise it returns a non-zero exit status.$$ The process number of this shell - useful for including in filenames, to
make them unique.$! The process id of the last command run in the background.$- The current options supplied to this invocation of the shell.$* A string containing all the arguments to the shell, starting at $1.$@@ Same as above, except when quoted.
Posted by
Vishnu Agrawal
at
2:05 PM
0
comments
Labels: linux, shell scripting