This post contains some bash
code-snippet to make my daily work easier.
Loop through array
In case of looping through an continous list of number:
If the list to loop is not continous, used the following form:
Note that arr
definition is without whitespace.
Counting number of lines in file
Sometime, after perform a long analysis of many simulations. We want to make sure if the analysis is done automatically, instead of looking into single file one-by-one, we can use bash
script to do so.
#!/bin/bash
for i in {1..100}
do
#echo "$i"
nl=$(wc -l < $i/G_traj_${i}.dat)
if [ $nl -eq 400008 ]; then
echo "traj $i : $nl --- DONE"
else
echo "traj $i : $nl --- Has not been DONE"
fi
done
In this example, we loop through all simulations (1 to 100) and use command: wc -l < $i/G_traj_${i}.dat
to count the number of lines in file. we assign the output from this command to nl
variable by packing previous command by $(command)
Search and replace in file
#!/bin/bash
cur_dir=`pwd`
for i in {1..100}
do
if [ -f "/net/people/plgqvuvan/plggligroup/qvuvan/10proj/synthesize/cat3/wt/$i/traj/1/prot_l213_dissociation_final.cor" ]; then
echo "copy cor and vel file for run $i"
rm -rf $i && mkdir $i
cp /net/people/plgqvuvan/plggligroup/qvuvan/10proj/synthesize/cat3/wt/$i/traj/1/prot_l213_dissociation_final.cor $i/
cp /net/people/plgqvuvan/plggligroup/qvuvan/10proj/synthesize/cat3/wt/$i/traj/1/prot_l213_dissociation_final.vel $i/
cp template/* $i/
sed "s/SETINDEX/${i}/g" -i ${i}/job_plgrid_gpu.sh
sed "s/SETINDEX/${i}/g" -i ${i}/control.cntrl
else
echo "simulation $i is not finished"
fi
done
- line 5: check if file
prot_l213_dissociation_final.cor
in specified directory existed. If yes, then run the code block. - line 11-12:
sed
command used to search and replace patternSETINDEX
in file${i}/job_plgrid_gpu.sh
by value of variable$i
Sync data between local and cluster computer
#!/bin/bash
rsync -avzhP plgqvuvan@pro.cyfronet.pl:/net/people/plgqvuvan/plggligroup/qvuvan/10proj/post_trans/cat3/stride_by5/combine_trajs/ post_trans/
The above command sync the data from cluster pro.cyfronet.pl to local computer. To exclude files (e.g xtc file), add this: –exclude=*.xtc