Scripts and Code Snippets
Contents
- 1 Bash scripts
- 2 Bash one-liners
- 2.1 Untar Everything In This Directory
- 2.2 Use bc to solve simple math expressions
- 2.3 Backup my SQL database
- 2.4 Dump all mySQL tables with a certain prefix
- 2.5 Backup my public html files
- 2.6 Count all the tif files in all the subdirectories
- 2.7 VirtualBox: mount virtual shared folder
- 2.8 ImageMagick: Crop out the center of all the images in a directory
- 2.9 Go into each subdirectory and output the first 10 images in each
- 2.10 ImageMagick: Convert all tiffs in this dir to 1 channel grayscale
- 2.11 Use egrep to output only certain stuff from a tiffdump
- 2.12 show files in this dir from largest to smallest
- 2.13 Take the first n objects from a directory and copy them elsewhere
- 2.14 Delete all files for which there isn't some accompanying file
- 2.15 SVN checkout
- 2.16 Automator script
- 2.17 Count lines of files in list
- 2.18 Combine FASTQ files
- 3 Perl
- 4 Javascript
- 5 C++
- 6 Python
- 7 TeX
Bash scripts
- Backup my SQL database
- Backup my public_html files
- Untar Everything In This Directory
- Dump all mySQL tables with a certain prefix
- Use bc to solve simple math expressions
- Rename everything in this directory to (number).jpg
- Go into each dir and rename everything in it to dir_name(number).jpg
- Rename xxxxx.jpg to xxxxx.tif
- nightly_build.sh
- switch_matlab_license.sh
- set_up_working_set.sh - Has complex finds with or'ed expressions
- convert_og_to_mp3.sh
- make_thumbnails.sh - shell script input arguments, find with printf, and ImageMagick convert. Arithmetic expressions use (( a > b )) syntax
Bash one-liners
Untar Everything In This Directory
for i in *.tar.gz; do tar -xvzf $i; done
Use bc to solve simple math expressions
#!/bin/sh #solve.sh bc << EOF scale=4 $@ quit EOF
Backup my SQL database
#!/bin/sh NOW=`date '+%Y-%m-%d__%H-%M-%S_%p'` echo "making database backup on $NOW..." /usr/local/mysql/bin/mysqldump -u user -ppassword -h db_host_name DatabaseName > ~/backups/db_backup_$NOW.sql
Dump all mySQL tables with a certain prefix
#/bin/sh # -B = run batch command mysql -u username -ppassword -h db \ -B --skip-column-names INFORMATION_SCHEMA \ -e "SELECT TABLE_NAME FROM TABLES WHERE TABLE_NAME LIKE 'pre_%'" \ | xargs mysqldump -u username -ppassword -h db -B database_name --tables > ~/drupal_db_backup.sql
Backup my public html files
#!/bin/sh CURR_DIR=`pwd` NOW=`date '+%Y-%m-%d__%H-%M-%S_%p'` echo "\nMaking backup of docs folder on $NOW..." cd /web/chriscoletta.com/ tar zcvf ~/backups/backup_files_$NOW.tar.gz docs/ cd $CURR_DIR
Count all the tif files in all the subdirectories
for i in *; do if [[ -d $i ]]; then echo $i ls ${i}/*.tif | wc fi done
- note the space between if and [[
mount -t vboxsf virtual_shared_folder /home/boincclient/Desktop/virtual_shared_folder
ImageMagick: Crop out the center of all the images in a directory
for i in ../../originals/heads/*.tif; do echo $i BASENAME=`echo $i | sed -r "s/.tif$//g"` convert "$i" -gravity Center -crop 90x90+0+0 +repage "${BASENAME}_cropped.tif" mv -v "${BASENAME}_cropped.tif" . done
Go into each subdirectory and output the first 10 images in each
for i in *; do echo $i; count=0; for j in $i/*.tiff; do if (( $count < 10 )); then echo "$count $j"; fi; count=$(($count+1)); done; done
ImageMagick: Convert all tiffs in this dir to 1 channel grayscale
for i in *.tif; do BASENAME=`echo $i | sed -r "s/\.tif$//"`; convert "$i" -colorspace Gray -flatten +matte "${BASENAME}_gray.tif"; done
Use egrep to output only certain stuff from a tiffdump
for i in *; do ls -lh $i; tiffdump $i | egrep 'ImageWidth|ImageLength'; done
show files in this dir from largest to smallest
ls -lhS
- USE CAPITAL S
Take the first n objects from a directory and copy them elsewhere
#!/bin/bash for dir in `find ../josiah_worms -type d`; do echo $dir name=`echo $dir | gsed -r "s/^.*\///"` mkdir $name count=0 for tiff in `find $dir -name "*.tiff"`; do if [[ $count -lt 3 ]]; then echo $tiff count=$(( $count + 1 )) basename=`echo $tiff | gsed -r "s/\.tiff//"` cp -v ${basename}.* $name cp -v ${basename}_* $name fi done done
Delete all files for which there isn't some accompanying file
#!/bin/bash for sigfile in `find . -name "*.sig"`; do basename=`echo $sigfile | sed -r "s/-.+\.sig//"` echo $basename if [ ! -e ${basename}.tif ]; then echo "no tiff for sigfile $sigfile" rm -v $sigfile fi done
SVN checkout
- Sequentially check out code from every day and do a line count on the project
- requires CLOC perl script and gnu date
#!/bin/bash for i in {1..221} do the_date=`date +"%Y-%m-%d" -d "2012-06-27 +$i day"` echo $the_date svn co -r {$the_date} http://wnd-charm.googlecode.com/svn/pychrm/trunk pc_trunk svn info pc_trunk | grep Revision > ${the_date}.txt cloc --csv pc_trunk >> ${the_date}.txt rm -rf pc_trunk done
Automator script
for i in "$@" do echo "processing $i" >> ~/local_watch_folder.log scp $i lgchrm19:/home/eckleyd/RealTimeClassification/ BASENAME=$(basename "$i") echo "basename is $BASENAME" >> ~/local_watch_folder.log ssh lgchrm19 "sh -c 'cd /home/eckleyd/RealTimeClassification/; nohup ./ScorePharynxBalanced.py $BASENAME > $BASENAME.log 2>&1 < /dev/null&'" mv $i /Users/eckleyd/Documents/RealTimedone done
Count lines of files in list
cat 20180727_filelist.txt | xargs -I {} sh -c "echo {}; zcat {} | wc -l" | tee linecounts.txt
Combine FASTQ files
COUNTER=1 for THISDIR in $(cat 20180727_dirlist.txt); do cd $THISDIR THESEARGS=$(ls -1 | paste -sd " " -) #CMD=$(printf "cat %s > %s.fastq.gz" "$THESEARGS" $THISDIR) #CMD=$( echo "cat $THESEARGS > $THISDIR.fastq.gz" ) #CMD=$( echo "pv -B 100m $THESEARGS > $THISDIR.fastq.gz" ) #DBG_MSG=$(printf "%3d: \"%s\"\n" $COUNTER "$CMD") #echo $DBG_MSG #`$CMD` printf "%3d" $COUNTER pv -B 100m $THESEARGS > $THISDIR.fastq.gz cd .. COUNTER=$(( $COUNTER + 1)) done
Perl
- Perl chomp user input example
- wndchrm_smoke_test.pl
- interior_image_finder.pl - Has complex array operations, arrays of hashes of arrays, file operations, etc.
- wndchrm test harness
- white_space_analyzer.pl - runs another executable and processes return value
- create_full_image_coverage_working_set.pl - finds the intersection of two lists, creates a third list, creates hard links, breaking out of nested loops with labeled next (break in C++ parlance)
- parse_html.pl - parse an html file apart for its tables and compile lists of the data contained therein.
- make_zeiss_pointlist.pl -- Perl CGI script and html form file
- create master dend file
- create_probability_density_points_from_html.pl - make kernel-smoothed probability density functions from a wndchrm report file.
- leave_one_out.pl - Perl 5.10 specific - uses ${^POSTMATCH} functionality to systematically digest strings of repeating patterns using regexp
- mds.pl - parse a text file, then pass arrays to subs for processing
- numerically_equivalent.pl
- subroutines
- passing command line args to a sub main
- usage of references in subroutine arguments
- use of quotemeta in regexp
- numerical sort syntax sort {$a <=> $b} ...
- reorder_list.pl - using sub to sort
- convert_m4a_to_mp3.pl
- classify_worm.pl - uses stat() to check file size and time of creation, and module Linux::Inotify2 to setup a callback that executes on writing a file to disk.
- codon_eval.pl - uses <STDIN>
perl -lne '$line = $_; /\/(\d\d\d)\//; print $line . "\t$1"' new_fake_sigs.fof > new_fake_sigss.fof
- perl one-liner to add something to the end of every line
- -e = execute the line of perl code enclosed in the single quotes
- -n = iterate over every line in the file
- -l = strip-off the newline of each line in the file, and add one back at the end of every iteration
Javascript
C++
- Increment after assignment
#include <iostream> #include <vector> using namespace std; int main() { vector<double> array (10, 10); vector<double>::iterator it = array.begin(); *it++ = 20.0; *it++ = 30.0; *it++ = 40.0; vector<double>::iterator other_it = array.begin(); for( ; other_it != array.end(); ++other_it ) cout << *other_it << endl; return 0; }
outputs:
chris@dk8rb2a19maclt2 ~ $ ./test_iterator 20 30 40 10 10 10 10 10 10 10
- fake_signature.cpp - using GNU scientific library random number generator, writing files, and directory manipulation in POSIX environment
- dump some stuff out into a time stamped file
#include <iostream> #include <iomanip> #include <fstream> #include <sstream> #include <time.h> #include <sys/time.h> time_t ltime; struct tm *Tm; struct timeval detail_time; ltime=time(NULL); Tm=localtime(<ime); std::ostringstream filename; filename << (Tm->tm_year+1900) << '-' << setw(2) << setfill('0') << (Tm->tm_mon+1) << '-' << setw(2) << setfill('0') << Tm->tm_mday << "_" << setw(2) << setfill('0') << Tm->tm_hour << ':' << setw(2) << setfill('0') << Tm->tm_min << ':' << setw(2) << setfill('0') << Tm->tm_sec << "."; gettimeofday(&detail_time,NULL); filename << setw(2) << detail_time.tv_usec / 1000; filename << "pixel_dump.txt"; std::cout << filename.str() << std::endl; ofstream pixel_dump_file ( filename.str().c_str(), ios::app ); int count = 0; int numpix = width*height*depth; while (count < numpix) { pixel_dump_file << data[count].intensity << std::endl; ++count; } pixel_dump_file.close();
Python
- pycharm_scale_px.py - scale pixel intensities to 16 bit range
- digest_network.py - gives statistics on node networks
- calculate_sig.py - WND-CHARM signature calculator
- classify_pharynx.py - Mark's real-time classification script
- copy_itunes_top_rated_to_here.py - digests an iTunes export files and systematically copies files to an external directory
- watch_folder.py
- deactivate and uninstall certain macports listed in a file
from subprocess import call with open( "universal5.txt") as file: for line in file: universal_line = line.replace( " (active)", "" ).strip() no_u_line = universal_line.replace( "+universal", "") no_u_line = "sudo port activate {}".format( no_u_line ) call( no_u_line, shell=True ) universal_line = "sudo port uninstall {}".format( universal_line ) print universal_line call( universal_line, shell=True )