Csci 132 Spring 13. Assignment 2 Due: Tuesday, March 5 (by 11:59PM)

Similar documents
Shell programming. Introduction to Operating Systems

UNIX shell scripting

Scripting. More Shell Scripts. Adapted from Practical Unix and Programming Hunter College

Assignment 3, Due October 4

CS Unix Tools & Scripting

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

Lecture 4. Log into Linux Reminder: Homework 1 due today, 4:30pm Homework 2 out, due next Tuesday Project 1 out, due next Thursday Questions?

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

Assignment 2. Summary. Some Important bash Instructions. CSci132 Practical UNIX and Programming Assignment 2, Fall Prof.

Shell Script Programs with If Then Else Statements

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Bash shell programming Part II Control statements

Conditional Control Structures. Dr.T.Logeswari

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Bourne Shell Reference

Bash scripting basics

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

28-Nov CSCI 2132 Software Development Lecture 33: Shell Scripting. 26 Shell Scripting. Faculty of Computer Science, Dalhousie University

9.2 Linux Essentials Exam Objectives

example: name1=jan name2=mike export name1 In this example, name1 is an environmental variable while name2 is a local variable.

Lab 5a Shell Script Lab 4 Using Arithmetic Operators in shell script

A shell can be used in one of two ways:

Practice 5 Batch & Shell Programming

Title:[ Variables Comparison Operators If Else Statements ]

Lab 4: Bash Scripting

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Computer Systems and Architecture

COMP 4/6262: Programming UNIX

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

INd_rasN SOME SHELL SCRIPTING PROGRAMS. 1. Write a shell script to check whether the name passed as first argument is the name of a file or directory.

Preview. Review. The test, or [, command. Conditions. The test, or [, command (String Comparison) The test, or [, command (String Comparison) 2/5/2019

UNIX Shell Scripts. What Is a Shell? The Bourne Shell. Executable Files. Executable Files: Example. Executable Files (cont.) CSE 2031 Fall 2012

1. Hello World Bash Shell Script. Last Updated on Wednesday, 13 April :03

There are some string operators that can be used in the test statement to perform string comparison.

RHCE BOOT CAMP. System Administration

Shell Start-up and Configuration Files

Vi & Shell Scripting

12.1 UNDERSTANDING UNIX SHELL PROGRAMMING LANGUAGE: AN INTRODUCTION Writing a Simple Script Executing a Script

DAVE LIDDAMENT INTRODUCTION TO BASH

Shell Programming. Introduction to Linux. Peter Ruprecht Research CU Boulder

CENG 334 Computer Networks. Laboratory I Linux Tutorial

More Scripting Techniques Scripting Process Example Script

CS214 Advanced UNIX Lecture 4

Lab 4: Shell Scripting

Assignment 2, due September 17

Linux shell & shell scripting - II

LINUX Scripts de Bash Shell. Exercise 1 - Working with Files

Basic Linux (Bash) Commands

Lecture 5. Essential skills for bioinformatics: Unix/Linux

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

COMP2100/2500 Lecture 17: Shell Programming II

Shell scripting and system variables. HORT Lecture 5 Instructor: Kranthi Varala

S E C T I O N O V E R V I E W

Subcontractors. bc math help for the shell. interactive or programatic can accept its commands from stdin can accept an entire bc program s worth

15-122: Principles of Imperative Computation

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Today. Review. Unix as an OS case study Intro to Shell Scripting. What is an Operating System? What are its goals? How do we evaluate it?

Windshield. Language Reference Manual. Columbia University COMS W4115 Programming Languages and Translators Spring Prof. Stephen A.

Lecture 02 The Shell and Shell Scripting

The Unix Shell & Shell Scripts

Week 6 Lesson 1: Control-Flow Statements (Continued)

Linux Bash Shell Scripting

PROGRAMMING PROJECT ONE DEVELOPING A SHELL

Name: Tej. D. Shah Subject:CC-304 Linux Uni. Practical programme College :L.J. College Of Computer Application. Questions:

Exploring UNIX: Session 3

Grep and Shell Programming

EECS2301. Special variables. $* and 3/14/2017. Linux/Unix part 2

bash Scripting Introduction COMP2101 Winter 2019

C Shell Tutorial. Section 1

Linux shell programming for Raspberry Pi Users - 2

1. What statistic did the wc -l command show? (do man wc to get the answer) A. The number of bytes B. The number of lines C. The number of words

Table of Contents. 1. Introduction. 2. Environment. 3. Shell Scripting Shell Scripting Introduction

Bash by example, Part 2

Make sure the parameter expansion is quoted properly. It may not be necessary here, but it is good practice.

Do not start the test until instructed to do so!

Unix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP

bash Execution Control COMP2101 Winter 2019

Unix Shell scripting. Dr Alun Moon 7th October Introduction. Notation. Spaces

Computer Systems and Architecture

Open up a terminal, make sure you are in your home directory, and run the command.

Essential Skills for Bioinformatics: Unix/Linux

CSCI 4152/6509 Natural Language Processing Lecture 6: Regular Expressions; Text Processing in Perl

What is Bash Shell Scripting?

CSC 2500: Unix Lab Fall 2016

Answers to Even-numbered Exercises

CSE 374: Programming Concepts and Tools. Eric Mullen Spring 2017 Lecture 4: More Shell Scripts

Using bash. Administrative Shell Scripting COMP2101 Fall 2017

CMPS 12A Introduction to Programming Lab Assignment 7

Useful Unix Commands Cheat Sheet

PHP. Interactive Web Systems

Lecture 8. Introduction to Shell Programming. COP 3353 Introduction to UNIX

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

UNIX Essentials Featuring Solaris 10 Op System

OPERATING SYSTEMS LAB LAB # 6. I/O Redirection and Shell Programming. Shell Programming( I/O Redirection and if-else Statement)

Introduction to Linux

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Shell script/program. Basic shell scripting. Script execution. Resources. Simple example script. Quoting

Math 98 - Introduction to MATLAB Programming. Fall Lecture 1

Unix as a Platform Exercises. Course Code: OS-01-UNXPLAT

Bash command shell language interpreter

Transcription:

Csci 132 Spring 13 Assignment 2 Due: Tuesday, March 5 (by 11:59PM) A. Readings Read and understand this part before starting working on the exercises of part B. Variables In a Bash script a variable can be dened simply by choosing its name and assigning it a value: MYVAR=5 Be careful, there should be no white space around the =. The following is incorrect: MYVAR = 5 # Wrong. Will give error message You can get the value that a variable holds by prepending the symbol to its name: Y=5 echo Y # Will print: 5 echo Y # Will print: Y You can perform arithmetic operations variables, numbers or both, using the (( operation )) X=5 X=(( X + 5 )) echo X # Will print: 10 Y=10 Z=(( X + Y )) echo Z # Will print: 20 if statements An if statement executes a set of commands if a condition is true, or another set of commands if the condition is false. Syntax: if [ condition ]; then command(s) # Will be executed only if condition is true command(s) # Will be executed only if condition is false *** Notice the white spaces in the line: if [ condition ]; then **** # Compares the rst argument with the number 10 and prints # a relevant message. if [ 1 -lt 10 ]; then echo 1 is less than 10 echo 1 is greater than 10

If you save the above script as comparewithten, then it can be run as: comparewithten 5 5 is less than 10 comparewithten 25 25 is greater than 10 while loop A while loop executes a set of commands repeatedly, for as long as its condition is true. Syntax: while [ condition ] do command(s) done x=1 while [ x -le 5 ] do echo Hello World x=(( x + 1 )) done The above will print Hello World 4 times. (Why?) File Conditions There are some useful conditions that we can include in our Bash scripts to check various things regarding les. Option Description -f le Checks if le exists -x le Checks if le is executable (and exists) -d dir Checks if dir is an existing directory # Checks if the le somele.dat exists FILE=somele.dat if [ -f FILE ]; then echo File FILE exists echo File FILE does not exist We can search for a given word in a le, using the grep lter in the command line: grep -Fxq word le Check grep s man page to see what the Fxq options do.

We can use the grep lter in our scripts to check if a given string exists in a le or not: word=hello #Variable that holds the string we want to search for le=myle #Variable that holds the le s name if grep -Fxq "word" le then echo Found! echo Not Found Notice the different syntax for the if statement in this case; there are no [ ] and the then keyword is in a new line. What makes this work is that the grep command will return 0 if the word is found and 1 if not. In Bash 0 is interpreted as true and 1 as false. So, if grep nds the word in the le it will return 0, which for Bash means that the condition of the if statement is true (and therefore print Found! ). If the word is not found, grep will return 1 which means that the if s condition is false, so it will print Not Found. B. Exercises 1. Write a script named range which will print all the integers between a lower and maximum value that the user should provide as arguments. For example: range 5 13 5 6 7 8 9 10 11 12 13 range 22 17 22 21 20 19 18 17

Your script should take exactly two arguments. In a different case, it should inform the user about the correct usage and quit. range 6 Usage: range <start> <end> Notice that if the rst argument is less than the second, the numbers are printed in ascending order. In the opposite case, they are printed in descending order. 2. Write a script called add_names that will get a name as argument and add this name in le callesd names_list.txt This le should contain names; one name per line. The script should check that the name does not exist in the le before adding it. If the name exists, the script should print a relevant message and quit. It should also check that only one argument is provided and in any other case print a message and quit. Example Assuming that we have a le that contains: Tom Mary Bob Alice Then after running: add_names John our le should now contain: Tom Mary Bob Alice John While: add_names Bob Name already exists And: add_names Nick Angelo I can only add one name at a time

Submission Create your scripts in your favorite editor in Diogenes. Save them in your home folder with the names: yourusername_assignment2_1 yourusername_assignment2_2 Change the permissions of the scripts to 600 Copy them to: /home/students/cs132