SlideShare a Scribd company logo
Bash Scripting

       Prepared by Ahmed Magdy Ezzeldin
File System

●   / is not allowed in a file name
●   file extensions are not important
●   File and folder names are case sensitive
●   Names containing spaces are escaped by  or ""
●   Hidden files start with .
●   Current directory alias . and parent directory alias ..
●   ls, ls -a, ls -l, cp, mv, rm, rm -rdf, ln, ln -sf
Variable and Strings

●   Strings are enclosed in “” '' or just written with spaces escaped
    by 
    VAR=”How are you”
    VAR='How are you'
    VAR=HOW are you
●   Variables
    $VAR
    ${VAR} # e.g. ${FILE}.txt
    echo “my name is $NAME”
    echo “my name is “$NAME
    read VAR
    read VAR1 VAR2
Math

●   echo $((1+1))
●   echo $[1+1]
●   echo `expr 1 + 1`
●   A=$((B*C))
●   let A+=1
●   echo "3.4+7/8-(5.94*3.14)" | bc
File Ownership and Permissions

●   Group and Ownership
    chown, chgrp, ls -l
    chown user:group file
    sudo chown

●   Permissions of files and directories
    chmod 755 file
    chmod +x file
    chmod -x file
                   (r)ead     (w)rite      e(x)ecute

                     4           2            1
Users, su, sudo

●   su, sudo, visudo, /etc/sudoers, su user
●   sudo bash
●   /etc/passwd
●   ~/
Processes and Subshells

●   ps, pgrep, top, jobs, fg, &, kill, signals
●   Sub-shells from :
    ●   Loops
    ●   $()
    ●   Back ticks ``
    ●   bash command
●   /proc
●   fuser
Loops

●   While loop
    while read VAR; do echo $VAR; done
●   For each loop
    for VAR in {1..5}; do echo $VAR; done
    for VAR in 1 2 3 4 5; do echo $VAR; done
    for VAR in {0..10..2}; do echo $VAR; done
    for VAR in $(seq 0 2 10); do echo $VAR; done
●   For loop with counter
    for (( i=0; i<=10; i+=2 )); do echo $i; done
if and switch case

●   If then, else, elif, fi
    if [ -d $F ]; then
        rm -rdf $F
    elif [ -f $F ]; then
        rm $F
    else
        echo “Unknown file type”
    fi
●   case, esac
    while read l; do
     case $l in
      1) echo "One";;
      2) echo "Two";;
      3) echo "Three";;
      *) echo "Invalid"; break;;
     esac
    done
I/O Redirection

●   >, >>, <, &>, 2&>, 2>, |
    echo “error” 1>&2 # redirect std_output to std_err
    some_error_producing_command 2>&1
    some_command &>> log_file_errors_and_output
    some_command >> log_file_output_only
    some_command 2>> log_file_errors_only
    command | filter_command
    command < file
    command > file # truncates and writes into file
    command1 2>&1 | command2
    command1 |& command2
●   Mute by redirecting to /dev/null file
Named Pipes and Network and Signal Processing
●   Network
      exec 9<>/dev/tcp/google.com/80
      echo -e "GET / HTTP/1.0nn" >&9
      while read line <&9; do echo $line; done
●   Named Pipes
      mkfifo pipe
      while read l; do echo $l; done < pipe
      echo “hi” > pipe # on another terminal window
●   Signal Processing
      #!/bin/bash
      function handle_signal () {
         echo "Hi man I got the signal"
      }
      trap "handle_signal; exit" SIGINT
      sleep 100
Cool Stuff and GUI
●   Install these: libnotify-bin, zenity, festival
●   date '+%l oclock' | festival –tts
●   zenity --list --hide-header --text="Are you coming today?" --checklist --column
    "" --column "" FALSE "Yes" FALSE "No" FALSE "May be"
●   zenity --calendar --date-format="%Y/%m/%d"
●   zenity --text="Select Hour" --title="Select Hour" --scale --min-value=0 --max-
    value=24 --value=17
●   zenity --text="Select Minutes" --title="Select Minutes" --scale --min-value=0
    --max-value=59 --value=0
●   zenity --info --text="Did you know this info"
●   zenity --error --text="There is an error"
●   zenity --entry | festival --tts
●   zenity --file-selection
●   <progres command> | zenity --progress --auto-close
●   notify-send -t 10 "hi man" "details"
Launchers

  ● When you drag a file or folder onto a launcher
  it is sent as $1 to the launcher script
Example Codes

●   Bluetooth Obex-push
    NEW_FILE_NAME=`echo $1 | awk -vFS="/" '{print $NF;}'`
    ussp-push 00:22:65:89:22:E6@9 "$1" "$NEW_FILE_NAME"
●   Close the computer when your browser finishes downloading
    your_file
    while true; do sleep 5; if [ -z "`fuser ~/your_file`" ]; then break;
    fi; done; sudo init 0
●   Tell me the time whenever I lock my screen or unlock it
●   Simple spider using curl and wget
●   Log your time easily and report to a Redmine service
●   Think of more ...
Thank you
References


    http://www.linuxcommand.org/lts0040.php

    http://www.cyberciti.biz/faq/bash-for-loop/

       http://linuxcommand.org/wss0160.php

  http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-10.html


 http://wiki.bash-hackers.org/syntax/redirection

  http://www.kernel.org/doc/man-pages/online/pages/man5/proc.5.html

More Related Content

What's hot (20)

PPTX
Comets notes
Ilija Dukovski
 
PDF
React PHP: the NodeJS challenger
vanphp
 
ODP
The promise of asynchronous PHP
Wim Godden
 
DOC
How to send files to remote server via ssh in php
Andolasoft Inc
 
PDF
Unix cheatsheet
Dhaval Shukla
 
PDF
ZeroMQ Is The Answer: DPC 11 Version
Ian Barber
 
PDF
How to stand on the shoulders of giants
Ian Barber
 
PDF
ZeroMQ: Messaging Made Simple
Ian Barber
 
KEY
Clojure + MongoDB on Heroku
Naoyuki Kakuda
 
ZIP
Web Apps in Perl - HTTP 101
hendrikvb
 
PDF
Any event intro
qiang
 
PDF
Créer une base NoSQL en 1 heure
Amaury Bouchard
 
PDF
Parsing JSON with a single regex
brian d foy
 
PDF
Shell Script
Adam Victor Brandizzi
 
PDF
Absolute Beginners Guide to Puppet Through Types - PuppetConf 2014
Puppet
 
PDF
Coming Out Of Your Shell - A Comparison of *Nix Shells
Kel Cecil
 
PDF
Devinsampa nginx-scripting
Tony Fabeen
 
PDF
Puppet Camp 2012
Server Density
 
ODP
Perl - laziness, impatience, hubris, and one liners
Kirk Kimmel
 
PDF
Laporan setting dns
Septian Adi
 
Comets notes
Ilija Dukovski
 
React PHP: the NodeJS challenger
vanphp
 
The promise of asynchronous PHP
Wim Godden
 
How to send files to remote server via ssh in php
Andolasoft Inc
 
Unix cheatsheet
Dhaval Shukla
 
ZeroMQ Is The Answer: DPC 11 Version
Ian Barber
 
How to stand on the shoulders of giants
Ian Barber
 
ZeroMQ: Messaging Made Simple
Ian Barber
 
Clojure + MongoDB on Heroku
Naoyuki Kakuda
 
Web Apps in Perl - HTTP 101
hendrikvb
 
Any event intro
qiang
 
Créer une base NoSQL en 1 heure
Amaury Bouchard
 
Parsing JSON with a single regex
brian d foy
 
Shell Script
Adam Victor Brandizzi
 
Absolute Beginners Guide to Puppet Through Types - PuppetConf 2014
Puppet
 
Coming Out Of Your Shell - A Comparison of *Nix Shells
Kel Cecil
 
Devinsampa nginx-scripting
Tony Fabeen
 
Puppet Camp 2012
Server Density
 
Perl - laziness, impatience, hubris, and one liners
Kirk Kimmel
 
Laporan setting dns
Septian Adi
 

Similar to Bash Scripting Workshop (20)

PDF
One-Liners to Rule Them All
egypt
 
PDF
Unleash your inner console cowboy
Kenneth Geisshirt
 
PDF
Shell scripting
Ashrith Mekala
 
PDF
Complete Guide for Linux shell programming
sudhir singh yadav
 
PPTX
LINUX_admin_commands.pptx
GuhanSenthil2
 
PPTX
Unix Shell Script - 2 Days Session.pptx
Rajesh Kumar
 
PDF
2023comp90024_linux2.pdf
LevLafayette1
 
PDF
Mastering the Unix Command Line
Howard Mao
 
PDF
Commands
damlepramod
 
PDF
Linux: A Getting Started Presentation
Nap Ramirez
 
PDF
Slides
abhishekvirmani
 
PPTX
Linux Shell Basics
Constantine Nosovsky
 
PPTX
Linux Fundamentals
DianaWhitney4
 
PPTX
Linux
RittikaBaksi
 
PPT
Unix shell scripting basics
Abhay Sapru
 
PPT
Unix Shell Scripting Basics
Dr.Ravi
 
TXT
An a z index of the bash commands
Ben Pope
 
PPT
ShellAdvanced shell scripting programm.ppt
ubaidullah75790
 
One-Liners to Rule Them All
egypt
 
Unleash your inner console cowboy
Kenneth Geisshirt
 
Shell scripting
Ashrith Mekala
 
Complete Guide for Linux shell programming
sudhir singh yadav
 
LINUX_admin_commands.pptx
GuhanSenthil2
 
Unix Shell Script - 2 Days Session.pptx
Rajesh Kumar
 
2023comp90024_linux2.pdf
LevLafayette1
 
Mastering the Unix Command Line
Howard Mao
 
Commands
damlepramod
 
Linux: A Getting Started Presentation
Nap Ramirez
 
Linux Shell Basics
Constantine Nosovsky
 
Linux Fundamentals
DianaWhitney4
 
Unix shell scripting basics
Abhay Sapru
 
Unix Shell Scripting Basics
Dr.Ravi
 
An a z index of the bash commands
Ben Pope
 
ShellAdvanced shell scripting programm.ppt
ubaidullah75790
 
Ad

More from Ahmed Magdy Ezzeldin, MSc. (12)

PDF
Distributed RDBMS: Challenges, Solutions & Trade-offs
Ahmed Magdy Ezzeldin, MSc.
 
PDF
Win any Interview like a Boss
Ahmed Magdy Ezzeldin, MSc.
 
PDF
Answer Selection and Validation for Arabic Questions
Ahmed Magdy Ezzeldin, MSc.
 
PDF
Arabic Question Answering: Challenges, Tasks, Approaches, Test-sets, Tools, A...
Ahmed Magdy Ezzeldin, MSc.
 
PDF
A survey of fault prediction using machine learning algorithms
Ahmed Magdy Ezzeldin, MSc.
 
PDF
GATE : General Architecture for Text Engineering
Ahmed Magdy Ezzeldin, MSc.
 
PDF
Networks and Natural Language Processing
Ahmed Magdy Ezzeldin, MSc.
 
PDF
Distributed Coordination-Based Systems
Ahmed Magdy Ezzeldin, MSc.
 
PDF
Distributed Systems Naming
Ahmed Magdy Ezzeldin, MSc.
 
PDF
Cyclcone a safe dialect of C
Ahmed Magdy Ezzeldin, MSc.
 
PPT
Objective C Memory Management
Ahmed Magdy Ezzeldin, MSc.
 
PPT
Object Role Modeling
Ahmed Magdy Ezzeldin, MSc.
 
Distributed RDBMS: Challenges, Solutions & Trade-offs
Ahmed Magdy Ezzeldin, MSc.
 
Win any Interview like a Boss
Ahmed Magdy Ezzeldin, MSc.
 
Answer Selection and Validation for Arabic Questions
Ahmed Magdy Ezzeldin, MSc.
 
Arabic Question Answering: Challenges, Tasks, Approaches, Test-sets, Tools, A...
Ahmed Magdy Ezzeldin, MSc.
 
A survey of fault prediction using machine learning algorithms
Ahmed Magdy Ezzeldin, MSc.
 
GATE : General Architecture for Text Engineering
Ahmed Magdy Ezzeldin, MSc.
 
Networks and Natural Language Processing
Ahmed Magdy Ezzeldin, MSc.
 
Distributed Coordination-Based Systems
Ahmed Magdy Ezzeldin, MSc.
 
Distributed Systems Naming
Ahmed Magdy Ezzeldin, MSc.
 
Cyclcone a safe dialect of C
Ahmed Magdy Ezzeldin, MSc.
 
Objective C Memory Management
Ahmed Magdy Ezzeldin, MSc.
 
Object Role Modeling
Ahmed Magdy Ezzeldin, MSc.
 
Ad

Recently uploaded (20)

PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 

Bash Scripting Workshop

  • 1. Bash Scripting Prepared by Ahmed Magdy Ezzeldin
  • 2. File System ● / is not allowed in a file name ● file extensions are not important ● File and folder names are case sensitive ● Names containing spaces are escaped by or "" ● Hidden files start with . ● Current directory alias . and parent directory alias .. ● ls, ls -a, ls -l, cp, mv, rm, rm -rdf, ln, ln -sf
  • 3. Variable and Strings ● Strings are enclosed in “” '' or just written with spaces escaped by VAR=”How are you” VAR='How are you' VAR=HOW are you ● Variables $VAR ${VAR} # e.g. ${FILE}.txt echo “my name is $NAME” echo “my name is “$NAME read VAR read VAR1 VAR2
  • 4. Math ● echo $((1+1)) ● echo $[1+1] ● echo `expr 1 + 1` ● A=$((B*C)) ● let A+=1 ● echo "3.4+7/8-(5.94*3.14)" | bc
  • 5. File Ownership and Permissions ● Group and Ownership chown, chgrp, ls -l chown user:group file sudo chown ● Permissions of files and directories chmod 755 file chmod +x file chmod -x file (r)ead (w)rite e(x)ecute 4 2 1
  • 6. Users, su, sudo ● su, sudo, visudo, /etc/sudoers, su user ● sudo bash ● /etc/passwd ● ~/
  • 7. Processes and Subshells ● ps, pgrep, top, jobs, fg, &, kill, signals ● Sub-shells from : ● Loops ● $() ● Back ticks `` ● bash command ● /proc ● fuser
  • 8. Loops ● While loop while read VAR; do echo $VAR; done ● For each loop for VAR in {1..5}; do echo $VAR; done for VAR in 1 2 3 4 5; do echo $VAR; done for VAR in {0..10..2}; do echo $VAR; done for VAR in $(seq 0 2 10); do echo $VAR; done ● For loop with counter for (( i=0; i<=10; i+=2 )); do echo $i; done
  • 9. if and switch case ● If then, else, elif, fi if [ -d $F ]; then rm -rdf $F elif [ -f $F ]; then rm $F else echo “Unknown file type” fi ● case, esac while read l; do case $l in 1) echo "One";; 2) echo "Two";; 3) echo "Three";; *) echo "Invalid"; break;; esac done
  • 10. I/O Redirection ● >, >>, <, &>, 2&>, 2>, | echo “error” 1>&2 # redirect std_output to std_err some_error_producing_command 2>&1 some_command &>> log_file_errors_and_output some_command >> log_file_output_only some_command 2>> log_file_errors_only command | filter_command command < file command > file # truncates and writes into file command1 2>&1 | command2 command1 |& command2 ● Mute by redirecting to /dev/null file
  • 11. Named Pipes and Network and Signal Processing ● Network exec 9<>/dev/tcp/google.com/80 echo -e "GET / HTTP/1.0nn" >&9 while read line <&9; do echo $line; done ● Named Pipes mkfifo pipe while read l; do echo $l; done < pipe echo “hi” > pipe # on another terminal window ● Signal Processing #!/bin/bash function handle_signal () { echo "Hi man I got the signal" } trap "handle_signal; exit" SIGINT sleep 100
  • 12. Cool Stuff and GUI ● Install these: libnotify-bin, zenity, festival ● date '+%l oclock' | festival –tts ● zenity --list --hide-header --text="Are you coming today?" --checklist --column "" --column "" FALSE "Yes" FALSE "No" FALSE "May be" ● zenity --calendar --date-format="%Y/%m/%d" ● zenity --text="Select Hour" --title="Select Hour" --scale --min-value=0 --max- value=24 --value=17 ● zenity --text="Select Minutes" --title="Select Minutes" --scale --min-value=0 --max-value=59 --value=0 ● zenity --info --text="Did you know this info" ● zenity --error --text="There is an error" ● zenity --entry | festival --tts ● zenity --file-selection ● <progres command> | zenity --progress --auto-close ● notify-send -t 10 "hi man" "details"
  • 13. Launchers ● When you drag a file or folder onto a launcher it is sent as $1 to the launcher script
  • 14. Example Codes ● Bluetooth Obex-push NEW_FILE_NAME=`echo $1 | awk -vFS="/" '{print $NF;}'` ussp-push 00:22:65:89:22:E6@9 "$1" "$NEW_FILE_NAME" ● Close the computer when your browser finishes downloading your_file while true; do sleep 5; if [ -z "`fuser ~/your_file`" ]; then break; fi; done; sudo init 0 ● Tell me the time whenever I lock my screen or unlock it ● Simple spider using curl and wget ● Log your time easily and report to a Redmine service ● Think of more ...
  • 16. References http://www.linuxcommand.org/lts0040.php http://www.cyberciti.biz/faq/bash-for-loop/ http://linuxcommand.org/wss0160.php http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-10.html http://wiki.bash-hackers.org/syntax/redirection http://www.kernel.org/doc/man-pages/online/pages/man5/proc.5.html