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:

Tuesday, January 4, 2011

How to change font and background color in shell terminal

Hera are some good links about changing fond and background colors in Linux shell terminal:

How to: Change / Setup bash custom prompt (PS1)






Note: the above image was copied from this URL.

Labels: , , ,

Wednesday, December 29, 2010

Linux script to delete a type of file under multiple directories

Environment
OEL 5.5x64 bit

Problem
There are many old ".txt" files to be deleted under a download directory, which contains many sub-directories, like
$ cd /download
$ ls -d
20090601 20090602 xxx
20090701 20090702 xxx
xxxx

Solution
$ for i in `ls -d 20090[678]*`; do rm $i/*.txt; done

Labels: ,