#!/usr/bin/bash
#command line arguments if the no of arguments are not matching the script will exit
path=$1
pattern=$2
echo "total number of arguments $#"
if [ ! "$#" = 2 ]
then
echo "Please provide the required arguments, No of arguments are not matching"
echo "Usage of this script is sh <<filename.sh>> arg1 arg2"
echo "Rerun the script with the required arguments\n Thank you for using this script"
exit 1
else
echo "Controller starts executing the script"
required_pattern=$(grep $pattern $path)
echo "$required_pattern"
fi
Output:
kalam@DESKTOP-OSJTLNE:~$ sh command_lin_arguments.sh
total number of arguments 0
Please provide the required argunments, No of arguments are not matching
Usage of this script is sh <<filename>> arg1 arg2
Rerun the script with the required arguments
Thank you for using this script
! --> Is Used to for negation of condition
$# --> It returns the total number of arguments
$1 -> Indicates 1st argument
$2 --> Indicates 2nd argument
No comments:
Post a Comment