0

I want all variables names are set in shell script. I have a file which contains key value pairs and I was read content from that file and store/set into variables. I want to do some processes if a variable is available/set otherwise I don't need to do those processes. How to achieve this.

For example I run loop in shell scripts in each iteration it gives one of the variables is set before that command.

If code like this

a=test1
b=test2
c=test3

for i in ???
do
    echo $i
done

then I want output like this

a b c

What command is used o achieve this.

Jeeva
  • 1
  • 1

3 Answers3

1

You could use set before and after setting the variables

e.g:

$ set > aux1
$ c=345
$ set > aux2
$ diff aux1 aux2
57c57
< PIPESTATUS=([0]="141" [1]="0")
---
> PIPESTATUS=([0]="0")
112a113
> c=345
malarres
  • 2,941
  • 1
  • 21
  • 35
0

If you have a pre-defined list of such variables, then you can test it like this:

for i in $(echo "a b c"); do
    echo $i
done
Vasily G
  • 859
  • 8
  • 16
  • I don't know the variable names because it created dynamically from input file. The above code is sample for what result i want. – Jeeva Dec 29 '15 at 10:09
0

If i could help you :

#!/bin/sh

#   Define list of tests
LIST_TESTS=`cat list_test.txt`

for TEST in ${LIST_TESTS}
do
    vartest=`echo ${TEST}`
    if [  "${vartest}" = "" ]
    # No Test
      then
        echo "*** WARNING***  Test not found"   
    else
        echo "${vartest} is available"
    fi
done

#   Second Define list of tests
tabTest=('test1' 'test2' 'test3')

i=0
while  [ "${tabTest[$i]}" != "" ]
do
    echo "${tabTest[$i]} is available"
    i=$(($i+1))
done
Mirouf
  • 92
  • 1
  • 6