SlideShare a Scribd company logo
introduction of
the shell script
basic sh and shell script usage
2013 usagi@WonderRabbitProject.net CC0
what's the shell script?
● it's a language
   ○ script language // like a C style
     ■ procedural model
     ■ function is available
     ■ variable is available
     ■ type system is not available // so text based
     ■ can call an external routine // system call
● it's as same the shell-self
   ○ APIs are commands on the system
if you learned a C style language
● control statements
  ○ loop
    ■ `for`
    ■ `while` // and `until`
  ○ conditional branching
    ■ `if`
    ■ `case` // equiv switch
● function and variable
  ○ `f(){}` // equiv `void f(void){}`
    ■ how to use input params and a return? --> next!
  ○ `ABC=hoge` // equiv `char* ABC = "hoge"`
    ■ and more --> next!!
how to use function
● how to get input params?
  ○ f(){ echo $1; echo $2; echo $3; }
    ■ it's equiv in C style:
        void f(char* a, char* b, char* c)
        { puts(a); puts(b); puts(c); }
● how to put return value?
  ○ n/a // shell script has not C style return value!
    ■ but, it has return code as same the system call
● how call function?
  ○ f hoge fuga piyo
    ■ it's equiv in C style
        f("hoge", "fuga", "piyo")
return value of function call
● n/a
● but, can use static variables and a trick
  ○ trick
    # define // it's line comment use the char of `#`
    f(){ echo "it's return value!"; }
    # call and get return value trick with backquote '`'
    RETURN_VALUE = `f`
    # and print
    echo $RETURN_VALUE
    ■ what's `$` token?
         ●   --> it's a variable to next!
how to use a variable?
● define a variable
  ○ HOGE=hogehogehoge
    FUGA=fugafugafuga
    num_of_my_awesome_defition=123456
● reference a variable
  ○ echo $HOGE
    echo "$HOGE-$FUGA"
    echo $num_of_my_awesome_definition >> $hoge
variable tips-1
● a variable is only string type
   ○ abc=1234 # quiv to abc="1234"
     # it's not a number
● writing rules
   ○ standard definition
     ■ abc=hogehoge # it's ok
     ■ abc = fugafuga # ERROR! it's a function call
        # $1='=', $2='fugafuga'; hint is white-space.
   ○ explicit value
     ■ def='#abc $abc @abc' # equiv: #abc $abc @abc
     ■ def="#abc $abc @abc'
        # equiv: #abc fugafuga @abc
variable tips-2
● implicit defined special values
   ○ $1 $2 $3 ...
     it's params by a function call or run a script with
     params.
     # f(){ echo $1; }
     // int main(int ac, char** params) { }
   ○ $#
     it's number of params
     # f(){ echo $#; }
     // int main(int ac, char** params) { }
   ○ $?
     it's a last result code (success or else)
     # echo $?
let's run a shell script
> pwd
/home/myname
> # it's comment line
> my_awesome_variable=1234
> echo $my_awesome_variable
1234
> my_awesome_function(){ echo $1; }
> my_awesome_function hoge
hoge
save to file a shell script
> echo 'echo $1' > my_awesome.sh
> cat my_awesome.sh
echo $1

you can write with the Vim or the other text
editor as you like if you want to use.

> vim my_aersome2.sh
run a shell script from a file
● run the other process on the process
  > sh my_awesome.sh hoge
  hoge
  > bash my_awesome.sh fuga
  fuga
● run in the process
  > source my_awesome.sh piyo
  piyo
environment variable
● variable
  ○ shell-variable
    it's only visible from the shell process.
    > abc=hoge
    > sh <-- run a child process
    >> echo $abc <-- it's put the empty string
    >> exit
    > echo $abc <-- it's puts 'hoge'
  ○ environment-varialbe
    it's visible from the shell and child processes.
    > export abc=hoge
    > sh
    >> echo $abc <-- it's puts 'hoge'
shebang and executable-file
● shebang
  ○ > vim hoge.sh
        #!/bin/sh <-- it's shebang liine
        echo $1
● executable-file
  ○ set the executable flag
    > chmod +x hoge.sh
  ○ run
    > ./hoge.sh <-- don't forget path before filename!
    ■ hint: see the $PATH environment variable. it's
        search path for a command on your system. and
        think with a security policy.
shebang for any script-files
● shell script
  #!/bin/sh
● node.js script
  #!/usr/bin/node
● python script
  #!/usr/bin/python
● php script
  #!/usr/bin/php
● haskell script
  #!/usr/bin/runhaskell
     hint: > file hoge.sh
`for` statement
and an array like variable
# it's like an array separated by the space char
values='aaa bbb ccc'

# like the foreach in Java or for-in in JS
for value in $values
do
 echo $value
done
`seq` command
> seq 8
1
2
3
4
5
6
7
8
`...` <-- it's the back-quote pair,
not a single-quote pair('...')
# if use the single-quote pair
> echo 'seq 512 64 768'
seq 512 64 768
# if use the back-quote pair
> echo `seq 512 64 768`
512 576 640 704 768 <-- it's like an array string

the back-quote pattern is like an eval() in JS.
it's run a text as a script code. be cautious!
`for` statement
with `seq` command and ` pattern
it's give a loop with a numeric sequence.

for value in `seq 0 15`
do
 echo $value
done
`while` and `until` statements
● while <-- if true then loop
    while true
    do
      echo '\(^o^)/'
    done
● until <-- if false then loop
    until false
    do
      echo '/(^o^)\'
    done
`test` command for comparison
>   test 3 -eq 4 <-- equiv (3 == 4)
>   echo $?
1   <-- so false in shell-script
>   test 3 -ne 4 <-- equiv (3 != 4)
>   echo $?
0   <-- so true in shell-script
>   test 3 -gt 4 <-- equiv (3 > 4)
>   echo $?
1   <-- so false in shell-script
`test` command for file check
>   test -f /proc/cpuinfo <-- is the file available
>   echo $?
0
>   test -d /boot <-- is the directory available
>   echo $?
0
`expr` command
for calc a number from a string
# it's ng, not calc
> echo 1 + 2 + 4

#   it's ok, calc
>   echo `expr 1 + 2 + 4`
7
>   expr 5 + ( 5 - 5 ) * 5 / 5 % 5
5
#   note: ( ) * <-- need  escape-sequence.
`until` statement
with `test`, `expr` and ` pattern
it's give a loop with a numeric counter.

counter=15
until `test $counter -eq 0`
do
 echo $counter
 counter=`expr $counter - 1`
done
`if` statement
# if.sh                > source if.sh
f(){
                       >f0
  if test $1 -lt 0
  then                 zero
    echo 'negative'    >f1
  elif test $1 -gt 0   positive
  then
    echo 'positive'
                       > f -1
  else                 negative
    echo 'zero'
  fi
}
`case` statement
# case.sh              > source case.sh
f(){
                       >f0
  case $1 in
  0)                   zerp
   echo 'zero';;       >f5
  1)                   two-nine
   echo 'one';;
  [2-9])
                       > f -1
   echo 'two-nine';;   ????
  default)
   echo '????'
  esac
}
where are more information?
all of the information is
available on the man
pages. see the man pages.
man page is the best
unified answerer for all
GNU/Linux users.

> man sh
Ad

More Related Content

What's hot (20)

Advanced modulinos trial
Advanced modulinos trialAdvanced modulinos trial
Advanced modulinos trial
brian d foy
 
Learning Perl 6
Learning Perl 6 Learning Perl 6
Learning Perl 6
brian d foy
 
CLI, the other SAPI phpnw11
CLI, the other SAPI phpnw11CLI, the other SAPI phpnw11
CLI, the other SAPI phpnw11
Combell NV
 
The promise of asynchronous PHP
The promise of asynchronous PHPThe promise of asynchronous PHP
The promise of asynchronous PHP
Wim Godden
 
