<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: variable substitution in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/variable-substitution/m-p/5106186#M91252</link>
    <description>&lt;!--!*#--&gt;Hi Chris:&lt;BR /&gt;&lt;BR /&gt;I'd use an array of variables:&lt;BR /&gt;&lt;BR /&gt;# cat ./myscript&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;set -A diafail&lt;BR /&gt;count=0&lt;BR /&gt;for data in 1 2 3 4&lt;BR /&gt;do&lt;BR /&gt;    count=$((count+1))&lt;BR /&gt;&lt;BR /&gt;    if [[ ! -f /tmp/test ]] ; then&lt;BR /&gt;        diafail[$count]=1&lt;BR /&gt;    else&lt;BR /&gt;        diafail[$count]=0&lt;BR /&gt;    fi&lt;BR /&gt;&lt;BR /&gt;    if [[ ${diafail[$count]} -ne 1 ]] ; then&lt;BR /&gt;        echo "OK"&lt;BR /&gt;    else&lt;BR /&gt;        echo "BAD"&lt;BR /&gt;    fi&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
    <pubDate>Tue, 29 Apr 2008 16:18:03 GMT</pubDate>
    <dc:creator>James R. Ferguson</dc:creator>
    <dc:date>2008-04-29T16:18:03Z</dc:date>
    <item>
      <title>variable substitution</title>
      <link>https://community.hpe.com/t5/operating-system-linux/variable-substitution/m-p/5106183#M91249</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;I am trying to set a variable and run a test on it later but m struggling a bit:&lt;BR /&gt;&lt;BR /&gt;count=0&lt;BR /&gt;for data in 1 2 3 4&lt;BR /&gt;do&lt;BR /&gt;count=$((count+1))&lt;BR /&gt;&lt;BR /&gt;if [[ ! -f /tmp/test ]] ; then&lt;BR /&gt;&lt;BR /&gt;        diafail$count=1&lt;BR /&gt;&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;I basically want diafial$count to be tested later&lt;BR /&gt;&lt;BR /&gt;so&lt;BR /&gt;&lt;BR /&gt;if [[ diafail$count -ne 1 ]] ; then&lt;BR /&gt;&lt;BR /&gt;etc etc&lt;BR /&gt;&lt;BR /&gt;but this isnt working&lt;BR /&gt;&lt;BR /&gt;any ideas?&lt;BR /&gt;&lt;BR /&gt;thanks &lt;BR /&gt;&lt;BR /&gt;chris</description>
      <pubDate>Tue, 29 Apr 2008 15:43:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/variable-substitution/m-p/5106183#M91249</guid>
      <dc:creator>lawrenzo_1</dc:creator>
      <dc:date>2008-04-29T15:43:54Z</dc:date>
    </item>
    <item>
      <title>Re: variable substitution</title>
      <link>https://community.hpe.com/t5/operating-system-linux/variable-substitution/m-p/5106184#M91250</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;first, i would do some 'echo's in this script.&lt;BR /&gt;For example 'echo $count'&lt;BR /&gt;Then, I don't know if 'count=$((count+1))' works like you want.&lt;BR /&gt;Maybe 'count=(($count+1))'?&lt;BR /&gt;&lt;BR /&gt;HTH&lt;BR /&gt;Volkmar&lt;BR /&gt;</description>
      <pubDate>Tue, 29 Apr 2008 15:51:57 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/variable-substitution/m-p/5106184#M91250</guid>
      <dc:creator>V. Nyga</dc:creator>
      <dc:date>2008-04-29T15:51:57Z</dc:date>
    </item>
    <item>
      <title>Re: variable substitution</title>
      <link>https://community.hpe.com/t5/operating-system-linux/variable-substitution/m-p/5106185#M91251</link>
      <description>&lt;!--!*#--&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;in this case 'eval' is your friend or change to use arrays. I set test 1+3 in my example.&lt;BR /&gt;&lt;BR /&gt;touch /tmp/test1 /tmp/test3&lt;BR /&gt;cat /tmp/chtst&lt;BR /&gt;#!/usr/bin/ksh&lt;BR /&gt;typeset -i count=0 max=4&lt;BR /&gt;&lt;BR /&gt;while [ count+=1 -le max ]&lt;BR /&gt;do&lt;BR /&gt; if [ -f /tmp/test$count ]&lt;BR /&gt; then eval diafail$count=1&lt;BR /&gt; else eval diafail$count=0&lt;BR /&gt; fi&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;count=0&lt;BR /&gt;while [ count+=1 -le max ]&lt;BR /&gt;do&lt;BR /&gt;  if eval [ \$diafail$count -eq 1 ]&lt;BR /&gt;  then print test $count active&lt;BR /&gt;  fi&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;mfG Peter</description>
      <pubDate>Tue, 29 Apr 2008 16:03:15 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/variable-substitution/m-p/5106185#M91251</guid>
      <dc:creator>Peter Nikitka</dc:creator>
      <dc:date>2008-04-29T16:03:15Z</dc:date>
    </item>
    <item>
      <title>Re: variable substitution</title>
      <link>https://community.hpe.com/t5/operating-system-linux/variable-substitution/m-p/5106186#M91252</link>
      <description>&lt;!--!*#--&gt;Hi Chris:&lt;BR /&gt;&lt;BR /&gt;I'd use an array of variables:&lt;BR /&gt;&lt;BR /&gt;# cat ./myscript&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;set -A diafail&lt;BR /&gt;count=0&lt;BR /&gt;for data in 1 2 3 4&lt;BR /&gt;do&lt;BR /&gt;    count=$((count+1))&lt;BR /&gt;&lt;BR /&gt;    if [[ ! -f /tmp/test ]] ; then&lt;BR /&gt;        diafail[$count]=1&lt;BR /&gt;    else&lt;BR /&gt;        diafail[$count]=0&lt;BR /&gt;    fi&lt;BR /&gt;&lt;BR /&gt;    if [[ ${diafail[$count]} -ne 1 ]] ; then&lt;BR /&gt;        echo "OK"&lt;BR /&gt;    else&lt;BR /&gt;        echo "BAD"&lt;BR /&gt;    fi&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Tue, 29 Apr 2008 16:18:03 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/variable-substitution/m-p/5106186#M91252</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2008-04-29T16:18:03Z</dc:date>
    </item>
    <item>
      <title>Re: variable substitution</title>
      <link>https://community.hpe.com/t5/operating-system-linux/variable-substitution/m-p/5106187#M91253</link>
      <description>Since you are creating variable names on the fly, you want the shell to evaluate variables first, then parse the line, and as mentioned, the eval statement is the secret. eval says: take everything on the current line, evaluate all the variables then execute the line. This illustrates the technique:&lt;BR /&gt; &lt;BR /&gt;&lt;BR /&gt;for COUNT in 1 2 3 4&lt;BR /&gt;        do&lt;BR /&gt;        eval "VAR$COUNT=$COUNT"&lt;BR /&gt;        eval "print VAR$COUNT=\$VAR$COUNT"&lt;BR /&gt;        done&lt;BR /&gt; &lt;BR /&gt;Now a bit of explanation is needed so you turn on shell tracing like this:&lt;BR /&gt; &lt;BR /&gt;set -x&lt;BR /&gt;+ eval VAR1=1&lt;BR /&gt;+ VAR1=1&lt;BR /&gt;+ eval print VAR1=$VAR1&lt;BR /&gt;+ print VAR1=1&lt;BR /&gt;VAR1=1&lt;BR /&gt;+ eval VAR2=2&lt;BR /&gt;+ VAR2=2&lt;BR /&gt;+ eval print VAR2=$VAR2&lt;BR /&gt;+ print VAR2=2&lt;BR /&gt;VAR2=2&lt;BR /&gt;+ eval VAR3=3&lt;BR /&gt;+ VAR3=3&lt;BR /&gt;+ eval print VAR3=$VAR3&lt;BR /&gt;+ print VAR3=3&lt;BR /&gt;VAR3=3&lt;BR /&gt;+ eval VAR4=4&lt;BR /&gt;+ VAR4=4&lt;BR /&gt;+ eval print VAR4=$VAR4&lt;BR /&gt;+ print VAR4=4&lt;BR /&gt;VAR4=4&lt;BR /&gt; &lt;BR /&gt;So you see that $COUNT must first be turned into a simple character, then the entire line is evaluated. In the print statement, we escape the $ in the string $VAR$COUNT so that the $ remains but $COUNT gets turned into a number.&lt;BR /&gt; &lt;BR /&gt;This coding would be a bit easier to understand if you used an array:&lt;BR /&gt; &lt;BR /&gt;set -A DIAFAIL 0 0 0 0 0&lt;BR /&gt;for COUNT in 1 2 3 4&lt;BR /&gt;do&lt;BR /&gt;if [[ ! -f /tmp/test$COUNT ]]&lt;BR /&gt;then&lt;BR /&gt;DIAFAIL[$COUNT]=1&lt;BR /&gt;fi&lt;BR /&gt;done&lt;BR /&gt;echo ${DIAFAIL[@]}&lt;BR /&gt; &lt;BR /&gt;In the above example, the DIAFAIL array tests for /tmp/test1 then test2, test3 and finally test4. If any of them are not files, the corresponding array location will be 1, otherwise 0. Note that arrays always start at 0 so there will be a DIAFAIL[0] value printer with the [@] shorthand.&lt;BR /&gt; &lt;BR /&gt;</description>
      <pubDate>Tue, 29 Apr 2008 18:20:11 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/variable-substitution/m-p/5106187#M91253</guid>
      <dc:creator>Bill Hassell</dc:creator>
      <dc:date>2008-04-29T18:20:11Z</dc:date>
    </item>
    <item>
      <title>Re: variable substitution</title>
      <link>https://community.hpe.com/t5/operating-system-linux/variable-substitution/m-p/5106188#M91254</link>
      <description>&amp;gt;Volkmar: I don't know if 'count=$((count+1))' works like you want.&lt;BR /&gt;&lt;BR /&gt;This works.  But if you want count to be an integer you could use (()) for arithmetic expressions:&lt;BR /&gt;(( count = 0 ))&lt;BR /&gt;(( count += 1 ))&lt;BR /&gt;&lt;BR /&gt;&amp;gt;Maybe 'count=(($count+1))'?&lt;BR /&gt;&lt;BR /&gt;This doesn't return a value and gets a syntax error.&lt;BR /&gt;</description>
      <pubDate>Tue, 29 Apr 2008 18:27:44 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/variable-substitution/m-p/5106188#M91254</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2008-04-29T18:27:44Z</dc:date>
    </item>
    <item>
      <title>Re: variable substitution</title>
      <link>https://community.hpe.com/t5/operating-system-linux/variable-substitution/m-p/5106189#M91255</link>
      <description>ok lots of examples ....&lt;BR /&gt;&lt;BR /&gt;I'll give them a whirl and let you know.&lt;BR /&gt;&lt;BR /&gt;thanks&lt;BR /&gt;&lt;BR /&gt;Chris.</description>
      <pubDate>Wed, 30 Apr 2008 07:54:32 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/variable-substitution/m-p/5106189#M91255</guid>
      <dc:creator>lawrenzo_1</dc:creator>
      <dc:date>2008-04-30T07:54:32Z</dc:date>
    </item>
    <item>
      <title>Re: variable substitution</title>
      <link>https://community.hpe.com/t5/operating-system-linux/variable-substitution/m-p/5106190#M91256</link>
      <description>thanks all for the help and explanations ...&lt;BR /&gt;&lt;BR /&gt;Array apears to be the best option for my task .....&lt;BR /&gt;&lt;BR /&gt;Chris.</description>
      <pubDate>Wed, 30 Apr 2008 08:07:01 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/variable-substitution/m-p/5106190#M91256</guid>
      <dc:creator>lawrenzo_1</dc:creator>
      <dc:date>2008-04-30T08:07:01Z</dc:date>
    </item>
  </channel>
</rss>

