Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Quick Start - Bash Script
Simon Su
● Not a linux/unix guide ...
● Talk something basic about programming
shell script ...
○ Basic concepts
○ Basic commands
○ Vi
Outline
Shell Scripts
● sh
● bash
● ksh
● csh
● zsh
sh csh ksh bash tcsh zsh rc es
Job control N Y Y Y Y Y N N
Aliases N Y Y Y Y Y N N
Shell functions Y(1) N Y Y N Y Y Y
"Sensible" Input/Output redirection Y N Y Y N Y Y Y
Directory stack N Y Y Y Y Y F F
Command history N Y Y Y Y Y L L
Command line editing N N Y Y Y Y L L
Vi Command line editing N N Y Y Y(3) Y L L
Emacs Command line editing N N Y Y Y Y L L
Rebindable Command line editing N N N Y Y Y L L
User name look up N Y Y Y Y Y L L
Login/Logout watching N N N N Y Y F F
Filename completion N Y(1) Y Y Y Y L L
Username completion N Y(2) Y Y Y Y L L
Hostname completion N Y(2) Y Y Y Y L L
History completion N N N Y Y Y L L
Fully programmable Completion N N N N Y Y N N
Mh Mailbox completion N N N N(4) N(6) N(6) N N
Co Processes N N Y N N Y N N
Builtin artithmetic evaluation N Y Y Y Y Y N N
Can follow symbolic links invisibly N N Y Y Y Y N N
Periodic command execution N N N N Y Y N N
Custom Prompt (easily) N N Y Y Y Y Y Y
Sun Keyboard Hack N N N N N Y N N
Spelling Correction N N N N Y Y N N
Process Substitution N N N Y(2) N Y Y Y
Underlying Syntax sh csh sh sh csh sh rc r
Freely Available N N N(5) Y Y Y Y Y
Checks Mailbox N Y Y Y Y Y F F
Tty Sanity Checking N N N N Y Y N N
Can cope with large argument lists Y N Y Y Y Y Y Y
Has non-interactive startup file N Y Y(7) Y(7) Y Y N N
Has non-login startup file N Y Y(7) Y Y Y N N
Can avoid user startup files N Y N Y N Y Y Y
Can specify startup file N N Y Y N N N N
Low level command redefinition N N N N N N N Y
Has anonymous functions N N N N N N Y Y
List Variables N Y Y N Y Y Y Y
Full signal trap handling Y N Y Y N Y Y Y
File no clobber ability N Y Y Y Y Y N F
Local variables N N Y Y N Y Y Y
Lexically scoped variables N N N N N N N Y
Exceptions N N N N N N N Y
Basic Concept -
.bashrc / .bash_profile / .bash_history
● .bashrc
● .bash_profile
● .bash_history
Basic Concept - man page
● man [commands]
ex: man bash
Basic Concept - Declare & Comment
● #!/bin/bash
● #comments...
Basic Concept - Environment
● export KEY=VALUE
Basic Concept - source
● . [script file]
● source [script file]
Basic Concept - “|”
● ps -ef | grep root
● ps -ef | cat > /tmp/ps.log
● ps -ef | grep httpd | awk '{print $2}'
Basic Concept - “<” & “>” & “>>”
● mysql -uroot -p < test.sql
● ps -ef > ps.log
● bash [file] > /dev/null 2>&1
● ps -ef >> ps.log
Basic Concept - `[commands]`
● kill -9 `ps -ef | grep httpd | awk ‘{print $2}’`
● tail -f `svcs -L apache`
Basic Concept - ‘’ & “”
Basic Concept - & and &&
● bash [file] &
● bash [file1] && bash [file2]
Basic Concept - bash execution
● #!/bin/bash
● ./[file]
● bash [file]
● bash -x [file]
Basic Concept - Parameter input
● Input:
bash [file] [parameter 1] [parameter 2] …
● Receive:
$1, $2....
Basic Concept - Parameters of Bash
● $#
● $@
● $*
● $?
● alias ll=”ls -l”
Basic Commands - alias
Basic Commands - bg / fg
Process lock
screen..
ctrl + z
(process stop and
screen return)
bg
(process keep
running)
fg
(screen back to
process)
Basic Commands - background proc
● bash [script file] &
● nohup bash [script file] &
● nohup bash [script file] > /dev/null 2>&1 &
if …. then … elseif … fi
export i=5;
if [ $i -gt 5 ] ; then echo $i; fi
if [ $i -eq 5 ] ; then echo $i; fi
if Expression
then
Commands
elif Expression
then
Commands
else
Commands
fi
if (Expression)
then
Commands
elif (Expression)
then
Commands
else
Commands
fi
if [ Expression ]; then
Commands
elif [ Expression ]; then
Commands
else
Commands
fi
for / while / until loop
for name in { 1..10 }
do
….
done
for VariableName in List
do
Commands
done
while [Expression]
do
Commands
done
until [ Expression ]
do
Commands
done
function
helloworld(){
echo hello $1
}
helloworld simon
Basic Commands - find
● find [from] -name [pattern] -print
● find [from] -name [pattern] -type f
● find [from] -ctime 3 -print
● find [from] -atime +1 -print -exec ls -l {} ;
Basic Commands - awk
● awk -F "t" '{print $1,$2}' /etc/hosts
● cat /etc/hosts | awk -F "t" '{print $1,$2}'
Basic Commands - sed
● sed 's/old_text/new_text/g' [file]
● sed -i 's/old_text/new_text/g' [file]
Basic Commands - xargs
● ps -ef | grep node | awk '{print $2}' | xargs kill -9
Nice tool - bash_it
● https://github.com/revans/bash-it
● Install:
○ Check a clone of this repo:
git clone http://github.com/revans/bash-it.git ~/.bash_it
○ Run ~/.bash_it/install.sh
○ Edit your ~/.bash_profile file in order to customize bash-it.
Vi
Vi - View Mode
Vi - Edit Mode
Vi - Goto line & Search & Replace
● Goto line:
:[line number]
● Search:
/[key]
● Replace first:
%s/[old]/[new]
● Replace all:
%s/[old]/[new]/g
Vi - split, vsplit
● :vsp
● :vsp [file]
● :sp
● :s[ [file]
● ctrl+w
Vi - tabedit
● :tabe [file]
● gt
● n gt
Vi - set
● :set nu / :set nonu
● :set paste
Vi - execute command
● :![command]
Vi - ~/.vimrc

More Related Content

Quick start bash script

  • 1. Quick Start - Bash Script Simon Su
  • 2. ● Not a linux/unix guide ... ● Talk something basic about programming shell script ... ○ Basic concepts ○ Basic commands ○ Vi Outline
  • 3. Shell Scripts ● sh ● bash ● ksh ● csh ● zsh sh csh ksh bash tcsh zsh rc es Job control N Y Y Y Y Y N N Aliases N Y Y Y Y Y N N Shell functions Y(1) N Y Y N Y Y Y "Sensible" Input/Output redirection Y N Y Y N Y Y Y Directory stack N Y Y Y Y Y F F Command history N Y Y Y Y Y L L Command line editing N N Y Y Y Y L L Vi Command line editing N N Y Y Y(3) Y L L Emacs Command line editing N N Y Y Y Y L L Rebindable Command line editing N N N Y Y Y L L User name look up N Y Y Y Y Y L L Login/Logout watching N N N N Y Y F F Filename completion N Y(1) Y Y Y Y L L Username completion N Y(2) Y Y Y Y L L Hostname completion N Y(2) Y Y Y Y L L History completion N N N Y Y Y L L Fully programmable Completion N N N N Y Y N N Mh Mailbox completion N N N N(4) N(6) N(6) N N Co Processes N N Y N N Y N N Builtin artithmetic evaluation N Y Y Y Y Y N N Can follow symbolic links invisibly N N Y Y Y Y N N Periodic command execution N N N N Y Y N N Custom Prompt (easily) N N Y Y Y Y Y Y Sun Keyboard Hack N N N N N Y N N Spelling Correction N N N N Y Y N N Process Substitution N N N Y(2) N Y Y Y Underlying Syntax sh csh sh sh csh sh rc r Freely Available N N N(5) Y Y Y Y Y Checks Mailbox N Y Y Y Y Y F F Tty Sanity Checking N N N N Y Y N N Can cope with large argument lists Y N Y Y Y Y Y Y Has non-interactive startup file N Y Y(7) Y(7) Y Y N N Has non-login startup file N Y Y(7) Y Y Y N N Can avoid user startup files N Y N Y N Y Y Y Can specify startup file N N Y Y N N N N Low level command redefinition N N N N N N N Y Has anonymous functions N N N N N N Y Y List Variables N Y Y N Y Y Y Y Full signal trap handling Y N Y Y N Y Y Y File no clobber ability N Y Y Y Y Y N F Local variables N N Y Y N Y Y Y Lexically scoped variables N N N N N N N Y Exceptions N N N N N N N Y
  • 4. Basic Concept - .bashrc / .bash_profile / .bash_history ● .bashrc ● .bash_profile ● .bash_history
  • 5. Basic Concept - man page ● man [commands] ex: man bash
  • 6. Basic Concept - Declare & Comment ● #!/bin/bash ● #comments...
  • 7. Basic Concept - Environment ● export KEY=VALUE
  • 8. Basic Concept - source ● . [script file] ● source [script file]
  • 9. Basic Concept - “|” ● ps -ef | grep root ● ps -ef | cat > /tmp/ps.log ● ps -ef | grep httpd | awk '{print $2}'
  • 10. Basic Concept - “<” & “>” & “>>” ● mysql -uroot -p < test.sql ● ps -ef > ps.log ● bash [file] > /dev/null 2>&1 ● ps -ef >> ps.log
  • 11. Basic Concept - `[commands]` ● kill -9 `ps -ef | grep httpd | awk ‘{print $2}’` ● tail -f `svcs -L apache`
  • 12. Basic Concept - ‘’ & “”
  • 13. Basic Concept - & and && ● bash [file] & ● bash [file1] && bash [file2]
  • 14. Basic Concept - bash execution ● #!/bin/bash ● ./[file] ● bash [file] ● bash -x [file]
  • 15. Basic Concept - Parameter input ● Input: bash [file] [parameter 1] [parameter 2] … ● Receive: $1, $2....
  • 16. Basic Concept - Parameters of Bash ● $# ● $@ ● $* ● $?
  • 17. ● alias ll=”ls -l” Basic Commands - alias
  • 18. Basic Commands - bg / fg Process lock screen.. ctrl + z (process stop and screen return) bg (process keep running) fg (screen back to process)
  • 19. Basic Commands - background proc ● bash [script file] & ● nohup bash [script file] & ● nohup bash [script file] > /dev/null 2>&1 &
  • 20. if …. then … elseif … fi export i=5; if [ $i -gt 5 ] ; then echo $i; fi if [ $i -eq 5 ] ; then echo $i; fi if Expression then Commands elif Expression then Commands else Commands fi if (Expression) then Commands elif (Expression) then Commands else Commands fi if [ Expression ]; then Commands elif [ Expression ]; then Commands else Commands fi
  • 21. for / while / until loop for name in { 1..10 } do …. done for VariableName in List do Commands done while [Expression] do Commands done until [ Expression ] do Commands done
  • 23. Basic Commands - find ● find [from] -name [pattern] -print ● find [from] -name [pattern] -type f ● find [from] -ctime 3 -print ● find [from] -atime +1 -print -exec ls -l {} ;
  • 24. Basic Commands - awk ● awk -F "t" '{print $1,$2}' /etc/hosts ● cat /etc/hosts | awk -F "t" '{print $1,$2}'
  • 25. Basic Commands - sed ● sed 's/old_text/new_text/g' [file] ● sed -i 's/old_text/new_text/g' [file]
  • 26. Basic Commands - xargs ● ps -ef | grep node | awk '{print $2}' | xargs kill -9
  • 27. Nice tool - bash_it ● https://github.com/revans/bash-it ● Install: ○ Check a clone of this repo: git clone http://github.com/revans/bash-it.git ~/.bash_it ○ Run ~/.bash_it/install.sh ○ Edit your ~/.bash_profile file in order to customize bash-it.
  • 28. Vi
  • 29. Vi - View Mode
  • 30. Vi - Edit Mode
  • 31. Vi - Goto line & Search & Replace ● Goto line: :[line number] ● Search: /[key] ● Replace first: %s/[old]/[new] ● Replace all: %s/[old]/[new]/g
  • 32. Vi - split, vsplit ● :vsp ● :vsp [file] ● :sp ● :s[ [file] ● ctrl+w
  • 33. Vi - tabedit ● :tabe [file] ● gt ● n gt
  • 34. Vi - set ● :set nu / :set nonu ● :set paste
  • 35. Vi - execute command ● :![command]