SlideShare une entreprise Scribd logo
1  sur  14
Télécharger pour lire hors ligne
SHATRIX
Scripting

  A       Creating and Running Scripts
          Basic Script Architecture
  G       echo
  E       Variables
  N        read
       


          Conditional statements
  D       Loops
  A       Examples
Creating and Running Scripts

   Scripts are normal text files with x permission
       touch  script­name
       chmod  777  script­name
       chmod  u+x  script­name
   To run a script:
       full path
       ./script­name
       sh  script­name
       bash  script­name
Basic Script Architecure

   #!(shell-path)                       #!/bin/bash
        variables definition             echo ”Welcome”
                                          read var1
        read variables from user         echo $var1
        loops
        conditional statements
        print output to terminal
        pipe & redirection to other files
echo

   echo         displays a line of text
       echo  ”string”
       echo  $variable­name
       echo  ”string  $var­name”
       echo  ”string  $var­name  another­string”
       echo $(command)




           echo ”Welcome Linux users”
Variables
   Define a variable
       variable­name=VALUE
   Print a variable to screen
       echo  $variable­name
   System variables:
       Normal: has to be changed with each user
                   HOME     PWD    SHELL
       Environmental: changes with the login shell only
                   login shell with (su ­)
                   PATH
   env
read

   read       take a value from the user to a variable
       read  variable­name
       read  ­p  ”string”  variable­name


           read  ­p  ”Enter your name”  name
           echo  ”Your name is $name”
Conditional Statements
    if statement         #!/bin/bash
if [ condition ]          x=5
                          if [ $x = 5 ]
 then
                          then
   things to do              echo ”right”
 elif [ another­cond. ]   else
                             echo ”wrong”
 then                     fi
   things to do
 else
   things to do
fi
Conditional Statements

   case statement
                         #!/bin/bash
case $variable­name in   x=5
   value_1)              case $x in
      things to do;;     4)
                         echo ”x=4”;;
   value_2)              5)
      things to do;;     echo ”x=5”
                         echo ”ok”;;
   *)
                         *)
      default action;;   echo ”I don't know”;;
esac                     esac
Loops

    for loop
for  VARIABLE  in  ARRAY
do
    things on each value
done
                #!/bin/bash
                for i in 1 2 3 4 5
                do
                   echo ”current value is $i”
                done
Loops
    while loop
while  [ condition ]
do
                          #!/bin/bash
    things to do
                          VAR=0
done                      while [ $VAR ­lt 3 ]; do
                             echo $VAR
                             VAR=$[$VAR+1]
    equal (=)            done
    not equal (!=)
    Less than (­lt)
    Greater than (­gt)
Examples (User Login)
  #!/bin/bash
  user_name="shatrix"
  password="13"
  read ­p "User Name: " login_name
  stty ­echo
  read ­p "Password: " login_pass
  stty echo
  echo ""
  if [ $login_name = $user_name ];  then
     if [ $login_pass = $password ];  then
        echo "Now you are logged in....."
     else
        echo "Error, wrong password, try again"
     fi
  else
     echo "Error, user name doesn't exist"
  fi
Examples (Calculator)
  #!/bin/bash
  read ­p "Enter First Number: " f_num
  read ­p "Enter Second Number: " s_num
  read ­p "Enter Operation: " op

  case $op in
  +)
      echo "$f_num + $s_num = $[$f_num+$s_num]";;
  ­)
      echo "$f_num ­ $s_num = $[$f_num­$s_num]";;
  x)
      echo "$f_num x $s_num = $[$f_num*$s_num]";;
  /)
      echo "$f_num / $s_num = $[$f_num/$s_num]";;
  esac
Examples (Modified Calculator)
    #!/bin/bash
    read ­p "Enter First Number: " f_num
    read ­p "Enter Second Number: " s_num
    read ­p "Enter Operation: " op
    case $op in
    +)
        echo "$f_num + $s_num = $[$f_num+$s_num]";;
    ­)
        echo "$f_num ­ $s_num = $[$f_num­$s_num]";;
    x)
        echo "$f_num x $s_num = $[$f_num*$s_num]";;
    /)
        if [ $s_num != 0 ];  then
          echo "$f_num / $s_num = $[$f_num/$s_num]"
        else
          echo "Error, division by zero"
        fi;;
    *)
        echo "Wrong operation, please try again !!!"
    esac
Examples (PhoneBook)

Contenu connexe

Tendances