Functional Programming in PHP
Functional Programming in PHPFunctional Programming in PHP
Functional Programming in PHP
pwmosquito
 
Learning Perl 6 (NPW 2007)
Learning Perl 6 (NPW 2007)Learning Perl 6 (NPW 2007)
Learning Perl 6 (NPW 2007)
brian d foy
 
De 0 a 100 con Bash Shell Scripting y AWK
De 0 a 100 con Bash Shell Scripting y AWKDe 0 a 100 con Bash Shell Scripting y AWK
De 0 a 100 con Bash Shell Scripting y AWK
Adolfo Sanz De Diego
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?
Nikita Popov
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - php
Hung-yu Lin
 
Advanced modulinos
Advanced modulinosAdvanced modulinos
Advanced modulinos
brian d foy
 
Module 03 Programming on Linux
Module 03 Programming on LinuxModule 03 Programming on Linux
Module 03 Programming on Linux
Tushar B Kute
 
PerlScripting
PerlScriptingPerlScripting
PerlScripting
Aureliano Bombarely
 
PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021
Ayesh Karunaratne
 
Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015
Lin Yo-An
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string library
MomenMostafa
 
Advanced php
Advanced phpAdvanced php
Advanced php
Anne Lee
 
PerlTesting
PerlTestingPerlTesting
PerlTesting
Aureliano Bombarely
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping
MomenMostafa
 
OSDC.TW - Gutscript for PHP haters
OSDC.TW - Gutscript for PHP hatersOSDC.TW - Gutscript for PHP haters
OSDC.TW - Gutscript for PHP haters
Lin Yo-An
 
Perl 6 by example
Perl 6 by examplePerl 6 by example
Perl 6 by example
Andrew Shitov
 
Advanced modulinos trial
Advanced modulinos trialAdvanced modulinos trial
Advanced modulinos trial
brian d foy
 
Learning Perl 6
Learning Perl 6 Learning Perl 6
Learning Perl 6
brian d foy
 
CLI, the other SAPI phpnw11
CLI, the other SAPI phpnw11CLI, the other SAPI phpnw11
CLI, the other SAPI phpnw11
Combell NV
 
The promise of asynchronous PHP
The promise of asynchronous PHPThe promise of asynchronous PHP
The promise of asynchronous PHP
Wim Godden
 
Functional Programming in PHP
Functional Programming in PHPFunctional Programming in PHP
Functional Programming in PHP
pwmosquito
 
Learning Perl 6 (NPW 2007)
Learning Perl 6 (NPW 2007)Learning Perl 6 (NPW 2007)
Learning Perl 6 (NPW 2007)
brian d foy
 
De 0 a 100 con Bash Shell Scripting y AWK
De 0 a 100 con Bash Shell Scripting y AWKDe 0 a 100 con Bash Shell Scripting y AWK
De 0 a 100 con Bash Shell Scripting y AWK
Adolfo Sanz De Diego
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?
Nikita Popov
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - php
Hung-yu Lin
 
Advanced modulinos
Advanced modulinosAdvanced modulinos
Advanced modulinos
brian d foy
 
Module 03 Programming on Linux
Module 03 Programming on LinuxModule 03 Programming on Linux
Module 03 Programming on Linux
Tushar B Kute
 
PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021
Ayesh Karunaratne
 
Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015
Lin Yo-An
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string library
MomenMostafa
 
Advanced php
Advanced phpAdvanced php
Advanced php
Anne Lee
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping
MomenMostafa
 
OSDC.TW - Gutscript for PHP haters
OSDC.TW - Gutscript for PHP hatersOSDC.TW - Gutscript for PHP haters
OSDC.TW - Gutscript for PHP haters
Lin Yo-An
 

Viewers also liked (8)

Mastering the MongoDB Shell
Mastering the MongoDB ShellMastering the MongoDB Shell
Mastering the MongoDB Shell
MongoDB
 
Shell Script Tutorial
Shell Script TutorialShell Script Tutorial
Shell Script Tutorial
Quang Minh Đoàn
 
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
BITS
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
Gaurav Shinde
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
Dr.Ravi
 
