Find the answer to your Linux question:
Results 1 to 7 of 7
Hey guys, I know i am going to ask very noob things but I have been struggling for almost 3 days to come up with a script but I am ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2007
    Posts
    36

    Need help writing a script to test if webservices are up or not


    Hey guys,

    I know i am going to ask very noob things but I have been struggling for almost 3 days to come up with a script but I am failing again and again.

    What i am trying to do is to send a curl command to retrive a Webservice's response. If its 200 then I print *OK* otherwise I will print ALERT or some message.

    This is the command which i came up with after doing a bit research

    Code:
    #!/bin/bash
    user=$1
    pass=$2
    domain=$3
    path="/path/v1/module/order/"
    format="xml"
    url="http://$domain$path"
    
    while read order_num
    do
    	echo = "Sending request with $user:$pass to $url$pin.$format"
    	response=$(curl -o /dev/null --silent --GET -I --write-out '%{http_code}\n' --basic -u $user:$pass "$url$pin.$format")
    	echo "Response for order[$order_num] was $response"
    done < $4
    This code is working fine for the url which i input. However, I want to make it more generic.

    What i want to do is that I will create a file name input.txt which will have inputs

    Code:
    <label_name> <path_to_ws> <user> <pass>
    I will read it in my script and will spilt each line based on spaces by follwing script

    Code:
    config=$2
     
    oifs="$IFS"
    IFS=' '
    
    while read test_case
    do
     	read -a array <<< "${test_case}"
    	
    	# array[0] Test Case Name
    	# array[1] URL Path
    	# array[2] user name
    	# array[3] password
    	# line=${array[1]}
    	echo ${array[1]}
    	echo ${array[0]}
    Thats working fine too. But now i am stuck where I have to do string maniupluation. What I want to that my url would have a place_holder which I would have to replace from values from another file (assuming that everything is in order i.e., line one of input.txt file has place_holder value at line 1 of values.txt).

    i.e., my input file will have a line like
    Code:
    WS_1 /path/v1/module/order/* user_name pass
    and my values file would have
    Code:
    ord_321
    I want to replace this ord_321 in the above url which will be in array[1] so that i can send a curl command to that url
    Code:
    /path/v1/module/order/ord_321.xml
    I tried doing this but fialed
    Code:
    path=${array[1]:0:place_holder-1}$values[0]${array[1]:place_holder}
    Can someone help

    thanks,

  2. #2
    Linux Engineer docbop's Avatar
    Join Date
    Nov 2009
    Location
    Woodshed, CA
    Posts
    932
    There are lots of webpages on Bash string manipulation below is an example:

    Advanced Bash Shell Scripting Guide - Manipulating Strings


    Something to start thinking about is scripts like this tend to give a lot of false negatives so you need to add code to handle that with multiple retries to determine is just a network or internet hicup. Also better to check multiple pages on the site to determine entire site is up.

  3. #3
    Linux Engineer
    Join Date
    Apr 2012
    Location
    Virginia, USA
    Posts
    889
    try printing path to see what is being constructed, and then you can go from there.
    I suspect you might need to wrap parts of the variables in quotes "", possibly the entire part right of the = sign.

  4. #4
    Just Joined!
    Join Date
    Mar 2007
    Posts
    36
    thanks for the guidelines, I am going to try it now and will post if i need further help/guidelines ..

  5. #5
    Just Joined!
    Join Date
    Mar 2007
    Posts
    36
    so i wrote this
    Code:
            line=${array[1]}
            echo ${array[1]}
            echo ${array[0]}
            echo `expr index "$line" *`
    The line or array[1] contains following string
    Code:
    /path/v1/module/order/*
    But i got error at last line ... I tried with line variable and even with array. The error was
    Code:
    expr: syntax error

  6. #6
    Linux Engineer
    Join Date
    Dec 2013
    Posts
    1,201
    You need to quote the asterix.
    ie.
    Code:
    expr index $line "*"

  7. #7
    Just Joined!
    Join Date
    Mar 2007
    Posts
    36
    Quote Originally Posted by gregm View Post
    You need to quote the asterix.
    ie.
    Code:
    expr index $line "*"
    thanks (adding this to pass 10 characters limit while replying on linuxforums)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •