set variable to command output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting set variable to command output
# 1  
Old 07-04-2010
SOLVED: set variable to command output

I'm hoping you guys can help me out here. I've been trying different methods to try and get what IW as hoping would be a fairly simple script but has turned into a pain.

Bit of background - I am writing a script to check values in certain failes to ensure they are corerct. I'm runnign this on a Citrix Netscaler which is based on FreeBSD.

The problem I'm having is passing the output of a command into a variable.

currect script thats failing:

Code:
CVer="9,2,45,7"
 
RunningStore="/netscaler/ns_gui"
rFServicesCVer=$ cat $RunningStore/vpns/f_services.html | grep "var clientversion"
 
if [ $rFServicesCVer = "var clientversion=$CVer" ]
then
echo $runningpath/f_services.html contains the correct value
else
echo $runningpath/f_services.html contains the incorrect value
fi

When I run this script the $rFServicesCVer does not appear to get set

tested this by putting an echo before the if and I got the following output

Code:
if [ = var clientversion=9,2,45,7

if I echo the variable above as a test that the variable is set and I get nothing.

what is strange is when the script runs and the $rFServicesCVer is edclaired it seems to run the command and output the correct value so it's as if it's running the command rather then setting it as a variable.

any help much appreciated - if what I've written doesnt make sense let me know as I may not have explained it very well.

Thanks

Last edited by stuc; 07-04-2010 at 03:31 PM..
# 2  
Old 07-04-2010
Hi Stuc(k), welcome to the forum. For starters, try these modifications:
Code:
CVer="9,2,45,7"
 
RunningStore="/netscaler/ns_gui"
rFServicesCVer=$(cat "$RunningStore/vpns/f_services.html" | grep "var clientversion")
 
if [ "$rFServicesCVer" = "var clientversion=$CVer" ]
then
  echo $runningpath/f_services.html contains the correct value
else
  echo $runningpath/f_services.html contains the incorrect value
fi

Also, it is slightly more efficient to use:
Code:
rFServicesCVer=$(grep "var clientversion" "$RunningStore/vpns/f_services.html")

# 3  
Old 07-04-2010
Many thanks Scrutinizer that brackets have done the job! I'm now getting the variable set as expected.

I am for some reason getting "[: too many arguments" when I do the IF command though for some strange reason

Code:
 
CVer='"9,2,45,7"'
VPNNum="vpn9"
PersistentPath="/var/vpn/custom/"
 
RunningStore="/netscaler/ns_gui"
rFServicesCVer=$(cat $RunningStore/vpns/f_services.html | grep "var clientversion")
if [ $rFServicesCVer = "var clientversion=$CVer" ]
then
echo $runningpath/f_services.html contains the correct value
fi

I'm not sure I understand how the second suggestion you gave works

---------- Post updated at 06:21 PM ---------- Previous update was at 06:15 PM ----------

sorry - realised I hadnt quoted the first variable in the IF like you said and thats why I got that error.

Thanks again for your help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Set Command to output a log of every command executed in the script

Hi Guys, I like to output every command executed in the script to a file. I have tried set -x which does the same. But it is not giving the logs of the child script which is being called from my script. Is there any parameters in the Set command or someother way where i can see the log... (2 Replies)
Discussion started by: mac4rfree
2 Replies

2. Shell Programming and Scripting

Parse output path to set variable

I am looking to parse a text file output and set variables based on what is cropped from the parsing. Below is my script I am looking to add this feature too. All it does is scan a certain area of users directories for anyone using up more than X amount of disk space. It then writes to the... (4 Replies)
Discussion started by: es760
4 Replies

3. UNIX for Dummies Questions & Answers

set varibale to be output of a command

Hi there! :) How to set varibale to be output of a command in csh. I was using set i="date+'%y%m%d'" but the output is date+'%y%m%d' and without quites and with a single quote the output is the same :wall: :eek: Thanks in advance (2 Replies)
Discussion started by: FUTURE_EINSTEIN
2 Replies

4. Shell Programming and Scripting

Set shell variable from command

shell script has a command inside back quotes in method update_TABLE I need to store the count of number of Rows updated and store it in shell script variable "num" num = 0; Update_TABLE Update_TABLE() { `echo " set verify off feedback off echo off pagesize 0 head off... (4 Replies)
Discussion started by: finder255
4 Replies

5. Shell Programming and Scripting

set output of linux cmd as a variable in .exp

I am trying to make a script to take commands from a .txt file ( line by line) and pass it using send ( used in another function ) what i am trying to achieve is : set nol "`grep '' ${exp_path2}/cmdlist.txt | wc -l `" as in shell script nol=`grep '' $exp_path2/cmdlist.txt | wc -l` ... (0 Replies)
Discussion started by: dixyantar
0 Replies

6. HP-UX

What is the use of command set -- and set - variable?

Hi, I am using hp unix i want to know the use of the following commands set -- set - variable thanks (4 Replies)
Discussion started by: gomathi
4 Replies

7. Shell Programming and Scripting

Set specific part in command output into variable

I am trying unsuccessfully to set into a variable a specific part of command output: The command output will be as: line 1: <varied> line 2: 2 options: option 1: Set view: ** NONE ** or option 2: Set view: <different_name_of_views_always_without_spaces> and I would like to get into... (7 Replies)
Discussion started by: orit
7 Replies

8. UNIX for Dummies Questions & Answers

Set a variable from awk output

I have a file which I am processing using awk to spit out the following: export CLIENT=1 ; export USER=1 ; export METABASE=1 ; export TASK=1 ; export TOTAL=3 What i want to do now is execute that within the script so those variables are available to other commands. I've tried piping the... (3 Replies)
Discussion started by: Cranie
3 Replies

9. Shell Programming and Scripting

set variable command

Hi all, I want to set a variable in ksh shell (prompt) and echo the value. $ set x=5 $echo $x But it is returning null. Can any one please help. Thanks in advance (1 Reply)
Discussion started by: ammu
1 Replies

10. UNIX for Dummies Questions & Answers

Export command giving Variable Name vs the Value set for the Variable

I'm having an issue when I export within my program. I'm getting the variable name, not the variable value. I have a configuration file (config.txt) that has the values of the variables set as so: set -a export ARCHIVEPOSourceDir="/interfaces/po/log /interfaces/po/data" export... (2 Replies)
Discussion started by: ParNone
2 Replies
Login or Register to Ask a Question