SlideShare a Scribd company logo
1 of 12
www.techvilla.org.in
TECHVILLA
www.techvilla.org.in
www.techvilla.org.in
Working with the Shell
 What is shell?
 Different shells in linux.
 Programming in shell.
 Shell vs windows cmd
www.techvilla.org.in
What’s Shell?
 It’s acts an interface between the user and OS (kernel).It’s known as “ command
interpreter”.
 For Linux, the Bash is the default
 Why Shell?
 For routing jobs, such as system administration, without writing
programs
 However, the shell script is not efficient, therefore, can be used
for prototyping the ideas
 For example,
% ls –al | more (better format of listing directory)
% man bash | col –b | lpr (print man page of man)
www.techvilla.org.in
Shell basics
 Shell is the interface between end user and the Linux system, similar
to the commands in Windows
 Bash is installed as in /bin/sh
 Check the version
 % /bin/sh --version
Kernel
Other
programs
X window
bash
csh
www.techvilla.org.in
Pipe and Redirection
 Redirection (< or >)
% ls –l > lsoutput.txt (save output to lsoutput.txt)
% ps >> lsoutput.txt (append to lsoutput.txt)
% more < killout.txt (use killout.txt as parameter to more)
% kill -l 1234 > killouterr.txt 2 >&1 (redirect to the same
file)
% kill -l 1234 >/dev/null 2 >&1 (ignore std output)
 Pipe (|)
 Process are executed concurrently
% ps | sort | more
% ps –xo comm | sort | uniq | grep –v sh | more
% cat mydata.txt | sort | uniq | > mydata.txt (generates an
empty file !)
www.techvilla.org.in
Shell as a Language
 We can write a script containing many shell commands
 Interactive Program:
 grep files with POSIX string and print it
% for file in *
> do
> if grep –l POSIX $file
> then
> more $file
 fi
 done
Posix
There is a file with POSIX in it
 ‘*’ is wildcard
% more `grep –l POSIX *`
% more $(grep –l POSIX *)
% more –l POSIX * | more
www.techvilla.org.in
Writing a Script
 Use text editor to generate the “first” file
#!/bin/sh
# first
# this file looks for the files containing POSIX
# and print it
for file in *
do
if grep –q POSIX $file
then
echo $file
fi
done
exit 0
% /bin/sh first
% chmod +x first
%./first (make sure . is include in PATH parameter)
www.techvilla.org.in
Syntax
 Variables
 Conditions
 Control
 Lists
 Functions
 Shell Commands
 Result
 Document
www.techvilla.org.in
Variables
 Variables needed to be declared, note it is case-sensitive (e.g. foo, FOO, Foo)
 Add ‘$’ for storing values
% salutation=Hello
% echo $salutation
Hello
% salutation=7+5
% echo $salutation
7+5
% salutation=“yes dear”
% echo $salutation
yes dear
% read salutation
Hola!
% echo $salutation
Hola!
www.techvilla.org.in
Quoting
 Edit a “vartest.sh” file
#!/bin/sh
myvar=“Hi there”
echo $myvar
echo “$myvar”
echo `$myvar`
echo $myvar
echo Enter some text
read myvar
echo ‘$myvar’ now equals $myvar
exit 0
Output
Hi there
Hi there
$myvar
$myvar
Enter some text
Hello world
$myvar now equals Hello world
www.techvilla.org.in
Environment Variables
 $HOME home directory
 $PATH path
 $PS1 第一層提示符號 (normally %)
 $PS2 第二層提示符號 (normally >)
 $$ process id of the script
 $# number of input parameters
 $0 name of the script file
 $IFS separation character (white space)
 Use ‘env’ to check the value
www.techvilla.org.in
Shell vs windows cmd
 The difference of Windows and Linux
 Case sensitive in Linux
 / in Linux
 No relationship between executable file and extension
 don't search current path
 Security policy is more strict in Linux
 link file VS shortcut

More Related Content

What's hot

Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
Sudharsan S
 
BASH Guide Summary
BASH Guide SummaryBASH Guide Summary
BASH Guide Summary
Ohgyun Ahn
 
Shell programming 1.ppt
Shell programming  1.pptShell programming  1.ppt
Shell programming 1.ppt
Kalkey
 
Bash shell
Bash shellBash shell
Bash shell
xylas121
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
Dr.Ravi
 

What's hot (20)

OpenGurukul : Language : Shell Scripting
OpenGurukul : Language : Shell ScriptingOpenGurukul : Language : Shell Scripting
OpenGurukul : Language : Shell Scripting
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
 
