Ali’s Notes

A few cool bash tricks

Posted in shell tricks by aghoras on October 13, 2010

Today, I was trying to do some fancy stuff using a bash script and ended up learning a couple things in the process.

#!/bin/bash

changed_files_repo="changed_files.txt"
# apply the patch with
#  patch -u --ignore-whitespace --fuzz 300 --dry-run -i GDS_10-1.patch
patch_name="GDS_10-1.patch"

#get rid of the patch file if it exists
rm -f $patch_name
rm review_changes.sh

#Get a list of files that have changed
echo "Getting differences..."
#cleartool find das -type f -cview -version "created_since(4-Aug-2010)" -print > $changed_files_repo

echo "Converting newline chars..."
dos2unix $changed_files_repo

echo "Getting old versions..."

for i in `cat $changed_files_repo`; do
 regex='(.*)@@(.*)\\([0-9]+)$'
 if [[ $i =~ $regex ]]; then
 base=${BASH_REMATCH[1]}
 cc_dir=${BASH_REMATCH[2]}
 ver=${BASH_REMATCH[3]}
 #calculate the previous version
 prev_ver=$(($ver-1))

 #get the old version
 cmd="cleartool get -to \"$base.x\" \"$base@@$cc_dir\\$prev_ver\""
 #remove the target file otherwise get will fail
 rm -f "$base.x"
 echo $cmd
 eval $cmd

 #create the patch
 #make the patch with 200 lines of context using unified format and ignore blanks
 diff -U 200 -B  --ignore-space-change --ignore-all-space --ignore-blank-lines "$base.x" $base >> $patch_name    

 # create review file
 echo "cleartool diff -g -pred \"$base\"" >> review_changes.sh
 fi
done

 

This script uses several cool features of Bash. First, it uses Bash’s built in regular expression parser. It also shows how to use grouping for back referencing in Bash. The syntax is similar to PERL but it does not have all the built-in shortcuts. That’s why I had to use [0-9] instead of \d. Moreover, for some strange reason, the regular expression comparison must be done via an intermediate variable ($regex in this example). When I tried to put the regex in the “if” statement, the compare would fail.

The second point demonstrated by the script is the conversion of text to numbers and simple math operations on that number.

Lastly, the script shows how to create a patch file using diff.

Easiest Way of doing password-less ssh

Posted in shell tricks by aghoras on August 4, 2010

Create keys on the client if you already don’t have one:

client$ ssh-keygen -t rsa

Do not specify a keyword and hit enter twice.

Now, import the public key into the server

client$ ssh-copy-id -i ~/.ssh/id_rsa.pub server_user_name@server

Log in to the server and all shall be well with the world.


	

Cloning a Hard Drive

Posted in Linux, shell tricks by aghoras on January 13, 2010

Let’s say we want to clone an XP hard drive to a file on a linux machine that we can later restore.

  1. Boot the windows box with Knopix (or other live CD)
  2. Make sure the network is setup correctly
  3. On the linux machine that needs to store the image type: “nc -l 9000 | dd of=backup.gz
  4. On the source machine type “dd if=/dev/hda | gzip | nc -q 5 192.168.1.102 9000
  5. Wait

You may also be able to backup a given partition by using the partition name (hda1) instead of the whole device (hda)

Combining Lines with awk

Posted in shell tricks by aghoras on December 29, 2009

To combine every 4 lines in a file with a space in between:

awk ‘ORS=NR%4?” “:RS’ foo.txt

using rsync

Posted in shell tricks by aghoras on February 3, 2009

To sync all the files that match a pattern recursively:

rsync -rcv –include “*.java” –include “*/” –exclude “*” pdu@10.34.179.196:~/workspace/gds_source ~/tmp

Bash Prompt

Posted in shell tricks by aghoras on January 29, 2009

My Bash prompt:
export PS1=’\[\e[0;32m\][\u@\h] \[\e[1;37m\]\W\[\e[1;32m\]\$ \[\e[0;37m\] ‘

Screen Notes

Posted in shell tricks by aghoras on January 29, 2009

Screen Notes:

To make the window infor displayed on top of an xterm window:

hardstatus lastline "%w"
To make the line show up at the bottom of the window:

hardstatus alwayslastline "%w"

you can put these  in ~/.screenrc

Bash Notes

Posted in Linux, Programming, shell tricks by aghoras on May 22, 2007

Loops

for i in *.pdf ;
        do echo $i
done

or

for (( i=0; i< 4; i++)); do echo $i;done

Bash functions:

function PrintDo(){
        #print the first parameter
        echo $1
}
Follow

Get every new post delivered to your Inbox.