Bash shell
Bash shellBash shell
Bash shellxylas121
 
Module 03 Programming on Linux
Module 03 Programming on LinuxModule 03 Programming on Linux
Module 03 Programming on LinuxTushar B Kute
 
Shell programming 1.ppt
Shell programming  1.pptShell programming  1.ppt
Shell programming 1.pptKalkey
 
OpenGurukul : Language : Shell Scripting
OpenGurukul : Language : Shell ScriptingOpenGurukul : Language : Shell Scripting
OpenGurukul : Language : Shell ScriptingOpen Gurukul
 
Best training-in-mumbai-shell scripting
Best training-in-mumbai-shell scriptingBest training-in-mumbai-shell scripting
Best training-in-mumbai-shell scriptingvibrantuser
 
Licão 09 variables and arrays v2
Licão 09 variables and arrays v2Licão 09 variables and arrays v2
Licão 09 variables and arrays v2Acácio Oliveira
 
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...Zyxware Technologies
 
Unix shell scripting basics
Unix shell scripting basicsUnix shell scripting basics
Unix shell scripting basicsManav Prasad
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
Beautiful Bash: Let's make reading and writing bash scripts fun again!
Beautiful Bash: Let's make reading and writing bash scripts fun again!Beautiful Bash: Let's make reading and writing bash scripts fun again!
Beautiful Bash: Let's make reading and writing bash scripts fun again!Aaron Zauner
 
Methods of debugging - Atomate.net
Methods of debugging - Atomate.netMethods of debugging - Atomate.net
Methods of debugging - Atomate.netVitalie Chiperi
 
Talk Unix Shell Script
Talk Unix Shell ScriptTalk Unix Shell Script
Talk Unix Shell ScriptDr.Ravi
 
UNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming ConstructsUNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming ConstructsNihar Ranjan Paital
 

Tendances (20)

Bash shell
Bash shellBash shell
Bash shell
 
Module 03 Programming on Linux
Module 03 Programming on LinuxModule 03 Programming on Linux
Module 03 Programming on Linux
 
Chap06
Chap06Chap06
Chap06
 
Shell programming 1.ppt
Shell programming  1.pptShell programming  1.ppt
Shell programming 1.ppt
 
OpenGurukul : Language : Shell Scripting
OpenGurukul : Language : Shell ScriptingOpenGurukul : Language : Shell Scripting
OpenGurukul : Language : Shell Scripting
 
Best training-in-mumbai-shell scripting
Best training-in-mumbai-shell scriptingBest training-in-mumbai-shell scripting
Best training-in-mumbai-shell scripting
 
Licão 09 variables and arrays v2
Licão 09 variables and arrays v2Licão 09 variables and arrays v2
Licão 09 variables and arrays v2
 
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
 
First steps in C-Shell
First steps in C-ShellFirst steps in C-Shell
First steps in C-Shell
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
What is a shell script
What is a shell scriptWhat is a shell script
What is a shell script
 
Unix shell scripting
Unix shell scriptingUnix shell scripting
Unix shell scripting
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Unix shell scripting basics
Unix shell scripting basicsUnix shell scripting basics
Unix shell scripting basics
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Dades i operadors
Dades i operadorsDades i operadors
Dades i operadors
 
Beautiful Bash: Let's make reading and writing bash scripts fun again!
Beautiful Bash: Let's make reading and writing bash scripts fun again!Beautiful Bash: Let's make reading and writing bash scripts fun again!
Beautiful Bash: Let's make reading and writing bash scripts fun again!
 
Methods of debugging - Atomate.net
Methods of debugging - Atomate.netMethods of debugging - Atomate.net
Methods of debugging - Atomate.net
 
Talk Unix Shell Script
Talk Unix Shell ScriptTalk Unix Shell Script
Talk Unix Shell Script
 
UNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming ConstructsUNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming Constructs
 

En vedette

Building Embedded Linux Systems Introduction
Building Embedded Linux Systems IntroductionBuilding Embedded Linux Systems Introduction
Building Embedded Linux Systems IntroductionSherif Mousa
 
Operating systems Basics
Operating systems BasicsOperating systems Basics
Operating systems BasicsSherif Mousa
 
001 linux revision
001 linux revision001 linux revision
001 linux revisionSherif Mousa
 
Building Embedded Linux
Building Embedded LinuxBuilding Embedded Linux
Building Embedded LinuxSherif Mousa
 
