0% found this document useful (0 votes)
21 views13 pages

Unit 3 Part-1

Uploaded by

Hemanth Babu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views13 pages

Unit 3 Part-1

Uploaded by

Hemanth Babu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

UNIX AND SHELL PROGRAMMING

UNIT - III

Syllabus: Using the Shell-Command Line Structure-Met characters-Creating New


Commands-Command Arguments and Parameters-Program Output as Arguments-Shell
Variables- -More on I/O Redirection-Looping in Shell Programs.

1) Shell
The shell is a command interpreter. It is the layer between the operating system and kernel and
the user. The role of a shell is to interact with a system by entering command, executing
programs, and performing various tasks (file manipulation, process management, and system
administration).

2) Types of shell in unix


● Bourne shell (sh) : foundation for many other shells.
● C shell (csh) : resembles C programming language
● Korn shell (ksh) : Combine features of bourne and C shell, and offers better scripting
capabilities.
● Bash (Bourne again shell) : Most commonly used in modern unix and linux systems and
it is an improved version of Bourne shell.

3) Shell as an interface
The shell is a command line interface, it allows users to interact with the operating system. It
interprets and executes commands, it acts as an intermediary between the user and the underlying
system's kernel.
The shell provides two main modes for interaction:
Interactive Mode:
In interactive mode the shell reads and executes commands from the user one at a time,
providing immediate feedback. This is typically used when you open a terminal and interact
directly with the shell, such as typing commands.
Non-interactive Mode (Shell Scripting):
In non-interactive mode, commands are written into a file (called a script), and the shell executes
them without user interaction. It is useful for automating tasks, running scheduled jobs, or
managing large-scale operations that require the execution of multiple commands.

4) Shell features
The shell offers a variety of powerful features that make interacting with the operating system
efficient and flexible. Here are some of the key features:
Command Execution:
The primary function of the shell is to allow users to execute commands. These can be simple
tasks like listing files, creating directories, or more complex tasks like running programs.
Scripting:
Shell scripting refers to writing a series of commands in a file (called a script), which can be
executed as a single unit. Scripts automate repetitive tasks and make system management easier.
Input/Output Redirection:
Input/output (I/O) redirection allows users to control where the shell reads input from or sends
output to. Instead of reading input from the keyboard or sending output to the screen, redirection
allows input/output to be handled by files, other commands, or devices.
Pipes (|):
Pipes are used to pass the output of one command as the input to another command. This allows
for chaining commands and processing data in a step-by-step manner.

5) Command line structure


A command is a program that tells the Unix system to perform a function. The command-line
structure follows a specific syntax, which allows users to interact with the operating system
efficiently by typing commands. Understanding this structure is key to using the shell effectively.
command [options] [arguments]
Command : The first part of the command line is the command itself, which is the name of the
executable or built-in command that you want the shell to run. commands are case sensitive.
Options : Options modify the behavior of the command. They usually begin with a hyphen (-)
for single-letter options. An option modifies the command, changing the way it performs. More
than one option can be strung together.

Arguments: Arguments specify the target or the data on which the command operates. This can
be files, directories, or any other objects the command needs to work with. Where an argument
indicates what the command is to perform its action, usually a file or series of files.

Example:
Syntax: command -[option] [option] [option]

$ls -alR

- will perform a long list on all files in the current directory and recursively perform the list
through all sub-directories.
- for most commands you can separate the options, preceding each with a hyphen

Syntax: command -[option1] -[option2] -[option3]

$ls -a -l -R

- Options and syntax for a command are listed in the man page for the command.
6) UNIX METACHARACTERS (SPECIAL CHARACTERS)
● These are also called special characters or wildcard characters.
● Special characters have a special meaning to the shell.
● Meta-characters in Unix are special characters that have a unique meaning.
● They are used to perform various functions such as pattern matching input/output
redirection, and command chaining.
● The purpose of these characters help in making command execution flexible, dynamix
and more powerful by enabling operations such as file expansion, command substitution,
and automation.
● The most commonly used metacharacters in UNIX

