CSC 2500: Unix Lab Fall 2016

Similar documents
Conditional Control Structures. Dr.T.Logeswari

CSC 2500: Unix Lab Fall 2016

bash Execution Control COMP2101 Winter 2019

Shell Programming (bash)

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

Bash scripting basics

COMP 4/6262: Programming UNIX

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

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

CSC 2500: Unix Lab Fall 2016

Bourne Shell Reference

Shell programming. Introduction to Operating Systems

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

Computer Systems and Architecture

Shells & Shell Programming (Part B)

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

LING 408/508: Computational Techniques for Linguists. Lecture 5

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Програмиранев UNIX среда

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

Computer Systems and Architecture

Chapter 5 The Bourne Shell

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Shell Scripting. Winter 2019

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

A shell can be used in one of two ways:

Shell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Control Structures. CIS 118 Intro to LINUX

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell.

Introduction to Linux Basics Part II. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala

Shell Script Example. Here is a hello world shell script: $ ls -l -rwxr-xr-x 1 horner 48 Feb 19 11:50 hello* $ cat hello #!/bin/sh

UNIX shell scripting

COMP 2718: Shell Scripts: Part 1. By: Dr. Andrew Vardy

Shell Programming (Part 2)

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.

Shell Control Structures

Bash scripting Tutorial. Hello World Bash Shell Script. Super User Programming & Scripting 22 March 2013

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

Useful Unix Commands Cheat Sheet

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

Practice 5 Batch & Shell Programming

Prof. Navrati Saxena TA: R. Sachan Bharat Jyoti Ranjan

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

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Shell script. Shell Scripts. A shell script contains a sequence of commands in a text file. Shell is an command language interpreter.

EECS 470 Lab 5. Linux Shell Scripting. Friday, 1 st February, 2018

Lab 4: Shell scripting

Shell Control Structures

Shells and Shell Programming

Essential Skills for Bioinformatics: Unix/Linux

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

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Assignment clarifications

Vi & Shell Scripting

Bash shell programming Part II Control statements

Shells and Shell Programming

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

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

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

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

9.2 Linux Essentials Exam Objectives

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

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

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

Assignment 3, Due October 4

Title:[ Variables Comparison Operators If Else Statements ]

Linux shell programming for Raspberry Pi Users - 2

