0% found this document useful (0 votes)
7 views34 pages

Creating and Running Shell Scripts

This document provides an overview of creating and running shell scripts in a Linux environment, including setting execute permissions and using various methods to assign values to variables. It explains the use of environment and shell variables, exit status codes, and different types of loops (while, until, for) for controlling script execution. Additionally, it includes a case project for writing a menu-driven shell script that allows users to perform various file operations.

Uploaded by

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

Creating and Running Shell Scripts

This document provides an overview of creating and running shell scripts in a Linux environment, including setting execute permissions and using various methods to assign values to variables. It explains the use of environment and shell variables, exit status codes, and different types of loops (while, until, for) for controlling script execution. Additionally, it includes a case project for writing a menu-driven shell script that allows users to perform various file operations.

Uploaded by

ghostpiyanki
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Unit 4 Shells and

Scripting
T H E 3 C O R E C O M P O N E N TS O
F A B A S H SC R IPT
Creating Shell Scripts

• By default, files can’t be executed; not even root can execute a file unless
the file owner adds execute permission manually
• After creating a shell script and assigning execute permission, you must
enter the absolute or relative path to where it’s stored to run it.
• Objective: Create and run a shell script.
• Description: In this activity, you use the vim editor to create a shell script,
assign the necessary permission, and then run the script.
Activity
Activity…
Variables

• An environment variable is a placeholder for data that can change; typically, it


gets its value automatically from the OS startup or the shell being used.
• Linux is a multiuser OS, so more than one user can log in at the same time. For
this reason, each user has environment variables with different values to
define his or her working environment.
• For instance, the HOME environment variable stores the absolute pathname
to a user’s home directory, so it varies for each user.
• Other environment variables are the same for all users logged in to a machine,
such as the HOST environment variable that specifies the computer name
• The env command is used to display a list of all environment variables and
their stored values.
Environment variables
A shell variable

is similar to an environment variable, but its value is usually


assigned in a shell script.
These variables are related to a particular script, not necessarily
the global environment, as environment variables are.
Variables are also important for specifying how shell scripts run.
For instance, you can create a script that greets users by name.
There are three ways to store information in a variable: direct
assignment, the prompt method, and positional parameter
Using the Direct Assignment Method

1. Open a terminal window, type cd scripts, and press Enter.


2. Type vim scr2 and press Enter to open a new empty file in the vim
editor.
3. Enter extended mode and display line numbers.
Using the Direct Assignment Method…
Options for the find command
The Prompt Method
With the prompt method, the user is asked to enter a value for the
variable.
Positional Parameters

• The positional parameter method uses the order of arguments in a


command to assign values to variables on the command line.
• Variables from $0 to $9 are available, and their values are defined by what
the user enters.
• This method is useful in a script when you might want it to run differently
each time it’s used.
• When you issue the command to run a script, for example, its name as
well as all arguments following it are stored in positional parameters.
• For instance, to run the scr1 script, you use this command: ./scr1 The
filename is considered position 0 in the command, and the text ./scr1
becomes the value of the $0 variable
Example
Positional parameters…
More on positional parameter

1. Type vim scr4 and press Enter to open a new empty file in the vim
editor.
Exit Status Codes

• When you quit a program or a command, a numeric code called an exit


status code is sent to the shell.
• These codes differ, depending on the Linux distribution or the shell.
However, successful commands usually return the code 0, and failures
return a value greater than 0.
• The code isn’t actually displayed onscreen, but you can reference it via a
script or at the command line with the $? variable, as shown in the
following example:
Conditions
If condition example
File attribute operators in the BASH shell
Menu Scripts
Rewrite the above script with elif
The case Statement
• syntax
Activity: Using case Statements in a Menu Script
Looping

 while statement—A loop statement that tests a condition in much the


same way as if-then statements. The interpreter continues executing the
code in the while loop portion of the script as long as the condition is true.
until statement—A loop statement that causes the interpreter to continue
executing the code in the until loop portion of the script as long as the
condition is false.
for statement—A loop statement that specifies the number of times to
execute the portion of code.
do statement—A loop statement indicating the beginning of the code to be
repeated.
 done statement—A loop statement indicating the end of the code to be
repeated.
The while Loop
• A while loop repeats the commands between the do and done statements
as long as the tested condition is true (exit status code 0).
• When the command after the while statement returns an exit status code
greater than 0, the while statement fails, and the program begins executing
the commands after the done statement
Activity : Creating a while Loop
1. Next, to create a file used for testing the condition, type touch testfile and
press Enter.
2. Leave this terminal window open, open a second terminal window, and
switch to the scripts directory. To open a new empty file, type vim scr8 in the
second terminal window and press Enter.
3 Enter extended mode and display line numbers.
4. Switch to insert mode and type the following information, pressing Enter
after each line:
The until Loop
• An until loop repeats the commands
between do and done as long as the
tested condition is false (exit status code
is greater than 0)—in other words, until
the condition is true.
• When the command following the until
statement has the exit status code 0, the
until loop fails, and the program begins
executing the commands after the done
statement.
Example until loop
Activity : Creating an until Loop in a Menu Script
• Objective: Create a menu script that continues running until the user decides
to exit.
The for Loop
• A for loop repeats the commands between do and done a specified number
of times.
• Each time the script carries out the commands in the loop, a new value is
given to a variable.
• You can assign this value in the command with positional parameters, such
as listing the calendar months in a year:
Activity : Creating a for Loop
Summary
■ Shell scripts can be run only after the user has been given execute
permission for them.
■ Values are assigned to variables by direct assignment, positional parameters,
or the prompt method
■ Condition statements are used to run specified portions of a script matching
the condition.
■ An exit status code follows every command execution; for successful
commands, the exit status code is 0.
■ While loops repeat commands as long as a condition remains true. Until
loops repeat commands as long as a condition remains false. For loops repeat
commands a specified number of times.
Case Project

Writing a Shell Script Write a shell script that displays the following menu
items. Make sure users can continue running the script until they want to exit.

1. Display a list of files and directories in their current directory


2. Display a long list of files and directories in their current directory
3. Display the last 10 lines of the log file messages (/var/log/messages)
4. Display the contents of this script
5. Display the contents of this script in reverse order 6. Exit the program

You might also like