This question already has answers here:
Writing shell script to print a certain number of lines with certain arguments
(3 answers)
Closed 6 years ago.
I have two variables like below:
a=10 20 30 40
b=1000 2000 3000 4000
I need the desired output like below:
10|1000
20|2000
30|3000
40|4000
How can I achieve this?
a1=($a) # if a & b are already arrays, you don't need this step
b1=($b) # directly go to the for loop. use a,b instead of a1,b1
for((i=0;i<${#b1[#]};i++))
do
echo "${a1[$i]}|${b1[$i]}"
done
should do it .
Output
10|1000
20|2000
30|3000
40|4000
Related
This question already has answers here:
How to get a variable value if variable name is stored as string?
(10 answers)
Closed 1 year ago.
I want to be able to take the values of two variables and concatenate them together to form the identifier for another variable in a bash script.
final_answer="we did it"
one="final"
two="answer"
t="${one}_${two}"
echo ${$t} # would like this to echo we did it; currently give "${$t}: bad substitution"
Not sure this is possible but it seems like bash would have this capacity somehow.
Thank you!
$ echo "${!t}"
we did it
See http://mywiki.wooledge.org/BashFAQ/006#Indirection for details.
This question already has answers here:
Looping over pairs of values in bash [duplicate]
(6 answers)
Closed 3 years ago.
I have a folder with test cases named like test_1_in, test_1_out,
test_2_in, test_2_out etc. I want to write a script, that can test my program with these pairs, I suppose it would do something like this in a loop
diff <($program < $test_in) <($test_out)
So the question is what is the best way to do it in Bash? How to iterate through the pairs of files? And also how to capture exception, that is print some error message if diff shows some difference?
Iterate over *_in, and generate *_out using parameter expansions:
for test_in in test_*_in; do
test_out="${test_in%_*}_out"
if diff <("$program" "$test_in") "$test_out"; then
echo "$test_in failed."
fi
done
This question already has answers here:
Looping over pairs of values in bash [duplicate]
(6 answers)
Closed 1 year ago.
I have a folder with 18 pairs of files with the same name except one has an R1 and the other an R2 , e.g. SM000907_S1_R1.fastq and SM000907_S1_R2.fastq.
I would like to throw a command in a loop for all these pairs.
I've tried the following loop, but it's not working (it throws the error "wrong substitution"):
for sample in ${seq 1 18}; do
merge-paired-reads.sh SM000907_S$sample_R1.fastq SM000907_S$sample_R2.fastq > output
done
You can use either $(seq 1 18) or {1..18}, but not both at the same time.
Also, your command line does not work because you are using different variable names inadvertently. Surround them with braces.
Finally, as a good practice, quote all the strings that contain a variable:
for sample in {1..18}; do
merge-paired-reads.sh "SM000907_S${sample}_R1.fastq" "SM000907_S${sample}_R2.fastq" > output
done
This question already has answers here:
Bash - variable variables [duplicate]
(4 answers)
Dynamic variable names in Bash
(19 answers)
Closed 4 years ago.
Suppose I have several variable names like
V_1_T = a
V_2_T = b
V_3_T = c
...
and I want to extract the pointers a, b , c, ... in a bash loop in order to concatenate the values. My explicit wish is about reconstructing a message separated in several parts as explained in the gammu-smsd documentation.
I've tried the example in the doc but it doesn't work. The reason is that the code never points to the pointer of the variables but to the variables themselves, i.e. I get V_1_T at best and never a as I would.
I've also tried to put
${V_${i}_T} ; ""$"V_${i}_T"
with and without escape symbols for the commas, ..., but nothing worked ...
Any ideas ?
I'm working on the latest version of Raspbian + RaspberryPi.
Use indirect parameter expansion:
for i in 1 2 3; do
t="V_${i}_t"
echo "${!t}"
done
This avoids the use of eval shown in the docs you linked to.
This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 4 years ago.
I'm trying to assign a variable with a value I get from grep, here's my code
a="i
am
a
string
"
b="$a"|grep am
echo "$b"
I expect the result is am, but the result b is empty. But when I code echo "$a"|grep am directly, I get the right result. why and how can I assign the result to b?
Do it like this:-
**b=$(echo "$a"|grep am)
**