akcc.kleit.ac.in 04.docx · web viewakcc.kleit.ac.in

38
MODULE - 04 SHELL PROGRAMMING Ordinary variables Within the shell, a shell parameter or a variable is associated with the value .tehre are several kinds of parameters. Rules for naming variable sare 1.Variable name can be begin with the alhabets (a-z or A-Z)or underscore character. 2. Other variable names that contain special character lik @ $ # are reserved for special parameters. Prepared by Prof.Shanta Kallur,KLEIT Page 1

Upload: dangbao

Post on 22-Jun-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

MODULE - 04

SHELL PROGRAMMINGOrdinary variables

Within the shell, a shell parameter or a variable is associated with the value .tehre are several kinds of parameters.

Rules for naming variable sare

1. Variable name can be begin with the alhabets (a-z or A-Z)or underscore character.

2. Other variable names that contain special character lik @ $ # are reserved for special parameters.

1. User defined variables o It is created and maintained by the user.o This type of variable defined may use any valid variable name,.o but ot is good practice to avoid all uppercase names as many are used by

the shell.Example.: int a, float a1

Prepared by Prof.Shanta Kallur,KLEIT Page 1

Page 2: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

2. Predefined variables

Predefined variables are either shell variable or environment variables. Environmental variables define the working environment. It can be modified at the command line . It is used by some programs to customise them to user. An example is the environment variable DISPLAY which is used by

most of the programs. Environmental variables are managed by the shell; Ex: echo ${PATH} out put:/.:/usr/bin:/usr/xyz Here in the above example the PATH variable give the path of a file by

listing the directory. Environmental variable can be defined after the shell is created. The env command can be used to define an environment variable

setting for the program being launched. The set command is to display the set of environmental variables. To assign the value to a variable use the following syntax:

VARIABLE=value$p=10$x=20

Example for environmenta variables are :HOME,PATH,DISPLAY etc..

3. Shell variables A shell variable is name with a value assigned to it. Most variable values consist of text and most of variable names are

upper case. An environment variable is defined as part of initializing the operating

system. Some programs that are specific to environment can be defined before

the program is started. If the variable have no value the program then the program will terminate with the error message.

Shell variables are local to a particular instance of the shell such as shell scripts.

Shell variables are printed using the command set. Example : $x=10 echo “$x”;

Prepared by Prof.Shanta Kallur,KLEIT Page 2

Page 3: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

Here in the above example $x is the shell variable.it will be printed using the shell echo command.

The .profile file The .profile file is present in user home($HOME) directory. It customize as user individual working environment. Because the .profiel fiel is hidden ,ls –a command to list it. It contains your individual profile that overrides the variables set in

the /etc/profile file. It is used to set exported environment and terminal modes. You can customize your environment by modifying the .profile file. .profile controls the following:

1. Shells to open2. Keyboard sound3. Prompt appearance

The hidden fie is read every time you open a shell in Unix and you can put in commands to interact with the shell.

Read command The read statement is the shell’s internal tool for taking input from the

user. It is used with one or more variables.

When you use the statement Read name

Then the script pauses at that point to take input from the keyboard. Whatever you enter is stored in the variable name.

Example: if we create the shell script using vi p1.shIt contains the following codeEcho” enter the name”Read nameEcho”the entered name is :$name” When we compile the program using sh p1.shOutput: enter the name Suma

The entered name is: suma

Here $ symbol used with variable to fetch the value of a variable name.

The single read can be used to read one or more variable as follows Echo “ enter the name address”

Prepared by Prof.Shanta Kallur,KLEIT Page 3

Page 4: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

Read name address here it read the name and address 2 variables at a time. If the number of values are less than the varibles then leftover

variables will simply remain unassigned.Ex: Echo “ enter the name ,address” Read name here only name will have the value address will be unassigned .

Command line arguments The UNIX shell is a command line interface. In addition to providing a command line interface to the OS the shell

also provides a programming language. Shell commands consists of one or more words separated by blank

spaces. The first word to appear in a command line is the name of a

program .the remaining words get passed as arguments to the program. Arguments can be options that tell the program how to behave, they

can be files on that the program should operate on. In ls –l here –l is option or argument. Command line arguments are also known as positional parameters. When arguments are specified with a shell script they are assigned to

certain special variables. The first argument is read by the shell into the parameter is $1,the

second argument in to $2 so on. In addition to these positional parameters,there rae a few other special

parameters used by the shell, their significance is noted below;$* - it stores complete set of positional parameters.$# - it is set to the count or number of arguments.$0 –It holds the name of shell script$@- it conatins all the positional parameters is is as same as $*.

Example. Sh p1.sh f1 f2 f3 Here $0 is p1.sh , S1 is f1, $2 is f2 , $3 is f3 and $# is 3 and $* is f1 f2

f3.

e xit and exit status of a command

Prepared by Prof.Shanta Kallur,KLEIT Page 4

Page 5: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

exit command The exit command allows user to terminate execution from anywhere in shell

script and returns an exit value using format:Exit

If the user don’t use exit command then the shel scripts finish after the last command is executed.

Every command returns an exit stauts. A successful command returns 0, while unsuccessful one returns a non-zero

value that usually can be interpreted as error .The exit status

The $? Variable represents the exit status of the previous command. Exit status is a numerical value returned by every command upon its

completion. As a rule, most unix commands return exit status of 0 if they were successful

and 1 they were unsuccessful. When a command finishes execution ,it returns an exit code. The exit code is

not displayed on the screen by default. To examine the exit code you need to examine a special variable $?

Example: mv f1 f2 Echo $?It tells whether the f1 is moved to f2 or not by giving 0 or 1 output.

Logical operators for conditional executionThe && and || are used as logical operators.They have the following syntax:Command 1 && command 2 : here command 2 is executed once command 1 is succeededCommand 1 || command 2 here either of the command is true the condition is true.Example 1 : grep ‘ manager’ emp.lst && echo “pattern is found” here second is true and executed only when manager pattern is found in emp.lst file.

Example 2 : grep ‘ manager’ emp.lst || echo “pattern not found” here the || plays inverse role,the second command is executed only when first fails.

The if statement The first branching method uses the if-then elif-else-fi construct.

Prepared by Prof.Shanta Kallur,KLEIT Page 5

Page 6: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

Read elif as else if.If statement works on the condition,if the condition is true then do the work or else go to else part . The syntax of if-then-elif-else-fi block has the following syntax: if [ condition ]

ThenCommand1.. command n

elif [ condition ] Command1..command n

elseCommand1..command n

fi An if or elif is followed by the test.whenever the test evaluates to true the

shell executes the commands that follow and then continues with the rest of the script after the block’s end point,the fi.

The else statement is the default action.Given that neither of the if or elif pass then the shell performs the commands within the else block and scripts again continues after the fi.

Usage of elif and elses in an if statement are optional.

Program to check whether the entered name is correct or not

Echo”enter the name”

Read name

if [ $name=”VTU” ]

then echo “correct name”

else

echo “ not correct”

fi

The test command and its shortcut

Prepared by Prof.Shanta Kallur,KLEIT Page 6

Page 7: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

When we use if to evaluate expressions, we need to test statement because the true or false values returned by expressions cannot be directly handled by if.

Test uses certain operators to evaluate the condition to evaluate the condition on its right and returns either a true or false exit status which is then used by if foe decision making.

Test works in 3 ways as follows:1. Compare 2 numbers2. Compare 2 strings3. Checks a file’s attributes

The numerical comparison operators and their meanings used by test are as

-eq= equal to

-ne=not equal to

-gt=greater than

-lt=lesser than

-ge=greater than or equal to

-le=lesser than or equal to

Test statement does not display any output simply iy sets the parameter $?.Example 1: suppose we have created the file f1.sh it has the following code if test $# -eq 0 then echo “ no arguments” fi

When compiling sh f1.sh if we don’t give command line argument then output“ no arguments” will be printed and $? Will have the value 0. Instead if we give sh f1.sh file1.here fil1 is passed as an argument so $#is 1. The test will be failed because the $# is having values 1 so the program will be terminated.so $? Will have the value 1 as error code.Example 2: if test 100 –gt 99 then

echo “ greater”elseecho”smaller”fi

The while statement

Prepared by Prof.Shanta Kallur,KLEIT Page 7

Page 8: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

A while loop is different from the for loop in that it uses a test condition to determine whether or not to execute its command block.

As long as the condition returns true when tested, the loop executes the commands. If the condition returns false, then the script skips the loop and proceeds beyond its

termination point. The syntax for while loop is as follows:

While condition

do

Command1

.

.

.

Command n.

Done

Because the loop starts with the test, if the condition fails then command within the while condition will be skipped and the program will continue with statements after the while loop.

After performing the last command in the block the loop tests the condition again and determines whether to proceed or fall through it.

Example: an example to print the natural numbers

i=1

while [ $i –le 5 ]

do

echo “$i”

i =` expr $i + 1 `

done

output : 12345

For loop A for loop processes multiple commands on alist of items. The loop continues until the variable reaches a target value.

Prepared by Prof.Shanta Kallur,KLEIT Page 8

Page 9: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

But here, the for loop uses intermediate variable to execute actions on asset of items such as files or the results of a command.

The shell’s for loop is more flexible and useful than in most other languages. The syntax of for loop is follows:

for variable in list do

Command1 . . .Command n

done The for loop’s variable may be any legal shell’s variable.it need not be

declared prior to the loop. As the for loop executes the command block,the value of variable changes into

the next item found in list. Items in list must be separated by white space. The list may be generated using many methods like files, command line

arguments arrays and strings etc. Example 1: suppose if we have the file f1.sh having the following code

for $i in $*doecho “$i”donewhen we compile the program sh f1.sh f1 f2 f3 here $ * will have all 3 command line arguments. It prints output as f1f2f3

Example 2: for var in 0 1 2 3 4 5 doecho $vardone

the output is

0

1

2

3

4

5

Case statements The second form of branching is available is the case statement.

Prepared by Prof.Shanta Kallur,KLEIT Page 9

Page 10: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

A case differs from an if block in that it branches based upon the value of one variable.

The syntax of case is as follows:Case(variable) im

Pattern1) Command1

. . .

Command n

;;

Pattern2) Command1

. . .

Command n

;;

.

.

.

Pattern n) Command1

. . .

Command n

;;

esac

The case block takes as its argument a variable. To denote the variable, it must be surrounded by parentheses. The case compares the variables values against each pattern. The pattern may be any legal regular expression. If the variable’s value matches the

pattern, then the shell executes the command block immediately following the pattern.

The command block terminates with a pair of double semi colons.

Prepared by Prof.Shanta Kallur,KLEIT Page 10

Page 11: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

FRUIT="kiwi"

case "$FRUIT" in

"apple") echo "Apple pie is quite tasty."

;;

"banana") echo "I like banana nut bread."

;;

"kiwi") echo "New Zealand is famous for kiwi."

;;

esac

In the above example the FRUIT pattern will be assigned with value kiwi. It is sent in case statement, if the pattern is apple or banana then the statements followed pattern will not be executed because the value is kiwi .so it matches with the third pattern then the output “New Zealand is famous for kiwi” will be printed.

The set and shift commands and handling positional parameters.

Some UNIX commands like date produce single line output. We also pass the command output through filters like grep and head to produce a single line.

We had to use cut command to extract the fields from date command output. This can be done by one more shell command –set. Set assigns it augments to the positional parameters $1 $2 and son on. This

feature is especially useful for picking up individual fields from the output of a program.

Lets use set to convert its arguments to positional parameters. set 10 20 echo”\$1 is $1, \$2 is $2” , The output is $1 is 10 $2 is 20 If we do, echo “ the $# arguments are $*” ,Then the output is the 2 arguments

are 10 20 We will now use command substation to extract individual fields from the

date output without using cut: Set `date` Echo $* we get output as

Wed Jan 8 09:54:23 IST 2016 as output , here $1 is Wed $2 is Jan

$3 is 8 $4 is 09:54:23 $5 is IST $6 is 2016.so totally 6 arguments are 6.so $# is 6then echo ”today’s date is $2 $3,$6”it gives output as todays’s date is Jan 08 2016 Shift command

Prepared by Prof.Shanta Kallur,KLEIT Page 11

Page 12: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

The shift command transfers the contents of a positional parameters to its immediate lower numbered one.Example: echo “$@” Wed Jan 8 09:54:23 IST 2016Echo $1 $2 $3 output is Wed Jan 8 here $1 is Wed , $2 is Jan ,$3 is 8ShiftEcho $1 $2 $3 output is Jan 8 09:54:23ShiftEcho $1 $2 $3 output is 8 09:54:23 ISTShift 2 it shifts by 2 parameters and countsEcho $1 $2 $3 output is IST 2016Here $1 is IST , $2 is 2016 and $3 will not contain anything.

The here << document There are occasions when the data your program reads is fixed and limited. The shell uses << symbols to read the data from the same file containing the

script.this is referred as here document. Here document signifies that the data is here rather than in a separate file. Any command using standard input can also take input from a here document. This feature is useful when used with commands that don’t accept a filename

as argument. If the message is short you can have both command and message in the same

script:

wc -w <<EOFThis is a test. Apple juice. 100% fruit juice and no added sugar, colour or preservative.EOF

Here it reads text till it reads the EOF.

trap command

When you press the Ctrl+C or Break key at your terminal during execution of a shell program, normally that program is immediately terminated, and your command prompt

Prepared by Prof.Shanta Kallur,KLEIT Page 12

Page 13: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

returned. This may not always be desirable. For instance, you may end up leaving a bunch of temporary files that won't get cleaned up.

Trapping these signals is quite easy, and the trap command has the following syntax −

$ trap commands <signal list>

Some of signals are 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL

5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE

Here command can be any valid Unix command, or even a user-defined function, and signal list can be a list of any number of signals you want to trap.

There are two common uses for trap in shell scripts −

Clean up temporary files

Ignore signals

1. Cleaning Up Temporary Files

As an example of the trap command, the following shows how you can remove some files and then exit if someone tries to abort the program from the terminal –

$ trap "rm –f d1 d2 exit ” 2

From the point in the shell program that this trap is executed, the two files  d1 and d2 will be automatically removed if signal number 2 is received by the program.

2. Ignoring Signals

If the command listed for trap is null, the specified signal will be ignored when received. For example, the command −

$ trap '' 2

Specifies that the interrupt signal is to be ignored. You might want to ignore certain signals when performing some operation that you don't want interrupted. You can specify multiple signals to be ignored as follows −

$ trap '' 1 2 3 15

Prepared by Prof.Shanta Kallur,KLEIT Page 13

Page 14: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

Sample programs

1. Write a shell script to accept 2 file names as parameters to check whether thy have same permissions are not thenecho "Arguments are not passed"exit 0fils -l $1|cut -d " " -f1 >file1ls -l $2|cut -d " " -f1 >file2if cmp file1 file2then echo "File condition are identical"cat file1elseecho "File condition are not identical"echo "The permission of the $1 is"cat file1echo "permission of the $2 is"cat file2fi2.Calculatorsum=0i="y"

echo " Enter one no."read n1echo "Enter second no."read n2while [ $i = "y" ]doecho "1.Addition"echo "2.Subtraction"echo "3.Multiplication"echo "4.Division"echo "Enter your choice"read chcase $ch in 1)sum=`expr $n1 + $n2` echo "Sum ="$sum;; 2)sum=`expr $n1 - $n2` echo "Sub = "$sum;; 3)sum=`expr $n1 \* $n2` echo "Mul = "$sum;; 4)sum=`expr $n1 / $n2` echo "Div = "$sum;; *)echo "Invalid choice";;esacecho "Do u want to continue ?"read iif [ $i != "y" ]then exitfidone

3. Write as shell program to find the sum of n natural numbers.

Prepared by Prof.Shanta Kallur,KLEIT Page 14

Page 15: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

echo -n "Enter number : "

read n

i=1

sum=0

while [ $i -le $n ]

  do

   sum=$(( $sum + $i )) # calculate sum of digit

        i=$(( $i + 1 ))

  done

echo  "Sum of all digit  is $sum"

Prepared by Prof.Shanta Kallur,KLEIT Page 15

Page 16: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

4. Shell Script to Find Prime Number echo -n "Enter a number: "read numi=2

while [ $i -lt $num ]do if [ `expr $num % $i` -eq 0 ] then echo "$num is not a prime number" echo "Since it is divisible by $i" exit fi i=`expr $i + 1`doneecho "$num is a prime number "

5. Shell script to read a number and find whether the number is odd or even echo -n "Enter number : "read n rem=$(( $n % 2 ))if [ $rem -eq 0 ]then  echo "$n is even number"else  echo "$n is odd number"fi

Prepared by Prof.Shanta Kallur,KLEIT Page 16

Page 17: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

File inodes and the inode structure An inode is a control structure that conatins the key information

needed bt the operating system for a perticualr file. Inodes exists in a static form on disk. Kerner reads inodes into an incore inode to manipulate them. Several file names may be associated with single inode,but an

active inode is associated with exactly one file. Each file is exactly controlled by exactly one inode. Disk inode consists of following fields:

1. File owner id2. File type: regular device or directory3. File access permission4. File access time5. Number of links6. Table of contents of the disk address of data in a file.7. File size

The attributes of file its permissions and other control information are stored in the inode.

Change the contents of a file automatically implies a change to the inode but changing the inode does not imply that contents of the file change.

Advantages of inode:1.Data in small files can be accessed directly from the inode.2.Larger files can be accessed efficiently.3.Disks can be filled completely with little wasted space.Disadvantages of inode1. Access of data often requires long searching.2. Data blocks are not stored together leading poor performance.3. Transferring is inefficient.

Head and tail commands

Prepared by Prof.Shanta Kallur,KLEIT Page 17

Page 18: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

The head command in unix or linux system is used to print the first N lines from the file to the terminal. The syntax of head command is 

head [options] [files]

The head command options are: 

c : Prints the first N bytes of file; With leading -, prints all but the last N bytes of the file.n : Prints first N lines; With leading - print all but the last N lines of each file.

Head Command Examples: Create the following file in your linux or unix operating system for practicing the examples: 

> cat example.txt

linux storage

ubuntu os

fedora

1. Display first 10 lines 

By default, the head command prints the first 10 lines from a file. 

> head example.txt

2. Display first N lines 

Use the -n option to print the first n lines from a file. The following example prints the first 2 lines from the file: 

> head -n2 example.txt

linux storage

ubuntu os

tail commmand

Prepared by Prof.Shanta Kallur,KLEIT Page 18

Page 19: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

The tail command in unix or linux system is used to print the last N lines from the file on the terminal. Tail command is especially used with log files to read the last few lines to know about the error messages. The syntax of tail command is 

tail [options] [files]

The tail command options are: 

c : Prints the last N bytes of file; With leading +, prints the characters from the N byte in the file. n : Prints last N lines; With leading + prints lines from the Nth line in the file. f : Prints the appended lines on the terminal as the file grows.

Tail Command Examples Create the following file in your linux or unix operating system for practising the examples: 

> cat example.txt

virtual storage

oracle virtual instance

mysql backup

dedicated hosting server

cloud servers

1. Display last 10 lines 

By default, the tail command prints the last 10 lines from the file. 

> tail example.txt

2. Display last N lines 

Use the -n option to print the last n lines from the file. The following example prints the last 2 lines from the file: 

> tail -n2 example.txt

dedicated hosting server

cloud servers

Prepared by Prof.Shanta Kallur,KLEIT Page 19

Page 20: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

Soft Links & Hard LinksA link in UNIX is a pointer to a file. Like pointers in any programming languages, links in UNIX are pointers pointing to a file or a directory . Creating links is a kind of shortcuts to access a file. The two different types of links in UNIX are:

1. Soft Links or Symbolic Links2. Hard links

How to create Soft links and hard links? And how do we access them?     Say, you have a file named "file1" with the following contents:

$ cat file1

welcome

. To create a hard link of file1: we should give the following commnad

$ ln file1 file2

To create a soft link of file1: we should give the following command

$ ln -s file1 file3

 Once the links are created, the linked files contain the same content as of the original file. As shown below

$ cat file2

welcome

$ cat file3

Welcome

 "file2" and "file3" being the linked files, can I say which is a soft link & which is hard link?   Yes. When you do the listing of the files with "-li" option:

