UNIX and Shell Scripting - Module 4
UNIX and Shell Scripting - Module 4
Copyright © 2009 Accenture All Rights Reserved. Accenture, its logo, and Accenture High Performance Delivered are trademarks of Accenture.
Module 4 Objectives
• Upon completing this module, you will be able to:
– Describe how to execute the following UNIX commands
• General Purpose
• File Manipulation
• Text Processing
• Printing
• Network and Communication
• Compression/Decompression
• Process Management
Command Function
$ who -q • Displays only the names and number of users
currently logged in
– When this option works all other options do not work
with who.
– $ passwd
(after pressing enter key you will receive following three prompts.)
• Current passwd :
• New passwd :
• Confirm new passwd :
Command Function
$ cd ~ Moves to the HOME directory
Command Function
$ cat -n TestF4 Lists line numbers while displaying contents of
TestF4
Command Function
$ ln –s TestF2 TestFln Creates a soft link TestFln to file TestF2
1. f2 exists
2. f2 does not exist
1. cd ../..
2. mkdir ../bin
3. rmdir ..
4. ls ..
• Instructions:
– Create a file named TestFileCommand with at least ten lines and
perform the following tasks on the file:
• Rename the file TestFileCommand to cmdTest.
• Change the permissions of the file for following:
– User – read, write, and execute
– Group – read
– Others – read and execute
• List the first three lines of the file.
• List all the lines after line four.
• Create a soft link to the file cmdTest with name cmdTestLn.
• Using pipe
– The symbol | is the UNIX pipe symbol.
• Is used on the command line
• Means that the standard output of the command to the left of the pipe
gets sent as standard input of the command to the right of the pipe
pipe1 pipe2
Data flow
Kernel
Copyright © 2009 Accenture All Rights Reserved. 44
Using Filter
• A filter:
– Is a UNIX command that does some manipulation of the text of a file
– Transforms or filters the data it receives via the pipes with which it is
connected
Without –b option diff identifies blank Causes blanks (spaces and tabs) to
spaces or tabs as difference. compare equally even if an unequal
number of blanks exist.
Copyright © 2009 Accenture All Rights Reserved. 52
Sort Contents of File
• The sort command
– Sorts ASCII (Text) files
– Sorts according to the ASCII collating sequence, i.e. sorts the
spaces and the tabs first, then the punctuation marks followed by
numbers, uppercase letters, and lower case letters
Command Function
$ sort TestF1 Sorts the contents of TestF1 and
displays the sorted output on screen
$ sort TestF1 TestF2 Sorts the contents of TestF1 and
TestF2 respectively and displays the
result on screen
Command Function
$ cut -d “ “ -f 2,4 TestF1 Extracts 2nd and 4th the fields from the file
TestF1 and displays them on standard
output
$ cut -d “ “ -f 2,5 TestF1 > cutList1 Extracts fields 2 and 5 from file
TestF1 and sends to cutList1
$ cut -d “ “ -f 3,6 TestF2 > cutList2 Extracts fields 3 and 6 from file
TestF2 and sends to cutList2
$ paste cutLsit1 cutList2 Pastes two files cutList1, cutList2
vertically
–In the output original lines are
separated by TABs by default.
$ paste -d “|” cutList1 cutList2 Paste uses the tab as the default
delimiter, we can specify one or
more delimiter with –d option.
Copyright © 2009 Accenture All Rights Reserved. 64
Merge Two Files on Prompt
• The join command
– A join of the two relations specified by the lines of file1 and file2
– Files are joined on a common key field (column) that should exist in
both files.
– Both files must be sorted on the key field in the same order.
file1 file2
01123 HP 9000/825 01123 Hewlett Packard
02213 Sun 3/110 02213 Sun Microsystems
03320 Sun 4/110 03321 Sun Microsystems
08412 HP 9000/835 08412 Hewlett Packard
1. grep a b c
2. grep <Test> TestFile
1. rwxr-xrw-
2. rw-r-----
3. --x-w-r--
• Instructions:
– Create a file named TestGrep1 and TestGrep2, add ten to fifteen
lines in each file with at least ten words separated by | (pipe sign) in
each line
– Perform the following operations on the file:
• Search for the string accenture or Accenture.
• Cut the column 2-3 and 5-7 respectively from file TestGrep1.
• Paste the deleted columns on the screen.
• Find out the difference between both files and make changes to make
them identical.
• $ lprm
• $ lprm 295
Packet Received 0
indicates that there is no
connectivity between the
local and remote hosts.
Command Function
$ tar -cvf archive.tar TestF1 TestF2 • Creates archive file archive. tar for files TestF1
and TestF2
• Additionally, use of –v (verbose) option displays
the progress while tar works.
• To create archive, we need to specify the name of
the archive (with –f), the copy, or write operation (-
c), and the filenames.
$ tar -xvf archive.tar tar uses –x option to extract files from an archive
Command Function
• $ ps –f
Parent Process ID
• $ kill 2880
Status of the
background process
after completion
Job Executed
Job Executed
1. ps
2. ls
3. at
4. Batch
1. ps
2. at
3. &
4. kill
• Instructions:
– Create a file TestCmdG1 and TestCmdG2 with at least ten lines in
both the files
– Perform the following operations:
• Compress both the files in current working directory.
• Create archive file with the name TestCmdArc.tar containing both the
files.
• Extract the zip files.
• Extract the tar file TestCmdArc.tar.
• Instructions:
– Perform sort operation on file TestCmdG1 and send it to the
background.