UNIX - shell-scripting

 

A cheat sheet with some random examples – just one among of those thousands already existing on net...USP - yeah this one is mine!

1.      Execute all the scripts in present working directory, using ‘for’ loop:


for FileName in `ls -1`;
do
    $FileName >>$FileName.logfile 2>&1
done

2.      Same thing…using while loop:

ls -1 | while read FileName
do
    $FileName >> $FileName.logfile 2>&1
done

A rather practical use of this loop ( to update all the packages in Linux - one by one):


while read eachPackageName;
do
    yum groupinstall "`echo $eachPackageName `" -y > $LogFile. $eachPackageName
done < yum grouplist


3.      Same thing – once again, using some more complex things combined with while!

ls -1 >ListOfFiles
IFS=’ ‘
while read FileName
do
    $FileName>> $FileName.logfile 2>&1
done <ListOfFiles

Executing SQL’s from shell-script – the examples shown below use SQLLite
4.      Run the universal, select all from a shell-script. Output will be shown on terminal

sqlite3 TestDataBase<<!
    select * from TestTable;
!
5.      Select-all again, data would now go to a file…

sqlite3 TestDataBase > Output.txt <<!
    select * from TestTable;
!
cat Output.txt
6.       Some processing – select * shows columns separated by pipes – ‘|’ – use these as delimiters, and yes show 2nd column onwards…

sqlite3 TestDataBase   <<! | cut -d '|' -f 2-
    select * from TestTable;
!
cat Output.txt

7.       SQLLite -  sometimes we may like to see column headings and proper columns – example below illustrates that all the commands which could be issued from inside of SQLite prompt, could be issue from shell-script also.

sqlite3 TestDataBase   <<END_SQL
.mode column
.header on
    select DATA AS DATA from TestTable;
END_SQL

8.       Or,  show the script over multiple lines, and with proper indentation. But rememnber, ! or END_SQL – whatever you choose, should be in / starting from the first-column to indicate the end of input

sqlite3 TestDataBase   <<END_SQL | cut -d '|' -f 2-
.mode column
.header on
    select *
    from TestTable;
END_SQL

9.       A small trick…sometimes you would like to get rid of column header and blank lines.

sqlite3 TestDataBase   <<END_SQL | sed -e "/^$/d"  -e "/MyOwnData/d"
.mode column
.header on
    select DATA AS MyOwnData
    from TestTable;
END_SQL

10. The values retrieved from the column could be assigned to some variable also!

SomeVariable=`c TestDataBase   <<END_SQL | sed -e "s/-//g" | sed -e "/^$/d"  -e "/MyOwnData/d"
.mode column
.header on
select DATA AS MyOwnData
    from TestTable
where SoemColumn=$MyKeyValue;
END_SQL
`
echo $SomeVariable

If the $MyKeyValue is not a unique key, then $SomeVariable would contain a list – which could further be used in a for loop e.g. for i in `echo $SomeVariable`; do echo $SomeVariable; done

11.  Do everything + store output in a log!

FileFound=`sqlite3 $DBNAME <<! | sed -e "/^$/d"  -e "/DataFound/d"   | tee $TMPLOG 2>&1
   select "1" DataFound
     from  SomeStatusTable
    where KeyColumn = "$MY_KEY";
!`
Notice that now KeyCoulmn contains string values – char / varchar etc,so MY_KEY   – a variable which receives value from somewhere above in the script, hence a $ before this variable – is surrounded by double-quotes

12. Combine it with “tee” – print everything on console as well as to some file…e.g. reroute (concatenate) the output to OPC-Log file, and at the same time, use $LOG file as well   (following example has been picked up from ISQL - Informix SQL )

isql $DBNAME <<! | tee -a $OPC >> $LOG 2>&1
             dbinfo('sqlca.sqlerrd1') SQLERRD1,
      dbinfo('sqlca.sqlerrd2') SQLERRD2
      from systables where tabid = 1 ;
!


13. Send a job completion / failure report through mail

echo "From: $SenderID \nTo: $ReceiverID \nSubject: Some job failed\n\Mime-Version: 1.0\nContent-Type: text/plain\n" > /tmp/fileForSendingMail
cat $FailureReportFile >> /tmp/fileForSendingMail
/usr/lib/sendmail -t -oi < /tmp/fileForSendingMail

14. Send a job completion / failure report through mail - II

cat << EOMAIL | ${SendMailProgramName} > /dev/null 2>&1 
to:${eMail_List} 
from:Whatever@MailID.Com
subject:${SendingServerName} -  job ${JOB} failed
EOMAIL




15. A simple one this time -  probably you could do this with some grep option as well! To get the name of all the files which contain word "AnotherLazyBicyclist":

grep -ie "anotherlazybicyclist" * | cut -d : -f -1 | uniq

16.  Sleep...how to do that for less than a second...or for fractions:


'man sleep' says  sleep Seconds in its syntax...so what to do if you want a 'sleep' for less than a second...or say, for 3.4567 seconds?  Perl comes here for your rescue...you could add this line in your shell-script:

perl -e 'select(undef,undef,undef, 0.5)'...or use a variable YourWaitingPeriod - note
the multitudes of single quotes here...

perl -e 'select(undef,undef,undef,'$YourWaitingPeriod)'


17.  How to increment a variable - i.e X=X+1 - this is how you can do it in shell-script:

       MyNumber=`expr $ MyNumber + 1`
...note the single, back-quote and usge of expr in the example above.

No comments:

Post a Comment