Using multiple values for single variable in a loop - bash

I have a small loop problem as below.
I have 3 different values to be used while running the same script -
va1="some-value1"
va2="some-value2"
va3="some-value3"
Now I want to use these three variable values to be used for running the same command, like -
while (i=0,i<=3,i++)
do
bin/java -s (run something with $var);
done
Now I want $var taking the value of var1, var2 and var3 each time it runs,
so can someone please tell me how do we achieve the above?
I tried doing -
for $1 $2 $3
do
case 1
case 2
case 3
done
OR
while read a b c
do
<code assumed to have loop iteration>
done <<< $(command)
But it isnt working as expected... Would really appreciate your help on this.
Thanks,
Brian

You forgot the 'in' part of the syntax:
for var in $va1 $va2 $va3
do
command $var
done

try
while ((i=0,i<=3,i++))
do
eval bin/java -s \$var$i
done
This is a great example of how to use eval. Note that 1. the value of $i is seen by the shell has it scans the line. Then because $var is escaped like \$var, it is not
'make visible' in the first scan. 2. Eval forces a 2nd scan of the cmd-line, so it sees $var1 or whatever, and that value is substituted into the command-line for execution.
I hope this helps.
P.S. Welcome to StackOverflow and let me remind you of three things we usually do here: 1) As you receive help, try to give it too, answering questions in your area of expertise 2) Read the FAQs, http://tinyurl.com/2vycnvr , 3) When you see good Q&A, vote them up by using the gray triangles, http://i.imgur.com/kygEP.png , as the credibility of the system is based on the reputation that users gain by sharing their knowledge. Also remember to accept the answer that better solves your problem, if any, by pressing the checkmark sign , http://i.imgur.com/uqJeW.png

You can use indirect variable expansion with ${!...}:
va1="some-value1"
va2="some-value2"
va3="some-value3"
for ((i=1;i<=3;i++)); do
varname="va$i"
echo "$varname = ${!varname}"
done
This prints:
va1 = some-value1
va2 = some-value2
va3 = some-value3

Related

Extracting git commit information in GitHub action workflow- use of '$' symbol [duplicate]

