Table of contents
No headings in the article.
Special variables
$$ – This represents the process ID number (PID) of the current shell.
$0 – This variable represents the file name of the current script.$0 --it shows shell script name - dbbackup.sh
$# – This variable represents the number of arguments that are provided to a script.
$* -- all the args - it will give all the args as one string
$@ -- all the args - it will give each arg as a one one-string
$? -- previous command execution status
$! – This variable represents the process number of the last background command
command line arguments
during shell script execution, values passing through the command prompt are called command line arguments
for ex: while running a shell script we can specify the command line arguments as a sh script file.sh arg1 arg2 arg3"
important points while using CLA
we can specify n number of arguments, there is no limitation
each argument is separated by space
#!/bin/sh
echo “Script name: $0”
echo “My First favorite fruit: $1”
echo “My Second favorite fruit: $2”
echo “Total number of fruits: $#”
output
My first favorite fruit: Pineapple
My second favorite fruit: Mango
Total Number of fruit:2
-eq = equal =
-ne =not equal to
-lt = less then
-le = less than or equal to
-gt =greater then
-ge = greater than or equal to
output
We can use the symbols of the equation
-eq...... ==
-le....... < =
-lt........ <
-ge...... > =
-gt...... >
-ne...... !=
Array
- It is a sequential memory location
- if you store the variable in the array, we get what we want to display names
An array variable is capable of holding multiple values at the same time
array_name = (value1, value2 … valuen)
After creating the array variable, we can access the same in the following way –
${array_name[index]}
array_name represents the name of the array and index represents the index of the value
#!/bin/sh
FRUIT[0] = “Pineapple”
FRUIT[1] = “Mango”
FRUIT[2] = “Apple”
FRUIT[3] = “Grapes”
FRUIT[4] = “Banana”
echo “All the fruits present in the array are : ${FRUIT[*]}”
output: All the fruits present in the array are: Pineapple Mango Apple Grapes Banana
strings
zero or more characters enclosed in a single or double quotes
example : name = "kusuma" ,' Suma'
name is a variable
kusuma , Suma is a string
echo $string_var
or echo $(string_var)
----- it is going to display the value of the string value
echo ${#string_var}
---- it will give the length of the variable
echo ${string_var}
Kusuma l, working in an organization, Infosys ------it is going to show value only after the 20 words i.e it counts even the space and all explanatory marks
echo ${string_var:20:14}
Kusuma ----- the first number is for how many characters we have to leave and the second number is how many to display
echo ${string_var: -8}
organization
vi string.sh
output
Arithmetic operations
expr 2 + 3
expr 2 - 3
expr 2 * 3
expr 2 % 3
expr 2 / 3
vi aropr.sh
note: #must give space between expressions like 2 <space> + <space> 3 otherwise it will not going to execute
output
here we use \* operation will be executed = 6
echo "the edition of two and three is:" `expr 2 + 3
o/p - the edition of two and three is : 5
read command
it will make scripts interactive mode
write a shell script, accept the user name for the user, and display it back to the user
echo "Please enter your name"
read username
echo" the name which you entered is:" $username
vi read. sh
output