Computer tools > bash

bash

Summary

Hello_World.sh

#!/bin/bash
echo Hello World

transpose.sh

#!/bin/bash
# Usage: tranpose file
file=$1
awk '{for (j=1; j<=NF; j++) {tab[NR,j]=$j}; n_col=NF} END {for (j=1; j<=n_col; j++) {for (i=1; i<=NR; i++) {printf "%s ",tab[i,j]}; printf "\n"}}' $file

run_file.sh

#!/bin/bash
# Usage: run_file file.ext
# Runs a file depending on its .ext extension.
for file in $@; do
	base=${file%.*}
	ext=${file#*.}

	case $ext in
		c)
			gcc -Wall $file -o ${file%.c}
			./${file%.c}
			;;
		f | f77 | f95)
			gfortran $file # change to fortran
			./$base
			;;
		py)
			python $file
			;;
		plt)
			gnuplot $file
			# output=`eval echo \`awk -F\' 'BEGIN {test=0} /set output/ {if (test==0) {print $2; test=1}}' $file\``
			output=`eval echo \`awk -F[\'\"] '/set output/ {print $2}' $file\``
			output_base=${output%.*}
			output_ext=${output##*.}
			cygstart $output
			;;
		vbs)
			wscript.exe $file
			;;
		awk)
			awk -f $file
			;;
		pl)
			perl $file
			;;
		tex)
			'/cygdrive/c/Program Files/MiKTeX 2.9/miktex/bin/pdflatex' $file #custom your path to pdflatex (downloadable required program)
			cygstart ${file%.tex}.pdf
			;;
		*)
			cygstart $file
			;;
	esac
done

clean_perl_equalities.sh

#!/bin/bash
# Usage: clean_perl_equalities file1 file2...
# Puts spaces before and after double equal signs
for file in $@; do
	perl -i.bak -f - <<- 'EOF' $file
	while (<>) {
		s/(\s)*==(\s)*/ == /g;
		print $_;
	}
	EOF
done

merge_pdf.sh

#!/bin/bash
# Usage: merge_pdf file1 file2
# Remove white spaces in filenames before use!
# Not always stable: check the merged pdf!
file1=$1
file2=$2
file_merged=test-merged.pdf
perl -f - <<- 'EOF' $file1 $file2 > $file_merged
#use strict;
#use warnings;

sub max {
($a, $b) = @_;
if ($a>$b) {return $a}
else {return $b}
}

my @lines1, @lines2;

open($fh1, $ARGV[0]) or die "1er fichier inexistant";
while (<$fh1>) {
	if (/\/Count (\w)/) {
		$n_Count1 = $1;
	}
	if (/\/Parent ([0-9]*) 0 R/) {
		$n_Parent1 = $1;
	}
	if ($_ =~ "trailer"){
		$test_trailer1=1
	}
	if ($test_trailer1 == 1){
		push @lines_end1, $_; 
	} else 
	{
		push @lines1, $_;
	}
}

my $delta_n_xref = 500;

my $Kids2;
open($fh2, $ARGV[1]) or warn "2e fichier inexistant";
while (<$fh2>) {
	if (/\/Count (\w)/) {
		$n_Count2 = $1;
	}
	if (/([0-9]*) 0 obj/) {
		$n_ref = $1 + $delta_n_xref;
		s/([0-9]*) 0 obj/$1+$delta_n_xref . " 0 obj"/ge;
	}
	if (/!?(\/Parent )?([0-9]*) 0 R/) {
		$n_ref = $1 + $delta_n_xref;
		s/([0-9]*) 0 R/$1+$delta_n_xref . " 0 R"/ge;
	}
	if (/\/Parent ([0-9]*) 0 R/) {
		s/\/Parent $1/\/Parent $n_Parent1/;
	}
	if (/\/Kids\[ (.*)\]/) {
		$Kids2 = $1;
	}
	if ($_ =~ "trailer"){
		$test_trailer2=1;
	}
	if ($test_trailer2 != 1){
		push @lines2, $_;
	}
}

for (@lines1) {
	if (/\/Count (\w)/) {
		my $n_Count = $n_Count1+$n_Count2;
		s/$1/$n_Count/;
	}
	if (/\/Kids\[ (.*)\]/) {
		s/($1)/$1 $Kids2/;
	}
	print $_;
}

for (@lines2) {
	print $_;
}

for (@lines_end1) {
	print $_;
}
EOF

fit.sh

#!/bin/bash -x
# usage: fit 'f(x)=a*x+b' data_file a b
func_eq=$1
func=`awk -F'(' '{print $1}' <<< $func_eq`
var=`awk -F'[()]' '{print $2}' <<< $func_eq`
echo $var
file=$2
shift 2
parameters=$@

cat <<- EOF > ${file}_fit.plt
#!/bin/gnuplot
$func_eq
fit $func(x) '$file' via `awk '{gsub(OFS, ", "); print}' <<< $parameters`

#parameters='$parameters'
#do for [i=1:words(parameters)] {parameter=word(parameters, i); print parameter}

!for parameter in $parameters; do awk '/'\$parameter'.*=*\(*%\)/ {text=\$0; match(\$0, "\\\\(.*", tab)} END {print "sigma_'\$parameter' : " substr(text, RSTART+1, RLENGTH-3)}' fit.log; done

set label 1 sprintf("$parameters = %.2f", $parameters) at graph .2,.8

set output '${file}_fit.eps'
set term postscript eps enhanced color 25
plot [0:] '$file' w lp lw 7, $func(x) w l lw 7
EOF

gnuplot ${file}_fit.plt

fit_python.sh

#!/bin/bash
# Usage: fit_python 'x = [0.0, 1.0, 2.0]' 'y = [1.0, 0.5, 0.0]' 'p[0] + p[1]*x + p[2]*x**2' 'p0 = [1.0,1.0,1.0]' file
x_list=$1
y_list=$2
fit_func=$3
initial_guess=$4
file=$5

cat <<- EOF > ${file}_fit.py
#!/usr/bin/python
from scipy import optimize
from numpy import array

$x_list
$y_list
# use the lambda shortcut or define standard functions with def fit():
fit = lambda p, x: $fit_func
err = lambda p, x, y: fit(p,x) - y
$initial_guess
# calls optimize.leastsq to find optimal parameters, converts lists into numpy.array on the fly
p, success = optimize.leastsq(err, p0, args=(array(x), array(y)), ftol=5e-9, xtol=5e-9)
print p
EOF

python ${file}_fit.py