Prepared by Prof.Shanta Kallur,KLEIT Page 20

Page 21: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

$ ls -li

total 20

9962464 -rw-r--r-- 2 guru users 8 Mar  9 file1

9962464 -rw-r--r-- 2 guru users 8 Mar  9 file2

9962471 lrwxrwxrwx 1 guru users 5 Mar  9 file3 -> file1

we cannot say which is the original file and which one was the hard-linked file. Once a hard-link is created, it is like 2 files pointing to the same location. In fact, once a hard link is created on a file, using the term 'original file' is actually incorrect.

On deleting the file "file1", the soft linked file "file3" will become inaccessible. However, the hard linked file "file2" can still be accessed.

Differences between hard links and soft links are as follows:

Hard links Soft linksHard links can be created only within the file system.

Soft Links can be created across file systems.

Hard links have the same inode number as the original file.

Soft link has a different inode number than the original file.

On deleting the original file, hard linked file can still be accessed.

On deleting the original file, soft link cannot be accessed.

Hard links do not need any extra data memory to save since it uses links

Soft link needs extra memory to store the original file name as its data.

Source file should exist. Source file need not exist for soft link creation.

Can be created only on files, not on directories.

Can be created on a file or on a directory.

Access  to the file is faster compared to soft link.

Access to the file is slower due to the overhead to access file.

Cut and paste commands

Prepared by Prof.Shanta Kallur,KLEIT Page 21

Page 22: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

The cut command is to remove the sections from each line of files. The syntax as follows:

Cut[options]..[files] It prints the selected parts of lines from each file to standard output or on display

device.Options are1. –d-delimeter it uses the delimiter instead of tab such as space | etc..2. –f select the fields3. –c =character list or single character: seletct nly the characters1. For example, let's say you have a file named data.txt which contains the

following text:one two three four five

alpha beta gamma delta epsilon

In this example, each of these words is separated by a tab character, not spaces. The tab character is the default delimiter of cut, so it will by default consider a field to be anything delimited by a tab.To "cut" only the third field of each line, use the command:cut -f 3 data.txt

...which will output the following:Three

Gamma

because these are 3 rd field output

2. If instead you want to "cut" only the second-through-fourth field of each line, use the command:

cut -f 2-4 data.txt

...which will output the following:

two three four

beta gamma delta

Prepared by Prof.Shanta Kallur,KLEIT Page 22

Page 23: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

3. cut -c 3 data.txt

Outputs the third character of every line of the file file.txt, omitting the others.

e

a

because these are the 3 rd character in each line of file.

Paste CommandPaste command is one of the useful commands in unix or linux operating system. The paste command merges the lines from multiple files. The paste command sequentially writes the corresponding lines from each file separated by a TAB delimiter on the unix terminal. 

The syntax of the paste command is

paste [options] files-list

The options of paste command are:

-d : Specify of a list of delimiters.

-s : Paste one file at a time instead of in parallel.

--version : version information

--help : Help about the paste command.

Paste Command Examples::

suppose we create a files file1 and file2 file3 and contents are as follows

> cat file1

Unix

Linux

Windows

Prepared by Prof.Shanta Kallur,KLEIT Page 23

Page 24: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

> cat file2

Dedicated server

Virtual server

> cat file3

Hosting

Machine

Operating system

1. Merging files in parallel

By default, the paste command merges the files in parallel. The paste command writes corresponding lines from the files as a tab delimited on the terminal.

> paste file1 file2

Unix Dedicated server

Linux Virtual server

Windows

> paste file2 file1

Dedicated server Unix

Virtual server Linux

Windows

Prepared by Prof.Shanta Kallur,KLEIT Page 24

Page 25: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

2. Specifying the delimiter

Paste command uses the tab delimiter by default for merging the files. You can change the delimiter to any other character by using the -d option.

> paste -d"|" file1 file2

Unix|Dedicated server

Linux|Virtual server

Windows|

The sort command and its usage with different options

sort command is used to sort a file, arranging the records in a particular order. By default, the sort command sorts file assuming the contents are ascii. Using options in sort command, it can also be used to sort numerically. Let us discuss it with some examples:

File with Ascii data: Let us consider a file with the following contents: 

