Selected Bash shell tricks from Camp CDL breakout group

1,194 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Selected Bash shell tricks from Camp CDL breakout group

  1. 1. Bash Shell Tips and Tricks John, Mark, Brian, Michael, Ken, Lisa
  2. 2. Interactive shell puzzle 1 You just typed $ ls –l policy.py stats.py ui_common.py Count lines of the last file and make a .bak copy of it using less than 20 keystrokes (no mouse, repeat key counts)
  3. 3. Interactive shell answer 1 You just typed $ ls –l policy.py stats.py ui_common.py Count lines of the last file and make a .bak copy of it using less than 20 keystrokes (no mouse, repeat key counts): $ wc !$ $ cp !$ !$.bak or $ cp !${,.bak}
  4. 4. Interactive shell puzzle 2 You just typed $ ls –l policy.py rats.py ui_common.py Correct rats.py to stats.py using 6 keystrokes (no mouse, repeat key counts)
  5. 5. Interactive shell answer 2 You just typed $ ls –l policy.py rats.py ui_common.py Correct rats.py to stats.py using 6 keystrokes (no mouse, repeat key counts): $ ^r^st
  6. 6. Bash conditional if [[ -d $x ]] then cd $x make cd .. fi
  7. 7. Bash conditional - improved if [[ -d “$x” ]] then ( cd $x ; make ) fi
  8. 8. Bash conditional - alternate [[ -d “$x” ]] && ( cd $x ; make )
  9. 9. Bash iteration for x in *.reclist do sort $x > $x.sorted done
  10. 10. Bash iteration with break for x in *.reclist do [[ -d “$x” ]] && break sort $x > $x.sorted done
  11. 11. Bash iteration - interactive $ for x in *.reclist > do > [[ -d “$x” ]] && break > sort $x > $x.sorted > done
  12. 12. Bash variables Default values if [[ $i < ${max=100} ]] ... Arithmetic let n=2+2*3 n++ n*=3; echo $n ((n=(1+2))); echo $n  3 But x=4; x+=6; echo $x  46  27
  13. 13. Michael McKinnon’s trick For Windows users, to dynamically change the hover over value to reflect the machine you’re logged into (eg, via Putty): PROMPT_COMMAND= 'echo -ne "033]0;${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}007"'
  14. 14. Brian’s tricks http://www.tldp.org/LDP/abs/html/ set –u exits on undefined variables set –e exit immediately if a command exits with a non-zero status set –x echo commands before execution use functions pwd -P
  15. 15. Ken’s tricks while read p; do echo $p done < peptides.txt set –x; trap read debug

×