Help in scripting, store value in variable

Hi all,

I have a script in which i need to run a command like "/opt/dell/srvadmin/sbin/omreport about" and output will be something like

Version : 6.3.0
Copyright : Copyright (C) xxx Inc. 1995-2010 All rights reserved.
Company : xxx Inc.

In this i need to save the version to a variable and Ensure that the version is 6.3.0.

If the version is 6.3.0 exit with a return code of 0 (success)
If the version is not 6.3.0 exit with a failure code

Please help me out in this ...

Thanks in advance

  • Renjesh Raju

Try this,

#!/bin/sh
ver=`opt/dell/srvadmin/sbin/omreport about | awk 'NR==1&&/Version/{print $3}'`
echo $ver
if [ $ver == "6.3.0" ] ; then exit 0 ; else exit 1; fi

It is throwing an error like

line 2: ver: command not found

line 4: [: =: unary operator expected

There should not be space between ver= and `.

 
ver=`opt/dell/srvadmin/sbin/omreport about | awk 'NR==1&&/Version/{print $3}'`
echo $ver
if [ "$ver" == "6.3.1" ] ; then exit 0 ; else exit 1; fi

hi,

If your output contains only one Version then below code works:

a=`/Sep/example.sh | awk -F: '/Version/ { print $2 }'`
echo $a

Slight correction, if I may?

a=`/Sep/example.sh | awk '/Version/ { print $NF }'`
echo $a

Otherwise you'd need -F"[ :]" to handle the spaces as well as the colon.

V=$(/opt/dell/srvadmin/sbin/omreport about | grep Version)
[ "${V#*: }" = "6.3.0" ]

The exit code of the script is the exit code of last command
Version=6.3.0 => $?=0
otherwise $?=1

Using the Exact Match feature of "grep" we can avoid accidental matches such as 6.3.07 . In this context the exit code from "grep" will be either zero (success) or one (string not found).

/opt/dell/srvadmin/sbin/omreport about | grep -qx 'Version : 6.3.0' ; REPLY=$?
exit ${REPLY}

try this:

VAR1=$(/opt/dell/srvadmin/sbin/omreport about | cut -f2 -d ":" | sed 's/.* //')
echo "Version is: ${VAR1}"
if [ "${VAR1}" == "6.3.0" ] ; then exit 0 ; else exit 1; fi

Ya , now it's working ... thanks a lot guys ...

---------- Post updated at 06:41 PM ---------- Previous update was at 03:39 PM ----------

Still showing syntax error ...

$ ./Dell_OMSA_6_30
script Dell_OMSA_6_30 executing 2010/09/14 07:06:51
 
./Dell_OMSA_6_30[56]: 0403-057 Syntax error at line 102 : `"' is not matched.

Code area where it is creating error ...

ver=`/opt/dell/srvadmin/sbin/omreport about | awk 'NR==1&&/Version/{print $3}'`
if [ "$ver" == "6.3.0" ]; then exit 0 ; else exit 1; fi

Thanks
Renjesh Raju

Have you tried this proposition ?

Try this,

ver=$(/opt/dell/srvadmin/sbin/omreport about | awk 'NR==1&&/Version/{print $3}')
if [ "$ver" == "6.3.0" ] ; then exit 0 ; else exit 1; fi

Still showing same error ...

$ ./Dell_OMSA_6_30
script Dell_OMSA_6_30 executing 2010/09/14 07:46:03
 
./Dell_OMSA_6_30[56]: Syntax error at line 102 : `"' is not matched.

.....:frowning:

The error message is about unmatched quotes of some sort.
If you can't see the error, please post the whole script Dell_OMSA_6_30 .