0% found this document useful (0 votes)
203 views

Shell Programming - Part 1: I. Schell Script Overview

This document provides an overview of shell programming and variables in shell scripts. It discusses shell scripts having two modes: interactive and non-interactive. Non-interactive scripts allow grouping instructions into files called shell scripts. Shell scripts include instructions, variables, and control structures. They can be executed by calling the sh command with the script filename and arguments, or directly if the file has execute permissions. The document also covers user-defined and predefined variables in shell scripts, including defining, concatenating, and assigning the output of commands to variables. It lists some predefined variables and variables set by the shell during script execution like $0-9, $*, and $#.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
203 views

Shell Programming - Part 1: I. Schell Script Overview

This document provides an overview of shell programming and variables in shell scripts. It discusses shell scripts having two modes: interactive and non-interactive. Non-interactive scripts allow grouping instructions into files called shell scripts. Shell scripts include instructions, variables, and control structures. They can be executed by calling the sh command with the script filename and arguments, or directly if the file has execute permissions. The document also covers user-defined and predefined variables in shell scripts, including defining, concatenating, and assigning the output of commands to variables. It lists some predefined variables and variables set by the shell during script execution like $0-9, $*, and $#.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Shell Programming – Part 1

I. Schell Script Overview


The shell is the programming layer that understands and executes the commands entered by a
user. In some systems such as Windows, the shell is called a command interpreter.

The linux shell (/bin/sh) works in an interactive mode and non-interactive mode.

1. Interactive Mode:

Shell is the interface between the user and the Unix System.

The instructions executed by the shell come from the terminal.

Unix Instructions /bin/sh

2. Non-Interactive Mode:

In this mode, the user is able to group the instructions in files called Shell scripts. In such way,
the user can define his own instructions

Unix Instructions /bin/sh


Shell Script

1|Page
2.1.Shell script components

A shell scripts includes:

 The different instructions used in interactive mode.


 Variables
 Control structures (conditions, loops,..)
2.2.Shell script execution

To execute a script file, there is two possible ways:

 If the file containing the script does not have the “execute” right:

sh <filename> [<argument> …]

The sh instruction will execute the set of instructions included in the file.

<argument>: the list of inputs which are necessary to the execution of the script

 If the file containing the script has the execution right

./<filename> [<argument> …]

In this case, we don‟t need to call the sh instruction. The file is already executable and it is
considered as a new UNIX instruction.

Example :

2|Page
II. Variables in Shell

A shell variable is identified by its name. Shell manages only alphanumeric variables.

There are two kinds of variables:

 User variables
 Predefined Variables
1. User Variables

To call a user variable, we have to preced its name by the „$‟ character.

1.1.User variable definition

To define and to attribute a value to a variable, the following syntax is used:

<variable_name>=<variable_value>

Example:

Note: echo instruction is used to display a text or a value of a variable.

1.2.User variables concatenation

To concatenate two user variables in UNIX, we have to call them one after the other

Example:

3|Page
1.3.Associate a UNIX instruction to a user variable

<variable_name>=`<instruction>`

According to this syntax, the instruction is first executed and then its result will be stored in the
variable value.

Example:

2. Predefined variables
The predefined variables are the variables defined by the Linux OS, such as:

 HOME: the home directory


 TERM: the terminal type
 LOGNAME: the user name
 PATH: the list of directories
 PS1: prompt (the charchter $ or #)
 PS2: the second prompt (the charchter >)

To know the other predefined variables, type the env instruction.

Try the env instruction in your computer. What is your logname? .................................................

4|Page
3. Variables affected by the shell during the script execution
$0 Name of the running procedure
$1…$9 Value of the 1st argument….Value of the 9th argument
$* List of arguments
$# Number of arguments

Example:

Create a new file called “tryvar” and put the following instructions on it:

5|Page

You might also like