003 scripting
003 scripting003 scripting
003 scripting
Sherif Mousa
 
Unix shell scripts
Unix shell scriptsUnix shell scripts
Unix shell scripts
Prakash Lambha
 
Mastering the MongoDB Shell
Mastering the MongoDB ShellMastering the MongoDB Shell
Mastering the MongoDB Shell
MongoDB
 
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
BITS
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
Dr.Ravi
 
Ad

Similar to Linux shell script-1 (20)

php programming.pptx
php programming.pptxphp programming.pptx
php programming.pptx
rani marri
 
Shell Scripts
Shell ScriptsShell Scripts
Shell Scripts
Dr.Ravi
 
Operating_System_Lab_ClassOperating_System_2.pdf
Operating_System_Lab_ClassOperating_System_2.pdfOperating_System_Lab_ClassOperating_System_2.pdf
Operating_System_Lab_ClassOperating_System_2.pdf
DharmatejMallampati
 
shellScriptAlt.pptx
shellScriptAlt.pptxshellScriptAlt.pptx
shellScriptAlt.pptx
NiladriDey18
 
34-shell-programming asda asda asd asd.ppt
34-shell-programming asda asda asd asd.ppt34-shell-programming asda asda asd asd.ppt
34-shell-programming asda asda asd asd.ppt
poyotero
 
Web Technology_10.ppt
Web Technology_10.pptWeb Technology_10.ppt
Web Technology_10.ppt
Aftabali702240
 
OS.pdf
OS.pdfOS.pdf
OS.pdf
ErPawanKumar3
 
34-shell-programming.ppt
34-shell-programming.ppt34-shell-programming.ppt
34-shell-programming.ppt
KiranMantri
 
Shell Programming_Module2_Part2.pptx.pdf
Shell Programming_Module2_Part2.pptx.pdfShell Programming_Module2_Part2.pptx.pdf
Shell Programming_Module2_Part2.pptx.pdf
HIMANKMISHRA2
 
Unix Shell Programming subject shell scripting ppt
Unix Shell Programming subject shell scripting pptUnix Shell Programming subject shell scripting ppt
Unix Shell Programming subject shell scripting ppt
Radhika Ajadka
 
How to run PHP code in XAMPP.docx (1).pdf
How to run PHP code in XAMPP.docx (1).pdfHow to run PHP code in XAMPP.docx (1).pdf
How to run PHP code in XAMPP.docx (1).pdf
rajeswaria21
 
SymfonyCon 2017 php7 performances
SymfonyCon 2017 php7 performancesSymfonyCon 2017 php7 performances
SymfonyCon 2017 php7 performances
julien pauli
 
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
 of 70UNIX Unbounded 5th EditionAmir Afzal .docx of 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
MARRY7
 
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docxof 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
adkinspaige22
 
AOS_Module_3ssssssssssssssssssssssssssss.pptx
AOS_Module_3ssssssssssssssssssssssssssss.pptxAOS_Module_3ssssssssssssssssssssssssssss.pptx
AOS_Module_3ssssssssssssssssssssssssssss.pptx
rapiwip803
 
Web 8 | Introduction to PHP
Web 8 | Introduction to PHPWeb 8 | Introduction to PHP
Web 8 | Introduction to PHP
Mohammad Imam Hossain
 
Introduction in php
Introduction in phpIntroduction in php
Introduction in php
Bozhidar Boshnakov
 
KT on Bash Script.pptx
KT on Bash Script.pptxKT on Bash Script.pptx
KT on Bash Script.pptx
gogulasivannarayana
 
Shell programming
Shell programmingShell programming
Shell programming
Moayad Moawiah
 
Php basics
Php basicsPhp basics
Php basics
hamfu
 
php programming.pptx
php programming.pptxphp programming.pptx
php programming.pptx
rani marri
 
Shell Scripts
Shell ScriptsShell Scripts
Shell Scripts
Dr.Ravi
 
