Loops are powerful in that they can substantially reduce the time in processing
The statements can be written in one line too instead of block, but legibility is better for block, so, it is preferred.
#############################################For loop
#Code will print 0 to 10, in a column
a=10
for i in $(seq 0 $a ); do echo $i;done
--------------------------
#Code will run a For loop using the list file as argument. This code reads the list and prints and copies.
for i in `cat list`;
do echo "Welcome $i times"
cp "$i" "$i".bak
done
--------------------------
echo "Bash version ${BASH_VERSION}..."
for i in 1 2 3 4
do echo "Clap $i times"
done
--------------------------
echo "Bash version ${BASH_VERSION}..."
for i in {1..10}
do echo "Clap $i times"
done
--------------------------
for isolate in path
do echo $isolate"
grep -w $isolate|sort $1;
done
--------------------------
#Code will run a For loop using the files in the directory 'data_analysis' that start with pattern 'isolates', prints the legend and and extracts the first line.
for file in /home/seema/data_analysis/isolates*
do
echo -n "Result is: "
grep -v '^\s*$' "$file" | head -n1
done > data_result
#
for file in /home/pseema/denovo_analysis/homology_results/*
do
echo "hi" | wc -l
done < /home/pseema/denovo_analysis/input_files/IS_element_list
#
for isolate in $(cat file)
do
awk '{if ($1 >= 450 && $1 <= 500 && $2 >= 750 && $2 <= 1000) {print $1,$2}}' file > out_file
done
#
tuberculosis= (`ls dir/*.bam`)
for i in ${tuberculosis [*]}
do
bamtools coverage -in $i -out ${i%.file}_$cvg
done
#Nested for loop (outer loop starts, inner loop starts, inner loop ends, out loop ends)
outer=1 # Set outer loop counter
# Beginning of outer loop.
for a in 1 2
do
echo "####Pass $outer in outer loop####"
inner=0 # Reset inner loop counter
for b in 1 2
do
echo "####Pass $inner in inner loop####"
let "inner+=1" # Increment inner loop counter
done
let "outer+=1" # Increment outer loop counter.
echo # Space between output blocks in pass of outer loop
done
exit 0
#Another nested loop example
for i in 1 2; do for j in a b ; do echo $j; done; done
#Creates numbers starting 00 to 99
for i in 0 1 2 3 4 5 6 7 8 9
do
for j in 0 1 2 3 4 5 6 7 8 9
do
echo "$i$j"
done
done
#Another modification of the above code
for i in $(seq 0 9)
do
for j in $(seq 0 9)
do
echo "$i*$j"
done
done
#Use of for loop in a function
mpileup_func ()
variable_name= (`ls $path/file.bam`)
for i in ${variable_name [*]}
do
samtools mpileup -BI -f $ref $i > ${i%.bam}.$mpileup
done
#Copy a single file
for i in file do copy i path_to_dir
#Copy multiple files
for i in file1 file2 do copy i path_to_dir
-----------------------------------
#To download documents on the terminal from a serverfor i in {1..22}
do
wget "ftp://ftp.ncbi.nih.gov/genomes/Homo_sapiens/CHR_${i}/hs_alt_CHM1_1.1_chr${i}.fa.gz"
gunzip "hs_alt_CHM1_1.1_chr${i}.fa.gz"
done
While loop
Suppose input_file has data
1
2
3
#Read a input list and find corresponding files and perform the commands
while read i;do
echo $i
done < input_file
1
2
3
-----------------------------------------------------#Read a input list and find corresponding files and perform the commands
while read isolate;
do
echo " Result found"
done < input_file
-----------------------------------------------------
#e.g.
#For the code to run an isolate list and the fasta sequences are needed
while read isolate;
do
blastn -query gene1 -subject $isolate.fasta | grep 'Strand'| wc
blastn -query gene2 -subject $isolate.fasta | grep 'Strand'| wc
done < isolate_list >> blast_result
-----------------------------------------------------
#Nested loop (great to compare isolate with isolate genome)
while read strain;
do
while read isolate;
do
commands...
done < input_file
done < input_file
#############################################
Foreach loop
#! /bin/bash
foreach person (Allen Mary Sam)
echo "$person"
end
exit 0
-----------------------
#! /bin/bash
foreach user ($argv)
echo -n "$person: "
grep "^"$user"." file | cut -f1 -d':'
end
exit 0
No comments:
Post a Comment