Grep strings for different cases


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep strings for different cases
# 1  
Old 12-07-2015
Grep strings for different cases

Hi All,
Good morning
I have a below code which is working & getting expected output. the problem in this code is it is executing 3 if conditions, my requirement is
suppose if first condition is success then it should print echo statement & exit from if condition
else if the 1st if condition got failed then it should goto next 2nd if condition and executes cmds... here if its success then it should print echo statement & exit from if condition
else if the 2nd if condition got failed then it should goto next 3rd if condition and executes cmds... here if its success then it should print echo statement & exit from if condition else print echo statements given in code
I have tried using case but its not generating expetected results

Code:
#!/bin/ksh
set -x
while read server
do 
var=`/usr/bin/curl-u user:password http://$server 2>&1`
        printf "%b\n" "${var}" | grep Denied
        if [ `echo $?` = 0 ];then 
                echo "$server invalidcred"
        fi
                printf "%b\n" "${var}" | grep apache
                if [ `echo $?` = 0 ];then 
                        echo "$server is up and running"
                fi
                        printf "%b\n" "${var}" | grep "couldn't"
                        if [ `echo $?` = 0 ];then 
                                echo "$server notresponding"
else
                                echo "$server unknown error"
                        fi
done < num

the output stored in variable may contain either any one of the below:
1> access Denied for server
2> access for apache is successfull
3> couldn't access the server
if the above three are not stored in a variable then it prints a msg "unknown error"

can you please me in correcting the code

Last edited by sam@sam; 12-07-2015 at 11:37 AM..
# 2  
Old 12-07-2015
Hi,
You defined var=denied, so you can simplified your code to:
Code:
#!/bin/ksh
set -x
while read server
do
var=denied
echo "$server invalidcred"
done < num

Regards.
This User Gave Thanks to disedorgue For This Post:
# 3  
Old 12-07-2015
the output stored in variable may contain either any one of the below:
1> access Denied for server
2> access for apache is successfull
3> couldn't access the server
if the above three are not stored in a variable then it prints a msg "unknown error"

more num
URL1
URL2
URL3

I have tried using exit 0 but its existing from code in 1st iteration...

Last edited by sam@sam; 12-07-2015 at 01:17 PM..
# 4  
Old 12-07-2015
Ok, as suggered by RudiC, you can use case:
Code:
#!/bin/ksh
set -x
while read server
do        
        var=`/usr/bin/curl-u user:password http://$server 2>&1`
        case "$var" in
        *Denied*)
                echo "$server invalidcred"
                ;;
        *apache*)
                echo "$server is up and running"
                ;;
        *"couldn't"*)
                echo "$server notresponding"
                ;;
        *)
                echo "$server unknown error"
                ;;
        esac
done < num

Regards.
This User Gave Thanks to disedorgue For This Post:
# 5  
Old 12-07-2015
Perfect Thanks its working > disedorgue & RudiC
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep for strings

Hi, Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36 How can I grep for the strings chrome and safari from a file, and if chrome print Chrome/40.0.2214.94 to a file and also count the number of times chrome is found? ... (4 Replies)
Discussion started by: cyberfrog
4 Replies

2. Shell Programming and Scripting

Grep process with two strings

Hi, I am trying to get the PID of a process that is defunct. I have two quesitons on this. 1) how do I pass in two strings? This is what I am using now: ps -ef |egrep -i "programname|<defunct>" But this returns multiple results with the word defunct in them. 2) How do I make it so that... (1 Reply)
Discussion started by: ChickenPox
1 Replies

3. Shell Programming and Scripting

GREP between last occurrences of two strings

Hi I have the server.log file as: Server Stopped ABC DEF GHI JKL Server Started MNO PQR STU Server Stopped VWX YZ ABC Server Started Server Stopped 123 456 789 (9 Replies)
Discussion started by: ankur328
9 Replies

4. Red Hat

Grep between two strings in shell

<cisco:subname> <cisco:sptp>Cisco PortA Series</cisco:sptp> <cisco:aliasNameList xsi:nil="true"/> <cisco: owner xsi:nil="true"/> <cisco:subportname> <cisco:cpt>Cisco SubPort B Series</cisco:cpt> ... (3 Replies)
Discussion started by: itsspy
3 Replies

5. Shell Programming and Scripting

Grep 2 same strings in a same line??

I have this code TrackingId:1362412470675;MSISDN:; INFO - number of clietns:3:Received response is: EMSResponse , protocolVersion=5, purchaseOptions=null, serviceData=ServiceData , screenData=CanvasData ]], title=null, titleResource=MessageResource], screenType=null]], serviceId=idBamboo,... (7 Replies)
Discussion started by: nikhil jain
7 Replies

6. UNIX for Dummies Questions & Answers

Grep help (match two strings one line)

Hello, Here I have some grep command which is not working correctly: cat file1.txt: apples Date: Sun, 24 Feb 2013 8:14:06 -0800 peaches melons cherry sky cloud green purple yellow cat file2.txt: apples Date peaches melons 0800 cherry sky cloud green purple black (2 Replies)
Discussion started by: holyearth
2 Replies

7. Shell Programming and Scripting

Can't grep multiple strings

I have a script that periodically checks the Apache error_log to search for a specific error that causes it to hand and, if found, it restarts the service. I recently found another error that forces it to hand and won't serve pages until it is reset. What I'm trying to do is to get the script to... (3 Replies)
Discussion started by: cfjohnsn
3 Replies

8. Shell Programming and Scripting

grep two strings in a file..

Hello All, I have a big file about 1000 lines. Now i am trying to grep a particular string and printing the lines from the string. say for example in 500th line i have the date as "Mon Wed 14 20:15:24 2010". now i in my case i need to grep the combination of the strings "Mon Wed 14" and the... (7 Replies)
Discussion started by: intiraju
7 Replies

9. Shell Programming and Scripting

Grep Multiple Strings

Hi, Can any one pelase tell me how to grep multiple strings from multiple files in a singel folder? grep -E "string1|string2|string3|string4|string..." its taking lots of time.. can any please tell me fast grep??? URGENT (10 Replies)
Discussion started by: durgaprasad
10 Replies

10. Shell Programming and Scripting

want to grep only strings in a file?

Hai, Just want to print only alphanumeric in a file ex:- fdsdsklf#@^%$#hf output:- fdsdsklfhf plz, help me:o (5 Replies)
Discussion started by: balan_mca
5 Replies
Login or Register to Ask a Question