Using SSH-Agent With Screen

Carl’s excellent post describes how to set up Screen with ssh-agent so that when a new screen session is started, windows with names were set up and logged onto different servers automatically. It was not clear how to create the configuration file .screenrc_escP to me initially, but after a bit of trying the following steps worked:

  • open ~/.screenrc_escP in vi
  • type string “escape”
  • press tab key
  • press ctrl-v: holding the control key press the v key
  • press ctrl-p: holding the control key press the p key
  • type character p

In my terminal window character ctrl-p is in bold:

Screen Configuration File
.screenrc_escP

The escape sequence for the outer screen is ctrl-p instead of ctrl-a– to see the windows created in the outer screen type ctrl-p + ” — holding the control key press p, release the control key, press double quotes:

Screen Wrapper
Escape Sequence for Outer Screen is ctrl-p and Nested Screen is ctrl-a

I use ctrl-p at bash prompt to go to the previous command, so I chose ctrl-y as my escape character for the outer screen. After setting up config files and ssh-agent, starting a screen session created different windows and ssh connections opened to different servers.

Advertisements

Split a File Based on Line Pattern

csplit command can be used to split a file on line pattern. Below is a sample XML file and csplit command to create one file for each folder element:

<folder>
	folder1 - line 1
	folder1 - line 2
</folder>

<folder>
	folder2 - line 1
</folder>

<folder>
	folder3 - line 1
</folder>

<folder>
	folder4 - line 1
</folder>

csplit.exe --prefix=folder --suffix-format=%05d.txt --elide-empty-files folder.txt '/[:space:]*<\/folder>/+1' {*}
  • –prefix=folder: files created will have the filename prefix ‘folder’
  • –suffix-format=%05d.txt: files created will have suffix five digits (%05d) and ‘.txt’ (e.g. folder00000.txt)
  • –elide-empty-files: remove empty files (an empty file is created if there is an empty line after the last folder element)
  • –elide-empty-files: remove empty files (an empty file is created if there is an empty line after the last folder element)
  • ‘/[:space:]*/+1’: regular expression to match “zero or more spaces followed by ‘. +1 is used to output the matching line (i.e., also)
  • csplit - split file based on line pattern
    csplit - split file based on line pattern

Bash Keyboard Shortcuts – Programming Function Keys

I often backup a file by adding time stamp to the file using command like the one below:

mv catalina.out catalina.out.`date +%F-%I%M%p`

Date command “date +%F-%I%M%p” generates time stamp in the format YYYY-MM-DD-HHMM(AM/PM) (e.g. 2011-01-06-0729PM). Instead of typing the full date command, function keys F1, F2, etc. (not sure what other keys can be programmed) can be set to print it on command line when pressed. This post describes how to program F2 key so that when pressed:

  • deletes a character (backspace)
    • Tab completion adds a space after the filename. This is to delete the space character.
  • prints .`date +%F-%I%M%p` on the command line
Bash Keyboard Shortcut
Before Pressing F2
Bash Keyboard Shortcut
After Pressing F2

Steps:

  1. Open file “.inputrc” in user home directory (vi ~/.inputrc)
  2. Type the following characters (without + sign and spaces):  ” + ctrl-v + F2″
    • double quote
    • while holding the control key press v (similar to holding shift key to type letters in caps)
    • press F2
    • double quote
  3. Type colon
  4. Type the following characters (without + sign and spaces): ” + ctrl-v + ctrl-h + .`date +%F-%I%M%p` + ”
    • double quote
    • while holding the control key press v
    • while holding the control key press h — ctrl-h is for deleting a character (ctrl-h works at command prompt also)
    • .`date +%F-%I%M%p`
    • double quote

The lines should now appear as in the picture below:

Bash Keyboard Shortcut
Entry in .inputrc

Open a new bash shell and pressing F2 should now output .`date +%F-%I%M%p`

How to extract difference between two files?

Say that you have two files file1.txt and file2.txt as shown below:

file1.txt file2.txt
satish
devarapalli
java
linux
memory
slf4j
osgi
tomcat
apache
axis2
satish
apache
slf4j
osgi
java
linux
axis2
tomcat

Using commands sort and comm, we can identify the lines that are common in both files, lines unique in file1 and lines unique in file2. Below are the steps:

  1. Command comm works on sorted files, so the first step is to sort both file1.txt and file2.txt
    • sort file1.txt > file1_sorted.txt
    • sort file2.txt > file2_sorted.txt
  2. Find lines common to both files
    • comm -12 file1_sorted.txt file2_sorted.txt  | nl
      • Option “-12” means suppress unique lines in first (file1_sorted.txt) and second (file2_sorted.txt) files.
      • “| nl” — add line numbers to output
  3. Find lines unique in file2_sorted.txt
    • comm -13 file1_sorted.txt file2_sorted.txt  | nl
      • Option “-13” means suppress unique lines in first (file1_sorted.txt) file and lines common in both the files (3 – lines common in both files)
  4. Find lines unique in file1_sorted.txt
    • comm -23 file1_sorted.txt file2_sorted.txt  | nl
      • Option “-23” means suppress unique lines in second (file2_sorted.txt) file and lines common in both the files (3 – lines common in both files

Bash Tips and Tricks #1

Below are a few aliases that I use almost everyday at work:

  • Navigating directories
    alias ..='cd ..'
    alias ...='cd ../..'
    alias ....='cd ../../../..'
    alias .....='cd ../../../../..'
    

    With these aliases in place, use “..” to navigate one-level up, “…” to navigate two-levels up, etc.

  • Navigating to project related directories
    alias cdtomcat='cd "/cygdrive/c/appservers/tomcat"'
    alias cdlogs='cd "/cygdrive/c/appservers/tomcat/logs"'
    alias cdws='cd "/cygdrive/c/satish/eclipse/workspace"'
    

    To navigate to Tomcat logs directory, type “cdlogs” at the command prompt; To navigate to project workspace, type “cdws”. You get the idea, right?

  • Converting cygwin directory in to windows format
    alias cpwd='echo $PWD | sed -e "s/\/cygdrive\/c/c:/g" | sed -e "s/\//\\\/g"'
    

    Look at the screenshot below to get an idea

  • Open windows explorer
    alias winexp='explorer.exe `cpwd`'
    

    typing “winexp” at the command prompt will open windows explorer in the current working directory — this alias uses cpwd alias set up above

  • The cd command in cygwin takes directory that is in cygwin path format. For example, to navigate to C:\Satish\Software, the command is “cd /cygdrive/c/Satish/Software”. Below is a tiny function that changes to a directory given path in windows format (e.g. c:\Satish\Software)
    function wincd(){
    	dir=`echo "$1" | sed -e 's/\\\\/\\//g'`
    	cd $dir
    }
    

    Command wincd “C:\Satish\Software” changes the working directory to /cygdrive/c/Satish/Software — note that double quotes around the path are required. See picture below:

Find Jars

Below is a tiny bash function to find jar file(s) containing a particular class:

function findjars(){
	for i in `find . -type f -iname "*.jar"`; do
		count=`jar tvf $i | grep -i $1 | wc -l`;
		if [ $count -ge 1 ]; then
			echo $i;
		fi;
	done;
}

I have added this function in .bashrc file in home directory and whenever I need to fnd a jar containing a particular class, I just type findjars classname (e.g. findjars BasicDataSource)