a) Asterisk ( * ) : The * ( asterisk) meta character is used to match any and all characters.
Example:
- List all files in the working directory that begin with the letter s regardless of what
characters comes after it
- The * (asterisk) meta character can be used anywhere in the filename.
- It does not necessarily have to be the last character.
$ ls *.txt
list all files having .txt
$ ls file*
lists all files that start with the name file
$ ls *file
list all files that end with the name file
b) Question mark ( ? ): The question mark meta character is used to match a single
character in a file name.
Example:
- List all files in the working directory that has a single character, or two characters
or three characters
$ls ?
$ls ??
$ls ???
- List all files in the working directory that has three characters starting with k.
$ls k??
- Like the asterisk, the ? (question mark) meta character can be used as a substitution for
any character in the filename.
$ ls file?.txt
- list files like file1.txt, fileA.txt, but not file10.txt
$ ls file?
- lists all files that have one character after file
$ ls ??file
- list all files that have two characters before file
c) Brackets [ ] :
- Brackets ( [....] ) are used to match a set of specified characters.
- A comma separates each character within the set.
Example:
List all files beginning with “ a “, “ b “, or “ c “.
$ ls [a,b,c]*
- It is also represented as,
$ ls [abc]* # without giving comma separator
- list all files that have a single digit after ‘file’
$ ls file [0-9]
- list all files that have any one letter before ‘file’
$ ls [a-z]file
- [^ ] matches any character except those inside the brackets. List all files that have digits
after file’. file1, file2, file3, file33
$ ls file [^0-9]
d) Hyphen - :
- Using the - (hyphen) metacharacter with [ ] (brackets) is used to match a specified range
of characters.
Example:
- List all files beginning with a lowercase letter of the alphabet
$ ls [a-z]*
- If there are directory names in the specified range. their name and contents will
also be displayed.
e) Pipe ( | ) : The pipe character takes the output of one command and uses it as the input
for another command.
$ ls -l | grep "Oct"
This lists files with details and pipes the result to grep, which searches for the string "Oct".
f) && (Logical AND) : Executes the second command only if the first command succeeds
(returns 0).
$ mkdir new_folder && cd new_folder
If mkdir new_folder succeeds, then cd new_folder is executed.
g) || (Logical OR) : Executes the second command only if the first command fails (non-zero
exit status).
$ mkdir existing_folder || echo "Folder exists"
If mkdir existing_folder fails, the message "Folder exists" is printed.
h) ; (Command Separator) : Allows you to run multiple commands sequentially,
regardless of whether the previous command succeeds or fails.
$ echo "First command"; echo "Second command"
Both commands will be executed one after the other.
i) ' ' (Single Quotes) : Encloses a string literally, preventing any expansion or interpretation
of special characters within it.
$ echo 'This is $100'
The output will be This is $100.
j) " " (Double Quotes) : Encloses a string but allows variable and command substitution.
$ name="John" $ echo "Hello, $name"
The output will be Hello, John.
k) \\ (Backslash) : Used to escape special characters, treating them literally.
$ echo "This is a dollar sign: \$"
The output will be This is a dollar sign: $.
l) & (Background Process) : Sends a command to run in the background, freeing the
terminal for further input.
$ long_running_command &
Special Meta-Characters for Process Substitution
m) $() (Substitution) : Command substitution replaces a command with its output.
$ echo "Current time: $(date)"
n) $? (Exit Status) : Retrieves the exit status of the last executed command.
$ echo $?

6.1 Redirection metacharacters ( I/O redirection )

Standard streams: UNIX defines three standard streams (files) that are used by
commands. Stream - is simply a sequence of byte
● Standard input : The stream representing input, which is connected to the
keyboard.
● Standard output : The stream representing output, which is connected to the
display.
● Standard error : The stream representing the error message that produced from
shell,. this is also connected to display.
Unix assigns a file descriptor to each stream
● Standard input STDIN 0
● Standard output STDOUT 1
● Standard error STDERR 2
- Unix treats everything as a file, Regular file, Directory and even Device files.
- Whenever we execute a program/command at the terminal, 3 files will open (std input,
std output, std error)
- Redirection - changing the standard input / output devices while executing a command.
- It is possible to change the source from where the input is taken by a program as well as
the destination to where the output is sent by a program.
- This mechanism of changing the input source and/or destination is called redirection.
- The UNIX redirection operators are
● Standard output redirection ( > )
● Standard output redirection with appending (>>)
● Standard input redirection ( < )
● Error redirection ( > )

