Commonly used tests on files in shell scripting

August 13, 2013

Here are some commonly used tests:

   -d FILE
          FILE exists and is a directory
   -e FILE
          FILE exists
   -f FILE
          FILE exists and is a regular file
   -h FILE
          FILE exists and is a symbolic link (same as -L)
   -r FILE
          FILE exists and is readable
   -s FILE
          FILE exists and has a size greater than zero
   -w FILE
          FILE exists and is writable
   -x FILE
          FILE exists and is executable
   -z STRING
          the length of STRING is zero
Advertisements

Emacs and GDB Cheatsheet

March 29, 2013

How to switch screens in Emacs ?
(Control + x) then press o

When you have to set a breakpoint, go to the window showing the code. Come to the line where you want to put the breakpoint. Press (Ctrl + x) then space.

There are three other commands that you will find useful: (1) undisplay followed by a number n will remove item n from the display list. (2) print followed by an expression will evaluate and print the expression once (without adding it to the display list). (3) step is similar to “next”, executing one statement. The difference is that the step command tries to step into the body of each function, and execute the lines of each function one at a time. Since heatwave has no functions, we’re using next. (Also, step would cause problems with heatwave, since the debugger would try to step into the input >> and output << functions.

How to log gdb output to a file ?
set logging file mylog.txt
set logging on

http://www.cs.berkeley.edu/~mavam/teaching/cs161-sp11/gdb-refcard.pdf

http://refcards.com/docs/gildeas/gnu-emacs/emacs-refcard-a4.pdf


Revising awk

January 15, 2013

Awk basics :

NR: Keeps a current count of the number of input lines.

• NF: Keeps a count of the number of words in an input line. The last field in the input line can be designated by $NF.

• FILENAME: Contains the name of the current input file.

• FS: Contains the field separator character. The default is “white space”, meaning space and tab characters. FS can be reassigned to another character to change the field separator.

• RS: Stores the current “record separator” character. Since, by default, an input line is the input record, the default record separator character is a “newline”.

• OFS: Stores the “output field separator”, which separates the fields when Awk prints them. The default is a “space” character.

• ORS: Stores the “output record separator”, which separates the output lines when Awk prints them. The default is a “newline” character.

• OFMT: Stores the format for numeric output. The default format is “%.6g”, which will be explained when “printf” is discussed

Awk examples :

  1.  Write a command to find the sum of bytes (size of file) of all files in a
    directory.
    ls -l | awk ‘BEGIN {sum=0} {sum = sum + $5} END {print sum}’

References :

  1. http://oreilly.com/catalog/unixnut3/chapter/ch11.html
  2. http://www.chemie.fu-berlin.de/chemnet/use/info/gawk/gawk_17.html

http://www.catonmat.net/blog/awk-one-liners-explained-part-one/


Substitute (Search and replace) command in the vi editor

June 5, 2012

1. How to replace an occurrence of “old_string” by “new_string” in a file

{code} :s/old_string/new_string/g{/code}

2. How to replace an occurrence of “old_string” by “new_string” in a file withinthe range of lines 1 to 10

:1,10s/old_string/new_string/g
3. How to replace an occurrence of “old_string” by “new_string” in a file withinthe range of lines 1 to the current line ?

The current line (where the cursor is located) can be specified as a single dot (.).

:1,.s/old_string/new_string/g

4. How to replace an occurrence of “old_string” by “new_string” in a file withinthe range of lines from the current line to the last line?

The last line can be specified as a dollar sign ($).

:.,$s/old_string/new_string/g

5. How do you search the whole file ?

:%s/old_string/new_string/g or
:1,$s/old_string/new_string/g
6. How can you execute a search and replace on the current line and the next five lines ?

The beginning or ending line for a range may be given as a positive or negative number of lines offset from the current line.

:.,+5s/old_string/new_string/g

7. How can you execute a search and replace from five lines above the current line through the current line ?

:-5,.s/old_string/new_string/g

8. How can you perform a search and replace from five lines above the current line through five lines below the current line ?

:-5,+5s/old_string/new_string/g

 


Useful Screen Commands

May 22, 2012

screen -list : display a list of all the screens
screen -r : reconnect to a screen
screen -x : reattach to a screen that is already attached
C-c – Create a new screen window
C-n – shift to the next screen
C-p – shift to the previous screen

C-a C-w – see the list of windows connected to this screen

http://www.catonmat.net/download/screen.cheat.sheet.pdf


Redirection and File Descriptors

April 25, 2012

Standard File Descriptors in Unix :

  • Stderr : 2
  • Stdin : 0
  • Stdout : 1

Redirection Examples :

Example 1 :

bash$ echo “Hello” > a.txt

is the same as

bash$ echo “Hello” 1> a.txt

Example 2 :

bash$ cat < a.txt means use the contents of the file when cat reads from stdin which is the same as doing
bash$ cat 0< a.txt

In all the cases the redirection occurs before the echo or the cat command is executed.

Redirections operations all happen from left to right and pipe has precedence over redirection.

 


Bit Bucket or Black Hole in Unix

April 25, 2012

/dev/null is the bit bucket or black hole in unix.

This is usually used to direct unwanted outputs of a process so that they are lost or discarded.

This does not mean that directories can be deleted by moving them to /dev/nul. The best way to delete them is using the rm command.

Usage :

bash$ ls

first.sh

bash$ rm g

rm: cannot remove `g’: No such file or directory

bash$ rm g 2>/dev/null

Here the error output stream gets redirected to /dev/null. Hence, the error message is not shown on the screen.

More information on : Implementing a null filesystem in Unix