Operating_System_Lab_ClassOperating_System_2.pdf
Operating_System_Lab_ClassOperating_System_2.pdfOperating_System_Lab_ClassOperating_System_2.pdf
Operating_System_Lab_ClassOperating_System_2.pdf
DharmatejMallampati
 
shellScriptAlt.pptx
shellScriptAlt.pptxshellScriptAlt.pptx
shellScriptAlt.pptx
NiladriDey18
 
34-shell-programming asda asda asd asd.ppt
34-shell-programming asda asda asd asd.ppt34-shell-programming asda asda asd asd.ppt
34-shell-programming asda asda asd asd.ppt
poyotero
 
34-shell-programming.ppt
34-shell-programming.ppt34-shell-programming.ppt
34-shell-programming.ppt
KiranMantri
 
Shell Programming_Module2_Part2.pptx.pdf
Shell Programming_Module2_Part2.pptx.pdfShell Programming_Module2_Part2.pptx.pdf
Shell Programming_Module2_Part2.pptx.pdf
HIMANKMISHRA2
 
Unix Shell Programming subject shell scripting ppt
Unix Shell Programming subject shell scripting pptUnix Shell Programming subject shell scripting ppt
Unix Shell Programming subject shell scripting ppt
Radhika Ajadka
 
How to run PHP code in XAMPP.docx (1).pdf
How to run PHP code in XAMPP.docx (1).pdfHow to run PHP code in XAMPP.docx (1).pdf
How to run PHP code in XAMPP.docx (1).pdf
rajeswaria21
 
SymfonyCon 2017 php7 performances
SymfonyCon 2017 php7 performancesSymfonyCon 2017 php7 performances
SymfonyCon 2017 php7 performances
julien pauli
 
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
 of 70UNIX Unbounded 5th EditionAmir Afzal .docx of 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
MARRY7
 
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docxof 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
adkinspaige22
 
AOS_Module_3ssssssssssssssssssssssssssss.pptx
AOS_Module_3ssssssssssssssssssssssssssss.pptxAOS_Module_3ssssssssssssssssssssssssssss.pptx
AOS_Module_3ssssssssssssssssssssssssssss.pptx
rapiwip803
 
Php basics
Php basicsPhp basics
Php basics
hamfu
 
Ad

Recently uploaded (20)

Herbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptxHerbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptx
RAJU THENGE
 
Rococo versus Neoclassicism. The artistic styles of the 18th century
Rococo versus Neoclassicism. The artistic styles of the 18th centuryRococo versus Neoclassicism. The artistic styles of the 18th century
Rococo versus Neoclassicism. The artistic styles of the 18th century
Gema
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
Computer crime and Legal issues Computer crime and Legal issues
Computer crime and Legal issues Computer crime and Legal issuesComputer crime and Legal issues Computer crime and Legal issues
Computer crime and Legal issues Computer crime and Legal issues
Abhijit Bodhe
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
TechSoup
 
APGAR SCORE BY sweety Tamanna Mahapatra MSc Pediatric
APGAR SCORE  BY sweety Tamanna Mahapatra MSc PediatricAPGAR SCORE  BY sweety Tamanna Mahapatra MSc Pediatric
APGAR SCORE BY sweety Tamanna Mahapatra MSc Pediatric
SweetytamannaMohapat
 
Kenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 CohortKenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 Cohort
EducationNC
 
dynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south Indiadynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south India
PrachiSontakke5
 
How to Create A Todo List In Todo of Odoo 18
How to Create A Todo List In Todo of Odoo 18How to Create A Todo List In Todo of Odoo 18
How to Create A Todo List In Todo of Odoo 18
Celine George
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Grade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable WorksheetGrade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable Worksheet
Sritoma Majumder
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
Herbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptxHerbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptx
RAJU THENGE
 
