0
Bash Shell Tips and Tricks
John, Mark, Brian, Michael, Ken, Lisa
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...
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...
Interactive shell puzzle 2
You just typed
$ ls –l policy.py rats.py ui_common.py

Correct rats.py to stats.py using 6 keys...
Interactive shell answer 2
You just typed
$ ls –l policy.py rats.py ui_common.py

Correct rats.py to stats.py using 6 keys...
Bash conditional
if [[ -d $x ]]
then
cd $x
make
cd ..
fi
Bash conditional - improved
if [[ -d “$x” ]]
then
( cd $x ; make )
fi
Bash conditional - alternate
[[ -d “$x” ]] &&
( cd $x ; make )
Bash iteration
for x in *.reclist
do
sort $x > $x.sorted
done
Bash iteration with break
for x in *.reclist
do
[[ -d “$x” ]] &&
break
sort $x > $x.sorted
done
Bash iteration - interactive
$ for x in *.reclist
> do
>
[[ -d “$x” ]] && break
>
sort $x > $x.sorted
> done
Bash variables
Default values
if [[ $i < ${max=100} ]] ...
Arithmetic
let n=2+2*3 n++ n*=3; echo $n
((n=(1+2))); echo $n ...
Michael McKinnon’s trick
For Windows users, to dynamically change the
hover over value to reflect the machine you’re
logge...
Brian’s tricks
http://www.tldp.org/LDP/abs/html/
set –u exits on undefined variables
set –e exit immediately if a command ...
Ken’s tricks
while read p; do
echo $p
done < peptides.txt
set –x; trap read debug
Upcoming SlideShare
Loading in...5
×

Selected Bash shell tricks from Camp CDL breakout group

876

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
876
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×