Monday, 8 October 2012

Beginner's Introduction to Shell Script: PART 3

Beginner's Introduction to Shell Script: PART 1
Beginner's Introduction to Shell Script: PART 2

Beginner's Introduction to Shell Script: PART 3

3.1. System and Administrative Commands
Mastering the commands on your Linux machine is an indispensable prelude to writing effective shell scripts.

1.Users and Groups
Show all logged on users. This is the approximate equivalent of who -q.

sankar@new-host ~]$ users

root sankar sankar xavier

[sankar@new-host ~]$ who







root     pts/1        2012-10-08 09:54 (:1.0)
xavier   pts/2        2012-10-08 09:54 (:2.0)
sankar   :0           2012-10-08 09:55
sankar   pts/5        2012-10-08 11:28 (:0)

[sankar@new-host ~]$



2.[sankar@new-host ~]$ type -a echo
   echo is a shell builtin
   echo is /bin/echo


3. let
The let command carries out arithmetic operations on variables.
[sankar@new-host ~]$

   #!/bin/bash

   echo
   let a=11            # Same as 'a=11'
   let a=a+5           # Equivalent to  let "a = a + 5"
                       # (Double quotes and spaces make it more readable.)
   echo "11 + 5 = $a"  # 16

4. set

The set command changes the value of internal script variables/options. 



[sankar@new-host shell script]$ set
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="3" [1]="2" [2]="25" [3]="1" [4]="release" [5]="i686-redhat-linux-gnu")
BASH_VERSION='3.2.25(1)-release'
CATALINA_HOME=/usr/share/tomcat
CLASSPATH=.:/usr/java/default/jre/lib:/usr/java/default/lib:/usr/java/default/lib/tools.jar
COLORS=/etc/DIR_COLORS.xterm
COLORTERM=.............


5. unset

The unset command deletes a shell variable, effectively setting it to null.










bash$ unset PATH

bash$ echo $PATH

6. type 

type cmd identifies "cmd." Unlike whichtype is a Bash builtin. The useful -a option to type identifies keywords andbuiltins, and also locates system commands with identical names.