Yocto - Embedded Linux Distribution Maker
Yocto - Embedded Linux Distribution MakerYocto - Embedded Linux Distribution Maker
Yocto - Embedded Linux Distribution MakerSherif Mousa
 
Building Embedded Linux Full Tutorial for ARM
Building Embedded Linux Full Tutorial for ARMBuilding Embedded Linux Full Tutorial for ARM
Building Embedded Linux Full Tutorial for ARMSherif Mousa
 
linux minimal os tutorial - by shatrix
linux minimal os tutorial - by shatrixlinux minimal os tutorial - by shatrix
linux minimal os tutorial - by shatrixSherif Mousa
 
Embedded Linux from Scratch to Yocto
Embedded Linux from Scratch to YoctoEmbedded Linux from Scratch to Yocto
Embedded Linux from Scratch to YoctoSherif Mousa
 
Linux shell script-1
Linux shell script-1Linux shell script-1
Linux shell script-1兎 伊藤
 
OTT in Azerbaijan - Project Brief
OTT in Azerbaijan - Project BriefOTT in Azerbaijan - Project Brief
OTT in Azerbaijan - Project BriefFarhad Shahrivar
 
Embedded linux barco-20121001
Embedded linux barco-20121001Embedded linux barco-20121001
Embedded linux barco-20121001Marc Leeman
 
sasikumarj_resume
sasikumarj_resumesasikumarj_resume
sasikumarj_resumeSasi Kumar
 
Ensoft dvb 1
Ensoft dvb 1Ensoft dvb 1
Ensoft dvb 1sarge
 
Assistencia geologica
Assistencia geologicaAssistencia geologica
Assistencia geologicacrom68
 
The move from a hardware centric design to a software centric design: GStream...
The move from a hardware centric design to a software centric design: GStream...The move from a hardware centric design to a software centric design: GStream...
The move from a hardware centric design to a software centric design: GStream...Marc Leeman
 
Text mining on the command line - Introduction to linux for bioinformatics
Text mining on the command line - Introduction to linux for bioinformaticsText mining on the command line - Introduction to linux for bioinformatics
Text mining on the command line - Introduction to linux for bioinformaticsBITS
 

En vedette (20)

Building Embedded Linux Systems Introduction
Building Embedded Linux Systems IntroductionBuilding Embedded Linux Systems Introduction
Building Embedded Linux Systems Introduction
 
Operating systems Basics
Operating systems BasicsOperating systems Basics
Operating systems Basics
 
001 linux revision
001 linux revision001 linux revision
001 linux revision
 
Building Embedded Linux
Building Embedded LinuxBuilding Embedded Linux
Building Embedded Linux
 
Yocto - Embedded Linux Distribution Maker
Yocto - Embedded Linux Distribution MakerYocto - Embedded Linux Distribution Maker
Yocto - Embedded Linux Distribution Maker
 
Building Embedded Linux Full Tutorial for ARM
Building Embedded Linux Full Tutorial for ARMBuilding Embedded Linux Full Tutorial for ARM
Building Embedded Linux Full Tutorial for ARM
 
005 skyeye
005 skyeye005 skyeye
005 skyeye
 
linux minimal os tutorial - by shatrix
linux minimal os tutorial - by shatrixlinux minimal os tutorial - by shatrix
linux minimal os tutorial - by shatrix
 
Embedded Linux from Scratch to Yocto
Embedded Linux from Scratch to YoctoEmbedded Linux from Scratch to Yocto
Embedded Linux from Scratch to Yocto
 
Linux shell script-1
Linux shell script-1Linux shell script-1
Linux shell script-1
 
Shell Script Tutorial
Shell Script TutorialShell Script Tutorial
Shell Script Tutorial
 
OTT in Azerbaijan - Project Brief
OTT in Azerbaijan - Project BriefOTT in Azerbaijan - Project Brief
OTT in Azerbaijan - Project Brief
 
Embedded linux barco-20121001
Embedded linux barco-20121001Embedded linux barco-20121001
Embedded linux barco-20121001
 
sasikumarj_resume
sasikumarj_resumesasikumarj_resume
sasikumarj_resume
 
Ensoft dvb 1
Ensoft dvb 1Ensoft dvb 1
Ensoft dvb 1
 
How To Handle An IRD Audit - Atainz
How To Handle An IRD Audit - AtainzHow To Handle An IRD Audit - Atainz
How To Handle An IRD Audit - Atainz
 
