Unix Shell Programming
Ability Enhancement Course
               AEC410
ABILITY ENHANCEMENT COURSE ASSESSMENT :
• The assessment of the Ability Enhancement Course shall comprise of
 Continuous Internal Evaluation (CIE) of 50 marks and a Semester End
 Examination (SEE) of 50 marks.
• The CIE will comprise two tests of 30 marks each having Part-A with MCQs
 and Part-B with descriptive Questions. Another 20 marks of CIE will be
 assessed through quizzes, Assignments, Presentations, etc.
• The SEE will comprise Part-A with MCQs and Part-B with descriptive
 Questions
Unit 1
• The architecture of UNIX
• Features of UNIX
• Introduction to UNIX file system
• vi editor
• File handling utilities and security by file permissions
• Basic UNIX commands (PATH, man, echo, printf, script, passwd, uname,
  who, date, stty, pwd, cd, mkdir, rmdir, ls, cp, mv, rm, cat, more, wc, lp,
  od, tar, gzip).
What is Unix?
• The Unix operating system is a set of programs that act as a link between the computer and
  the user.
• The computer programs that allocate the system resources and coordinate all the details of
  the computer's internals are called the operating system or the kernel.
• Users communicate with the kernel through a program known as the shell. The shell is a
  command line interpreter; it translates commands entered by the user and converts them
  into a language that is understood by the kernel.
• Unix was originally developed in 1969 by a group of AT&T employees Ken Thompson,
  Dennis Ritchie, Douglas McIlroy, and Joe Ossanna at Bell Labs.
• There are various Unix variants available in the market. Solaris Unix, AIX, HP Unix and BSD
  are a few examples.
• Linux is also a flavor of Unix which is freely available.
Unix Architecture
• Kernel: The kernel is the heart of the operating system. It interacts with the
   hardware and performs most of the tasks like memory management, task
   scheduling, and file management.
It contains two basic parts of the OS:
Process Control and Resource Management: It performs the following tasks
 Process Management
 File Management
 Device Management
 I/O Management
 Memory Management
• Shell: The shell is the utility that processes your requests. When you type in a
   command at your terminal, the shell interprets the command and calls the
   program that you want. The shell uses standard syntax for all commands. C
   Shell, Bourne Shell, and Korn Shell are the most famous shells which are
   available with most of the Unix variants.
Unix Architecture
• Commands and Utilities: There are various commands and utilities which
  you can make use of in your day-to-day activities. cp, mv, cat and grep,
  etc. are a few examples of commands and utilities. There are over 250
  standard commands plus numerous others provided through 3rd party
  software. All the commands come along with various options.
• Files and Directories: All the data of Unix is organized into files. All files
  are then organized into directories. These directories are further
  organized into a tree-like structure called the file system.
• *Utility: is a standard UNIX program that provides a support process for
  users. Three common utilities are Text editors, Search Programs, and sort
  programs.
Features:
• Portable
• Multiuser
• Multitasking
• Networking
• Organized File System
• Device Independence
• Utilities
• Services
Introduction to UNIX File System:
• Filenames:
   • Length:14 -255 characters
   • Avoid special characters : > or < must not be used
   • Use Dividers: _ ,. ,-
   • Use Extensions
   • Never start a filename with a period. Filenames that start with a period are hidden
     files in UNIX.
Wildcards:
• ?: Any Single character.
• […]: Single character from the set
• *: zero or more characters
Ex: c?         c?t    c??t          ?a?
Matches: c1,ca,cat,c12t, bat, car etc….
EX: f[aoei]d f[a-d]t         f[A-z][0-9]    f[A-Za-z][0-9]
Matches: fad, fod, fed, fid, fat, fa3, f^2 etc….
Does not match: fud, faa….
* : every file
f*: all files which begins with f
*f : all files which ends in f
*.* every file whose name has a period.
Wildcards with echo command:
$ echo f?t
f1t fat fbt fgt fwt
File types
Listing Files
• To list the files and directories stored in the current directory, use the
  following command:
• $ls
• The command ls supports the -l option which would help you to get more
  information about the listed files –
• $ls –l
  In the ls -l listing example, every file line begins with a d, -, or l. These
  characters indicate the type of file that's listed
