A variable is a character string to which we assign a value. The value assigned could be number, text, filename etc. A variable is nothin...
A variable is a character string to which we assign a value. The value assigned could be number, text, filename etc.
A variable is nothing more than a pointer to the actual data.
Variable Names:
The name of a variable can contain only letters (a-z,A-z), numbers (0 to 9) or underscore character(_)
For example below are the valid variable names
- name
- Token_A
- VAR1
Below are invalid variables
- 1Var
- Var-var1
Define and Access the variable
You can define the variable using
varname = variablevalue
for example
name="Raman"
To Read the variable at runtime
read name
To Access the name variable you need to use $ symbol before variable name
echo $name
Read-only Variables
If a variable is read-only then you cannot change the value of the variable
It will not allow to change the value of name variable.
Unsetting the variable
unset command is used to unset the variable value but you cannot unset a readonly variable.
Special Type of Variable in Shell Script
$0:- Name of the file
$n:- It corresponds to the respective command line argument
$#:- The number of supplied arguments to the script
$*:- All the arguments are double-quoted.
$@:- All the arguments are individually double-quoted.
$?:- The exit status of the last command executed.
$$:- The process number of the current shell
$!: The process number of the last background command.
Example
Execute the script
./test.sh "First Argurment" "Second Argument"
Output
File name = ./test.sh
First argument = First Argurment
Second argument = Second Argument
Number of arguments = 2
All the arguments = First Argurment Second Argument
Each argument = First Argurment Second Argument
Exit status of last command = 0
Process number of current shell = 3091
Process number of the last background command =
Example
To Print the grade of a student on the basis of command line argument
Execute
sh test.sh raman 90
Output
raman Got A Grade
Example
Comparing two strings
Execute
sh test.sh First First
Strings are equal
Assignment
- Write a program to find how many command-line argument are passed while executing the script if the number of passed arguments is more than 3 then print a message "Too many arguments are passed" otherwise print " You are good to go"
- Pass 2 numeric arguments to a program script on execution and find the greater number.
It evaluates the given expression and displays its corresponding output. It is mainly used for addition, subtraction, multiplication, division etc. It also does the regular expression for string operations like substring, length of the string etc
expr --version
expr --help
expr 12 + 10
expr 12 \* 3
Example
To find the sum of 2 numbers
Example
Find the total number of lines of 2 files and print sum of these total lines.
Example
Find the total number of lines of 2 files that are passed as command-line arguments and print the sum of these total lines.
Example
Write a program to find "unix" word in a file and if it is found then print it as a unix file otherwise print it is not a unix file ( using hardcode values for the file name and search char/ using command-line argument)
Example
Search the total number of characters in a file and also pass the code coverage percentage value if the total number of characters is less than code coverage then printing that code coverage is not possible.
Execute
sh count.sh myfile.txt 90
Number of characters = 47
Please go ahead with code coverage
----
COMMENTS