Skip to content

Instantly share code, notes, and snippets.

@kinow
Last active October 7, 2021 00:13
Show Gist options
  • Save kinow/8188e5a74582d3291435393c281a0133 to your computer and use it in GitHub Desktop.
Save kinow/8188e5a74582d3291435393c281a0133 to your computer and use it in GitHub Desktop.
Files loops
File1.fastq
File2.fastq
#!/bin/bash
set -e
# https://github.com/koalaman/shellcheck/wiki/SC2013
while IFS= read -r file
do
echo "Processing file ${file}"
[[ "${file}" =~ ^File([0-9]+).fastq$ ]] && true
index=${BASH_REMATCH[1]}
if [ -z "${index}" ] || [ "${index}" = "" ]; then
echo "Could not locate parameters for file ${file}!" 1>&2
exit 1
fi
params_file="parameter_list${index}.txt"
if [ ! -f "${params_file}" ]; then
echo "Parameters file ${params_file} not found!" 1>&2
exit 1
fi
echo "Parameters file ${params_file}"
parameters=$(head -n 1 "${params_file}")
command_line="blabla ${file} ${parameters}"
echo "Command: ${command_line}"
done < file_list.txt
echo "Bye!"
exit 0
@kinow
Copy link
Author

kinow commented Oct 7, 2021

#!/bin/bash
set -e # for errors

# https://github.com/koalaman/shellcheck/wiki/SC2013
while IFS= read -r file  # kind of meh, can be a for-loop too, but `shellcheck job.sh` fails...
do
  echo "Processing file ${file}"
  [[ "${file}" =~ ^File([0-9]+).fastq$ ]] && true # regex to extract the number, which fails for File.fastq, so we just ignore the exit code with "or true" that returns exit 0
  index=${BASH_REMATCH[1]} # match 0 is the string File1.fastq, and then each entry of the hash contains the `(...)`matches from the regex
  if [ -z "${index}" ] || [ "${index}" = "" ]; then # error handling, you can ignore it
    echo "Could not locate parameters for file ${file}!" 1>&2
    exit 1
  fi
  params_file="parameter_list${index}.txt" # create the name of the file
  if [ ! -f "${params_file}" ]; then # error handling, you can ignore it
    echo "Parameters file ${params_file} not found!" 1>&2
    exit 1
  fi
  echo "Parameters file ${params_file}"
  parameters=$(head -n 1 "${params_file}") # guess it's safer to get just the first line?
  command_line="blabla ${file} ${parameters}" # le command
  echo "Command: ${command_line}"
done < file_list.txt

echo "Bye!"

exit 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment