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

PPT
Shell Scripting
PDF
Shell scripting
PPTX
Shell scripting
PPT
Unix And Shell Scripting
PPTX
Basics of shell programming
PPT
Chap06
PPTX
Shell programming 1.ppt
PPT
Unix Shell Scripting Basics
Shell Scripting
Shell scripting
Shell scripting
Unix And Shell Scripting
Basics of shell programming
Chap06
Shell programming 1.ppt
Unix Shell Scripting Basics

What's hot (20)

PDF
Shell scripting
ODP
Shellscripting
PDF
Complete Guide for Linux shell programming
PDF
Shell scripting
PPTX
Unix - Shell Scripts
PPTX
Linux shell env
PPTX
Easiest way to start with Shell scripting
PPTX
Shell & Shell Script
PPT
Shell programming
PPT
Linux shell scripting
PPT
Unix Shell Scripting Basics
PDF
Intro to Linux Shell Scripting
PPTX
system management -shell programming by gaurav raikar
PPTX
Bash Shell Scripting
PDF
Unix shell scripting tutorial
PDF
Introduction to shell scripting
PPT
Bash shell
PPTX
SHELL PROGRAMMING
PPTX
Bash Shell Scripting
PDF
Unix Shell Scripting
Shell scripting
Shellscripting
Complete Guide for Linux shell programming
Shell scripting
Unix - Shell Scripts
Linux shell env
Easiest way to start with Shell scripting
Shell & Shell Script
Shell programming
Linux shell scripting
Unix Shell Scripting Basics
Intro to Linux Shell Scripting
system management -shell programming by gaurav raikar
Bash Shell Scripting
Unix shell scripting tutorial
Introduction to shell scripting
Bash shell
SHELL PROGRAMMING
Bash Shell Scripting
Unix Shell Scripting
Ad

Similar to Quick start bash script (20)

PPTX
Shell scripting
PDF
One-Liners to Rule Them All
PPT
Unix shell scripting basics
PDF
Unleash your inner console cowboy
KEY
Advanced Shell Scripting
PPTX
First steps in C-Shell
PPTX
shellScriptAlt.pptx
PDF
Bash Scripting Workshop
PDF
Shell Script
PDF
Basic basic solaris quick referent card
PDF
PPTX
Unix shell scripting basics
PDF
Bash production guide
PDF
PPT
Talk Unix Shell Script
PPT
04 using and_configuring_bash
DOCX
Quize on scripting shell
DOCX
What is a shell script
Shell scripting
One-Liners to Rule Them All
Unix shell scripting basics
Unleash your inner console cowboy
Advanced Shell Scripting
First steps in C-Shell
shellScriptAlt.pptx
Bash Scripting Workshop
Shell Script
Basic basic solaris quick referent card
Unix shell scripting basics
Bash production guide
Talk Unix Shell Script
04 using and_configuring_bash
Quize on scripting shell
What is a shell script
Ad

More from Simon Su (20)

PDF
Kubernetes Basic Operation
PDF
Google IoT Core 初體驗
PDF
JSDC 2017 - 使用google cloud 從雲到端,動手刻個IoT
PDF
GCPUG.TW meetup #28 - GKE上運作您的k8s服務
PDF
Google Cloud Platform Special Training
PDF
GCE Windows Serial Console Usage Guide
PDF
GCPNext17' Extend 開始GCP了嗎?
PDF
Try Cloud Spanner
PDF
Google Cloud Monitoring
PDF
Google Cloud Computing compares GCE, GAE and GKE
PDF
JCConf 2016 - Google Dataflow 小試
PDF
JCConf 2016 - Dataflow Workshop Labs
PDF
JCConf2016 - Dataflow Workshop Setup
PDF
GCPUG meetup 201610 - Dataflow Introduction
PDF
Brocade - Stingray Application Firewall
PDF
使用 Raspberry pi + fluentd + gcp cloud logging, big query 做iot 資料搜集與分析
PDF
Docker in Action
PDF
Google I/O 2016 Recap - Google Cloud Platform News Update
PDF
IThome DevOps Summit - IoT、docker與DevOps
PDF
Google Cloud Platform Introduction - 2016Q3
Kubernetes Basic Operation
Google IoT Core 初體驗
JSDC 2017 - 使用google cloud 從雲到端,動手刻個IoT
GCPUG.TW meetup #28 - GKE上運作您的k8s服務
Google Cloud Platform Special Training
GCE Windows Serial Console Usage Guide
GCPNext17' Extend 開始GCP了嗎?
Try Cloud Spanner
Google Cloud Monitoring
Google Cloud Computing compares GCE, GAE and GKE
JCConf 2016 - Google Dataflow 小試
JCConf 2016 - Dataflow Workshop Labs
JCConf2016 - Dataflow Workshop Setup
GCPUG meetup 201610 - Dataflow Introduction
Brocade - Stingray Application Firewall
使用 Raspberry pi + fluentd + gcp cloud logging, big query 做iot 資料搜集與分析
Docker in Action
Google I/O 2016 Recap - Google Cloud Platform News Update
IThome DevOps Summit - IoT、docker與DevOps
Google Cloud Platform Introduction - 2016Q3

Recently uploaded (20)

PPTX
A Presentation on Artificial Intelligence
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPT
Teaching material agriculture food technology
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Electronic commerce courselecture one. Pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
NewMind AI Weekly Chronicles - August'25 Week I
A Presentation on Artificial Intelligence
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Big Data Technologies - Introduction.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Building Integrated photovoltaic BIPV_UPV.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Network Security Unit 5.pdf for BCA BBA.
Digital-Transformation-Roadmap-for-Companies.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Teaching material agriculture food technology
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Electronic commerce courselecture one. Pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
NewMind AI Weekly Chronicles - August'25 Week I

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]