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 

 


Save as in vim

April 25, 2012

Suppose that you are editing hello.txt.

1):w world.txt will write hello.txt’s content to the file world.txt while keeping hello.txt as the opened buffer in vim.

2) :sav world.txt will first write hello.txt’s content to the file world.txt, then close buffer hello.txt, finally open world.txt as the current buffer.


CPUs on a linux box

April 19, 2012

1) Command to display the number of processors on the linux box

cat /proc/cpuinfo | grep processor | wc -l

2) taskset is used to set or retrieve the CPU affinity of a running process given its PID or to launch a new COMMAND with a given CPU affinity.

Usage :

tasset -c 9-11 <process name>

 


Export

April 19, 2012

In the previous post, we have seen that parents can’t directly access variables initialized by children.

Children inherit from parents only environment variables.

cat display.sh
#!/bin/sh
echo x=$x
echo X=$X
bash-3.00$ x=5
bash-3.00$ export X=5
bash-3.00$ echo x=$x X=$X
x=5 X=5
bash-3.00$ ./display.sh
x=
X=5

References :

1) http://boris.lk.net/unix/


External Commands to the Unix Shell

April 19, 2012

bash-3.00$ cat setx.sh
#!/bin/sh
X=5
export X
echo $X
bash-3.00$ vi setx.sh
bash-3.00$ cat setx.sh
#!/bin/sh
X=5
echo $X
y=6
echo $y
bash-3.00$ ./setx.sh
5
6
bash-3.00$ echo $X

bash-3.00$ echo $y

The way shell executes this is it forks and calls the exec function because this is an external command.

Hence, the parent process(shell) does not inherit the variables defined in the child process.

The source command is used to load the variables into the parent(current) shell environment. As seen earlier, executing that process wont make the variables visible in the current shell environment.

Example :

bash-3.00$ source setx.sh
5
6
bash-3.00$ echo $y
6
bash-3.00$ echo $X
5

bash-3.00$ unset X
bash-3.00$ echo $X

bash-3.00$ unset y
bash-3.00$ echo $y

Alternative Solution :

bash-3.00$ . setx.sh
5
6
bash-3.00$ echo $y
6


Alias

April 19, 2012

Alias is a way to create new commands.

Examples :

1) alias a=’ls -lrt’

a

2) alias ls=’ls -lrt’

ls shows the output of ls -lrt

3) unalias ls

ls now shows its normal output

4) If you want to use the original command :

\ls

5) List of all aliases

alias or alias -p

6)


Unix Commands

April 13, 2012

1) Which Shell are you using in Unix

echo $SHELL

2) Search the man pages whether something exists or not

man -k “topic”

3) Check the manual entry for topic

man “topic”

 

Reference : http://www.ibm.com/developerworks/aix/library/au-speakingunix2.html


Learning from Facebook

April 6, 2012

A recent article on the Facebook release engineering team offers a lot to learn for young engineers. At times, I have felt that many developers are blinded about the processes followed by IT and operations teams after development and testing phase is over. This article helps in overcoming more of those blind spots and goes towards the DevOps movement.

Learnings to be taken :

1) Facebook uses PHP for most of its development as it reduces the development and ramping up time. This allows organic agility to the company to move fast. However, scripting languages tend to be less efficient with CPU cycles usage.  To bridge this gap and to take the advantages of compiled languages, facebook has an automated code converter from PHP to C++ which they call HipHop. Read More about HipHop.

2) Facebook’s entire code can be compiled to a binary executable, 1.5 GB in size. Hence, during a release this entire binary is pushed to all the facebook servers.

3) Testing and Release : Facebook employs automated tests and regression to find bugs. The release takes place in two phases. In the first phase the code is released to some of the servers and exposed to a fraction of the user base.

Excerpt from the article :

” Facebook is designed to be stateless and distributed, in the sense that the user’s session isn’t tied to any particular server. Any given page request can be handled by any of the servers in Facebook’s infrastructure.

That approach offers a lot of resilience. When Facebook performs an update, it doesn’t have to worry about serializing and migrating the state of user sessions. ”

Hence, a deployment doesnt require any downtime of the prod servers.

4) Feedback Analytics : Facebook uses different metrics and compares historical data to make sure everything is working fine after a release. What was interesting to learn was that they also use twitter feeds to judge positive and negative sentiments of the users after a release.

Overall an interesting article on software engineering and provides a peek into the working of facebook.


Famous Quotes – April, 2012

April 6, 2012

1) Education is the best provision for old age. -Aristotle

2) Who is not satisfied with himself will grow; who is not sure of his own correctness will learn many things. -Chinese Proverb

3) The fool speaks, the wise man listens. -Ethiopian Proverb

4) The drowning man is not troubled by rain. -Persian Proverb

5) The beginning of wisdom is to call things by their right names. -Chinese Proverb