Bash Scripting CS 580U - Fall 2018.

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Integer variables #!/bin/csh # sum of numbers from $argv[1] to $argv[2] set low = $argv[1] set high = $argv[2] set sum = 0 set current = $low while ( $current.
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
CS 497C – Introduction to UNIX Lecture 34: - Shell Programming Chin-Chih Chang
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Shell Programming. Shell Scripts (1) u Basically, a shell script is a text file with Unix commands in it. u Shell scripts usually begin with a #! and.
Shell Script Examples.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Using the Bash Shell. Linux Shell Options Linux provides a range of options for shells. bash –The GNU Bourne Again Shell (bash) bsh – The Bourne Shell.
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
Introduction to Shell Script Programming
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.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
An Introduction to Unix Shell Scripting
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
CST8177 bash Scripting Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
July 17, 2003Serguei A. Mokhov, 1 Shells and Shell Scripts COMP 444/5201 Revision 1.3 January 25, 2005.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Linux Operations and Administration
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 11: Shell.
Shell Programming Any command or a sequence of UNIX commands stored in a text file is called a shell program. It is common to call this file a command.
#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
Writing Scripts Hadi Otrok COEN 346.
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.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Chapter Six Introduction to Shell Script Programming.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Batch Files Weaker form of UNIX shell scripts Copyright © by Curt Hill.
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
 Prepared by: Eng. Maryam Adel Abdel-Hady
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
ULI101 Week 10. Lesson Overview ● Shell Start-up and Configuration Files ● Shell History ● Alias Statement ● Shell Variables ● Introduction to Shell Scripting.
COMP075 OS2 Bash Scripting. Scripting? BASH provides access to OS functions, like any OS shell Also like any OS shell, BASH includes the ability to write.
Bash Shell Scripting 10 Second Guide.
Department of Computer Engineering
Linux Shell Programming
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Agenda Korn Shell Functions Korn Shell Variables
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
CAP652 Lecture - Shell Programming
Shell Scripting March 1st, 2004 Class Meeting 7.
Agenda Control Flow Statements Purpose test statement
What is Bash Shell Scripting?
Pepper (Help from Dr. Robert Siegfried)
LING 408/508: Computational Techniques for Linguists
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
CSE 303 Concepts and Tools for Software Development
Linux Shell Script Programming
CSC 352– Unix Programming, Fall, 2011
Introduction to Bash Programming, part 3
Presentation transcript:

Bash Scripting CS 580U - Fall 2018

The Shell The shell is an interactive command line interface for working with your computer The windowed GUI that you are used to is just a wrapper for the shell The shell is also called ‘Terminal’, and ‘Command Line’ The Unix shell (sh) was the interface for the first version of the Unix OS. Bash is an extension of the interface that adds features

Unix Shell Bourne shell (sh) was the first major shell C and TC shell (csh and tcsh) had improved command interpreters, but were less popular than Bourne shell for programming Bourne Again shell (bash) was an improvement of Bourne shell and added features like history and tab completion Other Bash-like shells: Korn shell (ksh), Z shell (zsh) Bash is the dominating Unix shell today You can find the bash executable in /bin/bash on your linux machine When you run the terminal, if you are running bash, you are just executing /bin/bash

Shell Features Different shells have different features, and you might want to switch between them to suit the work you are doing BASH has the following features: tab auto-completion directory shortcuts such as ~ BASH also has a scripting feature where you can write a script to run multiple shell commands The shell scripting language is a macro language A macro language uses text replacement

Why use bash? Why not use Python or Ruby or some other full programming language? Not dependant on version (Python3 vs Python2) and more integrated into the OS Designed for system level operations In practice, use BASH when you need to run a repeated set of commands. If you start requiring a lot of logic, switch to a programming language

Sets of Commands What if I wanted to encrypted a file on my system, remove the original file, and create a flag file. What shell commands could I use to do this? Flag File: a file that’s only purpose is to let me know something happened I could do the following in the command line: gpg -c example.c rm example.c touch example.log

Scripting Commands My previous encryption script required 3 commands: gpg -c example.c rm example.c touch example.log These commands will be the same every time, so why not automate them? I will need a variable for the file name, but otherwise, everything else is scriptable

Writing A Bash Script Traditionally, shell scripts end in the .sh extension encryptlog.sh All of the shell scripts we’ll see in this course begin with a shebang (#!) followed by the full path of the shell we’d like to use as an interpreter: /bin/bash #! /bin/bash # This is the beginning of a shell script Any line that begins with a # (except the shebang) is a comment.

Writing and Running the Script Write your commands just like you would in the shell gpg -c example.c rm example.c touch example.log You can now run your script with: sh encryptlog.sh Or you can mark it as an executable and run it directly chmod +x encryptlog.sh ./encryptlog.sh

Variables Our filename will change, so we should make it a variable MYFILE=”example” variables are in all caps by convention Shell Scripts are whitespace sensitive Notice there are no spaces around the assignment operator when creating the variable. This is essential. Now you can use your variable with the $ gpg -c $MYFILE.c rm $MYFILE.c touch $MYFILE.log

Command Line Arguments You can script any valid sequence of bash commands however, it is more useful if you can take arguments from the shell When you invoke the script, you can pass in arguments ./encryptlog.sh example You can access CLA in your script as $1, $2, $3, … and so on gpg -c $1.c rm $1.c touch $1.log

Special parameters $0 $# $@ returns the name of the shell script running as well as its location in the file system $# is the number of parameters passed $@ gives an array of words containing all the parameters passed to the script

Read The read command allows you to prompt for input and store it in a variable. #!/bin/bash echo -n “Enter name of file to encrypt: ” read FILE echo “Type 'y' to remove the original file” rm -i $FILE Line 2 prompts for a string that is read in line 3. Line 4 uses the interactive remove (rm -i) to ask the user for confirmation.

Environmental Variables There are two types of variables Local variables Environmental variables Environmental variables are set by the system They can usually be listed by using the “env” command They can be explicitly set by using the “export” command Environmental variables hold special values. echo $SHELL /bin/bash

Single vs Double Quoted Strings When assigning character data containing spaces or special characters, the data must be enclosed in either single or double quotes. Using double quotes to show a string of characters will allow any variables in the quotes to be resolved or interpolated var=“test string” newvar=“Value of var is $var” echo $newvar Value of var is test string Single quotes prints the string exactly as it is

Encryption Script classwork #!/bin/bash echo WARNING! This is dangerous echo if you forget the encryption key, echo your file will be lost forever gpg -c $1 echo “Type 'y' to remove the original file” rm -i $1 echo $1 >> encrypted.log Encryption Script classwork

Arithmetic Evaluation The let statement can be used to do mathematical functions: let X=10+2*7 echo $X 24 An arithmetic expression can be evaluated by $[expression] echo “$[123+20]” 143

if statements Conditionals let us decide whether to perform an action or not by evaluating an expression. if [ expression ] #Put spaces after [ and before ], and around the operators and operands. then statements elif [ expression ] #the elif (else if) and else sections are optional then statements else statements fi

expressions An expression can be: String, Numeric, File, and Logical String Comparisons: = #compare if two strings are equal != #compare if two strings are not equal -n #evaluate if string length is greater than zero -z #evaluate if string length is equal to zero Examples: [ s1=s2 ] #true if s1 same as s2, else false [ s1 != s2 ] #true if s1 not same as s2, else false [ -n s1 ] #true if s1 has a length greater then 0, else false [ -z s2 ] #true if s2 has a length of 0, otherwise false

Number Comparisons Numeric comparisons use flags Number Comparisons -eq #compare if two numbers are equal -ge #compare if one number is greater than or equal to a number -le #compare if one number is less than or equal to a number compare -ne #if two numbers are not equal -gt #compare if one number is greater than another number -lt #compare if one number is less than another number Examples: [ n1 -eq n2 ] [ n1 -ge n2 ] [ n1 -le n2 ]

file expressions Files have their own set of operators to make working with files easy: Operators -d #check if path given is a directory -f #check if path given is a file -e #check if file name exists -s #check if a file has a length greater than 0 Examples [ -d fname ] [ -s fname ]

for statements Example #!/bin/bash let sum=0 for num in 1 2 3 4 5 do let “sum = $sum + $num” done echo $sum The for structure is used when you are looping through a range of variables. for var in list do statements done statements are executed with var set to each value in the list

while statements The while loop terminates as soon as the condition becomes false. If condition never becomes false, loop will never exit. while expression do statements done

Command Results You can execute any bash command within your script and use the results the special tick character, ` , allows you to use the result of a command in your script Example To get a list of files in the current folder FILES=`ls` To the contents of a file as a string DATA=`cat file.txt`

Debugging Bash provides two options which will give useful information for debugging -x displays each line of the script with variable substitution before execution -v displays each line of the script as typed before execution Example #!/bin/bash –v or #!/bin/bash –x or #!/bin/bash –xv

Encryption Script - Part 2 #!/bin/bash -xv #This is a comment LIST=`ls` if [ $# -gt 0 ] ; then LIST=$@ fi echo "Please enter a passcode: " read PASS for FILE in $LIST; do if [ -f $FILE ]; then echo $PASS | gpg -c $FILE done echo "Do you want to delete the files (y/n)?" read ANS if [ $ANS = 'y' ]; then rm $FILE Encryption Script - Part 2 Classwork

Limitations Slow Difficult to do low level operations: Dependency hell Since everything comes from external commands, every command will go through a fork and exec. Hence expensive and slow. Difficult to do low level operations: It’s quite difficult to, say, tokenize strings Dependency hell It is not easy to just assume that an external command will do what we expect it to do. For instance, stuff like PATH variable plays a role. Also the exact implementation of the command may vary on different systems.