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
Related
This question already has answers here:
How to match nothing if a file name glob has no matches [duplicate]
(2 answers)
How to skip the for loop when there are no matching files?
(2 answers)
Closed 3 days ago.
I have some output files like output1.bin, output2.bin and I need to add them to the list output_files. It works as expected when output files exists, but it adds "output*.bin" in the list variable if output1.bin, output2.bin does not exists. I expected that if "${outputdir}"/output*.bin doesn't match any file it should not even iterate through it i.e. I expeted the list to be empty but it adds output*bin name to it. Can someone explain the behavior and the fix ?
for bin_file in "${outputdir}"/output*.bin; do
output_files+=("${bin_file}")
done
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:
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:
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