- The input source is redirected using the < (less than) operator.
- The output destination is redirected using the > (greater than) or >> (double greater than)
operators.
- The file descriptors 0 and 1 are implicitly prefixed to the redirection operators < and >
respectively by the shell.
- The output of a program can be redirected using either > or >> operator.
- when > is used, the destination files are overwritten.
- when >> is used, the present output will be appended to an existing file.
- In either case if the destination file does not exist, it is created.

Standard output redirection (>)

It allows you to send the output of a command to a file, overwriting its contents if the file already
exists.
Example: If you want to save the output of the ls command to a file named list.txt:

ls > list.txt

This will write the list of files in the current directory to list.txt. If list.txt already
exists, its content will be replaced by the new output.

Standard output redirection with appending (>>):


It allows the user to send the output of a command to a file, adding (appending) the output at the
end of the file without overwriting its existing contents. If you want to append the output of the
echo command to an existing file output.txt:
echo "hello world" >> output.txt

This will add the text "hello world" to the end of output.txt. If the file doesn't exist, it will
be created.

Standard input redirection:

cat and wc commands


Standard input redirection allows a program to take input from a file instead of the keyboard. By
default, programs read input from the keyboard (stdin). With input redirection, you can specify a
file from which the program reads.

Example:
If you have a file input.txt containing some text, and you want to count the number of
words in the file using the wc command:

wc -w < input.txt

wc -w command counts the words in input.txt by reading from it instead of waiting for
keyboard input.

Error redirection ( > )

Unix allows you to redirect error messages (stderr) to a file. By default, errors are displayed on
the terminal but with error redirection the error can be stored in a file.

Example: If you try to list a non-existent directory and want to capture the error in a file
error.txt:
ls non_existent_directory 2> error.txt

Here 2 stands for standard error and any error message (e.g., "No such file or directory") will be
saved in error.txt instead of being printed on the terminal.

Examples of I/O redirection:


- Creating a new file called sample1 using >
$cat > sample1
Hi Hello
- By using the ctrl+d command, we come out from the cat editor.
- Displaying contents of sample1 using <
$cat < sample1
Hi Hello

- Appending the sample1 file contents using >>


$cat >> sample1
We are CSE

- Displaying modified contents of sample1 using <


$cat < sample1
Hi Hello
We are CSE

- Redirection the contents of sample1 to another file called sample2


$cat < sample1 > sample2
$cat < sample2
Hi Hello
We are CSE
7. CREATING NEW COMMANDS:
Given a sequence of commands that is to be repeated more than a few times. it would be
convenient to make it into a new command with its own name.
- suppose you intend to count the number of users frequently with the pipeline
$ who | wc -l
- and you want to make a new program ‘nu’ to do that. the first step is to create an
ordinary file contains ‘ who | wc -l ‘
syntax: $ echo ‘who | wc -l’ > nu
- So run the shell with its input coming from the file ‘ nu ‘ instead of the terminal:
$ who
you tty2 sep 28 7:51
rhh tty4 sep 28 10:02
ma tty5 sep 28 9:38
ava tty6 sep 28 10:17
$ cat nu
who | wc -l
$ sh < nu
4