First Column: Represents the file type and the permission given on the file.
Second Column: Represents the number of memory blocks taken by the file or directory.
Third Column: Represents the owner of the file. This is the Unix user who created this file.
Fourth Column: Represents the group of the owner. Every Unix user will have an associated group.
Fifth Column: Represents the file size in bytes.
Sixth Column: Represents the date and the time when this file was created or modified for the last time.
Seventh Column: Represents the file or the directory name.
Hidden Files
An invisible file is one, the first character of which
is the dot or the period character (.). Unix
programs (including the shell) use most of these
files to store configuration information.
Some common examples of hidden files include
the files −
 .profile − The Bourne shell ( sh) initialization script
 .kshrc − The Korn shell ( ksh) initialization script
.cshrc − The C shell ( csh) initialization script
.rhosts − The remote shell configuration file
To list the invisible files, specify the -a option to ls –
Display Content of a File
• Display contents of the file:
• Display the contents with the line number
• wc command to get a count of the total number of lines,
  words, and characters contained in a file.
Copying Files
To make a copy of a file use the cp command. The basic syntax of the
command is −
Renaming Files
To change the name of a file, use the mv command. Following is the
basic syntax
Deleting Files
vi Editors
• An editor is a utility that facilitates the editing task- creation /Modification of
  text files.
• Editors are of 2 types:
   • Line Editors
   • Screen Editors
• Line Editors: changes are applied to a line or a group of lines. They are useful
  when you want to make global changes over a group of lines. Ex: if extra space
  is to be added at the start of the line, all the lines can be selected at once,
  instead of adding to each line.
• Screen Editors: Presents a whole screen of text at a time. Each line of the text in
  context with other lines can be viewed at once.
Starting the vi Editor
  2 Modes:
• Command mode − This mode enables you to
  perform administrative tasks such as saving
  files, executing commands, moving the cursor,
  cutting and pasting lines or words, and finding
  and replacing. In this mode, whatever you type
  is interpreted as a command.
• Insert mode − This mode enables you to insert
  text into the file. Everything that's typed in this
  mode is interpreted as input and finally, it is put
  in the file.
You can move around the file with the
arrow keys, or use h - j - k -
 l keys. h-l for left-right, j-k for
down-up.
Once you are done editing you can
press the esc key to exit insert mode,
and go back to command mode.
Editing Files
• To edit the file, you need to be in the insert mode. There are
  many ways to enter insert mode from the command mode −
Deleting Characters
Vi save and Exit Command
• :w - saves the contents of the buffer without quitting vi
• :w filename – Write contents of buffer to new file and continues.
• ZZ – saves the contents of the buffer and exit
• :wq - saves the contents of the buffer and exit
• :q - Exit from vi if only buffer is unchanged
• :q! – Exit from vi without saving.
Adding text
Deleting text
  Moving
  Cursor
   Join
   Undo
 Scrolling
    Saving
    and
   Quitting
Basic UNIX Commands
PATH
• PATH is an environment variable used by the shell to determine
  where to look for executable programs.
• PATH is interpreted as a list of directory names separated by a colon.
  When any command is typed on the command line, the SHELL looks
  up into each PATH directory in the list in turn until the executable file
  with the command name is encountered
• $PATH
 man
• man command in Linux
  is used to display the
  user manual of any
  command that we can
  run on the terminal
    echo
• Echo is a Unix/Linux
  command tool used
  for displaying lines
  of text or string
  which are passed
  as arguments on
  the command line.
  This is one of the
  basic command in
  linux and most
  commonly used in
  shell scripts.
printf
   In Unix and Unix-like operating systems, printf is a shell builtin that formats and prints data
   %s specifier: It is basically a string specifier for string output.
   %b specifier: It is same as string specifier but it allows us to interpret escape sequences with
    an argument.
   %d specifier: It is an integer specifier for showing the integral values
   %f specifier: It is used for output of floating point values.
script
• script command in Linux is used to make typescript or record all the
  terminal activities. After executing the script command it starts
  recording everything printed on the screen including the inputs and
  outputs until exit. By default, all the terminal information is saved
  in the file typescript, if no argument is given. The script is mostly
  used when we want to capture the output of a command or a set
  of commands while installing a program or the logs generated on
  the     terminal     while     compiling    open     source    codes,
  etc. script command uses two files i.e. one for the terminal output
  and other for the timing information.