Assistencia geologica
Assistencia geologicaAssistencia geologica
Assistencia geologica
 
The move from a hardware centric design to a software centric design: GStream...
The move from a hardware centric design to a software centric design: GStream...The move from a hardware centric design to a software centric design: GStream...
The move from a hardware centric design to a software centric design: GStream...
 
Linux Workshop , Day 3
Linux Workshop , Day 3Linux Workshop , Day 3
Linux Workshop , Day 3
 
Text mining on the command line - Introduction to linux for bioinformatics
Text mining on the command line - Introduction to linux for bioinformaticsText mining on the command line - Introduction to linux for bioinformatics
Text mining on the command line - Introduction to linux for bioinformatics
 

Similaire à 003 scripting

Unit 11 configuring the bash shell – shell script
Unit 11 configuring the bash shell – shell scriptUnit 11 configuring the bash shell – shell script
Unit 11 configuring the bash shell – shell scriptroot_fibo
 
Raspberry pi Part 25
Raspberry pi Part 25Raspberry pi Part 25
Raspberry pi Part 25Techvilla
 
390aLecture05_12sp.ppt
390aLecture05_12sp.ppt390aLecture05_12sp.ppt
390aLecture05_12sp.pptmugeshmsd5
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell ScriptingRaghu nath
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
BASH Variables Part 1: Basic Interpolation
BASH Variables Part 1: Basic InterpolationBASH Variables Part 1: Basic Interpolation
BASH Variables Part 1: Basic InterpolationWorkhorse Computing
 
Bash and regular expressions
Bash and regular expressionsBash and regular expressions
Bash and regular expressionsplarsen67
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation TutorialLorna Mitchell
 
Licão 12 decision loops - statement iteration
Licão 12 decision loops - statement iterationLicão 12 decision loops - statement iteration
Licão 12 decision loops - statement iterationAcácio Oliveira
 
Unix 1st sem lab programs a - VTU Karnataka
Unix 1st sem lab programs a - VTU KarnatakaUnix 1st sem lab programs a - VTU Karnataka
Unix 1st sem lab programs a - VTU KarnatakaiCreateWorld
 
Unleash your inner console cowboy
Unleash your inner console cowboyUnleash your inner console cowboy
Unleash your inner console cowboyKenneth Geisshirt
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting BasicsSudharsan S
 
Case, Loop & Command line args un Unix
Case, Loop & Command line args un UnixCase, Loop & Command line args un Unix
Case, Loop & Command line args un UnixVpmv
 
34-shell-programming.ppt
34-shell-programming.ppt34-shell-programming.ppt
34-shell-programming.pptKiranMantri
 
Perl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim PerlchinaPerl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim PerlchinaLin Yo-An
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perlworr1244
 
Talk Unix Shell Script 1
Talk Unix Shell Script 1Talk Unix Shell Script 1
Talk Unix Shell Script 1Dr.Ravi
 

Similaire à 003 scripting (20)

Unit 11 configuring the bash shell – shell script
Unit 11 configuring the bash shell – shell scriptUnit 11 configuring the bash shell – shell script
Unit 11 configuring the bash shell – shell script
 
Raspberry pi Part 25
Raspberry pi Part 25Raspberry pi Part 25
Raspberry pi Part 25
 
390aLecture05_12sp.ppt
390aLecture05_12sp.ppt390aLecture05_12sp.ppt
390aLecture05_12sp.ppt
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
BASH Variables Part 1: Basic Interpolation
BASH Variables Part 1: Basic InterpolationBASH Variables Part 1: Basic Interpolation
BASH Variables Part 1: Basic Interpolation
 
Bash and regular expressions
Bash and regular expressionsBash and regular expressions
Bash and regular expressions
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation Tutorial
 
Licão 12 decision loops - statement iteration
Licão 12 decision loops - statement iterationLicão 12 decision loops - statement iteration
Licão 12 decision loops - statement iteration
 
KT on Bash Script.pptx
KT on Bash Script.pptxKT on Bash Script.pptx
KT on Bash Script.pptx
 
Unix 1st sem lab programs a - VTU Karnataka
Unix 1st sem lab programs a - VTU KarnatakaUnix 1st sem lab programs a - VTU Karnataka
Unix 1st sem lab programs a - VTU Karnataka
 
Bash Scripting
Bash ScriptingBash Scripting
Bash Scripting
 
Unleash your inner console cowboy
Unleash your inner console cowboyUnleash your inner console cowboy
Unleash your inner console cowboy
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
 