Example (1) :
$pwd
/home/501
$ echo ‘pwd’ > sample #Redirecting pwd command to a file called sample
$cat sample
pwd
$sh < sample #Executing sample fie like pwd command using sh command
/home/501
Example (2) : (As an internal command)
- To count number of users with pipeline frequently, the command used is
$ who | wc -l
3
- An ordinary file can be created to contain ‘who | wc -l’
$echo ‘who | wc -l’ > sample2
- The program input can be redirected to a file rather than the terminal.
$ who
501 dev/pts/0 jul 13 10:20
502 dev/pts/1 jul 13 10:30
503 dev/pts/2 jul 13 10:40
$ cat sample2
$who | wc -l
$sh < sample2
3
- The output is the same as the initial command.
Example (3) : (As an external command)
- To count number of users with pipeline frequently, the command used is
$ who | wc -l
3
- an ordinary file can be created to contain ‘who | wc -l’
$ echo ‘who | wc -l’ > sample2
- the program input can be redirected to a file rather than the terminal
$ who
501 dev/pts/0 jul 13 10:20
502 dev/pts/1 jul 13 10:30
503 dev/pts/2 jul 13 10:40
$ cat sample2
$who | wc -l

$mkdir bin #Creating bin directory in current directory

$echo $PATH #setting path using the $PATH system variable


/home/501/bin

$mv sample2 bin #Moving sample file to bin directory

$cd bin

bin] $sample2
permission denied #Because there is no execution permission to the User/Owner

bin] $chmod u+x sample2 #Giving execute permission to user using chmod command

bin] $sample2 #Executing sample command directly like who | wc -l


3

7.1 CREATING (alias) NEW COMMANDS IN SHELL :


- Alias instructs the shell to replace one string with another when one writes commands.
- Aliases are used to customize the shell session interface using alias, frequently used
commands can be invoked using a different preferred term and complex or commonly
used options can be used as the default for a given command.
syntax: alias [ name = [ ‘command’ ] ]
options: alias [ -p ] [ name = “ value “ ]
Examples: Alias P could be created for the commonly used “pwd” command which shows the
current location of the user in the directory structure by typing the following command.
$ alias P = “pwd”
and then just by typing “ P “ will indicates /home/vignan
- An alias names ls could be created for the command “ls-al” as follows
$ alias ls = “ ls - al “ ( or )
$ alias ls = ‘ls -al‘
It is a commonly used command that by default lists the names of the files and directories within
the current directory.
- This can also be written by using a single character instead of two characters for the alias
name
$ alias l = ‘ls - al‘
- To have the ls alias always display the content of the /etc directory, it could be rewritten
as.
$ alias l = ‘ls -al/etc’
- The alias pl could be created to first launch pwd, and then immediately launch ls;
$ alias Pl = ‘pwd ; ls’
- Creating two separate aliases simultaneously by using separate command
$ alias P = “pwd” ; l = “ls - al”
- Create an alias to clear the screen
$ alias cls = ‘clear’
use the alias $ cls
clear the screen
- To create an alias ‘ls’ to change the default action of ls
$ alias ls = ‘ls - - a’
use the alias
$ ls
- To create various alias using cd command to go into sub-sub directories
$ alias .. = ‘cd..’
$ alias … = ‘cd ../..’
Unalias syntax: Remove each name from the list of defied aliases
unalias [ -a ] name [name….]
Specifies the name of the alias that you want to remove
Example: unalias home

8. COMMAND ARGUMENTS AND PARAMETERS:

- The shell programs will interpret the arguments such as filenames and options while
running the program.

- command line arguments ( also known as positional parameters ) are the arguments
specified at the command prompt with a command or script to be executed.

- The locations at the command prompt of the arguments as well as the location of the
command, or the script itself are stored in corresponding variables. These variables are
special shell variables.

Variable Description

$0 Represent the command or script

$1 to $9 Represents argument 1 through 9

$ { 10 } and so on Represent argument 10 and further

$# Represent the total number of arguments

$* Represents all arguments

$$ Represent the pid of a running script


Example:
$ cx sample
This command is short form for
$ chmod +x sample
The contents of cx are
chmod, +x and sample
When a shell executes file containing commands, every occurrence of $1 will be replaced by first
argument and $2 will be replaced by second argument and so on.
- let cx contain
$ chmod +x S1
- If the below command is run
$ cx sample
Here subshell will replace the $1 with the first argument, “sample”.
Shell can handle even multiple arguments.
$ chmod + x $1 $2 $3 $4 $5 $6 $7 $8 $9
A shorthand notation for this would be
$ chmod + x $*
The argument $0 represents the command to be executed.

You might also like