Skip to content

Instantly share code, notes, and snippets.

@telatin
Created March 5, 2018 16:14
Show Gist options
  • Save telatin/40525157f1a169712030316e181947f5 to your computer and use it in GitHub Desktop.
Save telatin/40525157f1a169712030316e181947f5 to your computer and use it in GitHub Desktop.
#!/bin/bash
# A first example of 'for loop' to iterate a set of commands over a list of files
SamFilesCount=$(ls *.sam | wc -l);
if [[ $SamFilesCount -eq 0 ]]
then
echo " Warning: there are no .sam files to process in this directory!"
exit;
fi
for SamFile in *.sam
do
echo -n "Converting ${SamFile}... "
if [[ -s "${SamFile}" ]]
then
samtools view -bS ${SamFile} | samtools sort -o ${SamFile/.sam/.bam} -
samtools index ${SamFile/.sam/.bam}.bam
echo "Done!"
else
echo "Skipping $SamFile: it's empty!"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment