Search This Blog

Saturday, September 30, 2023

Shell script 6

Question:

Grep the multiple patterns in one shot and print the lines which matches from the file. Include if the no of arguments are not matching to the total no of arguments

Answer: 

 #!/usr/bin/bash

#command line arguments if the no of arguments are not matching the script will exit

path=$1

pattern1=$2

pattern2=$3

echo "total number of arguments $#"

if [ ! "$#" = 3 ]

then

        echo "Please provide the required argunments, No of arguments are not matching"

        echo "Usage of this script is sh <<filename.sh>> arg1 arg2 arg3"

        echo "Rerun the script with the required arguments\nThank you for using this script"

        exit 1

else

        echo "Controller starts executing the script"

        required_pattern=$(grep -E "$pattern|$pattern2" $path)

        echo "$required_pattern"

fi


Output:

kalam@DESKTOP-OSJTLNE:~$ sh command_lin_arguments.sh filename.sh name Telugu

total number of arguments 3

Controller starts executing the script

echo "Please Provide the user name and state name"

read name state_name

echo "User provided name is :$name and His state name is : $state_name "

if [ "$state_name" =  "andra" ]; then

        echo "$name speaks Telugu"

else

        echo "$name does not speak Telugu"

fi

Note:

https://www.blogger.com/blog/post/edit/7948084828664881049/5347819150177177366?hl=en

consider this script name i used for above example 


No comments:

Post a Comment

What is the sanity checks you have done for STA?

 Sanity checks for STA: Linking Checks: We need to check., is there any missing modules or missing pins in a library. this is done by link c...