SlideShare ist ein Scribd-Unternehmen logo
1 von 16
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

React PHP: the NodeJS challenger
React PHP: the NodeJS challengerReact PHP: the NodeJS challenger
React PHP: the NodeJS challengervanphp
 
The promise of asynchronous PHP
The promise of asynchronous PHPThe promise of asynchronous PHP
The promise of asynchronous PHPWim Godden
 
How to send files to remote server via ssh in php
How to send files to remote server via ssh in phpHow to send files to remote server via ssh in php
How to send files to remote server via ssh in phpAndolasoft Inc
 
ZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 VersionZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 VersionIan Barber
 
How to stand on the shoulders of giants
How to stand on the shoulders of giantsHow to stand on the shoulders of giants
How to stand on the shoulders of giantsIan Barber
 
ZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made SimpleZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made SimpleIan Barber
 
Clojure + MongoDB on Heroku
Clojure + MongoDB on HerokuClojure + MongoDB on Heroku
Clojure + MongoDB on HerokuNaoyuki Kakuda
 
Web Apps in Perl - HTTP 101
Web Apps in Perl - HTTP 101Web Apps in Perl - HTTP 101
Web Apps in Perl - HTTP 101hendrikvb
 
Any event intro
Any event introAny event intro
Any event introqiang
 
Créer une base NoSQL en 1 heure
Créer une base NoSQL en 1 heureCréer une base NoSQL en 1 heure
Créer une base NoSQL en 1 heureAmaury Bouchard
 
Parsing JSON with a single regex
Parsing JSON with a single regexParsing JSON with a single regex
Parsing JSON with a single regexbrian d foy
 
Absolute Beginners Guide to Puppet Through Types - PuppetConf 2014
Absolute Beginners Guide to Puppet Through Types - PuppetConf 2014Absolute Beginners Guide to Puppet Through Types - PuppetConf 2014
Absolute Beginners Guide to Puppet Through Types - PuppetConf 2014Puppet
 
Coming Out Of Your Shell - A Comparison of *Nix Shells
Coming Out Of Your Shell - A Comparison of *Nix ShellsComing Out Of Your Shell - A Comparison of *Nix Shells
Coming Out Of Your Shell - A Comparison of *Nix ShellsKel Cecil
 
Devinsampa nginx-scripting
Devinsampa nginx-scriptingDevinsampa nginx-scripting
Devinsampa nginx-scriptingTony Fabeen
 
Perl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersPerl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersKirk Kimmel
 
Laporan setting dns
Laporan setting dnsLaporan setting dns
Laporan setting dnsSeptian Adi
 

Was ist angesagt? (20)

Comets notes
Comets notesComets notes
Comets notes
 
React PHP: the NodeJS challenger
React PHP: the NodeJS challengerReact PHP: the NodeJS challenger
React PHP: the NodeJS challenger
 
The promise of asynchronous PHP
The promise of asynchronous PHPThe promise of asynchronous PHP
The promise of asynchronous PHP
 
How to send files to remote server via ssh in php
How to send files to remote server via ssh in phpHow to send files to remote server via ssh in php
How to send files to remote server via ssh in php
 
Unix cheatsheet
Unix cheatsheetUnix cheatsheet
Unix cheatsheet
 
ZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 VersionZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 Version
 
How to stand on the shoulders of giants
How to stand on the shoulders of giantsHow to stand on the shoulders of giants
How to stand on the shoulders of giants
 
ZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made SimpleZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made Simple
 
Clojure + MongoDB on Heroku
Clojure + MongoDB on HerokuClojure + MongoDB on Heroku
Clojure + MongoDB on Heroku
 
Web Apps in Perl - HTTP 101
Web Apps in Perl - HTTP 101Web Apps in Perl - HTTP 101
Web Apps in Perl - HTTP 101
 
Any event intro
Any event introAny event intro
Any event intro
 
Créer une base NoSQL en 1 heure
Créer une base NoSQL en 1 heureCréer une base NoSQL en 1 heure
Créer une base NoSQL en 1 heure
 
Parsing JSON with a single regex
Parsing JSON with a single regexParsing JSON with a single regex
Parsing JSON with a single regex
 
Shell Script
Shell ScriptShell Script
Shell Script
 
Absolute Beginners Guide to Puppet Through Types - PuppetConf 2014
Absolute Beginners Guide to Puppet Through Types - PuppetConf 2014Absolute Beginners Guide to Puppet Through Types - PuppetConf 2014
Absolute Beginners Guide to Puppet Through Types - PuppetConf 2014
 
Coming Out Of Your Shell - A Comparison of *Nix Shells
Coming Out Of Your Shell - A Comparison of *Nix ShellsComing Out Of Your Shell - A Comparison of *Nix Shells
Coming Out Of Your Shell - A Comparison of *Nix Shells
 
Devinsampa nginx-scripting
Devinsampa nginx-scriptingDevinsampa nginx-scripting
Devinsampa nginx-scripting
 
Puppet Camp 2012
Puppet Camp 2012Puppet Camp 2012
Puppet Camp 2012
 
Perl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersPerl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one liners
 
Laporan setting dns
Laporan setting dnsLaporan setting dns
Laporan setting dns
 

Ähnlich wie Bash Scripting Workshop

One-Liners to Rule Them All
One-Liners to Rule Them AllOne-Liners to Rule Them All
One-Liners to Rule Them Allegypt
 
Shell Scripts
Shell ScriptsShell Scripts
Shell ScriptsDr.Ravi
 
101 3.4 use streams, pipes and redirects
101 3.4 use streams, pipes and redirects101 3.4 use streams, pipes and redirects
101 3.4 use streams, pipes and redirectsAcácio Oliveira
 
