for loop in shell script now working as expected [duplicate] - bash

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

Related

Bash - Renaming episode names to remove junk [duplicate]

This question already has answers here:
Rename files using a regex with bash [duplicate]
(4 answers)
bulk file renaming in bash, to remove name with spaces, leaving trailing digits
(3 answers)
Rename multiple files based on pattern in Unix
(24 answers)
Better way to rename files based on multiple patterns
(5 answers)
Closed 12 months ago.
When I am looking at episode names, often they're in the format of:
The.Show.Name.S06E01.lots.of.junk-often_with_chars.mkv
The.Show.Name.S06E02.lots.of.junk-often_with_chars.avi
Show.Name.S06E03.lots.of.junk-often_with_chars.mp4
I'd like to end up with a simple:
Show Name S06E01.mkv
Show Name S06E02.avi
Show Name S06E03.mp4
Ideas on a simple bash command or two I could run to clean up all the junk, remove the periods other than the end, allow for any three-digit file extension?
Thank you.

grep names in a small file matching a large file [duplicate]

This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
grep not showing result which read id from file
(2 answers)
Closed 12 months ago.
My small file contains this information line by line:
abc.123
abc.258
abc.952
I wanted to get those lines matching in my bigger file (~30Gb). I tried this command but it didn't give me any result.
grep -f small.txt big.txt
I have tested all abc.123, abc.258 and abc.952 does exist in my bigger file, meaning that I tried to grep each of these names one by one it gave me the exact result I want.
grep "abc.123" big.txt
I have no idea where I could possibly go wrong?

Bash : for i in *.xls return one result even if there is not xls's files [duplicate]

This question already has answers here:
How to skip the for loop when there are no matching files?
(2 answers)
How to match nothing if a file name glob has no matches [duplicate]
(2 answers)
Closed 1 year ago.
I've got an empty directory. The command:
for i in *.xls; do echo $i; done
return
*.xls
Could you explain why? And how to correct this please?
Thanks

How to iterate over pairs of files with corresponding names in Bash [duplicate]

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

loop over pairs of files with common name in bash [duplicate]

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

Resources