bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Similar documents
bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

bash Execution Control COMP2101 Winter 2019

A shell can be used in one of two ways:

bash Scripting Introduction COMP2101 Winter 2019

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Useful Unix Commands Cheat Sheet

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

Shell programming. Introduction to Operating Systems

Shells & Shell Programming (Part B)

COMP 4/6262: Programming UNIX

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

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

Bourne Shell Reference

More Scripting Techniques Scripting Process Example Script

CSC 2500: Unix Lab Fall 2016

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

Computer Systems and Architecture

Conditional Control Structures. Dr.T.Logeswari

9.2 Linux Essentials Exam Objectives

Lab 4: Shell scripting

Shells and Shell Programming

Computer Systems and Architecture

Shells and Shell Programming

Linux shell programming for Raspberry Pi Users - 2

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

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

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

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

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

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

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

CS Unix Tools & Scripting

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Bash scripting basics

bash Data Administrative Shell Scripting COMP2101 Fall 2017

Linux Systems Administration Shell Scripting Basics. Mike Jager Network Startup Resource Center

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

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

Shell Programming (bash)

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

Last Time. on the website

Lab #12: Shell Scripting

CS214 Advanced UNIX Lecture 4

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

Control Structures. CIS 118 Intro to LINUX

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

UNIX System Programming Lecture 3: BASH Programming

Assignment clarifications

UNIX shell scripting

Title:[ Variables Comparison Operators If Else Statements ]

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

bash, part 3 Chris GauthierDickey

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

Vi & Shell Scripting

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

Bash shell programming Part II Control statements

Assignment 3, Due October 4

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

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

Linux 系统介绍 (III) 袁华

Overview of the UNIX File System

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

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

Introduction to Shell Scripting

RHCE BOOT CAMP. System Administration

LOG ON TO LINUX AND LOG OFF

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

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.

Linux Essentials Objectives Topics:

Shell Programming (Part 2)

More Scripting and Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

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

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

CSE 390a Lecture 6. bash scrip'ng con'nued; remote X windows; unix 'dbits

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

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

22-Sep CSCI 2132 Software Development Lecture 8: Shells, Processes, and Job Control. Faculty of Computer Science, Dalhousie University

Shell Start-up and Configuration Files

While Statement Examples. While Statement (35.15) Until Statement (35.15) Until Statement Example

Basic Linux (Bash) Commands

Essential Skills for Bioinformatics: Unix/Linux

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

COMP2100/2500 Lecture 17: Shell Programming II

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

BASH and command line utilities Variables Conditional Commands Loop Commands BASH scripts

Operating Systems, Unix Files and Commands SEEM

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 script. Shell Scripts. A shell script contains a sequence of commands in a text file. Shell is an command language interpreter.

Linux Bash Shell Scripting

Review of Fundamentals

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

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

Fall 2006 Shell programming, part 3. touch

Unix Jeopardy 4, 3/31/2016. CSC 352, UNIX Dr. D. Parson, Session 4 of Unix Jeopardy, Shell Programming

Introduction to Bash Programming. Dr. Xiaolan Zhang Spring 2013 Dept. of Computer & Information Sciences Fordham University

More Linux Shell Scripts

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

Transcription:

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Command Lists A command is a sequence of commands separated by the operators ; & && and ; is used to simply execute commands in order with no dependence on each other echo "Files in current directory: " ; ls & is used to put a command into the background, background tasks can be viewed with the jobs command sleep 60 & sleep 120 & sleep 30 & jobs && and cause the command on the right to be run only on the success and failure respectively of the command on the left ls && echo "ls was successful" ls /flooble echo "ls failed"

Command Pipeline A command pipeline is a sequence of commands separated by the character The character causes the output (/dev/stdout) of the command on the left of the to be connected to the input (/dev/stdin) of the command on the right The exit status of a pipeline is the exit status of the last command in the pipeline echo -n "Number of non-hidden files in this directory: " ; ls wc -l echo "Process count by username:" ; ps -eo user --no-headers sort uniq -c ls /flooble wc -l && echo "Worked!"

Practice Create the script below Run the script without creating a variable named MYVAR in your current shell Try it with MYVAR existing but empty, then existing with data in it (don't forget to export it!) #!/bin/bash # This script demonstrates testing if a variable named MYVAR exists and whether it is empty # It is expected that you use this script to test if MYVAR is inherited from a parent process # since it is not created or modified in this script [ -v MYVAR ] && echo "The variable MYVAR exists" [ -v MYVAR ] echo "The variable MYVAR does not exist" [ -v MYVAR ] && [ -n "$MYVAR" ] && echo "The variable MYVAR has data in it" [ -v MYVAR ] && [ -z "$MYVAR" ] && echo "The variable MYVAR is empty"

Compound Commands Using pipelines, s, and exit status, we can construct more complex commands We can use the exit status of the test ( or [ ) command, and the [[ expression to perform commands based on the results of evaluating expressions Conditional execution can be done in more sophisticated ways using the if, while, for, and case commands

Exit Status Every process that runs, produces an exit status when it ends, either intentionally using the exit [status] command, or automatically due to script bailout, or end of file, or signal reception The shell can access that status using the special variable? Exit status 0 normally means successful completion Any time a command might fail and cause problems, your script should be doing something to recognize and deal with the possible failure echo $? [ $? = "0" ] handle error

User Input The read command can be used ask for user input and assign it to a variable You should always validate the input a user gives Always think about what happens if a user just hits enter read -p Enter something: myvar [ -n "$myvar" ] echo "You didn't give me any input" read -p "Enter 3 colours: " colour1 colour2 colour3 [ -n "$colour1" -o -n "$colour2" -o -n "$colour3" ] echo "Need 3 colours" read -s -p "Password: " passwd read -n 1 -p "[Y/n]: " choice [[ "$choice" =~ [ynyn] -z "$choice" ]] echo "Must enter a y, n, or enter" [ $(expr match "$choice" '^[ynyn]$') = 1 -o -z "$choice" ] echo "Must enter a y, n, or enter"

If Command Action can be taken, or not taken, based on the exit status of a command The test command can evaluate unary or binary expressions, so it can be a very useful command for the For a of available expression operators, refer to the man page if ; then else fi if [ expr ]; then fi

Testing Data We can do unary tests to determine if variables exist using -v varname, if strings have anything in them using -n string or -z string We can do binary tests comparing string data to other values using =,<,>,!= including static values We can do binary tests comparing integer data using -eq, -ne, -lt, -le, -gt, -ge This can be used to validate user input as well as test data retrieved from elsewhere in the system

Testing Files We can do unary tests for file existence (-e), type (-f,- d,-h,-p,-b,-c,-s), permissions (-r,-w,-x,-k,-u), ownership (-O,-G), size (-s), modification (-N), and whether a file is an open terminal device (-t fd) We can do binary tests on files based on their dates (- nt, -ot), and determine if two filenames are hard linked (-ef)

Practice What bash tests might you use for each of the following? Create a test for each of them. (e.g. file=/etc/resolv.conf ; [ -e "$file" ] && echo "$file exists" ) Does the /etc/resolv.conf file exist Is /bin/ls executable Is /tmp a directory Is /etc/hosts a directory Can you read /etc/shadow as an ordinary user Can you write /etc/network/interfaces as an ordinary user Is /bin/passwd setuid Which is newer, /etc/hosts or /etc/resolv.conf Is /bin/pidof a symbolic link or a regular file

Practice Create a script to test the following: (e.g. if [ -v SHELL ]; then echo "SHELL exists" ; else echo "SHELL does not exist" ; fi ) Does the variable SHELL exist Does the variable FLOOBLE exist Is anything assigned to $USER Is there anything assigned to $FLOOBLE a=1;b=01;c=02 Are $a and $b equal to each other alphabetically or numerically Which of $a and $c is less than the other, numerically and alphabetically arr=(a b c d) Test if $arr has more or less than one element

While Command A can be executed repeatedly based on the exit status of another The break or continue or exit commands can be used in the do to get out of a loop early while ; do done

Practice Use a while loop to display the contents of the interfaces and ips arrays from the lab 2 script Get a random number from 1 to 10, then use a while loop to ask the user for a number from 1 to 10 repeatedly until they guess your number correctly, exiting early if the user does not make a valid guess Modify the guessing game to give the user hints about whether to go higher or lower after each guess they make

For Command The for command allows repeated execution of a either substituting values from a word in a variable or by evaluating expressions for varname in word; do done for (( initial expression; test expression; loop expression )); do done

Practice Use a word for loop to display the contents of our animals arrays from the getanimal.sh script, the loop should iterate over the colour names and use them to display the animals with their colours Get a number of rolls from the user from 1 to 5 for some virtual dice, then use the expression for loop to roll a pair of virtual dice that many times, displaying the results of each roll

Case Command The case command allows execution of a based on the value of a variable or word case $var in pattern ) ;; pattern pattern ) ;; * ) ;; esac