7 Replies - 14318 Views - Last Post: 30 March 2013 - 02:20 AM Rate Topic: -----

#1 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 644
  • Joined: 03-April 12

Shell script (SH, BASH)

Posted 15 March 2013 - 07:00 AM

Does anyone know (and care to tell me too) how to handle variable names with variable names?

For example:

export MY_VAR_$1=10

if [ $2 -eq "new" ]; then
  MY_VAR_$1=`uname -r`
  ./another_script MY_VAR_$1
fi



The "export"-part works, but the rest of it doesn't.

If I "source" the script with the parameters "JOHN" and "new", printenv shows "MY_VAR_JOHN" but the
"MY_VAR_$1=`uname -r`" fails: shell doesn't find COMMAND "MY_VAR_JOHN".

Why does the shell think the variable name is a command? How should that be done?

This post has been edited by turboscrew: 15 March 2013 - 07:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Shell script (SH, BASH)

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: Shell script (SH, BASH)

Posted 15 March 2013 - 03:14 PM

What about :

  export MY_VAR_$1=`uname -r`


Was This Post Helpful? 0
  • +
  • -

#3 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 644
  • Joined: 03-April 12

Re: Shell script (SH, BASH)

Posted 18 March 2013 - 02:14 AM

View Post#define, on 16 March 2013 - 12:14 AM, said:

What about :

  export MY_VAR_$1=`uname -r`


Well, why not (even if it's not nice) but then there is:
 ./another_script MY_VAR_$1


How do I make the call?

If I call the script:
my_script FIRST new

i wish that "another_script" to be called as:
 ./another_script 3.2.0-23-generic

instead it is now called:
 ./another_script MY_VAR_FIRST


If I put
 ./another_script $MY_VAR_$1

I get
 ./another_script FIRST


If I put
 ./another_script ${MY_VAR_$1}

I get
${MY_VAR_$1}: bad substitution

This post has been edited by turboscrew: 18 March 2013 - 02:15 AM

Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5411
  • View blog
  • Posts: 27,423
  • Joined: 10-May 07

Re: Shell script (SH, BASH)

Posted 18 March 2013 - 02:25 AM

Why are you calling a script name with the 2nd argument as the name of the script? It seems rather prone for failure. Why not just handle that 2nd value in the script you are already in?
Was This Post Helpful? 0
  • +
  • -

#5 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 644
  • Joined: 03-April 12

Re: Shell script (SH, BASH)

Posted 18 March 2013 - 05:21 AM

OK, I'm trying to write a script that monitors temperatures and voltages and the like of bunch of devices.
The number of devices is not fixed. New devices may be added and some other devices removed.

The devices can be of different kind having different number of things to monitor.

My idea is:
1) Main script that "calls" monitoring of different devices.
2) Each device has name in the main script.
3) Each kind of device has its own monitoring script that knows how to get the monitored values
and what values need to be monitored.
4) All monitored values are checked against limits - that's the monitoring action.
The limits are value limits and value change limits.
5) Each kind-of-device script calls limit-checking script to check the value

The idea is that the main script "sources" the device type script, and the device type script exports its variables
that need to be preserved from a call to another call. The variable names are built from the device name from the
main script and the device variable names from the device type script.

Now it should be quite easy to set up the script to monitor, say 5 of these devices and 12 of those devices.
Only two device type specific scripts (and 5+12 names in the main script) are needed.
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5411
  • View blog
  • Posts: 27,423
  • Joined: 10-May 07

Re: Shell script (SH, BASH)

Posted 18 March 2013 - 09:47 AM

How I would handle this is have a flat text file called devices. Read that file & for loop it.

devices=`cat devices.cfg`
for device in ${devices} 
do
  ./script ${device}
  # or just code here...
done



Just to note, I do not know the answer to your initial question. I have had to tackle this before, & I believe it to be a script-dependent issue. As to say, I think maybe Bash can not handle it & Korn shell can?
Was This Post Helpful? 0
  • +
  • -

#7 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 644
  • Joined: 03-April 12

Re: Shell script (SH, BASH)

Posted 18 March 2013 - 01:43 PM

View Postno2pencil, on 18 March 2013 - 06:47 PM, said:

How I would handle this is have a flat text file called devices. Read that file & for loop it.

devices=`cat devices.cfg`
for device in ${devices} 
do
  ./script ${device}
  # or just code here...
done



Just to note, I do not know the answer to your initial question. I have had to tackle this before, & I believe it to be a script-dependent issue. As to say, I think maybe Bash can not handle it & Korn shell can?


I didn't quite get it. What would you put into the config-files?

Let's say you have 5 units of type 1 that has only one temperatute to monitor and, say, 8 units with type 2 units that have 3 temperatures and 2 voltages to monitor. Typically all the data to be monitored (and a lot more) is got from the devices with one status request the response to which the script has to pick the monitored info.

I know that bash can handle arrays, but I didn't find a working way to use them in this situation. Also, I would not use shell-specific features if I could avoid that. I also wouldn't like to use 13 dedicated scripts. I'd rather use 2 scripts called with different names and "messaging ids" or "addresses".

The info that needs to be preserved per thing to monitor is basically state, last value and previous value. Type 2 unit should then need to have 5 x 3 = 15 values to remember over the check period, when the type 1 unit only has 3 such values. Then there are 4 limits for each monitored thing - even the same type of devices may have different limits.

And someone should later on be able to maintain that.

This post has been edited by turboscrew: 18 March 2013 - 01:46 PM

Was This Post Helpful? 0
  • +
  • -

#8 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 644
  • Joined: 03-April 12

Re: Shell script (SH, BASH)

Posted 30 March 2013 - 02:20 AM

It's solved now, eval did the trick.

Main script:
export MY_VAR_$1=0;
eval "MY_VAR_$1"=\""`uname -r`"\";
eval "./another_script \"\$MY_VAR_${1}\"";
printenv | grep MY_VAR



another_script:
echo $1



A run:
$ . ./koe.sh name
3.2.0-23-generic
MY_VAR_name=3.2.0-23-generic


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1