Rococo versus Neoclassicism. The artistic styles of the 18th century
Rococo versus Neoclassicism. The artistic styles of the 18th centuryRococo versus Neoclassicism. The artistic styles of the 18th century
Rococo versus Neoclassicism. The artistic styles of the 18th century
Gema
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
Computer crime and Legal issues Computer crime and Legal issues
Computer crime and Legal issues Computer crime and Legal issuesComputer crime and Legal issues Computer crime and Legal issues
Computer crime and Legal issues Computer crime and Legal issues
Abhijit Bodhe
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
TechSoup
 
APGAR SCORE BY sweety Tamanna Mahapatra MSc Pediatric
APGAR SCORE  BY sweety Tamanna Mahapatra MSc PediatricAPGAR SCORE  BY sweety Tamanna Mahapatra MSc Pediatric
APGAR SCORE BY sweety Tamanna Mahapatra MSc Pediatric
SweetytamannaMohapat
 
Kenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 CohortKenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 Cohort
EducationNC
 
dynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south Indiadynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south India
PrachiSontakke5
 
How to Create A Todo List In Todo of Odoo 18
How to Create A Todo List In Todo of Odoo 18How to Create A Todo List In Todo of Odoo 18
How to Create A Todo List In Todo of Odoo 18
Celine George
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Grade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable WorksheetGrade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable Worksheet
Sritoma Majumder
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 