This question already has answers here:
Backticks vs braces in Bash
(3 answers)
Brackets ${}, $(), $[] difference and usage in bash
(1 answer)
Closed 4 years ago.
I have two questions and could use some help understanding them.
What is the difference between ${} and $()? I understand that ()
means running command in separate shell and placing $ means passing
the value to variable. Can someone help me in understanding
this? Please correct me if I am wrong.
If we can use for ((i=0;i<10;i++)); do echo $i; done and it works fine then why can't I use it as while ((i=0;i<10;i++)); do echo $i; done? What is the difference in execution cycle for both?
The syntax is token-level, so the meaning of the dollar sign depends on the token it's in. The expression $(command) is a modern synonym for `command` which stands for command substitution; it means run command and put its output here. So
echo "Today is $(date). A fine day."
will run the date command and include its output in the argument to echo. The parentheses are unrelated to the syntax for running a command in a subshell, although they have something in common (the command substitution also runs in a separate subshell).
By contrast, ${variable} is just a disambiguation mechanism, so you can say ${var}text when you mean the contents of the variable var, followed by text (as opposed to $vartext which means the contents of the variable vartext).
The while loop expects a single argument which should evaluate to true or false (or actually multiple, where the last one's truth value is examined -- thanks Jonathan Leffler for pointing this out); when it's false, the loop is no longer executed. The for loop iterates over a list of items and binds each to a loop variable in turn; the syntax you refer to is one (rather generalized) way to express a loop over a range of arithmetic values.
A for loop like that can be rephrased as a while loop. The expression
for ((init; check; step)); do
body
done
is equivalent to
init
while check; do
body
step
done
It makes sense to keep all the loop control in one place for legibility; but as you can see when it's expressed like this, the for loop does quite a bit more than the while loop.
Of course, this syntax is Bash-specific; classic Bourne shell only has
for variable in token1 token2 ...; do
(Somewhat more elegantly, you could avoid the echo in the first example as long as you are sure that your argument string doesn't contain any % format codes:
date +'Today is %c. A fine day.'
Avoiding a process where you can is an important consideration, even though it doesn't make a lot of difference in this isolated example.)
$() means: "first evaluate this, and then evaluate the rest of the line".
Ex :
echo $(pwd)/myFile.txt
will be interpreted as
echo /my/path/myFile.txt
On the other hand ${} expands a variable.
Ex:
MY_VAR=toto
echo ${MY_VAR}/myFile.txt
will be interpreted as
echo toto/myFile.txt
Why can't I use it as bash$ while ((i=0;i<10;i++)); do echo $i; done
I'm afraid the answer is just that the bash syntax for while just isn't the same as the syntax for for.
your understanding is right. For detailed info on {} see bash ref - parameter expansion
'for' and 'while' have different syntax and offer different styles of programmer control for an iteration. Most non-asm languages offer a similar syntax.
With while, you would probably write i=0; while [ $i -lt 10 ]; do echo $i; i=$(( i + 1 )); done in essence manage everything about the iteration yourself

Bad substitution on bash substringing operation

I am trying to get this simple function working:
p4edit(){
p4 edit ${$1:25}
}
I read the other popular bad substitution question on SO and it did not seem to help me or be related to my problem. What am I doing wrong here? I want to cut off the first 25 characters of the argument provided to my function.
I have noticed a simple echo ${"test":3} fails the same way, but this succeeds:
test="test"
echo ${test:3}
I am just running this in a bash instance.
You have too much money! (too many dollar signs). Use:
p4edit(){
p4 edit ${1:25}
}
To extract the 25th-and-onwards characters from $1.
Why two times a $ ?
p4edit(){
echo ${1:25}
}
works fine for me. String functions in bash are a bit tricky, since they are not really consistent. But ${} already defines, that you are looking for a variable. So only submit the name to it. There are some stringfunctions with ${#var} but as far as I know, there is never a $ inside a ${}

Bash - When to use '$' in front of variables?

I'm very new to bash scripting, and as I've been searching for information online I've found a lot of seemingly contradictory advice. The thing I'm most confused about is the $ in front of variable names. My main question is, when is and isn't it appropriate to use that syntax? Thanks!
Basically, it is used when referring to the variable, but not when defining it.
When you define a variable you do not use it:
value=233
You have to use them when you call the variable:
echo "$value"
There are some exceptions to this basic rule. For example in math expresions, as etarion comments.
one more question: if I declare an array my_array and iterate through
it with a counter i, would the call to that have to be $my_array[$i]?
See the example:
$ myarray=("one" "two" "three")
$ echo ${myarray[1]} #note that the first index is 0
two
To iterate through it, this code makes it:
for item in "${myarray[#]}"
do
echo $item
done
In our case:
$ for item in "${myarray[#]}"; do echo $item; done
one
two
three
I am no bash user that knows too much. But whenever you declare variable you would not use the $, and whenever you want to call upon that variable and use its value you would use the $ sign.

Contents of ls appearing in middle of output

I am encountering a very weird situation when wrapping a bash script call in echo $(). This is strange enough that I don't know what code to present, so I will describe the general situation. I have a script, which we will call "run.sh", and it has some output. This is generally formatted quite nicely, with whitespace and line breaks.
I am trying to compare this output with a value that I got when I ran it once previously. To do this, the code compares the "new" value with the old by checking if these two are the same, i.e.:
expression=$(./runProcess.sh "$process");
expected=$(cat UnitTests/expect-process-$process);
if [ "$expression" == "$expected" ]; then
Clearly to get a value of "old" to compare with future testings I need to compute $(./runProcess.sh) by hand. When I do this, I get a version of the output with significantly less whitespace. However it is clearly wrong, because the contents of ls turn up in the middle of it. By that I mean that I get the following type of output running these two commands:
./runProcess.sh g,g:
R2With2Gluons =
+ ncol*i_*pi_^2*A*g^2 * (
- 17/24*d_(mu1,mu2)*d_(m1,m2)*p1.p1
- 31/8*d_(mu1,mu2)*d_(m1,m2)*p1.p2
- 17/24*d_(mu1,mu2)*d_(m1,m2)*p2.p2
+ 7/12*d_(m1,m2)*p1(mu1)*p1(mu2)
+ 1/24*d_(m1,m2)*p1(mu1)*p2(mu2)
+ 89/24*d_(m1,m2)*p1(mu2)*p2(mu1)
+ 7/12*d_(m1,m2)*p2(mu1)*p2(mu2)
);
0.01 sec out of 0.01 sec
echo $(./runProcess.sh g,g):
R2With3Gluons = + coeff(m1,m2,m3)*ncol*pi_^2*A*g^3 Auto Diagrams UnitTests colourCalc.frm form.set functions.frm output.frm process.frm process.mid qgraf2form.frm qgrafProcessor.py runProcess.sh runProcesses.sh test vertices.frm ( + 35/24*d_(mu1,mu2)*p1(mu3) - 35/24*d_(mu1,mu2)*p2(mu3) - 35/24*d_(mu1,mu3)*p1(mu2) + 35/24*d_(mu1,mu3)*p3(mu2) + 35/24*d_(mu2,mu3)*p2(mu1) - 35/24*d_(mu2,mu3)*p3(mu1) ); 0.40 sec out of 0.40 sec
And here is ls:
ls:
Auto form.set process.mid runProcesses.sh
Diagrams functions.frm qgraf2form.frm test
UnitTests output.frm qgrafProcessor.py vertices.frm
colourCalc.frm process.frm runProcess.sh
I can provide exact examples if necessary, but I hope this is illuminating enough. Why could this possibly be happening? I'm using bash on OS X Mountain Lion.
Use more quotes!!!
Try:
echo "$(./run.sh)"
instead. (Yes, with quotes).
Try:
old=$(./run.sh)
echo "$old"
you'll have the correct output (with $old in quotes). Now, regarding your test, use, as advised by sampson-chen:
[[ "$old" == "$(./run.sh)" ]]
(you don't need to quote the variables or the command substitution when assigning the variable old, but, as a general rule, you can use quotes every time). ((see Gordon Davisson's excellent comments to this post, that I've actually upvoted, with a few caveats about globs and quoting variables inside [[ ... ]])).
Edit. As you've edited your post, I see you're using an inefficient cat. Instead of:
expected=$(cat UnitTests/expect-process-$process)
please use
expected=$(< "UnitTests/expect-process-$process")
It's hard to say without your exact script, but for starters, your comparison:
old == $(./run.sh);
should be:
if [[ "$old" == "$(./run.sh)" ]]; then

Shell script variable problem

I'm trying to write a shell script to automate a job for me. But i'm currently stuck.
Here's the problem :
I have a variable named var1 (a decreasing number from 25 to 0
and another variable named
var${var1} and this equals to some string.
then when i try to call var${var1} in anywhere in script via echo it fails.
I have tried $[var$var1], ${var$var} and many others but everytime it fails and gives the value of var1 or says operand expected error.
Thanks for your help
It's probably better if you use an array, but you can use indirection:
var25="some string"
var1=25
indirect_var="var$var1"
echo ${!indirect_var} # echoes "some string"
There's only one round of variable expansion, so you can't do it directly. You could use eval:
eval echo \${var$var1}
A better solution is to use an array:
i=5
var[$i]='foo'
echo ${var[$i]}
It sounds like you need bash variable indirection. Take a look at the link below.
http://mywiki.wooledge.org/BashFAQ/006

Resources