170 likes | 397 Views
Linux Shell Programming Tutorial 3. Amin Farbod. 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 …. Programming vs. Scripting. Programming Langs. C++/C , Java, …
E N D
Linux Shell ProgrammingTutorial 3 Amin Farbod CS3SH3: Operating Systems Concepts, Winter 2005
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 • … CS3SH3: Operating Systems Concepts, Winter 2005
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, … CS3SH3: Operating Systems Concepts, Winter 2005
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 CS3SH3: Operating Systems Concepts, Winter 2005
“Hello world” script • The traditional example: • #!/bin/bash • echo Hello World • To execute: • chmod 755 hello.sh • ./hello.sh • What if we omit the first line ? CS3SH3: Operating Systems Concepts, Winter 2005
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 • … CS3SH3: Operating Systems Concepts, Winter 2005
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 CS3SH3: Operating Systems Concepts, Winter 2005
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 CS3SH3: Operating Systems Concepts, Winter 2005
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? CS3SH3: Operating Systems Concepts, Winter 2005
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 CS3SH3: Operating Systems Concepts, Winter 2005
Conditional Sentences #!/bin/bash T1="foo" T2="bar" if [ "$T1" = "$T2" ]; then echo expression evaluated as true else echo expression evaluated as false fi CS3SH3: Operating Systems Concepts, Winter 2005
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’ ? CS3SH3: Operating Systems Concepts, Winter 2005
Functions • #!/bin/bash • function quit { • exit } • function e { • echo $1 } • e Hello • e World • quit • echo foo CS3SH3: Operating Systems Concepts, Winter 2005
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 CS3SH3: Operating Systems Concepts, Winter 2005
Reading Input #!/bin/bash echo Please, enter your firstname and lastname read FN LN echo "Hi! $LN, $FN !" CS3SH3: Operating Systems Concepts, Winter 2005
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; CS3SH3: Operating Systems Concepts, Winter 2005
Reference • This tutorial is based on: “BASH Programming - Introduction HOW-TO” http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html#toc10 CS3SH3: Operating Systems Concepts, Winter 2005