Unit 10 investigating and managing
Unit 10 investigating and managingUnit 10 investigating and managing
Unit 10 investigating and managingroot_fibo
 
Unix shell scripting basics
Unix shell scripting basicsUnix shell scripting basics
Unix shell scripting basicsAbhay Sapru
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting BasicsDr.Ravi
 
Really useful linux commands
Really useful linux commandsReally useful linux commands
Really useful linux commandsMichael J Geiser
 
Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...
Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...
Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...Vi Grey
 
44CON London 2015 - 15-Minute Linux Incident Response Live Analysis
44CON London 2015 - 15-Minute Linux Incident Response Live Analysis44CON London 2015 - 15-Minute Linux Incident Response Live Analysis
44CON London 2015 - 15-Minute Linux Incident Response Live Analysis44CON
 
A journey through the years of UNIX and Linux service management
A journey through the years of UNIX and Linux service managementA journey through the years of UNIX and Linux service management
A journey through the years of UNIX and Linux service managementLubomir Rintel
 
Logrotate sh
Logrotate shLogrotate sh
Logrotate shBen Pope
 
Ansible tips & tricks
Ansible tips & tricksAnsible tips & tricks
Ansible tips & tricksbcoca
 

Ähnlich wie Bash Scripting Workshop (20)

One-Liners to Rule Them All
One-Liners to Rule Them AllOne-Liners to Rule Them All
One-Liners to Rule Them All
 
Unix tips and tricks
Unix tips and tricksUnix tips and tricks
Unix tips and tricks
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Perl basics for Pentesters
Perl basics for PentestersPerl basics for Pentesters
Perl basics for Pentesters
 
Mac OSX Terminal 101
Mac OSX Terminal 101Mac OSX Terminal 101
Mac OSX Terminal 101
 
Shell Scripts
Shell ScriptsShell Scripts
Shell Scripts
 
IO Streams, Files and Directories
IO Streams, Files and DirectoriesIO Streams, Files and Directories
IO Streams, Files and Directories
 
Linux And perl
Linux And perlLinux And perl
Linux And perl
 
101 3.4 use streams, pipes and redirects
101 3.4 use streams, pipes and redirects101 3.4 use streams, pipes and redirects
101 3.4 use streams, pipes and redirects
 
Python build your security tools.pdf
Python build your security tools.pdfPython build your security tools.pdf
Python build your security tools.pdf
 
Unit 10 investigating and managing
Unit 10 investigating and managingUnit 10 investigating and managing
Unit 10 investigating and managing
 
Unix shell scripting basics
Unix shell scripting basicsUnix shell scripting basics
Unix shell scripting basics
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
 
Really useful linux commands
Really useful linux commandsReally useful linux commands
Really useful linux commands
 
Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...
Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...
Phishing for Root (How I Got Access to Root on Your Computer With 8 Seconds o...
 
44CON London 2015 - 15-Minute Linux Incident Response Live Analysis
44CON London 2015 - 15-Minute Linux Incident Response Live Analysis44CON London 2015 - 15-Minute Linux Incident Response Live Analysis
44CON London 2015 - 15-Minute Linux Incident Response Live Analysis
 
A journey through the years of UNIX and Linux service management
A journey through the years of UNIX and Linux service managementA journey through the years of UNIX and Linux service management
A journey through the years of UNIX and Linux service management
 
Logrotate sh
Logrotate shLogrotate sh
Logrotate sh
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Ansible tips & tricks
Ansible tips & tricksAnsible tips & tricks
Ansible tips & tricks
 

Mehr von Ahmed Magdy Ezzeldin, MSc.

Mehr von Ahmed Magdy Ezzeldin, MSc. (12)

Distributed RDBMS: Challenges, Solutions & Trade-offs
Distributed RDBMS: Challenges, Solutions & Trade-offsDistributed RDBMS: Challenges, Solutions & Trade-offs
Distributed RDBMS: Challenges, Solutions & Trade-offs
 
Win any Interview like a Boss
Win any Interview like a BossWin any Interview like a Boss
Win any Interview like a Boss
 
Answer Selection and Validation for Arabic Questions
Answer Selection and Validation for Arabic QuestionsAnswer Selection and Validation for Arabic Questions
Answer Selection and Validation for Arabic Questions
 
Arabic Question Answering: Challenges, Tasks, Approaches, Test-sets, Tools, A...
Arabic Question Answering: Challenges, Tasks, Approaches, Test-sets, Tools, A...Arabic Question Answering: Challenges, Tasks, Approaches, Test-sets, Tools, A...
Arabic Question Answering: Challenges, Tasks, Approaches, Test-sets, Tools, A...
 
A survey of fault prediction using machine learning algorithms
A survey of fault prediction using machine learning algorithmsA survey of fault prediction using machine learning algorithms
A survey of fault prediction using machine learning algorithms
 
GATE : General Architecture for Text Engineering
GATE : General Architecture for Text EngineeringGATE : General Architecture for Text Engineering
GATE : General Architecture for Text Engineering
 
Networks and Natural Language Processing
Networks and Natural Language ProcessingNetworks and Natural Language Processing
Networks and Natural Language Processing
 
Distributed Coordination-Based Systems
Distributed Coordination-Based SystemsDistributed Coordination-Based Systems
Distributed Coordination-Based Systems
 
Distributed Systems Naming
Distributed Systems NamingDistributed Systems Naming
Distributed Systems Naming
 
Cyclcone a safe dialect of C
Cyclcone a safe dialect of CCyclcone a safe dialect of C
Cyclcone a safe dialect of C
 
Objective C Memory Management
Objective C Memory ManagementObjective C Memory Management
Objective C Memory Management
 
Object Role Modeling
Object Role ModelingObject Role Modeling
Object Role Modeling
 

Kürzlich hochgeladen

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 

Kürzlich hochgeladen (20)

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 

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