Sunteți pe pagina 1din 11

Shell Programming for Windows Users

April 15, 2015

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

How to use this book

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

Any unix version or a flavour of linux


Text editor (vim, emacs or anything)
Knowledge of any Imperative Programming Language such as C,C++,
or Java
Familarity with usage and terminology of Operating Systems

1.3

Shell Programming

Shell programming is equivalent to batch programming for Windows. Here


you write your desired commands for batch processing. But, you get a lot
more features, which can be used to automate various tasks.
The motivation for learning shell programming can be many.
Automating system administration tasks
Hacking (called as Shellcode)
Automating development process
1

CHAPTER 1. SHELL, GENTLE INTRODUCTION


Definition: Shell
A shell is a system software that allows the user to run the commands.
It can be used as a command line interface and also as a programming
language.

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

Executing a shell program

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

1.5. A SIMPLE SHELL PROGRAM

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

A simple shell program

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 echo " What is your name "


2 read A
3 echo " Hello \ $A "

Decipher the program


In the 1st line, echo is a command which is used to display the message
on screen. In the 2nd line, read , reads a input from user and stores it in
variable A. 3rd line outputs the message to screen. Mark, these quotations
have a very special meaning in shell scripting. We will learn about these in
later texts.
Command Info
echo: You can use the option -n to remove the newline character at
the end

CHAPTER 1. SHELL, GENTLE INTRODUCTION

1.6

Variables

The A is a variable. Variables in Shell Scripting has no data type associated


to it. Means, you can store any string or integer or floating point data in a
variable.
Command Info
To create a new variable use <variable name>=<value>. Mark the
spacing. If space is there, then shell interprets it differently
Linux provides a set of predefined variables. These variables are defined
when a user login. To show all predefined variables, try using the env command. This is equivanent to system variables in Windows systems.
Command Info
env: Displays the definition of variables
Exercise
Create a script named exec1 1.sh and do the following
Create a variable var and initialize it to a string and display
Assign a integer to the same var and display
Display the current shell. (Hint: Use the variable SHELL)
Display the current directory. (Hint: Use the variable PWD)
Add two numbers with the numbers being taken as user input.
(Hint: Use expr command)
Concatenation of two strings , with strings being taken as input.
(Example, for the input abc and def , the output should be
abcdef .)
Display the current user, current hostname and PATH variable
(Hint: Use the env command to get the corresponding
variable)
Hope you are done with exercises, specially the last one. By now you
should have got the idea to set and use the variables. On an end note, PATH
stores the variables which contains the executable files. Its is equivalent to
PATH in Windows systems. Explore PATH.

1.6. VARIABLES

1.6.1

Assigning variables by command substitution

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

CHAPTER 1. SHELL, GENTLE INTRODUCTION

1.6.3

Local Variables, Shell Variables and Environment


Variables

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.

Environemnt Variables are set by the operating system during login.


These variables are available to the shell and all child process of shell.
Exercise
For these exercise, first you need to unset the variables A and B
In a script ex2.sh, set A=5 and B=6. Save the script and run it
using ./ex2.sh
Display the variables A and B on terminal. What did you get?
Explain the reason
Now, run the script as . ./ex2.sh. Display the variables on terminal. What did you get? Explain the reason
(Hint: All these are related to child shell and parent shell)

1.7

Command Line Arguments-Positional Parameters

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. COMMAND LINE ARGUMENTS-POSITIONAL PARAMETERS 7

1.7.1

Special Variables associated with Positional Parameters


$# Number
of
arguments
passed to the script
$* All the arguments passed to
the script
$@ All the arguments passed to
the script
$0 Script name
$1, $2, . . . Corresponding
positional
paramters. First argument
is $1 , second is $2 and so
on.

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

More special Variables

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

S-ar putea să vă placă și