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

Shell Scripts

The document provides an introduction to shell scripts, explaining their purpose and how to create and execute them. It covers basic concepts such as executing commands, taking input, and using command line arguments with examples of simple scripts. Key features include variable evaluation, command substitution, and the use of special interpretive lines to specify the shell for execution.

Uploaded by

pika2233445566
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)
8 views13 pages

Shell Scripts

The document provides an introduction to shell scripts, explaining their purpose and how to create and execute them. It covers basic concepts such as executing commands, taking input, and using command line arguments with examples of simple scripts. Key features include variable evaluation, command substitution, and the use of special interpretive lines to specify the shell for execution.

Uploaded by

pika2233445566
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

Shell Scripts

Introduction
Aiusha V Hujon
Department of Computer Science
SAC
Shell Scripts
• When a group of commands have to be
executed regularly
– They should be stored in a file
– The file itself executed as a shell script or shell
program
– Not mandatory but .sh can be used
Shell Scripts cont…
• Shell scripts are executed in a separate child
shell process
• This sub shell need not be the same as your
login shell
• By default the child and the parent belong to
the same type
• However you can provide a special
interpretive line in the first line of the script to
specify a different shell for your script
First script
• It runs three echo commands
• The use of variable evaluation
• Command substitution
• Prints the calendar of the year 2010
• Comment character #
• Interpretive line begins with #! Followed by
the path name of the shell to be used for
running the script
First.sh
• #!/bin/sh
• #first.sh sample script
• echo "Today's date is `date`" #command substitution
• echo "This year’s calendar: "
• cal 2021
• echo "My shell is bash”
First script cont…
• first.sh
• To run the script make it executable first
• chmod +x first.sh
• Run the script as ./first.sh
Taking input
• read variablename
• Example
– A script that reads a pattern and a filename and
search whether the pattern exist in the file
– search1.sh
search1.sh
• #!/bin/sh
• #using read

• echo "Enter a file name "


• read fname
• echo "Enter a pattern to be searched "
• read pattern
• echo "Searching $pattern in $fname "
• grep "$pattern" $fname
• echo "End of program"
Taking Input cont...
• A single read statement can be used with one
or more variables
– If the number of arguments supplied < number of
variables accepting them
• Then leftover variables remain unassigned
– If the number of arguments supplied > number of
variables accepting them
• Then remaining words are assigned to the last variable
Using command line arguments
• When arguments are specified with a shell
script, they are assigned to certain special
variables called positional parameters
• The first argument is assigned to $1, the second
to $2 and so on
• $*- stores the complete set of positional
parameters as a single string
• $#- it is set to the number of arguments
specified
Using command line arguments cont...
• $0-holds the command name itself
• Example
– A script that uses command line arguments for a
pattern and a filename and search whether the
pattern exist in the file
– search2.sh
• Arguments can go up to $9
search2.sh
• #!/bin/sh

• echo "Program name is $0"


• echo "The number of arguments specified is
$#"
• echo "The arguments are $*"
• grep "$2" $1
• echo "Program over"
Using command line arguments cont...
• When a multiword string is used as an
argument you must quote it
• ./search2.sh emp.txt “assistant manager”

You might also like