Bash production guide
Bash production guideBash production guide
Bash production guide
 
Case, Loop & Command line args un Unix
Case, Loop & Command line args un UnixCase, Loop & Command line args un Unix
Case, Loop & Command line args un Unix
 
34-shell-programming.ppt
34-shell-programming.ppt34-shell-programming.ppt
34-shell-programming.ppt
 
Perl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim PerlchinaPerl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim Perlchina
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Talk Unix Shell Script 1
Talk Unix Shell Script 1Talk Unix Shell Script 1
Talk Unix Shell Script 1
 

003 scripting

  • 1. SHATRIX Scripting A  Creating and Running Scripts  Basic Script Architecture G  echo E  Variables N read   Conditional statements D  Loops A  Examples
  • 2. Creating and Running Scripts  Scripts are normal text files with x permission  touch  script­name  chmod  777  script­name  chmod  u+x  script­name  To run a script:  full path  ./script­name  sh  script­name  bash  script­name
  • 3. Basic Script Architecure  #!(shell-path) #!/bin/bash  variables definition  echo ”Welcome”  read var1  read variables from user  echo $var1  loops  conditional statements  print output to terminal  pipe & redirection to other files
  • 4. echo  echo displays a line of text  echo  ”string”  echo  $variable­name  echo  ”string  $var­name”  echo  ”string  $var­name  another­string”  echo $(command) echo ”Welcome Linux users”
  • 5. Variables  Define a variable  variable­name=VALUE  Print a variable to screen  echo  $variable­name  System variables:  Normal: has to be changed with each user  HOME     PWD    SHELL  Environmental: changes with the login shell only  login shell with (su ­)  PATH  env
  • 6. read  read take a value from the user to a variable  read  variable­name  read  ­p  ”string”  variable­name read  ­p  ”Enter your name”  name echo  ”Your name is $name”
  • 7. Conditional Statements  if statement #!/bin/bash if [ condition ] x=5 if [ $x = 5 ]  then then    things to do echo ”right”  elif [ another­cond. ] else echo ”wrong”  then fi    things to do  else    things to do fi
  • 8. Conditional Statements  case statement #!/bin/bash case $variable­name in x=5    value_1) case $x in       things to do;; 4) echo ”x=4”;;    value_2) 5)       things to do;; echo ”x=5” echo ”ok”;;    *) *)       default action;; echo ”I don't know”;; esac esac
  • 9. Loops  for loop for  VARIABLE  in  ARRAY do     things on each value done #!/bin/bash for i in 1 2 3 4 5 do echo ”current value is $i” done
  • 10. Loops  while loop while  [ condition ] do #!/bin/bash     things to do VAR=0 done while [ $VAR ­lt 3 ]; do    echo $VAR    VAR=$[$VAR+1]  equal (=) done  not equal (!=)  Less than (­lt)  Greater than (­gt)
  • 11. Examples (User Login) #!/bin/bash user_name="shatrix" password="13" read ­p "User Name: " login_name stty ­echo read ­p "Password: " login_pass stty echo echo "" if [ $login_name = $user_name ];  then if [ $login_pass = $password ];  then echo "Now you are logged in....." else echo "Error, wrong password, try again" fi else echo "Error, user name doesn't exist" fi
  • 12. Examples (Calculator) #!/bin/bash read ­p "Enter First Number: " f_num read ­p "Enter Second Number: " s_num read ­p "Enter Operation: " op case $op in +)     echo "$f_num + $s_num = $[$f_num+$s_num]";; ­)     echo "$f_num ­ $s_num = $[$f_num­$s_num]";; x)     echo "$f_num x $s_num = $[$f_num*$s_num]";; /)     echo "$f_num / $s_num = $[$f_num/$s_num]";; esac
  • 13. Examples (Modified Calculator) #!/bin/bash read ­p "Enter First Number: " f_num read ­p "Enter Second Number: " s_num read ­p "Enter Operation: " op case $op in +)     echo "$f_num + $s_num = $[$f_num+$s_num]";; ­)     echo "$f_num ­ $s_num = $[$f_num­$s_num]";; x)     echo "$f_num x $s_num = $[$f_num*$s_num]";; /)     if [ $s_num != 0 ];  then       echo "$f_num / $s_num = $[$f_num/$s_num]"     else       echo "Error, division by zero"     fi;; *)     echo "Wrong operation, please try again !!!" esac