3

Am having difficulty checking if the contents of a variable contains a certain value or not e.g "Device_info"

The main code

if [ "${NUMBERRESULTS}" -eq 10 ] 
then
    echo "Success"
else
    echo "Failed"
fi

The above query/script in addition to the variable initialisation produced 14 results when i run it (devices.sh) which was accurate but in this case I want to verify if the $NUMBERRESULTS variable or the result contains the device_info value or not. see below

Here is the code I am writing

if [[ $NUMBERRESULTS =~ .*device_info.* ]] - i tried using the =~ operator
then
    echo "Success a device is present";
else
    echo "Failed no device is present"
fi

See the values/fields i need to verify if they are present or not in the $NUMBERRESULTS variable results/contents.

"device_info": {      i need to check if this field/value is present or not.
    "connected_count": 36,
    "current_config_id": 1618,
    "default_config_id": 1618,
    "first_connected": 1454576905,
    "last_connected": 1454931872,
    "registered": 1454576905,
    "ip_address": "90.152.29.158",
    "model_number": "GT-I9505",
    "serial_number": "restful_api_device",
    "mac_address": "00:00:00:00:00:00",
    "smartcard": "unknown",
    "hardware_version": "0",
    "software_version": "83886336",
    "manufacturer": "SAMSUNG"

thanks very much

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • You expect a variable whose name says it contains a number, and which you're already testing as if it *does* contain only a number, to instead contain a bunch of JSON text? That's... very misleading naming, at best. – Charles Duffy Feb 10 '16 at 17:48
  • 2
    Also, for extracting content from JSON input in bash, there are very good dedicated tools such as `jq` available, it's not good form to try to roll your own. – Charles Duffy Feb 10 '16 at 17:49
  • Anyhow: Before this can be answered, you should create a reproducer: Code someone else can copy-and-paste to run that will produce exactly the same behavior you're seeing now. Do that, and describe what the behavior you *want* is, and this will actually be answerable. Right now, your question is internally inconsistent (we don't even know what data type your variable contains -- and it looks like perhaps you don't either), so it's not in a good place to be answered. – Charles Duffy Feb 10 '16 at 17:50
  • Are you just trying to check if the${NUMBERRESULTS} variable is equal to "device_info"? `if [[ ${NUMBERRESULTS} = "device_info" ]] ; then ; echo "success" ; else ; echo "fail" ; fi` – IT_User Feb 10 '16 at 18:42
  • OR... `if [[ $(echo ${NUMBERRESULTS} | grep -c device) != "0" ]] ; then ; echo "success" ; else ; echo "fail" ; fi` This will count how many times "device" is in the ${NUMBERRESULTS} variable, and if it finds the word "device" at all, it will print success. – IT_User Feb 10 '16 at 18:45
  • @bluerojo - I am trying to verify/check if the value "device_info" is present in the results after running the code. if it is not in the results echo FAIL, if it is present echo SUCCESS. thats all. – Steady_Eddy Feb 11 '16 at 09:52
  • @bluerojo - I am trying to verify/check if the results contain the correct values e.g "device_info", "connected_count" etc if the vaues are present and correct then echo SUCCESS else echo FAIL. I hope my explanation is simple enough. thanks lads. – Steady_Eddy Feb 11 '16 at 10:23
  • See initial code below that returns the number of results as 14 which is correct. NUMBERRESULTS=$(curl -s "http://privateserver:3000/devices/restful_api_device/information"| jq '.[]|length') echo NUMBERRESULTS="${NUMBERRESULTS}" if [ "${NUMBERRESULTS}" -eq 10 ] then echo "Success" else echo "Failed" fi – Steady_Eddy Feb 11 '16 at 10:25
  • Now, i want to check that the results contains the correct values e.g check that the value "device_info" etc is present as expected. – Steady_Eddy Feb 11 '16 at 10:30

0 Answers0