passwd
• The passwd command changes passwords for user accounts. A
  normal user may only change the password for their own account,
  while the superuser may change the password for any account.
  passwd also changes the account or associated password validity
  period.
• Options
   • passwd -S <username>
   • The -S option displays the status of user account password settings.
   •   For example:
   •   # passwd -S evans
   •   evans PS 2020-09-07 0 99999 7 -1 (Password set, SHA512 crypt.)
   •   The output above shows the account evans was created on 7th
       September 2020 and has a password set with SHA512 encryption.
• passwd -u <username>
This option will unlock the password. This option works for an account
that already has the password locked.
       $passwd -u user2
• passwd -l <username>
   $passwd -l user1
• passwd -d <username>
       This is a quick way to delete a password for an account.
• passwd -e <username>
This is a quick way to expire a password for an account. The user will
be forced to change the password during the next login attempt.
uname
• The command ‘uname‘ displays the information about the system.
  • uname [OPTION]
  1. -a option: It prints all the system information in the following order: Kernel
  name, network node hostname, kernel release date, kernel version, machine
  hardware name, hardware platform, operating system
 who
• The standard Unix command who displays a list of users who
  are currently logged into the computer.
     • Who -m -H (Command to display the hostname and user associated with the
       input/output devices)
     • who -a (To display all details of currently logged in users )
     • Who -all
     • who -p -h (all active processes that are spawned by the NIT process)
     • who -d -H (complete list of all dead processes)
date
• date command is
  used to display the      Options with Examples:
  system date and
  time.           date     1. $date: current date and time, including the abbreviated day name,
  command is also
  used to set date and        abbreviated month name, day of the month, the time separated by
  time of the system.         colons, the time zone name, and the year.
  By default the date
  command displays             Tue Oct 10 22:55:01 PDT 2017
  the date in the time
  zone on which            2. -u Option: Displays the time in GMT(Greenwich              Mean
  Unix/linux operating
  system              is   Time)/UTC(Coordinated Universal Time )time zone.
  configured.      You
  must be the super-       3. Using –date option for displaying past dates:
  user     (root)    to              $date --date="2 year ago"
  change the date and
  time.
tty and stty
• tty command writes the name of your terminal to standard output
• stty displays or changes the characteristics of the terminal.
pwd
• The pwd command writes to standard output the full path name of
  your current directory (from the root directory). All directories are
  separated by a / (slash). The root directory is represented by the first
  /, and the last directory name is your current directory.
• $pwd
   • (Print Working Directory)
cd
• cd command known as change directory command.
• cd /: this command is used to change directory to the root directory,
  The root directory is the first directory in your file system hierarchy.
• cd ~ : this command is used to change directory to the home directory.
• cd .. : this command is used to move to the parent directory of current
  directory, or the directory one level up from the current directory. “..”
  represents parent directory.
mkdir
• mkdir command allows the user to create directories (also referred to
  as folders in some operating systems ). This command can create
  multiple directories at once as well as set the permissions for the
  directories. It is important to note that the user executing this
  command must have enough permissions to create a directory in the
  parent directory, or he/she may receive a “permission denied” error.
more
• more command is used to view the text files in the command prompt,
  displaying one screen at a time in case the file is large (For example log files).
  The more command also allows the user do scroll up and down through the
  page.
• more [-options] [-num] [+/pattern] [+linenum] [file_name]
• [-options]: any option that you want to use in order to change the way the file is
  displayed. Choose any one from the followings: (-d, -l, -f, -p, -c, -s, -u)
• [-num]: type the number of lines that you want to display per screen.
• [+/pattern]: replace the pattern with any string that you want to find in the text
  file.
• [+linenum]: use the line number from where you want to start displaying the
  text content.
• [file_name]: name of the file containing the text that you want to display on the
  screen.
Options:
• -d : Use this command in order to help the user to navigate. It displays
  “[Press space to continue, ‘q’ to quit.]” and displays “[Press ‘h’ for
  instructions.]” when wrong key is pressed.
• -f : This option does not wrap the long lines and displays them as such.
• -p : This option clears the screen and then displays the text.
• -c : This command is used to display the pages on the same area by
  overlapping the previously displayed text.
• -s : This option squeezes multiple blank lines into one single blank line.
• -u : This option omits the underlines.
rmdir:      Removes the directory from the system. The directory must be empty
before you can remove it.
Use the ls -al command to check whether the directory is empty.
ls: Used to list files and directories. The contents of your current working directory.
cp: This command is used to copy files or groups of files or directories. It creates
an exact image of a file on a disk.
mv:      Moves files and directories from one directory to another or renames a file or
directory.
rm:     Use the rm command to remove files you no longer need. The rm command
removes the entries for a specified file, group of files, or certain select files from a
list within a directory.
cat:  To print the content of a file onto the standard output. concatenation of files
which combines multiple files into a single file.
wc
• wc stands for word count. As the name implies, it is mainly used for counting purpose. It is used
  to find out number of lines, word count, byte and characters count in the files specified in the
  file arguments.
• By default it displays four-columnar output.
• First column shows number of lines present in a file specified, second column shows number of
  words present in the file, third column shows number of characters present in file and fourth
  column itself is the file name which are given as argument.
    • Syntax: wc [OPTION]... [FILE]...
• -l: This option prints the number of lines present in a file. With this option wc command
  displays two-columnar output, 1st column shows number of lines present in a file and 2nd itself
  represent the file name.
• -w: This option prints the number of words present in a file. With this option wc command
  displays two-columnar output, 1st column shows number of words present in a file and 2nd is
  the file name.
• -c: This option displays count of bytes present in a file. With this option it display two-columnar
  output, 1st column shows number of bytes present in a file and 2nd is the file name.
• -m: Using -m option ‘wc’ command displays count of characters from a file.
• lp: submits files for printing or alters a pending job..
Tag                                         Description
--                                          Marks the end of options; use this to print a file whose name begins with a dash (-).
-E                                          Forces encryption when connecting to the server.
-U username                                 Specifies the username to use when connecting to the server.
                                            This option is provided for backwards-compatibility only. On systems that support it, this option forces the print file to be copied to
-C
                                            the spool directory before printing. In CUPS, print files are always sent to the scheduler via IPP which has the same effect.
-d destination                              Prints files to the destination printer.
-h hostname[:port]                          Chooses an alternate server.
-i job-id                                   Specifies an existing job to modify.
-m                                          Sends an email when the job is completed.
-n copies                                   Sets the number of copies to print from 1 to 100.
-o "name=value [name=value ...]"            Sets one or more job options.
-q priority                                 Sets the job priority from 1 (lowest) to 100 (highest). The default priority is 50.
-s                                          Do not report the resulting job IDs.
-t "name"                                   Sets the job name.
                                        Specifies when the job should be printed. A value of immediate will print the file immediately, a value of hold will hold the job
-H hh:mm-H hold-H immediate-H restart-H
                                        indefinitely, and a UTC time value (HH:MM) will hold the job until the specified UTC (not local) time. Use a value of resume with the -i
resume
                                        option to resume a held job. Use a value of restart with the -i option to restart a completed job.
                                            Specifies which pages to print in the document. The list can contain a list of numbers and ranges (#-#) separated by commas (e.g. 1,3-
-P page-list                                5,16). The page numbers refer to the output pages and not the document's original pages - options like "number-up" can affect the
                                            numbering of the pages.
od
• od command in Linux is used to convert the content of input in different
  formats with the octal format as the default format. This command is especially
  useful when debugging Linux scripts for unwanted changes or characters. If
  multiple files are specified, the od command concatenates them in the listed
  order to form the input. It can display output in a variety of other formats,
  including hexadecimal, decimal, and ASCII. It is useful for visualizing data that is
  not in a human-readable format, like a program’s executable code.
• Syntax: od [OPTION]... [FILE]...
• -b Option: It displays the contents of input in octal format.
• -c Option: It displays the contents of input in character format.
• -An Option: It displays the contents of input in character format but with no
  offset
• -A Option: It displays input contents in different formats by concatenating
  some special character with -A.
tar
• The Linux ‘tar’ stands for tape archive, which is used to create Archive and extract the Archive files. tar command in Linux is
  one of the important commands which provides archiving functionality in Linux. We can use the Linux tar command to
  create compressed or uncompressed Archive files and also maintain and modify them.
• Syntax:
• tar [options] [archive-file] [file or directory to be archived]
• Options:
• -c: Creates Archive
• -x: Extract the archive
• -f: creates archive with the given filename
• -t: displays or lists files in the archived file
• -u: archives and adds to an existing archive file
• -v: Displays Verbose Information
• -A: Concatenates the archive files
• -z: zip, tells tar command that creates tar file using gzip
• -j: filter archive tar file using tbzip
• -W: Verify an archive file
• -r: update or add file or directory in an already existing .tar file
 gzip
• gzip command compresses files. Each single file is compressed into a single file. The
 compressed     file   consists   of   a   GNU     zip   header    and    deflated   data.
 If given a file as an argument, gzip compresses the file, adds a “.gz” suffix, and deletes
 the original file. With no arguments, gzip compresses the standard input and writes
 the compressed file to standard output.
File Permissions
Every file in Unix has the following attributes –
• Owner permissions − The owner's permissions determine what actions
  the owner of the file can perform on the file.
• Group permissions − The group's permissions determine what actions a
  user, who is a member of the group that a file belongs to, can perform
  on the file.
• Other (world) permissions − The permissions for others indicate what
  action all other users can perform on the file.
The Permission Indicators
• While using ls -l command, it displays various information related to file
  permission as follows :
• The permissions are broken into groups of threes, and each position in the
  group denotes a specific permission, in this order: read (r), write (w), execute
  (x) −
• The first three characters (2-4) represent the permissions for the file's owner.
  For example, -rwxr-xr-- represents that the owner has read (r), write (w) and
  execute (x) permission.
• The second group of three characters (5-7) consists of the permissions for the
  group to which the file belongs. For example, -rwxr-xr-- represents that the
  group has read (r) and execute (x) permission, but no write permission.
• The last group of three characters (8-10) represents the permissions for
  everyone else. For example, -rwxr-xr-- represents that there is read (r) only
  permission.
File Access Modes
• The permissions of a file are the first line of defense in the security of
  a Unix system. The basic building blocks of Unix permissions are the
  read, write, and execute permissions, which have been described
  below −
• Read Grants the capability to read, i.e., view the contents of the file.
• Write Grants the capability to modify, or remove the content of the
  file.
• Execute Users with execute permissions can run a file as a program.
Directory Access Modes
• Directory access modes are listed and organized in the same
  manner as any other file. There are a few differences that need to
  be mentioned:
• Read Access to a directory means that the user can read the
  contents. The user can look at the filenames inside the directory.
• Write Access means that the user can add or delete files from the
  directory.
• Execute Executing a directory doesn't really make sense, so think of
  this as a traverse permission. A user must have execute access to
  the bin directory in order to execute the ls or the cd command.
Changing Permissions
• To change the file or the directory permissions, you use the chmod
  (change mode) command. There are two ways to use chmod — the
  symbolic mode and the absolute mode.
• Using chmod in: The easiest way for a beginner to modify file or
  directory permissions is to use the symbolic mode. With symbolic
  permissions you can add, delete, or specify the permission set you
  want by using the operators in the following table. Symbolic Mode
Then each example chmod command from the preceding table is run on the testfile, followed by
ls –l, so you can see the permission changes –
Using chmod with Absolute mode
• The second way to modify permissions with the chmod command is to
  use a number to specify each set of permissions for the file. Each
  permission is assigned a value, as the following table shows, and the
  total of each set of permissions provides a number for that set
Changing Owners and Groups
While creating an account on Unix, it assigns a owner ID and a group ID to
each user. All the permissions mentioned above are also assigned based
on the Owner and the Groups.
Two commands are available to change the owner and the group of files −
chown − The chown command stands for "change owner" and is used to
change the owner of a file.
chgrp − The chgrp command stands for "change group" and is used to
change the group of a file
Write the commands for :
• Rahul wants to keep documentation of the work he does on the
  command prompt.
• Delete the password of the system.
• Seema wants to know the version of the kernel of the system she is
  working.
• Ram wants to know the users currently logged in
• Change the input terminal
• Count the number of lines in the file
• Print the file and send the message when the job is done.
• Compress the file