Consider the following Lua code:
local var1, var2;
Is var2 a local variable here? Or is only var1 a local?
Both are local.
Both variables are local, and both are given a value of nil.
To assign them to 2 different values, simply:
local var1,var2 = 1,2
Related
vv=1
cc() { local vv=2; echo $vv; unset vv; echo "${vv}3"; }
cc
echo $vv
Gives:
2
3
1
I was expecting:
2
13
1
How can I access the global variable once a variable with the same name has been set local in a function?
I don't think you can. If it's an exported environment variable you can find it by reading the environment, but as far as a global variable masked by a local one, AFAIK you're out of luck. Check the contents of, and copy as necessary, before declaring your local variable.
if suppose I have an local variable input <- 100 Can You suggest me How to use the local varaibles in RS.eval(c1,xx <-input) please comment
I have a lengthy program with several variables i want to check up on periodically.
rather than typing
print var1
print var2
print var2
and so on, ho can I either get output for multiple variables from one print
(I tried print var1, var2, var3 but that didn't work.)
or
How can I write my own function to do the same thing as repetitiously typing print for each available?
is there any easier way?
You were close enough
print (var1, var2, ...)
Incidentally, you can use p as a shorthand for print:
p (var1, var2, ...)
If you simply want to monitor those variables for changes, then you need to watch them:
watch var1
watch var2
This way, any time the value of var1 etc. changes, GDB will notify you and print old and new value.
There are several ways, but what you are looking for is probably display command.
Do:
display var1
display var2
display var3
Then just debug as usual and the values will be printed whenever debugger stops.
If you need to do this in many session you can write command to a script and source it in gdb.
I have a properties file to set some environment variables say:
mydata.properties:
VAR1=Data1
VAR2=Data2
Now I want to define a new variable called VAR3 that can hold either VAR1 or VAR2 like:
mydata.properties:
VAR1=Data1
VAR2=Data2
VAR3=VAR2
To make these variables to be available to my bash session I am using this command
source mydata.properties
Now my requirement is to print the value for VAR3 so that it can print the internal data of the sourced VAR2 variable like:
Value of VAR3 is Data2
I tried different options like
echo "Value of VAR3 is $$VAR3"
This gives me junk output.
or echo "Value of VAR3 is ${$VAR3}"
This give me error as Value of ${$VAR3}: bad substitution
Please help me how to get this output.
If you really need to expand the variable that VAR3 points to (instead of just setting VAR3 to the value of VAR2 to begin with), you can use indirect variable expansion with ${!varname}:
$ VAR1=Data1
$ VAR2=Data2
$ VAR3=VAR2
$ echo "${!VAR3}"
Data2
I don't use bash, but
mydata.properties:
VAR1=Data1
VAR2=Data2
VAR3=$VAR2
should do it. Note the extra $ infront of var2 in the last line.
You might want to get into the habit of doing it this way:
VAR3=${VAR2}
This is the same as VAR3=$VAR2, but when you are using variables embedded in other text (like VAR3=${VAR2}_foo), you will need the {}, so it's a good idea to use them by default.
You want
VAR2=$VAR3
then
echo "Value of VAR3 is $VAR3"
Note the single $ in both places. You use the $ to reference the value of variables, so you need to use it when assigning the value of VAR2, and when printing its value in the echo command
I think you want to know how to make a reference in BASH. The syntax can be rather tricky. It involves using eval and echo to be able to set the value:
VAR2=Data2
VAR3=VAR2 #Reference!
echo "The value of $VAR3 is $(eval echo \$$VAR3)"
It's better to use hash arrays instead of trying to use references directly:
REF[VAR3]=$VAR2
echo "The value of VAR3 = ${REF[VAR3]}"
Or, as others point out, you could have simply done this:
VAR3="$VAR2"
if you merely want to set $VAR3 to be the same as $VAR2, and you don't care about references.
I'm trying to determine the existing HDDs in each system using a for loop as show below, the problem is when I try to set the variable using the below code i get sda=true: command not found. What is the proper way to do this?
#!/bin/bash
for i in a b c d e f
do
grep -q sd$i /proc/partitions
if [ $? == 0 ]
then
sd$i=true
else
sd$i=false
fi
done
You need to use an array or declare:
declare sd$i=true
I would use an array in this case. For example:
$ i=a
$ sd[$i]=true
$ echo ${sd[a]}
true
As another poster stated, if you want to do this without an array, you can instead make a local variable by using syntax like declare sd$i=true. If you want to make a global variable, use export sd$i=true.
BASH FAQ entry #6: "How can I use variable variables (indirect variables, pointers, references) or associative arrays?": "Assigning indirect/reference variables"