Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 311 - Lecture 04 Outline Change your shell Sed and awk Tricks with Bash scripts Assignment 1 discussion 1CS 311 Operating SystemsLecture 04.

Similar presentations


Presentation on theme: "CS 311 - Lecture 04 Outline Change your shell Sed and awk Tricks with Bash scripts Assignment 1 discussion 1CS 311 Operating SystemsLecture 04."— Presentation transcript:

1 CS 311 - Lecture 04 Outline Change your shell Sed and awk Tricks with Bash scripts Assignment 1 discussion 1CS 311 Operating SystemsLecture 04

2 Change your shell Go to engr.oregonstate.edu Login to TEACH Under “Account Tools”, make sure you change your UNIX shell to “/bin/sh” or “/bin/bash” 2CS 311 Operating SystemsLecture 04

3 Useful tricks with Bash Bash scripts are very flexible You have to think out-of-the-context of popular programming languages like C, Java, C#, etc. Lecture 04CS 311 Operating Systems3

4 Using Variables Displaying variables – Example: column=2 cut –d”:” –f$column file #value of column gets substituted in this line and hence cut retrieves the second column in the file. echo hello${column}world Storing the output of command in a variable – Example: totallines=`wc –l file`#Note the use of tilde quotes If a variable has multiple values in it, the values are separated by space. (Ex: files=`ls –l`) If you want to retain the output format of the commands use temporary files Lecture 04CS 311 Operating Systems4

5 Using variables Many commands in UNIX take a file as input. Suppose you have a string in a variable line=“John5080” and you have to cut the second column from the variable cut –f2 $line#won’t work echo $line | cut –f2#works perfectly You can use variables anywhere (literally anywhere) you want, just remember to use $ sign if you need to access the value Lecture 04CS 311 Operating Systems5

6 Searching To search a string in a file we use grep Suppose we have a text file as below NameMarksTotal A5050 B7575 If you have to do a case insensitive search for “A”, you give - grep –i “a” file This will also retrieve the line “Name Marks Total” since the line also has “a”. To search for the word “A” use the word boundaries (“\b \b”) grep –i “\ba\b” file#prints only the word “a” Lecture 04CS 311 Operating Systems6

7 Assignment 1 Tips Try to break the code into smaller steps instead of programming everything at once. If you are not sure what a command does, execute in a terminal and then use it in your code. Implement –f, -s and the default switch first as they are easier compared to others. First do the conditional branching right, then you can insert the code into the appropriate section. To make your code look better have the code for each option in a separate shell script file. Then execute the appropriate shell script file based on the input. Lecture 04CS 311 Operating Systems7


Download ppt "CS 311 - Lecture 04 Outline Change your shell Sed and awk Tricks with Bash scripts Assignment 1 discussion 1CS 311 Operating SystemsLecture 04."

Similar presentations


Ads by Google