Wednesday, June 22, 2011

Linux shell scripts tips

1 Adding new lines in echo command in a shell script
echo "first line \n second line"
The above command will not generate two lines of output, the output is like:
first line \n second line

Following command will do the trick:
echo -e "first line \n second line"

The output:
first line
second line

Note: http://techteam.wordpress.com/2008/09/25/n-not-creating-new-line-in-bash-script/

2 Dynamically creating log file name by using latest date
hostname >> `hostname`.$(date '+%Y''%m''%d-%H%M').log
or
hostname >> `hostname`.`date +"%Y_%m_%d_%H_%M"`.log
Note: http://www.unix.com/unix-advanced-expert-users/151905-command-run-across-servers.html

3 Executing commands/script in multiple servers from a shell script:

LIST="serv1 serv2 serv3 serv4 serv5 serv6 serv7 serv8 serv9 serv0"
echo -e "$LIST \n which server do you want to scan ?"
read ans
for i in $ans
do
ssh $i exec /bin/bash < /home/oracle/scripts/grep_hostname.sh
done

Labels: