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

Slides:



Advertisements
Similar presentations
CST8177 awk. The awk program is not named after the sea-bird (that's auk), nor is it a cry from a parrot (awwwk!). It's the initials of the authors, Aho,
Advertisements

1 Unix Talk #2 AWK overview Patterns and actions Records and fields Print vs. printf.
CS 497C – Introduction to UNIX Lecture 32: - Shell Programming Chin-Chih Chang
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
Introduction to Unix – CS 21 Lecture 13. Lecture Overview Finding files and programs which whereis find xargs Putting it all together for some complex.
Guide To UNIX Using Linux Third Edition
Guide to Linux Installation and Administration, 2e1 Chapter 6 Using the Shell and Text Files.
Lecture 02CS311 – Operating Systems 1 1 CS311 – Lecture 02 Outline UNIX/Linux features – Redirection – pipes – Terminating a command – Running program.
Grep, comm, and uniq. The grep Command The grep command allows a user to search for specific text inside a file. The grep command will find all occurrences.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Linux & Shell Scripting Small Group Lecture 4 How to Learn to Code Workshop group/ Erin.
Shell Script Examples.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Advanced File Processing
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
Chapter 2 Basic SQL SELECT Statements Oracle 10g: SQL.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
LIN 6932 Unix Lecture 6 Hana Filip. LIN 6932 HW6 - Part II solutions posted on my website see syllabus.
An Introduction to Unix Shell Scripting
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
GNU Compiler Collection (GCC) and GNU C compiler (gcc) tools used to compile programs in Linux.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
EMT 2390L Lecture 8 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts.
Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command to search for.
Essential Shell Programming by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Chapter Five Advanced File Processing Guide To UNIX Using Linux Fourth Edition Chapter 5 Unix (34 slides)1 CTEC 110.
Chapter Five Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command.
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
CSC 352– Unix Programming, Spring 2015 April 28 A few final commands.
Introduction to Unix – CS 21 Lecture 12. Lecture Overview A few more bash programming tricks The here document Trapping signals in bash cut and tr sed.
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
Searching and Sorting. Why Use Data Files? There are many cases where the input to the program may come from a data file.Using data files in your programs.
Writing Scripts Hadi Otrok COEN 346.
Chapter Six Introduction to Shell Script Programming.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 6 – sed, command-line tools wrapup.
Lecture 02 File and File system. Topics Describe the layout of a Linux file system Display and set paths Describe the most important files, including.
Linux+ Guide to Linux Certification, Second Edition
CSC 352– Unix Programming, Fall 2011 November 8, 2011, Week 11, a useful subset of regular expressions, grep and sed, parts of Chapter 11.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Experiment No. 13 Presented by, Mr. Satish Pise. Write a shell script which checks disk space and store the value to the variable and display it. #!/bin/sh.
Filters and Utilities. Notes: This is a simple overview of the filtering capability Some of these commands are very powerful ▫Only showing some of the.
Tutorial of Unix Command & shell scriptS 5027
Lesson 5-Exploring Utilities
CSE 374 Programming Concepts & Tools
Lecture 7 Introduction to Shell Programming
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Shell Script Assignment 1.
PROGRAMMING THE BASH SHELL PART IV by İlker Korkmaz and Kaya Oğuz
Writing Shell Scripts ─ part 3
Writing Shell Scripts ─ part 3
Tutorial of Unix Command & shell scriptS 5027
CSC 352– Unix Programming, Fall 2012
Tutorial of Unix Command & shell scriptS 5027
Intro to PHP & Variables
Agenda Control Flow Statements Purpose test statement
Guide To UNIX Using Linux Third Edition
CSC 352– Unix Programming, Spring 2016
Tutorial of Unix Command & shell scriptS 5027
Lab 5: Complex Inputs.
Introduction to Bash Programming, part 3
Essential Shell Programming
Presentation transcript:

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

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

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

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

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

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

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