Examples+in+shell+scripting

=Miscellaneous BASH scripts with brief explanations for their use=

My philosophy on scripting is simple... keep it simple, don't do too much, make it general (if necessary). As I learn new tricks, I try to replace older scripts. Additionally, I'm trying to go back and simplify some of the examples so it is clearer what is being done exactly.

code for i in *.bmp; do convert $i ${i%bmp}png; done code code awk '/     C/&&v++%2{sub(/      C/, "     CX")}{print}' acn.gro code code format="bash"
 * 1) replace every other occurrence of a pattern
 * 1) !/bin/bash

perl -0777 -i -pe 's/S H4 0\n1.0  1.5/S H4 0\n1.0   1.0/igs' *.intinpr code code format="bash"
 * 1) One-liner that reads file into memory (-0777) then edits in place a multiline regex (add -i.original to keep a copy)
 * 1) !/bin/bash

for i in *.chk; do x=${i%.chk}; file=`echo ${x##*/}`; echo "Chk: $file.chk to WFX: $file.wfx"; formchk $i >& /dev/null; aimqb.ish -verifyw=only -atoms=all -nogui $file.fchk >& /dev/null; done code code format="bash"
 * 1) Creates wfx from g09 chk (requires free or paid version of AIMAll)
 * 1) !/bin/bash

declare -a Nodes declare -a Procs

Nodes=(1 1 1 1 2 3 4 5 6 7 8) Procs=(1 2 4 12 12 12 12 12 12 12 12)

n=${#Nodes[*]}

for ((i=0; i<$n; i++)); do sed s~"NNNNN"~"${Nodes[$i]}"~g OSC-biphase1 | sed s~"MMMMM"~"${Procs[$i]}"~g | qsub sed s~"NNNNN"~"${Nodes[$i]}"~g OSC-biphase2 | sed s~"MMMMM"~"${Procs[$i]}"~g | qsub done code code
 * 1) In vim, delete lines by...

m a d ' a code code format="bash"
 * 1) alter queue time and release holds at OSC

for i in {3111924..3111997}; do if qstat | grep $i; then qalter -l walltime=72:00:00 $i; qrls $i; fi; done code code format="bash"
 * 1) Recursively touch all files and subdirectories inside a main file.
 * 2) Semi-colon terminates the -exec command and must be escaped (the \)

find. -exec touch {} \; code code format="bash"
 * 1) Quick gnuplot plots

cat > foo.plg <<EOF set terminal png set output "out.png" plot filename u 1:2 EOF

gnuplot -e "filename='file.dat'" foo.plg code code format="bash"
 * 1) !/bin/bash

echo "Instructions: Run in directory with stdout and stderr, redirect the files with code on the second line." echo "Purpose: Copies completed jobs back to a local directory (-p) flag prevents wasting time copying already copied files back."


 * 1) Some content altered for privacy purposes (XXXXXX).

for i in *.o; do x=${i%.o}; file=`echo ${x##*/}`; if cat $file.o | grep -q "00015.*Done"; then job=`grep "deffnm" $file.o | sed s~".*deffnm "~""~g`; cp -p /scratch/XXXXXX/lsf.$file/$job.* /home/XXXXXX/XXXXXX/data_solvents_excess/ethanol_sims; fi; done

code code format="bash"
 * 1) Use sed to replace newline characters in file with single spaces

sed ':a;N;$!ba;s/\n/ /g' tmp.dat > tmp2.dat code code format="bash"
 * 1) !/bin/bash


 * 1) Run through each output directory and check for .gro file. If exists, leaves. If not exist, moves to an error directory.

for i in gromacs-30_md_min-*; do cd $i; count=`ls -1 *.gro.gz 2>/dev/null | wc -l`; if [ $count != 0 ]; then cd ..; else cd ..; mv $i err/; fi; done code code format="bash"
 * 1) !/bin/bash


 * 1) Read file and remove duplicates at column $1

awk '!a[$1]++' allData1.dat > tmp code code format="bash"
 * 1) !/bin/bash

cat > react.gjf < ts.gjf < eof.gjf <ATOM/,/TRIPOS>BOND/p' $file.mol2 | cut -c 8-64 | tail -n +2 | head -n -3 > $file.foo cat react.gjf $file.foo eof.gjf > $file.gjf mv $file.mol2 mol2/; rm $file.foo fi echo "~pollard/submit_g09.qsub $file" | qsub -N $file -q maxrun10 -l nodes=1:cores8:ppn=8:g09 done

for i in T-*.mol2; do x=${i%.mol2}; file=`echo ${x##*/}` if [ ! -f $file.gjf ]; then sed -n '/TRIPOS>ATOM/,/TRIPOS>BOND/p' $file.mol2 | cut -c 8-64 | tail -n +2 | head -n -3 > $file.foo cat ts.gjf $file.foo eof.gjf > $file.gjf mv $file.mol2 mol2/; rm $file.foo fi echo "~pollard/submit_g09.qsub $file" | qsub -N $file -q maxrun10 -l nodes=1:cores8:ppn=8:g09 done

rm eof.gjf react.gjf ts.gjf

code code format="bash"
 * 1) Awk is used here to seek a string and print the 2nd line after a match (getline; getline means print the 2nd line not 1st and 2nd)

analyze_ions f-coords_105.arc -k f.key a 316 | awk '/Atom         EV          EM              EP/{getline;getline; print}' code code format="bash" rm *.1* *.2* *.3* *.4* *.5* *.6* *.7* *.8* *.9*

echo "Extracting frames... allowing 500 snapshots as equilibration." for i in *_5.arc; do x=${i%.arc}; file=`echo ${x##*/}`; ion=`echo ${x%-coords*}` frames=`grep "Water" $file.arc | wc -l` begin=500 step=4 atoms=`head -n1 $file.arc | cut -c 4-6 | sed s~" "~""~g` archive $file.arc 2 $begin $frames <<%EOF%

%EOF% echo "Zeroing ion coordinate and translating cluster... (please wait)" for ii in $(seq $begin $step $frames); do xyzedit $file.`printf "%03d" $ii` -k genericAMOEBA.key <<%EOF% 12 $atoms

%EOF% mv $file.xyz $file.`printf "%03d" $ii` done for ii in $(seq $begin $step $frames); do tail -n +2 $file.`printf "%03d" $ii` | cut -c 9-50 > $file-`printf "%03d" $ii`.foo if | $ion == 'cl' || $ion == 'f' ; then grep "[BlFNLK]" $file-`printf "%03d" $ii`.foo > ion.txt; grep -v "[BlFNLK]" $file-`printf "%03d" $ii`.foo > nion.txt cat head.txt ion.txt sep.txt nion.txt tail.txt > $file-`printf "%03d" $ii`.psi4_rimp2 sed -i s~"CHARGE"~"-1"~g $file-`printf "%03d" $ii`.psi4_rimp2 elif | $ion == 'na' || $ion == 'k' ; then grep "[BlFNLK]" $file-`printf "%03d" $ii`.foo > ion.txt; grep -v "[BlFNLK]" $file-`printf "%03d" $ii`.foo > nion.txt cat head.txt ion.txt sep.txt nion.txt tail.txt > $file-`printf "%03d" $ii`.psi4_rimp2 sed -i s~"CHARGE"~" 1"~g $file-`printf "%03d" $ii`.psi4_rimp2 fi rm $file.`printf "%03d" $ii` $file-`printf "%03d" $ii`.foo done done

mkdir -p test_out; mv *.psi4_rimp2 test_out rm *.txt rm *.1* *.2* *.3* *.4* *.5* *.6* *.7* *.8* *.9* code

TPP 19.1.15