Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 311 - Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.

Similar presentations


Presentation on theme: "CS 311 - Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03."— Presentation transcript:

1 CS 311 - Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03

2 What is shell? Command interpreter Lies between user and operating system Text-based Provides access to all UNIX user-level programs – Start programs – Manage running programs (job control) – Connect together programs (pipes) – Manage I/O to and from programs (redirection) 2CS 311 Operating SystemsLecture 03

3 What is shell script? Collection of shell commands or programs High level programming language – Variables, conditionals, loops, etc. Dynamic – No compiling, no variable declaration, no memory management Powerful/efficient – Do lots with little code – Can interface with any UNIX program String oriented 3CS 311 Operating SystemsLecture 03

4 When to use and not to use shell scripts? When To Use – Automating frequent UNIX tasks – Doing complex commands – As a glue to connect together other programs When not to use – Resource-intensive tasks, especially where speed is a factor (sorting, hashing, recursion) – Cross-platform portability required (use C or Java instead) – Complex applications, where structured programming is a necessity (type-checking of variables, function prototypes, etc.) 4CS 311 Operating SystemsLecture 03

5 Sha-bang or the hash-bang character Shell script may have an extension “.sh” To invoke a shell script prompt> sh script_name.sh Or we can let the command line interpret the script using a sha-bang or a hash-bang character (“#!”) – Add “ #!/bin/sh ” as the first line – chmod +x script_name.sh –./find_large.sh In scripts, comments are written using “#” Example: “#This is a comment” 5CS 311 Operating SystemsLecture 03

6 Command line arguments to shell program Command line parameters – Example: “cut –d’:’ –f1 sample” – cut, –d: and –f1 are passed as arguments to the script – In shell script, args are accessible as $1, $2, $3, etc. $0 will be the shell command invoked (‘cut’ in the example above) $# is the number of parameters $* contain all parameters 6CS 311 Operating SystemsLecture 03

7 Environment variables Built-in variables – $HOME – full path of home directory – $PATH – List of directories to search – $MAIL – full path of mailbox – $USER – username – $SHELL – full path of the login shell – $TERM – type of your terminal – $PWD – print current working directory Supports all variables shown by the “env” command 7CS 311 Operating SystemsLecture 03

8 Wildcards Characters used anywhere (esp in regular expressions) – * - matches zero or more characters – ? – matches exactly one character – [ ] – defines a range of characters or list of characters Example – “ls file*” would print all files with filename “file” and “file…” – “ls ????” matches all files with 4 letters – “ls file[0-9]” matches filename with “file093” or “file49332412098” etc. 8CS 311 Operating SystemsLecture 03

9 Using variables in shell Variables are any string that hold some value In bash scripts, variables need not be declared before using it. By default, bash treats all variable values as strings Example – value=123 (beware of spacing. No whitespaces before or after equal sign) To access the value of variable use “$” sign. – Example: “echo $value” prints 123 and “echo value” prints value 9CS 311 Operating SystemsLecture 03

10 Exit status Terminate a shell program using “exit” command Typically the program exits with a numbered status indicating success or failure – In UNIX, “exit 0” means success and “exit some_number” means failure Exit status useful in determining whether the command executed properly. $PIPESTATUS or $? shows the status of last executed command/process Lecture 03CS 311 Operating Systems10

11 Quoting Very important to understand each style of quotes and their functions. Single quote (‘ ‘): inhibit wildcard substitution, variable and command replacement. – Ex. echo '$name* is true' will print $name* is true Double quotes (“ “): inhibit wildcard replacement only – Ex. echo “$name* is true” will print John* is true if name=John Tilde quotes (` `): used for command substitution – Ex. echo `date` will print the system date and time 11CS 311 Operating SystemsLecture 03

12 Arithmetic operators Since all variables and its values are considered as string by default, we need to inform the shell interpreter we must use the values as numbers when doing arithmetic operations (using expr) echo “5 + 5” will print 5 + 5 echo `expr 5 + 5` will print 10 (notice the use of tilde quotes) Other arithmetic operators – expr $value1 - $value2 – expr $value1 * $value2 – expr $value1 / $value2 Lecture 03CS 311 Operating Systems12

13 Conditional Statements General Structure if [ condition ]#I have put multiple spaces just to then#emphasize the spacing … elif [ condition ] then … else … fi Make note of spacing between if and [, [ and condition, condition and ] 13CS 311 Operating SystemsLecture 03

14 Specify conditions in bash For numeric comparisons – “-lt” – less than – “-gt” – greater than – “-le” – less than or equal to – “-ge” – greater than or equal to – “-eq” – equal to Example: if [ $value -lt 5 ]#$value must be a number then echo “true” fi Lecture 03CS 311 Operating Systems14

15 Conditions in bash Relational operators – “-a” – AND – “-o” – OR – “-n” – NOT Example: if [ $value –gt 5 –a $value –lt 10 ] We can also use extended conditions with “if” by using “if [[ condition ]]” – By using extended conditions we can use our well known operators like, ==, &&, || String comparisons – Example: if [ “hello” = “$value” ] – Check whether string is null – if [ -z $string ] Lecture 03CS 311 Operating Systems15

16 Testing files Separate operators exists to test files in bash – “-e” file exists – “-s” file exists and not empty – “-f” regular file – Check this link: http://tldp.org/LDP/abs/html/fto.html Lecture 03CS 311 Operating Systems16

17 Looping While loop while [ condition ] do … done Lecture 03CS 311 Operating Systems17 for loop for arg in list do … done Check this link for more options on how to use the “list” in “for” - http://tldp.org/LDP/abs/html/loops1.htmlhttp://tldp.org/LDP/abs/html/loops1.html Example: for file in `ls.` will list through all files in the current directory (a hint for the assignment)

18 Debugging a shell script Use the command “set” within the script like shown below set –xv …#block of statements to test set – set –x - Print commands and their arguments as they are executed. Also substitutes variable values and evaluates wildcards. set –v - Print shell input lines as they are read. set – simply turns all the set flags off More info on set - http://www.computerhope.com/unix/uset.htm Lecture 03CS 311 Operating Systems18

19 Getting user input “read” command is used to get user input Example: echo “Enter a number” read number#value gets stored in number Using read to read lines from file while read line < file do … done Will read the file line by line till the last line Lecture 03CS 311 Operating Systems19


Download ppt "CS 311 - Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03."

Similar presentations


Ads by Google