linux shell programming - cs.ecu.edu€¦ · linux shell programming tutorial 3 sofe 3950u / csci...
TRANSCRIPT
![Page 1: Linux Shell Programming - cs.ecu.edu€¦ · Linux Shell Programming Tutorial 3 SOFE 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi . Linux Shells ! Shells originally](https://reader033.vdocuments.mx/reader033/viewer/2022060606/605b17aee4874c1910517fe9/html5/thumbnails/1.jpg)
Linux Shell Programming
Tutorial 3 SOFE 3950U / CSCI 3020U
Operating Systems
Instructor: Dr. Kamran Sartipi
![Page 2: Linux Shell Programming - cs.ecu.edu€¦ · Linux Shell Programming Tutorial 3 SOFE 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi . Linux Shells ! Shells originally](https://reader033.vdocuments.mx/reader033/viewer/2022060606/605b17aee4874c1910517fe9/html5/thumbnails/2.jpg)
Linux Shells
§ Shells originally came with UNIX § They are interactive environments which
let the user to access the computer resources
§ There are many shell § Bash § Tcsh § …
![Page 3: Linux Shell Programming - cs.ecu.edu€¦ · Linux Shell Programming Tutorial 3 SOFE 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi . Linux Shells ! Shells originally](https://reader033.vdocuments.mx/reader033/viewer/2022060606/605b17aee4874c1910517fe9/html5/thumbnails/3.jpg)
Programming vs. Scripting
§ Programming Langs. § C++/C , Java, …
§ Scripting Langs. § Bash, Perl, Tcl/tk, …
§ CPU instructions vs. executable files § Scripting Langs are normally interpretive
§ Slower § Easier to use and debug § Suitable for text processing, repetitive jobs, …
![Page 4: Linux Shell Programming - cs.ecu.edu€¦ · Linux Shell Programming Tutorial 3 SOFE 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi . Linux Shells ! Shells originally](https://reader033.vdocuments.mx/reader033/viewer/2022060606/605b17aee4874c1910517fe9/html5/thumbnails/4.jpg)
BASH
§ Bourne again shell (BASH) § The most widely used shell § In Linux environment you are interacting with
BASH on a daily basis § Operating system maintenance scripts are
generally bash scripts § We will use examples
![Page 5: Linux Shell Programming - cs.ecu.edu€¦ · Linux Shell Programming Tutorial 3 SOFE 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi . Linux Shells ! Shells originally](https://reader033.vdocuments.mx/reader033/viewer/2022060606/605b17aee4874c1910517fe9/html5/thumbnails/5.jpg)
“Hello world” script
§ The traditional example: 1. #!/bin/bash 2. echo Hello World
§ To execute: § chmod 755 hello.sh § ./hello.sh
§ What if we omit the first line ?
![Page 6: Linux Shell Programming - cs.ecu.edu€¦ · Linux Shell Programming Tutorial 3 SOFE 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi . Linux Shells ! Shells originally](https://reader033.vdocuments.mx/reader033/viewer/2022060606/605b17aee4874c1910517fe9/html5/thumbnails/6.jpg)
Redirection
§ There are three standard file descriptors in Linux § stdin, stdout, stderr
§ stdout and stderr are output devices, and stdin is an input device
§ We can § Redirect stdin to a file, to stderr § …
![Page 7: Linux Shell Programming - cs.ecu.edu€¦ · Linux Shell Programming Tutorial 3 SOFE 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi . Linux Shells ! Shells originally](https://reader033.vdocuments.mx/reader033/viewer/2022060606/605b17aee4874c1910517fe9/html5/thumbnails/7.jpg)
Redirection Examples
§ stdout to file § ls -l > ls-l.txt
§ stderr to file § grep da * 2> grep-errors.txt
§ stdout to stderr § grep da * 1>&2
§ stderr and stdout to file § rm -f $(find / -name core) &> /dev/null
![Page 8: Linux Shell Programming - cs.ecu.edu€¦ · Linux Shell Programming Tutorial 3 SOFE 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi . Linux Shells ! Shells originally](https://reader033.vdocuments.mx/reader033/viewer/2022060606/605b17aee4874c1910517fe9/html5/thumbnails/8.jpg)
Pipes
§ Using pipes you can feed the output of a program to another one as input
§ Example: § ls -l | grep "\.txt$" § Equal to: ls -l *.txt
![Page 9: Linux Shell Programming - cs.ecu.edu€¦ · Linux Shell Programming Tutorial 3 SOFE 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi . Linux Shells ! Shells originally](https://reader033.vdocuments.mx/reader033/viewer/2022060606/605b17aee4874c1910517fe9/html5/thumbnails/9.jpg)
Variables § Environment variables
§ There are no data types § No need to declare variables
§ Example: #!/bin/bash STR="Hello World!" echo $STR
§ Note that $ sign is used to dereference variables § What if we omit it?
![Page 10: Linux Shell Programming - cs.ecu.edu€¦ · Linux Shell Programming Tutorial 3 SOFE 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi . Linux Shells ! Shells originally](https://reader033.vdocuments.mx/reader033/viewer/2022060606/605b17aee4874c1910517fe9/html5/thumbnails/10.jpg)
Local Variables #!/bin/bash HELLO=Hello function hello {
local HELLO=World echo $HELLO } echo $HELLO hello echo $HELLO
§ Similar to internal function variables in C
![Page 11: Linux Shell Programming - cs.ecu.edu€¦ · Linux Shell Programming Tutorial 3 SOFE 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi . Linux Shells ! Shells originally](https://reader033.vdocuments.mx/reader033/viewer/2022060606/605b17aee4874c1910517fe9/html5/thumbnails/11.jpg)
Conditional Sentences #!/bin/bash T1="foo" T2="bar" if [ "$T1" = "$T2" ]; then
echo expression evaluated as true else
echo expression evaluated as false fi
![Page 12: Linux Shell Programming - cs.ecu.edu€¦ · Linux Shell Programming Tutorial 3 SOFE 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi . Linux Shells ! Shells originally](https://reader033.vdocuments.mx/reader033/viewer/2022060606/605b17aee4874c1910517fe9/html5/thumbnails/12.jpg)
Loops: for § #!/bin/bash
for i in $( ls ); do echo item:
$i done
§ #!/bin/bash for i in `seq 1 10`; do echo $i done
§ What is the meaning of ‘seq 1 10’ ?
![Page 13: Linux Shell Programming - cs.ecu.edu€¦ · Linux Shell Programming Tutorial 3 SOFE 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi . Linux Shells ! Shells originally](https://reader033.vdocuments.mx/reader033/viewer/2022060606/605b17aee4874c1910517fe9/html5/thumbnails/13.jpg)
Functions 1. #!/bin/bash 2. function quit { 3. exit } 4. function e { 5. echo $1 } 6. e Hello 7. e World 8. quit 9. echo foo
![Page 14: Linux Shell Programming - cs.ecu.edu€¦ · Linux Shell Programming Tutorial 3 SOFE 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi . Linux Shells ! Shells originally](https://reader033.vdocuments.mx/reader033/viewer/2022060606/605b17aee4874c1910517fe9/html5/thumbnails/14.jpg)
Input arguments #!/bin/bash
if [ -z "$1" ]; then echo usage: $0 directory exit fi ls –la $1 § If [ –z X] is used to check if X is has a length of zero
§ Can be used to check if something is defined
![Page 15: Linux Shell Programming - cs.ecu.edu€¦ · Linux Shell Programming Tutorial 3 SOFE 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi . Linux Shells ! Shells originally](https://reader033.vdocuments.mx/reader033/viewer/2022060606/605b17aee4874c1910517fe9/html5/thumbnails/15.jpg)
Reading Input #!/bin/bash echo Please, enter your firstname and lastname read FN LN echo "Hi! $LN, $FN !"
![Page 16: Linux Shell Programming - cs.ecu.edu€¦ · Linux Shell Programming Tutorial 3 SOFE 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi . Linux Shells ! Shells originally](https://reader033.vdocuments.mx/reader033/viewer/2022060606/605b17aee4874c1910517fe9/html5/thumbnails/16.jpg)
Return Value
#!/bin/bash cd /dada &> /dev/null echo rv: $? cd $(pwd) &> /dev/null echo rv: $?
§ A program return value is stored in $? § Remember in C:
return 0;
![Page 17: Linux Shell Programming - cs.ecu.edu€¦ · Linux Shell Programming Tutorial 3 SOFE 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi . Linux Shells ! Shells originally](https://reader033.vdocuments.mx/reader033/viewer/2022060606/605b17aee4874c1910517fe9/html5/thumbnails/17.jpg)
Reference
§ This tutorial is based on: “BASH Programming - Introduction HOW-TO”
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html#toc10