SHELL

SHELL

= resources = A Beginner's handbook BASH Programming - Introduction Advanced Bash-Scripting Guide Wicked Cool Shell Scripts: The Library LFS Unix Scripting An A-Z Index of the Bash command line for Linux 15 Practical Grep Command Examples In Linux / UNIX

= Import function from another script = . ./common.sh if [ "$1" = "" ]; then echo USAGE: echo "sh test.sh type" exit fi
 * 1) !/bin/sh

if `validtype $1`; then echo Valid type elif `validtype $2`; then echo Valid type 2 else echo Invalid type fi Here is common.sh: validtype { if [ "$1" = "TYPEA" ] || [ "$1" = "TYPEB" ] || [ "$1" = "TYPEC" ] || [ "$1" = "TYPED" ] || [ "$1" = "TYPEE" ]; then exit 0 else exit 1 fi }
 * 1) !/bin/sh

= Arithmetic Expansion = A simple math $((expression)) echo $((1 + 3 + 4))

= if and loop = inc {                          # The increment is defined first so we can use it   echo $(($1 + $2))              # We echo the result of the first parameter plus the second parameter }                 # We check to see that all the command line arguments are present if [ "$1" = "" ] || [ "$2" = "" ] || [ "$3" = "" ] then echo USAGE: echo "  counter startvalue incrementvalue endvalue" else count=$1                      # Rename are variables with clearer names value=$2 end=$3 while [ $count -lt $end ]     # Loop while count is less than end do     echo $count count=$(inc $count $value) # Call increment with count and value as parameters done fi
 * 1) !/bin/sh

= for loop = for m in {0..6};do mkdir chr$m for i in `cat Chr$m.SNPs` ; do   for j in {0..28}; do    grep -m 1 -w "^$i" /analysis/batch$j/birdseed.calls.txt >> chr$m/batch$j.birdseed.calls.txt done done done

for (( c=1; c<=5; c++ )) do echo "Welcome $c times..." done

FILES="$@" for f in $FILES do # if .bak backup file exists, read next file if [ -f ${f}.bak ] then echo "Skiping $f file..." continue # read next file and skip cp command fi # we are hear means no backup file exists, just use cp command to copy file /bin/cp $f $f.bak done
 * 1) !/bin/bash
 * 1) !/bin/bash

= space to tab =  myfile="filename" perl -0777 -i -pe "s/ +/\t/g" $myfile && perl -0777 -i -pe "s/^\t//g" $myfile && perl -0777 -i -pe "s/\n\t/\n/g" $myfile && perl -0777 -i -pe "s/\t+/\t/g" $myfile

= tab to space =  myfile="filename" perl -0777 -i -pe "s/\t+/ /g" $myfile && perl -0777 -i -pe "s/^ //g" $myfile && perl -0777 -i -pe "s/\n /\n/g" $myfile && perl -0777 -i -pe "s/ +/ /g" $myfile

= reverse a word =  str="program" echo $str | awk '{ for(i=length($0);i>=1;i--) printf("%s",substr($0,i,1)); }'

cat GWAS.NGS.txt | awk '{for(i=length($4);i>=1;i--) printf("%s",substr($4,i,1)); print "\t"$1"\t"$2"\t"$3 ; }' | awk '{print $2"\t"$3"\t"$4"\t"$1;}' > GWAS.NGS.v2.txt

= 'uniq' by column = sort -u -t, -k1,1 file -u for unique -t, so comma is the delimiter -k1,1 for the key field 1

= Array and List =

gene=(ZNF415 29 18 6 13) mygene="${gene[0]}" myor=`perl    stats/oddratio.pl     ${gene[1]} ${gene[2]} ${gene[3]} ${gene[4]}  ` mychi=`perl   stats/chisquared.pl     ${gene[1]} ${gene[2]} ${gene[3]} ${gene[4]} ` myfexact=`perl stats/fisherexact.pl      ${gene[1]} ${gene[2]} ${gene[3]} ${gene[4]} ` echo "$mygene $myor $mychi $myfexact"

= gnome and system commands =

xfce4-session gnome-session

gnome-control-center gnome-panel gnome-terminal gnome-about-me gnome-search-tool gnome-system-monitor gnome-default-applications-properties gnome-text-editor gnome-display-properties gnome-power-manager gnome-file-share-properties gnome-desktop-item-edit gnome-calculator

plasma-desktop

system-config-authentication system-config-firewall-tui system-config-network system-config-printer system-config-users system-config-date system-config-kdump system-config-network-cmd system-config-printer-applet systemsettings system-config-firewall system-config-keyboard system-config-network-tui system-config-services system-setup-keyboard

= chomd = find /path/to/base/dir -type d -print0 | xargs -0 chmod 755 find /path/to/base/dir -type f -print0 | xargs -0 chmod 644

= rename duplicates in a column = nawk '$2 in a {$2=$2 "_" ++a[$2]}{a[$2];print}' tmp2