bash scripts

how to check RHEL4 ?

%cat /etc/redhat-release
Red Hat Enterprise Linux AS release 4 (Nahant Update 5)

%uname -a
Linux box-123 2.6.9-55.ELhugemem #1 SMP Fri Apr 20 17:20:11 EDT 2007 i686 athlon i386 GNU/Linux

Brace Expansion

$ cp /etc/httpd/conf/httpd.conf{,.bak} // Notice that there is no character between the opening brace and the first comma.
$ diff /etc/httpd/conf/httpd.conf{.bak,}

I needed to create directories to hold some batch files, like "batch1", "batch2" etc got boring once I got to "batch3".
mkdir batch{1,2,3,4,5,6,7,8}

$ echo {one,two,red,blue}fish
onefish twofish redfish bluefish

$ echo fish{one,two,red,blue}
fishone fishtwo fishred fishblue

$ echo fi{one,two,red,blue}sh
fionesh fitwosh firedsh fibluesh

Notice that there are no spaces inside the brackets or between the brackets and the adjoining strings. If you include spaces, it breaks things:

$ echo {one, two, red, blue }fish
{one, two, red, blue }fish

$ echo "{one,two,red,blue} fish"
{one,two,red,blue} fish

However, you can use spaces if they're enclosed in quotes outside the braces or within an item in the comma-separated list:

$ echo {"one ","two ","red ","blue "}fish
one fish two fish red fish blue fish

Command Substitution

today=$(date +%d-%b-%Y)

$ echo $today

$ rpm -ql $(rpm -qa | grep httpd)

The inner command, rpm -qa | grep httpd, lists all the packages that have httpd in the name. The outer command, rpm -ql, lists all the files in each package.

Now, those of you who have experience with the Bourne shell might point out that you could perform command substitution by surrounding a command with back quotes, also called back-ticks. Using Bourne-style command substitution, the date assignment from above becomes:

today2=`date +%d-%b-%Y`

command history

by pressing Ctrl-R. After doing this, your prompt changes to:


Start typing a few letters of the command you're looking for, and bash shows you the most recent command that contains the string you've typed so far.

for loop

$ for file in * ; do cp $file $file.bak; done

Another way to write loops is to press Enter after each line instead of inserting a semicolon. bash recognizes that you are creating a loop from the use of the for keyword, and it prompts you for the next line with a secondary prompt. It knows you are done when you enter the keyword done, signifying that your loop is complete:

$ for file in *
> do cp $file $file.bak
> done
for i in *.rpm; do if grep -q 'infolimit.xml' $i; then echo $i; rpm -qlp $i |   grep 'infolimit.xml' ; fi; done
find . -name "*.rpm" | xargs rpm -qlp $i |  grep 'infolimit.xml'

dealing with the annoying ^M when read file in bash script.



# assign file descriptor 3 to input file
exec 3< $infile

# read til the end of the file
until [ $done ]
    read  -e  -u 3 myline   # -d $'\r'  
    if [ $? != 0 ]; then

 #   if [ ${#myline} -eq 1 ]; then
 #       continue
 #   fi

    if [ "$myline" = $'\r' ] ; then

    echo "the line length is : ${#myline}"

    # process file data line-by-line
    # in here ...
    echo  "the line is :($myline)"

    echo "do you want to continue? (y/n)"
    read confirm

    if [ "$confirm" = "n" ]; then
    elif [ "$confirm" = "y" ]; then


echo "The End"

processing by awk and sed example

#the sed delete any space or tab before , awk printf default is right alignment, if you want left alignment, %-33s
cat  ~/xyz.txt   | sed 's/[ \t]*,/,/g' | awk  -F ',' '{printf "%33s,%s.layout\n", $1,$2}'

uniq and other useful commands


A forced umount "umount -f" usually doesn't resolve anything either. The quick fix is a lazy unmount:
umount -l /mnt/deadmount


uniq -d print duplicated lines

find /path/to/dir -type f | xargs grep -l REGEX



linux signal

linux pad


set up

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License