bash$ type -a '['
[ is a shell builtin
[ is /usr/bin/[

bash$ type type
type is a shell builtin


3.2. Job Control Commands

1. times

Gives statistics on the system time elapsed when executing commands, in the following form:


[sankar@new-host ~]$ times
0m0.086s 0m0.042s
0m0.535s 0m0.419s

2. killall
The killall command kills a running process by name, rather than by process ID. If there are multiple instances of a particular command running, then doing akillall on that command will terminate them all.

Note: kill -l
 lists all the signals (as does the file /usr/include/asm/signal.h). A kill -9 is a sure kill, which will usually terminate a process that stubbornly refuses to die with a plain kill. Sometimes, a kill -15 works. A zombie process, that is, a child process that has terminated, but that the parent process has not (yet) killed,



Table. Job identifiers
NotationMeaning
%NJob number [N]
%SInvocation (command-line) of job begins with string S
%?SInvocation (command-line) of job contains within it string S
%%"current" job (last job stopped in foreground or started in background)
%+"current" job (last job stopped in foreground or started in background)
%-Last job
$!Last background process

3. cat

cat, an acronym for concatenate, lists a file to stdout.










[sankar@new-host ~]$ cat aaa

1. om sai ram
2. om sai ram
3. om sai ram
4. om sai ram

4. tac, is the inverse of cat, listing a file backwards from its end.


[sankar@new-host ~]$ tac aaa
4. om sai ram
3. om sai ram
2. om sai ram
1. om sai ram

5. rev

reverses each line of a file, and outputs to stdout


[sankar@new-host ~]$ rev aaa

mar ias mo .1
mar ias mo .2
mar ias mo .3
mar ias mo .4

6. chattr

Change file attributes.
One particularly interesting chattr option is i. A chattr +i filename marks the file as immutable. The file cannot be modified, linked to, or deleted, not even by root.


[root@new-host sankar]# chattr +i aaa

[sankar@new-host ~]$ chmod +x aaa
chmod: changing permissions of `aaa': Operation not permitted

[root@new-host sankar]# chattr -i aaa
[sankar@new-host ~]$ chmod +x aaa

7. xargs
A filter for feeding arguments to a command, and also a tool for assembling the commands themselves.
         This means that input piped to xargs may have linefeeds and other whitespace characters stripped out.

bash$ ls -l
total 0
 -rw-rw-r--    1 bozo  bozo         0 Jan 29 23:58 file1
 -rw-rw-r--    1 bozo  bozo         0 Jan 29 23:58 file2

bash$ ls -l | xargs
total 0 -rw-rw-r-- 1 bozo bozo 0 Jan 29 23:58 file1 -rw-rw-r-- 1 bozo bozo 0 Jan...


An interesting xargs option is -n NN, which limits to NN the number of arguments passed.

$ls | xargs -n 8 echo lists the files in the current directory in 8 columns.




8. expr

All-purpose expression evaluator:

expr 3 + 5
returns 8

expr 5 % 3
returns 2

expr 1 / 0
returns the error message, expr: division by zero
Illegal arithmetic operations not allowed.

expr 5 \* 3
returns 15

3.3. Time / Date Commands



1. IST Time(general)

[sankar@new-host ~]$ date
Wed Oct 10 15:22:31 IST 2012

2. UTC Time:

[sankar@new-host ~]$ date -u
Wed Oct 10 09:52:34 UTC 2012

3. zdump

Time zone dump: echoes the time in a specified time zone.

bash$ zdump EST
EST  Tue Sep 18 22:09:22 2001 EST


4. hwclockclock:














The hwclock command accesses or adjusts the machine's hardware clock. Some options require root privileges. The /etc/rc.d/rc.sysinit startup file uses hwclock to set the system time from the hardware clock at bootup.



3.4. Text Processing Commands

Commands affecting text and text files










1. sort
File sort utility, often used as a filter in a pipe. This command sorts a text stream or file forwards or backwards, or according to various keys or character positions. Using the -m option, it merges presorted input files.

2. tsort
Topological sort, reading in pairs of whitespace-separated strings and sorting according to input patterns.
         The results of a tsort will usually differ markedly from those of the standard sort command,











3. uniq
This filter removes duplicate lines from a sorted file.



$cat list-1 list-2 list-3 | sort | uniq > final.list
# Concatenates the list files,
# sorts them,
# removes duplicate lines,
# and finally writes the result to an output file.

The useful -c option prefixes each line of the input file with its number of occurrences.


bash$ cat testfile
This line occurs only once.
 This line occurs twice.
 This line occurs twice.
 This line occurs three times.
 This line occurs three times.
 This line occurs three times.


bash$ uniq -c testfile
      1 This line occurs only once.
       2 This line occurs twice.
       3 This line occurs three times.


bash$ sort testfile | uniq -c | sort -nr
      3 This line occurs three times.
       2 This line occurs twice.
       1 This line occurs only once.








4. cut
A tool for extracting fields from files.
         Particularly important are the -d (delimiter) and -f (field specifier) options.



[sankar@new-host ~]$ cat /etc/mtab
/dev/hdb3 / ext3 rw 0 0
proc /proc proc rw 0 0
sysfs /sys sysfs rw 0 0
devpts /dev/pts devpts rw,gid=5,mode=620 0 0
/dev/hdb5 /home ext3 rw 0 0
/dev/hdb2 /var ext3 rw 0 0
/dev/hdb1 /boot ext3 rw 0 0
tmpfs /dev/shm tmpfs rw 0 0
/dev/hdd1 /SOFTWARE fuseblk rw,allow_other,blksize=4096 0 0
none /proc/sys/fs/binfmt_misc binfmt_misc rw 0 0
sunrpc /var/lib/nfs/rpc_pipefs rpc_pipefs rw 0 0


[sankar@new-host ~]$ cut -d ' ' -f1,2 /etc/mtab /dev/hdb3 / proc /proc sysfs /sys devpts /dev/pts /dev/hdb5 /home /dev/hdb2 /var /dev/hdb1 /boot tmpfs /dev/shm /dev/hdd1 /SOFTWARE none /proc/sys/fs/binfmt_misc sunrpc /var/lib/nfs/rpc_pipefs



[sankar@new-host ~]$ uname -a
Linux new-host 2.6.18-194.3.1.el5 #1 SMP Thu May 13 13:09:10 EDT 2010 i686 i686 i386 GNU/Linux

[sankar@new-host ~]$ uname -a | cut -d" " -f1,3,11,12
Linux 2.6.18-194.3.1.el5 2010 i686









5. join
Consider this a special-purpose cousin of paste.


[sankar@new-host shell script]$ cat file1.data
100 Shoes
200 Laces
300 Socks
[sankar@new-host shell script]$ cat file2.data
100 $40.00
200 $1.00
300 $2.00
[sankar@new-host shell script]$ join file1.data file2.data
100 Shoes $40.00
200 Laces $1.00
300 Socks $2.00


6. head
lists the beginning of a file to stdout.


[sankar@new-host shell script]$ head -n 1 /etc/passwd
root:x:0:0:root:/root:/bin/bash



7. tail
lists the (tail) end of a file to stdout.


[sankar@new-host shell script]$ tail -n 1 /etc/passwd
sankar:x:522:522::/home/sankar:/bin/bash

8. grep
A multi-purpose file search tool that uses Regular Expressions.

         The -i option causes a case-insensitive search.
         The -w option matches only whole words.
         The -l option lists only the files in which matches were found, but not the matching lines.
         The -r (recursive) option searches files in the current working directory and all subdirectories below it.
         The -n option lists the matching lines, together with line numbers.
9. egrep -- extended grep -- is the same as grep -E



  bash $ egrep 'matches|Matches' file.txt
  Line 1 matches.
  Line 3 Matches.
  Line 4 contains matches, but also Matches

10. fgrep -- fast grep -- is the same as grep -F.

Note:
1. On some Linux distros, egrep and fgrep are symbolic links to, or aliases for grep, but     invoked with the -E and -F options, respectively.

2. To search compressed files, use zgrepzegrep, or zfgrep.











11. tr
character translation filter.

The -d option deletes a range of characters.

echo "abcdef"                 # abcdef
echo "abcdef" | tr -d b-d     # aef


$tr -d 0-9 <filename
# Deletes all digits from the file "filename".











12. wc
wc gives a "word count" on a file or I/O stream:










wc -w gives only the word count.
wc -l gives only the line count.
wc -c gives only the byte count.
wc -m gives only the character count.
wc -L gives only the length of the longest line.
13. fold
A filter that wraps lines of input to a specified width.

14. fmt
Simple-minded file formatter, used as a filter in a pipe to "wrap" long lines of text output.

15. col
This deceptively named filter removes reverse line feeds from an input stream.The chief use of col is in filtering the output from certain text processing utilities, 










16. nl
Line numbering filter: nl filename lists filename to stdout,


[sankar@new-host shell script]$ nl file1.data
     1  100 Shoes
     2  200 Laces
     3  300 Socks
[sankar@new-host shell script]$ cat -b file1.data
     1  100 Shoes
     2  200 Laces
     3  300 Socks

17. pr
Print formatting filter.
          The pr command combines much of the functionality of nlpastefoldcolumn, and expand.



Beginner's Introduction to Shell Script: PART 4
Beginner's Introduction to Shell Script: PART 5
Beginner's Introduction to Shell Script: PART 6














No comments:

Post a Comment