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.

 

Advertisements

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)