In shell script condition check is very easy and simple using if else statements. Here is a example which we would like to discuss with our readers.
Scenario: We have a variable that contains long string value, user wants to find matching value from given string and perform check whether that text/character has matched from string value or or not.
#Program in Bash ShellLine 1: CHECK_SOURCE="[main] INFO com.adobe.jcr.checknode.existence - Node does not exist"line 2: echo $CHECK_SOURCE | grep -w -o -i 'Node exists'Line 3: if [ $? == 0 ]; thenLine 4: echo " matched"Line 5: elseLine 6: echo "not matched"
Line 7: fi
Explanation:
In line 1 we have declared a variable CHECK_SOURCE which have some value.
In line 2 we are finding text "Node exists" in value of variable CHECK_SOURCE using grep command.
In line 3 using if statement we are checking number of matches from last executed statement. $? sign will return number of matches. If text passed in grep command will find some matching text in variable then $? will number of matching otherwise it will return zero.
Thanks for sharing this if-else condition concept. It is really helpful.
ReplyDeleteUnix Training in Chennai | Unix shell scripting Training in Chennai
Great post and informative blog. it was awesome to read, thanks for sharing
ReplyDeleteDevOps Training
DevOps Online Training
DevOps Training in Hyderabad
DevOps Project Training
DevOps Training in Ameerpet
DevOps Training Institute in Ameerpet
DevOps Online Training in Hyderabad
He has tremendous experience in Vedic astrology and expertise in Horoscope reading, Horoscope matching, Face Reading, Palm Reading, Future Predictions and other astrology fields. Being the famous Indian astrologer in USA he provides the best astrology prediction and horoscope reading services throughout the nation. Top Indian Astrologer in USA
ReplyDeleteVery Informative Post .Thanks for Sharing
ReplyDeleteWonderful Article. Thanks for sharing this pos
GCP DevOps Online Training
GCP DevOps Training
GCP DevOps Training in Hyderabad
GCP DevOps Online Training Institute
GCP DevOps Training institute in Ameerpet
GCP DevOps Training in Ameerpet
DevOps On Google Cloud Platform Online Training
DevOps GCP online Training in hyderabad