Documente Academic
Documente Profesional
Documente Cultură
ii
Contents
1 Shell, Gentle Introduction
1.1 How to use this book . . . . . . . . . . . . . . . . . . . . . . .
1.2 Requirement . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.3 Shell Programming . . . . . . . . . . . . . . . . . . . . . . . .
1.4 Executing a shell program . . . . . . . . . . . . . . . . . . . .
1.5 A simple shell program . . . . . . . . . . . . . . . . . . . . . .
1.6 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.6.1 Assigning variables by command substitution . . . . .
1.6.2 Unsetting Variables . . . . . . . . . . . . . . . . . . . .
1.6.3 Local Variables, Shell Variables and Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.7 Command Line Arguments-Positional Parameters . . . . . . .
1.7.1 Special Variables associated with Positional Parameters
1.7.2 More special Variables . . . . . . . . . . . . . . . . . .
iii
1
1
1
1
2
3
4
5
5
6
6
7
7
iv
CONTENTS
Chapter 1
Shell, Gentle Introduction
1.1
This is primarily meant for a self-learning for people not exposed to linux. To
use this book I would suggest, Get hands on. As soon as you encounter
a command or a program, run it and analyze.
1.2
Requirement
1.3
Shell Programming
On linux and various GUIs for unix, it is invoked as terminal. So, you can
find terminal under the Applications menu and invoke a shell.
Tip
Whenever you feel lost about the options in a command then use
apropos or man <command name>.
There are various types of shell available on a system. Some of the more
standard shells are
sh Bourne Shell, developed by Stephen Bourne, while being a computer
scientist at Bell Labs
csh C shell. Developed by Bill Joy, while being a grad student at UC
Berkeley
tcsh Improved version of C shell
ksh Korn Shell. Developed by David Korn at Bell Labs. Default shell
in AIX, Solaris family of Operating Systems
bash Bourne Again Shell. Written by Brian Fox for GNU Project
1.4
A shell program can be written in an editor such as vim. Shell Programs are
interpreted by a shell.
Command Info
To run the script there are three ways
sh <script name
Give the execute permission to the file.
Use chmod 744
<script name> and run it as ./<script name
.
./<script name
First method is a pretty straight forward method. You explicitly call the
shell program and it executes it. In this method, the current process spawns
a child process and the script it executed in that child process. Hence, the
changes made to the environment will not be reflected in the current shell.
Second method is giving the permissions. The script is given execute
permission. For security, generally execute permissions are granted only to
the user. At times, its logical to give the execute permissions to the group.
In this method, the current shell, spwans a child shell and commands are
executed in that shell.
Third method, its runs the command in the current shell i.e. all the
changes made to the environment are reflected in the current shell. Of course,
it requires to grant execute permissions to the script.
1.5
Here I have mentioned a simple shell program, which asks the user for a input
and prints a message on screen. To write a shell program, open your editor
using vi or vim on terminal and then type the code.
1.6
Variables
1.6. VARIABLES
1.6.1
One feature that shell provides it that of saving the value of command to a
variable. This is done using the backquotes or a more familiar way with
$(command).
Command Info
DATE=`
date` or DATE=$(date)
Exercise
Open the file /etc/passwd using the cat command.
Using appropriate commands, assign a variable data to the value
of your username, home directory and default shell. (Hint: Use
the tr, cut, grep command, piping them together.)
Using appropriate commands, assign to variable dir all the directories in the current directory. (Hint: Use the ls, tr commands)
Display the value of variable data and dir
Tip
Use man pages to explore about these commands
1.6.2
Unsetting Variables
When we initialize a variable, its value is set to the current variable. Unsetting does the opposite. However, it is not frequently used in practice.
Command Info
To unset a variable
unset <variable name>
Exercise
Unset the variable cat and dir creater earlier
1.6.3
Local Variables are the variables that is present within the current instance of a shell.
Shell Variables are set by the shell. Some of the variables overlap with
Environment variables.
1.7
Mostly, scripts are required to pass the input parameters while calling. Command line arguments are pretty easy in Shell scripting. As usual, to call
a program with command line parameters, ./script name <parameter 1>
<parameter 2> ... is used. Parameter are separated by the space. If the
value of a parameter has a space then the whole parameter should be enclosed
withing double quotes.
1.7.1
Exercise
Write a script to print all the positional parameters that are passed to
the script.
Write a script printing all the special variables associated with positional parameters.
1.7.2
There are a few more special variables that are provided by Linux. These
are
? : Execution status of the last process
$: Current shell PID
!: Last background command PID
Exercise
Display these variables