BASH Guide Summary
BASH Guide SummaryBASH Guide Summary
BASH Guide Summary
 
Chap06
Chap06Chap06
Chap06
 
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...
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Shell programming 1.ppt
Shell programming  1.pptShell programming  1.ppt
Shell programming 1.ppt
 
Unix And Shell Scripting
Unix And Shell ScriptingUnix And Shell Scripting
Unix And Shell Scripting
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Unix shell scripting
Unix shell scriptingUnix shell scripting
Unix shell scripting
 
Bash shell
Bash shellBash shell
Bash shell
 
Basics of shell programming
Basics of shell programmingBasics of shell programming
Basics of shell programming
 
Unix - Shell Scripts
Unix - Shell ScriptsUnix - Shell Scripts
Unix - Shell Scripts
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
system management -shell programming by gaurav raikar
system management -shell programming by gaurav raikarsystem management -shell programming by gaurav raikar
system management -shell programming by gaurav raikar
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
 
Shell & Shell Script
Shell & Shell Script Shell & Shell Script
Shell & Shell Script
 
Scripting 101
Scripting 101Scripting 101
Scripting 101
 
Shellscripting
ShellscriptingShellscripting
Shellscripting
 

Viewers also liked

Sistema de redes de seguridad CSS
Sistema de redes de seguridad CSSSistema de redes de seguridad CSS
Sistema de redes de seguridad CSS
Michael Castillo
 
Assignment 35 final
Assignment 35 final Assignment 35 final
Assignment 35 final
benchaisty
 
Daftar nama peserta uas paai 2013
Daftar nama peserta uas paai 2013Daftar nama peserta uas paai 2013
Daftar nama peserta uas paai 2013
Vendi Supendi
 

Viewers also liked (20)

Self determined learning: Creating personal learning environments for lifelon...
Self determined learning: Creating personal learning environments for lifelon...Self determined learning: Creating personal learning environments for lifelon...
Self determined learning: Creating personal learning environments for lifelon...
 
Barista sponsorship
Barista sponsorshipBarista sponsorship
Barista sponsorship
 
CARTILHA CNJ.PDF
CARTILHA  CNJ.PDFCARTILHA  CNJ.PDF
CARTILHA CNJ.PDF
 
Tarjeta
TarjetaTarjeta
Tarjeta
 
Volunteer Consultants for Women's Building in San Francisco
Volunteer Consultants for Women's Building in San FranciscoVolunteer Consultants for Women's Building in San Francisco
Volunteer Consultants for Women's Building in San Francisco
 
Sistema de redes de seguridad CSS
Sistema de redes de seguridad CSSSistema de redes de seguridad CSS
Sistema de redes de seguridad CSS
 
Apresentação
ApresentaçãoApresentação
Apresentação
 
Un bello corazón
Un bello corazónUn bello corazón
Un bello corazón
 
Fotos
FotosFotos
Fotos
 
Familia
FamiliaFamilia
Familia
 
Reptiles de san san 2010
Reptiles de san san 2010 Reptiles de san san 2010
Reptiles de san san 2010
 
รายชื่อผู้มีสิทธิ์สอบสัมภาษณ์ สกอ.
รายชื่อผู้มีสิทธิ์สอบสัมภาษณ์ สกอ.รายชื่อผู้มีสิทธิ์สอบสัมภาษณ์ สกอ.
รายชื่อผู้มีสิทธิ์สอบสัมภาษณ์ สกอ.
 
Quest Radio Station
Quest Radio StationQuest Radio Station
Quest Radio Station
 
Californication tv show
Californication tv showCalifornication tv show
Californication tv show
 
Expectativas e Ilusões
Expectativas e IlusõesExpectativas e Ilusões
Expectativas e Ilusões
 
NatalyaResume2015
NatalyaResume2015NatalyaResume2015
NatalyaResume2015
 
Assignment 35 final
Assignment 35 final Assignment 35 final
Assignment 35 final
 
OER Roadmap (OE Global Pre-conference workshop)
OER Roadmap (OE Global Pre-conference workshop)OER Roadmap (OE Global Pre-conference workshop)
OER Roadmap (OE Global Pre-conference workshop)
 
Daftar nama peserta uas paai 2013
Daftar nama peserta uas paai 2013Daftar nama peserta uas paai 2013
Daftar nama peserta uas paai 2013
 
Domain game
Domain gameDomain game
Domain game
 

Similar to Raspberry pi Part 4

Best training-in-mumbai-shell scripting
Best training-in-mumbai-shell scriptingBest training-in-mumbai-shell scripting
Best training-in-mumbai-shell scripting
vibrantuser
 
Bash shell programming in linux
Bash shell programming in linuxBash shell programming in linux
Bash shell programming in linux
Norberto Angulo
 
2-introduction_to_shell_scripting
2-introduction_to_shell_scripting2-introduction_to_shell_scripting
2-introduction_to_shell_scripting
erbipulkumar
 
Using Unix
Using UnixUsing Unix
Using Unix
Dr.Ravi
 

Similar to Raspberry pi Part 4 (20)

Raspberry pi Part 25
Raspberry pi Part 25Raspberry pi Part 25
Raspberry pi Part 25
 
Best training-in-mumbai-shell scripting
Best training-in-mumbai-shell scriptingBest training-in-mumbai-shell scripting
Best training-in-mumbai-shell scripting
 
Python for Linux System Administration
Python for Linux System AdministrationPython for Linux System Administration
Python for Linux System Administration
 
Bash shell programming in linux
Bash shell programming in linuxBash shell programming in linux
Bash shell programming in linux
 
2-introduction_to_shell_scripting
2-introduction_to_shell_scripting2-introduction_to_shell_scripting
2-introduction_to_shell_scripting
 
60761 linux
60761 linux60761 linux
60761 linux
 
UnixShells.ppt
UnixShells.pptUnixShells.ppt
UnixShells.ppt
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
Best training-in-mumbai-shell scripting
Best training-in-mumbai-shell scriptingBest training-in-mumbai-shell scripting
Best training-in-mumbai-shell scripting
 
Unixshellscript 100406085942-phpapp02
Unixshellscript 100406085942-phpapp02Unixshellscript 100406085942-phpapp02
Unixshellscript 100406085942-phpapp02
 
Complete Guide for Linux shell programming
Complete Guide for Linux shell programmingComplete Guide for Linux shell programming
Complete Guide for Linux shell programming
 
Shell_Scripting.ppt
Shell_Scripting.pptShell_Scripting.ppt
Shell_Scripting.ppt
 
Unix tutorial-08
Unix tutorial-08Unix tutorial-08
Unix tutorial-08
 
Linux Basics
Linux BasicsLinux Basics
Linux Basics
 
Linux command for beginners
Linux command for beginnersLinux command for beginners
Linux command for beginners
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
Using Unix
Using UnixUsing Unix
Using Unix
 
2023comp90024_linux.pdf
2023comp90024_linux.pdf2023comp90024_linux.pdf
2023comp90024_linux.pdf
 
Shell scripting - By Vu Duy Tu from eXo Platform SEA
Shell scripting - By Vu Duy Tu from eXo Platform SEAShell scripting - By Vu Duy Tu from eXo Platform SEA
Shell scripting - By Vu Duy Tu from eXo Platform SEA
 
Unix tutorial-08
Unix tutorial-08Unix tutorial-08
Unix tutorial-08
 

More from Techvilla

Raspberry pi Part 3
Raspberry pi Part 3Raspberry pi Part 3
Raspberry pi Part 3
Techvilla
 

More from Techvilla (20)

Raspberry pi Part 26
Raspberry pi Part 26Raspberry pi Part 26
Raspberry pi Part 26
 
Raspberry pi Part 24
Raspberry pi Part 24Raspberry pi Part 24
Raspberry pi Part 24
 
Raspberry pi Part 23
Raspberry pi Part 23Raspberry pi Part 23
Raspberry pi Part 23
 
Raspberry pi Part 22
Raspberry pi Part 22Raspberry pi Part 22
Raspberry pi Part 22
 
Raspberry pi Part 21
Raspberry pi Part 21Raspberry pi Part 21
Raspberry pi Part 21
 
Raspberry pi Part 20
Raspberry pi Part 20Raspberry pi Part 20
Raspberry pi Part 20
 
Raspberry pi Part 19
Raspberry pi Part 19Raspberry pi Part 19
Raspberry pi Part 19
 
Raspberry pi Part 18
Raspberry pi Part 18Raspberry pi Part 18
Raspberry pi Part 18
 
Raspberry pi Part 17
Raspberry pi Part 17Raspberry pi Part 17
Raspberry pi Part 17
 
Raspberry pi Part 16
Raspberry pi Part 16Raspberry pi Part 16
Raspberry pi Part 16
 
Rasperry pi Part 15
Rasperry pi Part 15Rasperry pi Part 15
Rasperry pi Part 15
 
Rasperry pi Part 13
Rasperry pi Part 13Rasperry pi Part 13
Rasperry pi Part 13
 
Rasperry pi Part 12
Rasperry pi Part 12Rasperry pi Part 12
Rasperry pi Part 12
 
Rasperry pi Part 10
Rasperry pi Part 10Rasperry pi Part 10
Rasperry pi Part 10
 
Rasperry pi Part 9
Rasperry pi Part 9Rasperry pi Part 9
Rasperry pi Part 9
 
Rasperry pi Part 8
Rasperry pi Part 8Rasperry pi Part 8
Rasperry pi Part 8
 
Rasperry pi Part 7
Rasperry pi Part 7Rasperry pi Part 7
Rasperry pi Part 7
 
Raspberry pi Part 6
Raspberry pi Part 6Raspberry pi Part 6
Raspberry pi Part 6
 
Raspberry pi Part 5
Raspberry pi Part 5Raspberry pi Part 5
Raspberry pi Part 5
 
Raspberry pi Part 3
Raspberry pi Part 3Raspberry pi Part 3
Raspberry pi Part 3
 

Recently uploaded

Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
dharasingh5698
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 

Recently uploaded (20)

Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 

Raspberry pi Part 4

  • 2. www.techvilla.org.in Working with the Shell  What is shell?  Different shells in linux.  Programming in shell.  Shell vs windows cmd
  • 3. www.techvilla.org.in What’s Shell?  It’s acts an interface between the user and OS (kernel).It’s known as “ command interpreter”.  For Linux, the Bash is the default  Why Shell?  For routing jobs, such as system administration, without writing programs  However, the shell script is not efficient, therefore, can be used for prototyping the ideas  For example, % ls –al | more (better format of listing directory) % man bash | col –b | lpr (print man page of man)
  • 4. www.techvilla.org.in Shell basics  Shell is the interface between end user and the Linux system, similar to the commands in Windows  Bash is installed as in /bin/sh  Check the version  % /bin/sh --version Kernel Other programs X window bash csh
  • 5. www.techvilla.org.in Pipe and Redirection  Redirection (< or >) % ls –l > lsoutput.txt (save output to lsoutput.txt) % ps >> lsoutput.txt (append to lsoutput.txt) % more < killout.txt (use killout.txt as parameter to more) % kill -l 1234 > killouterr.txt 2 >&1 (redirect to the same file) % kill -l 1234 >/dev/null 2 >&1 (ignore std output)  Pipe (|)  Process are executed concurrently % ps | sort | more % ps –xo comm | sort | uniq | grep –v sh | more % cat mydata.txt | sort | uniq | > mydata.txt (generates an empty file !)
  • 6. www.techvilla.org.in Shell as a Language  We can write a script containing many shell commands  Interactive Program:  grep files with POSIX string and print it % for file in * > do > if grep –l POSIX $file > then > more $file  fi  done Posix There is a file with POSIX in it  ‘*’ is wildcard % more `grep –l POSIX *` % more $(grep –l POSIX *) % more –l POSIX * | more
  • 7. www.techvilla.org.in Writing a Script  Use text editor to generate the “first” file #!/bin/sh # first # this file looks for the files containing POSIX # and print it for file in * do if grep –q POSIX $file then echo $file fi done exit 0 % /bin/sh first % chmod +x first %./first (make sure . is include in PATH parameter)
  • 8. www.techvilla.org.in Syntax  Variables  Conditions  Control  Lists  Functions  Shell Commands  Result  Document
  • 9. www.techvilla.org.in Variables  Variables needed to be declared, note it is case-sensitive (e.g. foo, FOO, Foo)  Add ‘$’ for storing values % salutation=Hello % echo $salutation Hello % salutation=7+5 % echo $salutation 7+5 % salutation=“yes dear” % echo $salutation yes dear % read salutation Hola! % echo $salutation Hola!
  • 10. www.techvilla.org.in Quoting  Edit a “vartest.sh” file #!/bin/sh myvar=“Hi there” echo $myvar echo “$myvar” echo `$myvar` echo $myvar echo Enter some text read myvar echo ‘$myvar’ now equals $myvar exit 0 Output Hi there Hi there $myvar $myvar Enter some text Hello world $myvar now equals Hello world
  • 11. www.techvilla.org.in Environment Variables  $HOME home directory  $PATH path  $PS1 第一層提示符號 (normally %)  $PS2 第二層提示符號 (normally >)  $$ process id of the script  $# number of input parameters  $0 name of the script file  $IFS separation character (white space)  Use ‘env’ to check the value
  • 12. www.techvilla.org.in Shell vs windows cmd  The difference of Windows and Linux  Case sensitive in Linux  / in Linux  No relationship between executable file and extension  don't search current path  Security policy is more strict in Linux  link file VS shortcut