Download presentation
Presentation is loading. Please wait.
Published byDarlene Bell Modified over 9 years ago
1
Linux Shell Programming Tutorial 3 ENGR 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi
2
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 …
3
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, …
4
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
5
“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 ?
6
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 …
7
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
8
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
9
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?
10
Local Variables #!/bin/bashHELLO=Hello function hello { local HELLO=World echo $HELLO } hello Similar to internal function variables in C
11
Conditional Sentences #!/bin/bashT1="foo"T2="bar" if [ "$T1" = "$T2" ]; then echo expression evaluated as true else echo expression evaluated as false fi
12
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’ ?
13
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
14
Input arguments #!/bin/bash #!/bin/bash if [ -z "$1" ]; then if [ -z "$1" ]; then echo usage: $0 directory echo usage: $0 directory exit exit fi fi ls –la $1 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
15
Reading Input #!/bin/bash echo Please, enter your firstname and lastname read FN LN echo "Hi! $LN, $FN !"
16
Return Value #!/bin/bash #!/bin/bash cd /dada &> /dev/null cd /dada &> /dev/null echo rv: $? echo rv: $? cd $(pwd) &> /dev/null cd $(pwd) &> /dev/null echo rv: $? echo rv: $? A program return value is stored in $? Remember in C: return 0;
17
Reference This tutorial is based on: “BASH Programming - Introduction HOW-TO” http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html#toc10
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.