$ cat file

Unix

Linux

Solaris

AIX

Linux

HPUX

Prepared by Prof.Shanta Kallur,KLEIT Page 25

Page 26: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

1. sort simply sorts the file in alphabetical order: 

$ sort file

AIX

HPUX

Linux

Linux

Solaris

Unix

All records are sorted alphabetically.

 2. sort removes the duplicates using the -u option: 

$ sort -u file

AIX

HPUX

Linux

Solaris

Unix

The duplicate 'Linux' record got removed. '-u' option removes all the duplicate records in the file. Even if the file have had 10 'Linux' records, with -u option, only the first record is retained.

3.  To sort a file numericallly: suppose we have the file called file10 and it has the contents1920492005

Prepared by Prof.Shanta Kallur,KLEIT Page 26

Page 27: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

To sort the numbers we have the following command

$ sort -n file10

5

19

20

49

200

   -n option can sort the decimal numbers as well.

4. sort file numerically in reverse order: 

$ sort -nr file10

200

49

20

19

5

'r' option does a reverse sort.

. The umask and default file permissions When user create a file or directory under Linux or UNIX, she create it with a default set of permissions. In most case the system defaults may be open or relaxed for file sharing purpose. For example, if a text file has 666 permissions, it grants read and write permission to everyone. Similarly a directory with 777 permissions, grants read, write, and execute permission to everyone.

The user file-creation mode mask (umask) is use to determine the file permission for newly created files. It can be used to control the default file permission for new files. It is a four-digit octal number. A umask can be set or expressed using: Symbolic values Octal values

Prepared by Prof.Shanta Kallur,KLEIT Page 27

Page 28: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

You can setup umask in /etc/bashrc or /etc/profile file for all users. By default most Linux distro set it to 0022 (022) or 0002 (002). Open /etc/profile or ~/.bashrc file, enter:

# vi /etc/profile

Append/modify following line to setup a new umask:

umask 022

Save and close the file. Changes will take effect after next login. All UNIX users can override the system umask defaults in their /etc/profile file

 If the default settings are not changed, files are created with the access mode 666 and directories with 777. In this example:

1. The default umask 002 used for normal user. With this mask default directory permissions are 775 and default file permissions are 664.

2. The default umask for the root user is 022 result into default directory permissions are 755 and default file permissions are 644.

3. For directories, the base permissions are (rwxrwxrwx) 0777 and for files they are 0666 (rw-rw-rw).

Two special files /dev/null and /dev/tty.

Unix systems provide two special files that are particularly useful in shell programming.The first file, /dev/null,is often known as the “bit bucket.” Data sent to this file is thrown away by the system. In other words,a program writing data to this file always believes that it has successfully written the data,but in practice,nothing is done with it. This is useful when you need a command’s exit status but not its output.if grep pattern myfile > /dev/null here it throws the output to dev/null filethen... Pattern is thereelse... Pattern is not therefi

Prepared by Prof.Shanta Kallur,KLEIT Page 28

Page 29: akcc.kleit.ac.in 04.docx · Web viewakcc.kleit.ac.in

In contrast to writes,reading from /dev/null always returns end-of-file immediately.Reading from /dev/null is rare in shell programming,but it’s important to know how the file behaves.The other special file is /dev/tty. When a program opens this file,Unix automaticallyredirects it to the real terminal (physical console or serial port,or pseudo-terminal for network and windowed logins) associated with the program. This is particularly useful for reading input that must come from a human,such as a password. It is also useful, although less so, for generating error messages;

printf "Enter new password: "                 #Prompt for inputstty -echo                                                 #Turn off echoing of typed charactersread pass < /dev/tty                                 #Read passwordprintf "Enter again: "                               #Prompt againread pass2 < /dev/tty                               #Read again for verificationstty echo                                                  #Don't forget to turn echoing back on

The stty (set tty) command controls various settings of your terminal (or window). The –echo option turns off the automatic printing (echoing) of every character you type; stty echo restores it.

* stty is possibly the most baroque and complicated Unix command in existence. See the stty(1) manpage for the gory details, or Unix in a Nutshell.

Prepared by Prof.Shanta Kallur,KLEIT Page 29