bash scripting a beginners guide to bourne again shell scripting
TRANSCRIPT
BASH Scripting
A beginners guide to Bourne Again SHell scripting
BASH script header
On the first line #!/bin/bash - Tells the operating system that the following will be
a script and not a regular text file You add - to the end to prevent users from spoofing
the interpreter Change the file to give it execute permissions
chmod +x test.sh
How scripts work
#!/bin/bash -- echo ”Hello” ls -al
/bin/bash echo ”Hello” /bin/bash ls -al
Variables – Global vs Local
defocus=”0.5” function defocus {
local defocus=”0.2” echo $defocus
} echo $defocus Output: 0.2 0.5
Executing shell commands
echo ”ls -al” echo `ls -al` Output: ls -al total 16 drwxr-x--- 2 user group 4096 2010-01-11 11:11 . drwxr-x--- 76 user group 4096 2011-06-01 17:21 ..
BASH menus
echo ”1) shorten file name” echo ”2) convert tifs into pifs” read -p ”selection: ” choice case $choice in 1 | [Ss][Hh][Oo][Rr][Tt]) echo ”you selected
shorten file names” ;; 2) echo ”you selected convert tifs” ;; *) echo ”nothing selected” ;; esac
Test operator
curDir=`basename $PWD` #curDir is /usr/tif if [ ”$curDir” != ”pif” ]
then echo ”you are not in the pif directory”
else fi
Rename a file
Input: micrograph image from bob.tif ls *.tif | while read file
do mv ”$file” ”`echo ”$file” | nawk ' BEGIN {OFS=”_”}
$1=$1 ' ` ” done Exit
Output: micrograph_image_from_bob.tif
Convert tifs into pifs
Directory has files 1.tif, 2.tif, etc read -p "What is the bin factor? (1,2,3) " bin ls -1 *.tif | awk -v bin=$bin -F. ' { newFile = $1;
printf( "tiff2pif %s %s.pif %s\n", $0, newFile, bin ); }' | bash
Output: tiff2pif 1.tif 1.pif 2
Shorten micrograph file name
Input: 5-26-11-ev71-Mab-dataset-120000.0V-50000X-0001.tif
ls -1 *.tif | awk -F- ' { newFile = $9; sub(/0*/, "", newFile); printf( "mv %s %s\n", $0,newFile); }' | bash
Output: 1.tif
Rename box files
Input: 1_box.pif ls -1 *box.pif | awk -F_ ' { newFile = $1;
printf( "mv %s %s.pif\n", $0, newFile ); }' | bash Output: 1.pif
Change the color of pdbs
Input file: pdb Input: x, y, z value; beginning and end value awk -v x=$x -v y=$y -v z=$z -v beg=$beg -v
end=$end ' { FIELDWIDTHS = "4 7 5 4 2 4 12 8 8 6 6 6 6" } $1=="ATOM" { NR>=beg && NR<=end; $7=x; $8=y; $9=z; print $1""$2""$3""$4""$5""$6" "$7" "$8" "$9" "$10""$11""$12""$13 } ' $pdb > color_$pdb
Create dats
Input: pixel size and voltage awk -F, ' {printf("echo \"../images/%s.pif\" >
%s.dat_000\n", $1, $1) } ' defocus.csv | bash awk -v pixDec=$pixDec -v volt=$volt -F,
'{printf("echo \" %s, 1, %s000.0, 0.0700, %s, %s, 0.0000, 2.00\" >> %s.dat_000\n", pixDec, volt, $2, $2, $1) } ' defocus.csv | bash
Output: ../images/1.pif 4, 1, 12000.0, 0.0700, 2.5, 2.5, 0.0000, 2.00