Friday, March 13, 2020

Extended Shell Scripts

Introduction:


Shell scripts have several required constructs that tell the shell environment what to do and when to do it. Of course, most scripts are more complex than the above one.
The shell is, after all, a real programming language, complete with variables, control structures, and so forth. No matter how complicated a script gets, it is still just a list of commands executed sequentially.
The following script uses the read command which takes the input from the keyboard and assigns it as the value of the variable PERSON and finally prints it on STDOUT.
#!/bin/sh

# Author : Plexus Networks
# Script follows here:

echo "What is your name?"
read PERSON
echo "Hello, $PERSON"
Here is a sample run of the script −
$./test.sh
What is your name?
Zara Ali
Hello, Zara Ali
$

n this chapter, we will learn how to use Shell variables in Unix. A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data.
A variable is nothing more than a pointer to the actual data. The shell enables you to create, assign, and delete variables.

Variable Names

The name of a variable can contain only letters (a to z or A to Z), numbers ( 0 to 9) or the underscore character ( _).
By convention, Unix shell variables will have their names in UPPERCASE.
The following examples are valid variable names −
_ALI
TOKEN_A
VAR_1
VAR_2
Following are the examples of invalid variable names −
2_VAR
-VARIABLE
VAR1-VAR2
VAR_A!
The reason you cannot use other characters such as !*, or - is that these characters have a special meaning for the shell.


In this chapter, we will discuss in detail about special variable in Unix. In one of our previous chapters, we understood how to be careful when we use certain nonalphanumeric characters in variable names. This is because those characters are used in the names of special Unix variables. These variables are reserved for specific functions.
For example, the $ character represents the process ID number, or PID, of the current shell −
$echo $$
The above command writes the PID of the current shell −
29949
The following table shows a number of special variables that you can use in your shell scripts −
Sr.No.Variable & Description
1
$0
The filename of the current script.
2
$n
These variables correspond to the arguments with which a script was invoked. Here n is a positive decimal number corresponding to the position of an argument (the first argument is $1, the second argument is $2, and so on).
3
$#
The number of arguments supplied to a script.
4
$*
All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $1 $2.
5
$@
All the arguments are individually double quoted. If a script receives two arguments, $@ is equivalent to $1 $2.
6
$?
The exit status of the last command executed.
7
$$
The process number of the current shell. For shell scripts, this is the process ID under which they are executing.
8
$!
The process number of the last background command.

Command-Line Arguments

The command-line arguments $1, $2, $3, ...$9 are positional parameters, with $0 pointing to the actual command, program, shell script, or function and $1, $2, $3, ...$9 as the arguments to the command.
Following script uses various special variables related to the command line −
#!/bin/sh

echo "File Name: $0"
echo "First Parameter : $1"
echo "Second Parameter : $2"
echo "Quoted Values: $@"
echo "Quoted Values: $*"
echo "Total Number of Parameters : $#"
Here is a sample run for the above script −
$./test.sh Zara Ali
File Name : ./test.sh
First Parameter : Zara
Second Parameter : Ali
Quoted Values: Zara Ali
Quoted Values: Zara Ali
Total Number of Parameters : 2



No comments:

Post a Comment

Networking terms in basic level

Basics of Computer Networking Open system: A system which is connected to the network and is ready for communication. Closed system...