Tuesday, May 26, 2009

SHELL SCRIPTING-II:


1. FILE DESCRIPTORS:

File descriptors are called file handles .
The three files opened for each command along with their corresponding file descriptors are
lStandard Input (STDIN), 0
lStandard Output (STDOUT), 1
lStandard Error (STDERR), 2

Ex:
command > file 2>&1
list > file 2>&1
Here STDOUT (file description 1) and STDERR (file descriptor 2) are redirected into the specified file.

Nohup process 2>&1& , 2=>STDERR


Command >file 2>&1
2> Output Error Msgs
1> Standard ERR

command 1> file1 2> file2
Here the STDOUT of the specified command is redirected to file1, and the STDERR (error messages) to file2.


NOTE:
n>&m
Here n and m are file descriptors (integers). If you let n=2 and m=1, you see that STDERR is redirected to
STDOUT. By redirecting STDOUT to a file, you also redirect STDERR



2. Shell Version of Cat:
#!/bin/sh
if [ $# -ge 1 ] ; then
for FILE in $@
do
exec 5<&0 < "$i"
while read LINE ; do echo $LINE ; done
exec 0<&5 5<&-
done
fi


3. Listing By Last Modifed :

start with ls -1 command again, but this time I'll use the -t (sort by last modified time) option
instead:
$ ls -1t /var/spool/mail


4. Shell function Returns Operating System..

getOSName() {
case ´uname -s´ in
*BSD)
echo bsd ;;
SunOS)
case ´uname -r´ in #version
5.*) echo solaris ;;
*) echo sunos ;;
esac
;;
Linux)
echo linux ;;
HP-UX)
echo hpux ;;
AIX)
echo aix ;;
*)
echo unknown ;;
esac
}

5. Copy Side Effects::
(Use Tar. )

Major prolem: can copy only Regular Files…… (files like file@ nt copied)
There are two other minor problems with using cp:
l Some versions of the cp command do not copy a file's owner and group. With these versions of cp,
the copied file has a different owner and group than the original.
l Some versions of cp do not copy a file's permissions correctly. With such a version of cp, the copied file might have different permissions than the original.


Using tar
By using tar you can move directories using the following procedure:
1. Make a tar file of the source directory.
2. Change to the destination directory.
3. Extract the source directory in the destination directory.


6. More n More –Magic of bin/sh:

When you ask a shell to execute the command $ date, the shell uses the system call exec to
ask the UNIX kernel to execute the command you requested.

The shell passes the name of the command that should be executed to the exec system call.
This system call reads the first two characters in a file to determine how to execute the command.


In the case of shell scripts, the first two characters are #!, indicating that the script needs to be interpreted by another program instead of executed directly.

The rest of the line is treated as the name of the interpreter to use.
Usually the interpreter is /bin/sh, but you can also specify options to the shell on this line.

No comments:

Post a Comment