Linux shell script-1

  • 1. introduction of the shell script basic sh and shell script usage 2013 [email protected] CC0
  • 2. what's the shell script? ● it's a language ○ script language // like a C style ■ procedural model ■ function is available ■ variable is available ■ type system is not available // so text based ■ can call an external routine // system call ● it's as same the shell-self ○ APIs are commands on the system
  • 3. if you learned a C style language ● control statements ○ loop ■ `for` ■ `while` // and `until` ○ conditional branching ■ `if` ■ `case` // equiv switch ● function and variable ○ `f(){}` // equiv `void f(void){}` ■ how to use input params and a return? --> next! ○ `ABC=hoge` // equiv `char* ABC = "hoge"` ■ and more --> next!!
  • 4. how to use function ● how to get input params? ○ f(){ echo $1; echo $2; echo $3; } ■ it's equiv in C style: void f(char* a, char* b, char* c) { puts(a); puts(b); puts(c); } ● how to put return value? ○ n/a // shell script has not C style return value! ■ but, it has return code as same the system call ● how call function? ○ f hoge fuga piyo ■ it's equiv in C style f("hoge", "fuga", "piyo")
  • 5. return value of function call ● n/a ● but, can use static variables and a trick ○ trick # define // it's line comment use the char of `#` f(){ echo "it's return value!"; } # call and get return value trick with backquote '`' RETURN_VALUE = `f` # and print echo $RETURN_VALUE ■ what's `$` token? ● --> it's a variable to next!
  • 6. how to use a variable? ● define a variable ○ HOGE=hogehogehoge FUGA=fugafugafuga num_of_my_awesome_defition=123456 ● reference a variable ○ echo $HOGE echo "$HOGE-$FUGA" echo $num_of_my_awesome_definition >> $hoge
  • 7. variable tips-1 ● a variable is only string type ○ abc=1234 # quiv to abc="1234" # it's not a number ● writing rules ○ standard definition ■ abc=hogehoge # it's ok ■ abc = fugafuga # ERROR! it's a function call # $1='=', $2='fugafuga'; hint is white-space. ○ explicit value ■ def='#abc $abc @abc' # equiv: #abc $abc @abc ■ def="#abc $abc @abc' # equiv: #abc fugafuga @abc
  • 8. variable tips-2 ● implicit defined special values ○ $1 $2 $3 ... it's params by a function call or run a script with params. # f(){ echo $1; } // int main(int ac, char** params) { } ○ $# it's number of params # f(){ echo $#; } // int main(int ac, char** params) { } ○ $? it's a last result code (success or else) # echo $?
  • 9. let's run a shell script > pwd /home/myname > # it's comment line > my_awesome_variable=1234 > echo $my_awesome_variable 1234 > my_awesome_function(){ echo $1; } > my_awesome_function hoge hoge
  • 10. save to file a shell script > echo 'echo $1' > my_awesome.sh > cat my_awesome.sh echo $1 you can write with the Vim or the other text editor as you like if you want to use. > vim my_aersome2.sh
  • 11. run a shell script from a file ● run the other process on the process > sh my_awesome.sh hoge hoge > bash my_awesome.sh fuga fuga ● run in the process > source my_awesome.sh piyo piyo
  • 12. environment variable ● variable ○ shell-variable it's only visible from the shell process. > abc=hoge > sh <-- run a child process >> echo $abc <-- it's put the empty string >> exit > echo $abc <-- it's puts 'hoge' ○ environment-varialbe it's visible from the shell and child processes. > export abc=hoge > sh >> echo $abc <-- it's puts 'hoge'
  • 13. shebang and executable-file ● shebang ○ > vim hoge.sh #!/bin/sh <-- it's shebang liine echo $1 ● executable-file ○ set the executable flag > chmod +x hoge.sh ○ run > ./hoge.sh <-- don't forget path before filename! ■ hint: see the $PATH environment variable. it's search path for a command on your system. and think with a security policy.
  • 14. shebang for any script-files ● shell script #!/bin/sh ● node.js script #!/usr/bin/node ● python script #!/usr/bin/python ● php script #!/usr/bin/php ● haskell script #!/usr/bin/runhaskell hint: > file hoge.sh
  • 15. `for` statement and an array like variable # it's like an array separated by the space char values='aaa bbb ccc' # like the foreach in Java or for-in in JS for value in $values do echo $value done
  • 16. `seq` command > seq 8 1 2 3 4 5 6 7 8
  • 17. `...` <-- it's the back-quote pair, not a single-quote pair('...') # if use the single-quote pair > echo 'seq 512 64 768' seq 512 64 768 # if use the back-quote pair > echo `seq 512 64 768` 512 576 640 704 768 <-- it's like an array string the back-quote pattern is like an eval() in JS. it's run a text as a script code. be cautious!
  • 18. `for` statement with `seq` command and ` pattern it's give a loop with a numeric sequence. for value in `seq 0 15` do echo $value done
  • 19. `while` and `until` statements ● while <-- if true then loop while true do echo '\(^o^)/' done ● until <-- if false then loop until false do echo '/(^o^)\' done
  • 20. `test` command for comparison > test 3 -eq 4 <-- equiv (3 == 4) > echo $? 1 <-- so false in shell-script > test 3 -ne 4 <-- equiv (3 != 4) > echo $? 0 <-- so true in shell-script > test 3 -gt 4 <-- equiv (3 > 4) > echo $? 1 <-- so false in shell-script
  • 21. `test` command for file check > test -f /proc/cpuinfo <-- is the file available > echo $? 0 > test -d /boot <-- is the directory available > echo $? 0
  • 22. `expr` command for calc a number from a string # it's ng, not calc > echo 1 + 2 + 4 # it's ok, calc > echo `expr 1 + 2 + 4` 7 > expr 5 + ( 5 - 5 ) * 5 / 5 % 5 5 # note: ( ) * <-- need escape-sequence.
  • 23. `until` statement with `test`, `expr` and ` pattern it's give a loop with a numeric counter. counter=15 until `test $counter -eq 0` do echo $counter counter=`expr $counter - 1` done
  • 24. `if` statement # if.sh > source if.sh f(){ >f0 if test $1 -lt 0 then zero echo 'negative' >f1 elif test $1 -gt 0 positive then echo 'positive' > f -1 else negative echo 'zero' fi }
  • 25. `case` statement # case.sh > source case.sh f(){ >f0 case $1 in 0) zerp echo 'zero';; >f5 1) two-nine echo 'one';; [2-9]) > f -1 echo 'two-nine';; ???? default) echo '????' esac }
  • 26. where are more information? all of the information is available on the man pages. see the man pages. man page is the best unified answerer for all GNU/Linux users. > man sh