EECS2301. Example. Testing 3/22/2017. Linux/Unix Part 3. for SCRIPT in /path/to/scripts/dir/* do if [ -f $SCRIPT -a -x $SCRIPT ] then $SCRIPT fi done

Shell Start-up and Configuration Files

CS Unix Tools & Scripting

Lecture 5. Essential skills for bioinformatics: Unix/Linux

Understanding bash. Prof. Chris GauthierDickey COMP 2400, Fall 2008

Lab #12: Shell Scripting

Cisco IOS Shell. Finding Feature Information. Prerequisites for Cisco IOS.sh. Last Updated: December 14, 2012

INTRODUCTION TO SHELL SCRIPTING Dr. Jeffrey Frey University of Delaware, IT

Linux Bash Shell Scripting

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

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

CS214 Advanced UNIX Lecture 4

Last Time. on the website

More Linux Shell Scripts

Bash Shell Programming Helps

CSE 390a Lecture 6. bash scripting continued; remote X windows; unix tidbits

Operating Systems. Lab Manual. Compiled by: Mr. Mohammed Al-Qadhi Computer Science Department

BASH Programming Introduction

Grep and Shell Programming

Shell Programming (ch 10)

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2018

Original Script. Display commands to manually creating an account. #!/bin/bash

Practical 04: UNIX / LINUX Shell Programming 1

More Scripting Techniques Scripting Process Example Script

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

Introduction to Shell Scripting

Lecture 02 The Shell and Shell Scripting

30 Bash Script Examples

More Raspian. An editor Configuration files Shell scripts Shell variables System admin

LOG ON TO LINUX AND LOG OFF

Unix Scripts and Job Scheduling. Overview. Running a Shell Script

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

Linux 系统介绍 (III) 袁华

Transcription:

CSC 2500: Unix Lab Fall 2016 Control Statements in Shell Scripts: Decision Mohammad Ashiqur Rahman Department of Computer Science College of Engineering Tennessee Tech University

Agenda User Input Special Shell Variables Decision Control Statements if-then-else Statements Exit status test case Statements Lab 6: Working with Control Statements M. Ashiq Rahman, Tennessee Tech University 2

Input Toward Writing a Bash Script Prompting user Command line arguments Decision if-then-else case Repetition do-while, repeat-until for select Functions Traps The trap command allows to execute a command when a signal is received by the script. M. Ashiq Rahman, Tennessee Tech University 3

User Input Reading user input: read varname1 [varsname2...] read p "<Prompt>" varname1 [varsname2...] Each word entered by the user is stored in the variables The last variable gets rest of input line #!/bin/bash read -p Enter Your Name: " first last echo "First Name: $first" echo "Last Name: $last" $ bash read.sh Enter Your Name: Ashiq Rahman First Name: Ashiq Last Name: Rahman M. Ashiq Rahman, Tennessee Tech University 4

Special Shell Variables $0 - Name of the current shell script $1-$9 - Positional parameters 1 through 9 $# - The number of positional parameters $* - All positional parameters, $* is one string $@ - All positional parameters, $@ is a set of strings $? - Return status of most recently executed command $$ - Process id of current process $ set one two three four $ echo $* one two three four $ echo $# 4 $ echo $1 one $ echo $3 $4 The set command is used here to assign values to positional parameters. M. Ashiq Rahman, Tennessee Tech University 5

The if Statement if commands1 then commands2 fi if condition then statement fi if commands1; then commands2 fi if commands1; then commands2; fi commands is a list of commands. If commands1 (condition) succeeds, then commands2 (satement) executes. A command returns 0 when it succeeds Exit status test command M. Ashiq Rahman, Tennessee Tech University 6

Exit Status A command (can be a set of commands) issues a value to the system when it terminate, called an exit status. This value ranges from 0 to 255, indicates the success or failure of the command s execution. A value of zero indicates success, any other indicates failure. ashiq@ubuntu:~$ ls -d /usr/bin /usr/bin ashiq@ubuntu:~$ echo $? 0 ashiq@ubuntu:~$ ls -d /usr/trash ls: cannot access /usr/trash: No such file or directory ashiq@ubuntu:~$ echo $? 2 ashiq@ubuntu:~$ true ashiq@ubuntu:~$ echo $? 0 ashiq@ubuntu:~$ false ashiq@ubuntu:~$ echo $? 1 ashiq@ubuntu:~$ if true; then echo "It's true."; fi It's true. ashiq@ubuntu:~$ if false; then echo "It's true."; fi ashiq@ubuntu:~$ M. Ashiq Rahman, Tennessee Tech University 7

The test Command test is used as part of the conditional execution of shell commands. Syntax- Unusually two forms: test expression [ expression ] It evaluates expression and returns true or false (exit status) Note the spaces after the [ and before the ] M. Ashiq Rahman, Tennessee Tech University 8

Example: File Testing #!/bin/bash if test x $1 then echo File $1 is executable!" fi #!/bin/bash if [ w $1] then echo "File $1 is executable!" fi $ chmod u+x Test.sh $./Test.sh Test.sh File Test.sh is executable File Testing (using test) -d file: True if file is a directory -f file: True if file is a regular file -r file: True if file is readable -w file: True if file is writable -x file: True if file is executable -s file: True if length of file is nonzero There are more M. Ashiq Rahman, Tennessee Tech University 9

More if Statements if-then-else if commands1; then commands2 else commands3 fi If-then-elif- if commands1; then commands2 elif commands3; then commands4 else commands5 fi elif: else if More elifs possible M. Ashiq Rahman, Tennessee Tech University 10

Relational Operators Numeric Operators: Description Equal Not equal Greater than Less than Greater than or equal Less than or equal Operators -eg -ne -gt -lt -ge -le String Operators: Description Operators Equal = Not equal!= The string size is zero -z The string size is non-zero -n The string (str) is not empty str M. Ashiq Rahman, Tennessee Tech University 11

Example: If-elif #!/bin/bash read -p "Enter Income Amount: " income read -p "Enter Expenses Amount: " expense balance=`expr $income - $expense` if [ $balance -eq 0 ]; then echo "You are breaking even." elif [ $balance -gt 0 ]; then echo Great! You made a profit of: \$$balance." else echo Oops! You lost \$`expr 0 - $balance`." fi $./Elif.sh Enter Income Amount: 1000 Enter Expenses Amount: 900 Awesome! you made a profit of: $900. $./Elif.sh Enter Income Amount: 1000 Enter Expenses Amount: 1200 Oops! You lost $200. M. Ashiq Rahman, Tennessee Tech University 12

Logical Operators Logical or Boolean operators Description Operators Logical negation! Logical or -o ( ) Logical and -a (&&) If you use or &&, you will need to use [[ and ]] instead of [ and ]. #!/bin/bash read -p "Enter Income Amount: " income read -p "Enter Expenses Amount: " expense balance=`expr $income - $expense` if [[ $income -lt 0 $expense -lt 0 ]]; then echo Invalid input(s)! Please try again." elif [ $balance -eq 0 ]; then.. fi $./Elif.sh Enter Income Amount: -100 Enter Expenses Amount: -20 Invalid input(s)! Please try again. M. Ashiq Rahman, Tennessee Tech University 13

The case Statement A decision based on multiple choices or options Syntax: case word in pattern1) commands1;; pattern2) commands2;;......... patternn) commandsn;; esac The pattern is checked against the word for a match The pattern may also contain: * - zero or more characters? - any one character [ ] - a character class Regular expressions can be used Multiple patterns can be listed using M. Ashiq Rahman, Tennessee Tech University 14

#!/bin/bash Example: case read -p "Enter the vulnerability score (0 10): " score case $score in [0 3]) echo Good! The system is at low risk." ;; [4 6]) echo The system is at medium risk." ;; [7 9]) echo Oops! The system is at high risk." ;; 10) echo Oops! The system is at high risk." ;; *) echo Wrong input! exit 1;; esac $./Case.sh Enter the vulnerability score (0 10): 8 Oops! The system is at high risk. $./Case.sh Enter the vulnerability score (0 10): 12 Wrong input! M. Ashiq Rahman, Tennessee Tech University 15

Lab 06 Objective: Shell variables Simple if decision statements The read command File redirection Homework: You will write a script named planner.sh. Your program asks the user some questions and construct a plan for the user by writing information to the user's.plan file in the user's home directory. M. Ashiq Rahman, Tennessee Tech University 16

Lab 06: Homework Submission Rename the shell script file to <FirstNameInitial><LastName>_planner.sh Submission Deadline: Friday, October 07, 2016 Submission Site: ilearn A Dropbox folder named Homework 06 Submission Content: The shell script file <FirstNameInitial><LastName>_planner.sh. M. Ashiq Rahman, Tennessee Tech University 17

Lab 06: Lab Practice Write a script which will ask for a username. If there is not a file that exists with the name username_age, the script will prompt the user s age. The script will then make sure that the user is old enough to use this program. Then it will write the age of the user to a file with the name username_age. If the file already exists, it will just display the age of the user. M. Ashiq Rahman, Tennessee Tech University 18

THANKS Acknowledgement: - https://www.howtoforge.com/tutorial/linux-shell-scripting-lessons-3/ - http://linuxcommand.org/lc3_wss0080.php - https://www.tutorialspoint.com/unix/unix-basic-operators.htm M. Ashiq Rahman, Tennessee Tech University 19