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

UNIX Shell Programming Essentials

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)
14 views13 pages

UNIX Shell Programming Essentials

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

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 [Link], [Link], but not [Link]
$ 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 [Link]:

ls > [Link]

This will write the list of files in the current directory to [Link]. If [Link] 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 [Link]:
echo "hello world" >> [Link]

This will add the text "hello world" to the end of [Link]. 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 [Link] containing some text, and you want to count the number of
words in the file using the wc command:

wc -w < [Link]

wc -w command counts the words in [Link] 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
[Link]:
ls non_existent_directory 2> [Link]

Here 2 stands for standard error and any error message (e.g., "No such file or directory") will be
saved in [Link] 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.

Common questions

Powered by AI

Creation and use of aliases in Unix enhance shell customization and user-friendliness by allowing users to define shorthand symbols for frequently used commands or complex command sequences, significantly reducing typing effort and errors . For instance, a user can create an alias 'ls' for 'ls -al' to quickly execute detailed listings without typing full commands . Aliases enable personalization of the shell environment, tailoring it to specific workflows and simplifying repetitive tasks, resulting in a more efficient and pleasant user experience. Moreover, aliases can be set in the user's shell profile to ensure persistent customization across sessions, making the shell more adaptable to personal preferences and enhancing productivity.

In Unix, double quotes (" ") and single quotes (' ') serve to handle string data differently, affecting how variable expansions and command substitutions are processed. Double quotes allow for variable and command substitution, meaning that strings within double quotes can include variables or commands that the shell will interpret; for instance, the command $name="John" followed by $ echo "Hello, $name" outputs 'Hello, John' . Single quotes, however, enclose string literals, preventing any interpretation of variables or special characters; a command like $ echo 'This is $100' will output 'This is $100', treating the dollar sign as a literal character . Thus, use double quotes when you need variable expansion and single quotes when you want the string to be taken literally without any processing.

Unix's redirection features allow for flexible data handling by redirecting input and output streams, facilitating more versatile script execution. The '>' operator sends the output of a command to a file, overwriting its contents if it already exists, which is useful for saving results or logs from scripts; for example, $ls > list.txt saves the output of the 'ls' command to a file . The '<' operator redirects input from a source file rather than the terminal, allowing scripts to process data from files; for example, using $sh < sample executes commands from the file 'sample' . These features enhance data manipulation and storage, allowing scripts to interface efficiently with system files and other data sources without requiring interactive user input.

Aliases in Unix shells improve command efficiency by allowing users to create shortcuts for long or frequently used command sequences. This customization leads to faster and more intuitive command execution. For example, a user can create an alias 'P' for the command 'pwd', enabling them to quickly display their current directory simply by typing 'P' . Similarly, an alias 'l' could replace the command 'ls -al' to save time when listing directory contents with detailed information . By reducing the number of keystrokes required and simplifying complex commands, aliases enhance productivity and reduce the cognitive load on users when performing routine tasks.

Meta-characters in Unix, such as the asterisk (*) and question mark (?), allow for enhanced flexibility and power in command execution by enabling pattern matching and wildcard operations. The asterisk (*) is used to match any and all characters in file names, allowing users to perform operations on multiple files with similar naming patterns, like listing all files with a .txt extension using the command $ls *.txt . The question mark (?) serves a similar purpose but matches only a single character, such as listing files with a specific single-character pattern: $ls file?.txt, which matches files like file1.txt but not file10.txt . These meta-characters thus enable dynamic file operations and automate tasks that would otherwise be cumbersome to perform individually, enhancing the efficiency of shell commands.

The pipe ('|') operator in Unix offers significant advantages in process communication and performance by connecting the output of one command directly as input to another, facilitating seamless data flow between processes . This allows for composite command sequences and real-time data processing without the need for intermediate files, reducing I/O overhead and improving efficiency. For instance, a command like $ ls -l | grep "Oct" passes the detailed list of files to the grep command, which filters results containing 'Oct'. Such pipelines are integral for handling large data sets and chaining commands to create complex workflows, leveraging Unix’s capability to handle multiple commands efficiently in a single command line. This greatly enhances productivity by enabling modular command structure and minimizes the resources required to store and forward command outputs, promoting efficient memory and CPU usage.

Process substitution using '$()' and exit status variable '$?' are powerful scripting tools in Unix that enhance program flow control and error handling. The '$()' syntax allows for command substitution, where the output of a command can be embedded within another command, providing dynamic input without writing to intermediate files. For example, $echo "Current time: $(date)" embeds the current date command output directly into the echo statement . Meanwhile, the '$?' variable retrieves the exit status of the last executed command, which is crucial for error handling in scripts. By checking '$?', a script can determine whether a previous command was successful (exit status 0) or failed (non-zero exit status) and branch execution accordingly. This combination allows scripts to dynamically manage data and handle errors gracefully, improving robustness and adjusting program flow based on command outcomes.

Special variables in Unix, such as '$0', '$1', and '$#', are essential for managing command line arguments and controlling script execution. The variable '$0' represents the name of the script or command being executed, allowing scripts to be aware of their invocation context . Variables '$1' to '$9' correspond to the first to the ninth arguments passed to the script, enabling scripts to access and manipulate specific inputs. For scripts requiring more arguments, '${10}' and beyond can be used to reference higher argument numbers. The '$#' variable provides the total number of arguments passed, allowing scripts to perform checks and validate input length before processing, ensuring appropriate argument handling. These variables enable flexible script design by allowing easy access to input data and dynamic argument management, essential for creating robust, reusable scripts that adapt to varying execution contexts.

File descriptors in Unix are integral to redirection, as they define how input/output streams are handled by the shell. Each command executed in the Unix shell is associated with three standard file descriptors: 0 (STDIN), 1 (STDOUT), and 2 (STDERR), representing standard input, output, and error streams, respectively . These descriptors allow Unix to treat everything as a file, creating a unified way to manage data streams. The '>' operator is used for output redirection, typically modifying file descriptor 1 (STDOUT) by sending command output to a specified file, replacing its contents unless appended with '>>'. The '<' operator redirects input by taking data from a specified file into file descriptor 0 (STDIN). This implicit association allows these commands to efficiently manage data flows in scripts and shell interactions by rerouting inputs and outputs without needing explicit descriptor references, enhancing usability and flexibility .

Advanced command chaining operators '&&' and '||' in Unix scripts provide enhanced control flow and efficiency by determining command execution based on preceding command success or failure. The '&&' operator executes the subsequent command only if the previous command is successful (exit status 0), allowing for conditional execution that can prevent unnecessary operations. For instance, $ mkdir new_folder && cd new_folder only changes the directory if the creation of 'new_folder' succeeds . On the other hand, the '||' operator executes the next command only if the preceding command fails (non-zero exit status), useful for executing fallback actions or error handling, such as $ mkdir existing_folder || echo "Folder exists", which prints an error message if the folder creation fails . These operators enable more robust scripting by ensuring commands are executed conditionally based on previous outcomes, reducing errors and optimizing resource use.

You might also like