CSE4251 The Unix Programming Environment

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

Types of selection structures
Shell Scripting Printing; Loops and Logic printf: formatted print printf is a more standardized method to print text to the screen. It can be used instead.
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
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.
Linux+ Guide to Linux Certification, Second Edition
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
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.
CSc 352 Shell Scripts Saumya Debray Dept. of Computer Science
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
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.
Lab 8 Shell Script Reference:
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.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
An Introduction to Unix Shell Scripting
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
2440: 211 Interactive Web Programming Expressions & Operators.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
OPERATING SYSTEMS DESIGN UNIX BASICS & SHELL SCRIPTING.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Linux+ Guide to Linux Certification, Third Edition
CS465 - UNIX The Bourne Shell.
#!/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.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group Module 9 Basic Scripting.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) More Shell Programming Ruibin Bai (Room AB326) Division of Computer Science The University.
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.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
Flow Control. The order in which commands execute in a shell script is called the flow of the script. When you change the commands that execute based.
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Linux+ Guide to Linux Certification, Second Edition
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
Linux Administration Working with the BASH Shell.
 Prepared by: Eng. Maryam Adel Abdel-Hady
Lab 7 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Shell scripts – part 1 Cs 302. Shell scripts  What is Shell Script? “Shell Script is series of command written in plain text file. “  Why to Write Shell.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
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.
Department of Computer Engineering
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett.
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Introduction to C++ Programming
CSC 352– Unix Programming, Spring 2016
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
Basics.
CSE 303 Concepts and Tools for Software Development
Shell Control Structures
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Basic shell scripting CS 2204 Class meeting 7
Presentation transcript:

CSE4251 The Unix Programming Environment Lecture 6 Bash scripting I: - script introduction; variables & operations conditions

shell script a series of shell commands placed in an ASCII text file Commands include Anything you can type on the command line Variables and Expressions Control statements (if, while, for, ...) Functions

“Hello World” in bash open/create a file: $ vim hello1.sh “#!” or “shebang” tells OS this is a shell script tell OS the script is to be interpreted by  /bin/bash, (i.e., this is a bash script instead of csh, ksh, ...) #!/bin/bash # This is a comment! # This is another comment! echo Hello, World!        #a comment after the cmd the command executed in this script

“Hello World” in bash open/create a file: $ vim hello1.sh add execution permission: $ chmod 744 ./hello1.sh run the script: $ ./hello1.sh #!/bin/bash # This is a comment! # This is another comment! echo Hello, World!        #a comment after the cmd [15:03:45][zhengm@apollo-tesla:~] $ ./hello1.sh Hello, World! [15:03:52][zhengm@apollo-tesla:~] $

“Hello World” in bash How about this: $ vim hello2.sh (run hello2.sh to see if the output matches your guess; we will cover the syntax in the following lectures) #!/bin/bash echo "Hello      World"       echo "Hello World" echo "Hello * World" echo Hello * World echo Hello      World echo "Hello" World echo Hello "     " World echo "Hello \"*\" World" echo `hello` world echo 'hello' world

Variables Set and use variables Set a variable based on user input: #!/bin/bash MY_MSG="Hello World" echo $MY_MSG #!/bin/bash echo What is your name? read USER_NAME echo "Hello $USER_NAME "

Variables Create a file based on user input Wrong: #!/bin/bash echo What is your name? read USER_NAME echo "Hello $USER_NAME “ echo "I will create a file called $USER_NAME_file for you" touch $USER_NAME_file Correct: #!/bin/bash echo What is your name? read USER_NAME echo "Hello $USER_NAME “ echo "I will create a file called $USER_NAME_file for you" touch ${USER_NAME}_file

Variables No space permitted on either side of = sign when initializing variables What happens if there is a space? # "VARIABLE =value" # ^ #Script tries to run "VARIABLE" command with one argument, "=value". # "VARIABLE= value" # ^ #Script tries to run "value" command with #the environmental variable "VARIABLE" set to "".

Scope of variables Unlike C, you can use a variable anywhere w/o declaration; it just means nothing/empty before setting E.g. myvar1.sh #!/bin/bash echo "MY_VAR is: $MY_VAR" MY_VAR="hi there" echo "MY_VAR is: $MY_VAR" $ ./myvar1.sh MY_VAR is: MY_VAR is: hi there

Scope of variables you can set a variable via command line, but you must “export” it so that it’s visible to later scripts [16:10:51][zhengm@apollo-tesla:~/cse4251] $ MY_VAR=hi [16:18:38][zhengm@apollo-tesla:~/cse4251] $ ./myvar1.sh MY_VAR is: MY_VAR is: hi there [16:18:40][zhengm@apollo-tesla:~/cse4251] $ export MY_VAR [16:19:22][zhengm@apollo-tesla:~/cse4251] MY_VAR is: hi set in cmd line invisible to the script export inherited value shows up

Scope of variables you can set a variable via command line, but you must “export” it so that it’s visible to later scripts [16:10:51][zhengm@apollo-tesla:~/cse4251] $ MY_VAR=hi [16:18:38][zhengm@apollo-tesla:~/cse4251] $ ./myvar1.sh MY_VAR is: MY_VAR is: hi there [16:18:40][zhengm@apollo-tesla:~/cse4251] $ export MY_VAR [16:19:22][zhengm@apollo-tesla:~/cse4251] MY_VAR is: hi Note: the 2nd line is “hi there”, which implies that the inherited “hi” is overwritten by the “hi there” defined in the script! #!/bin/bash echo "MY_VAR is: $MY_VAR" MY_VAR="hi there" echo "MY_VAR is: $MY_VAR"

Scope of variables By default, the value defined in the script has no effect outside of the script (disappears after the script finishes) [16:19:22][zhengm@apollo-tesla:~/cse4251] $ ./myvar1.sh MY_VAR is: hi MY_VAR is: hi there [16:19:24][zhengm@apollo-tesla:~/cse4251] $ echo $MY_VAR hi “hi there” defined in the script exists only when executing the script “hi there” is gone; “hi” is the value before running the script

Scope of variables To make a value defined in the script visible/usable outside of the script, source (. ) the script source (.) the script [16:34:39][zhengm@apollo-tesla:~/cse4251] $ . ./myvar1.sh MY_VAR is: hi MY_VAR is: hi there [16:36:58][zhengm@apollo-tesla:~/cse4251] $ echo $MY_VAR hi there “hi there” still there

Escape characters double quotes (""): most characters (e.g., space, tab, *, etc) are not interpreted (i.e., they are taken literally) inside double quotes #!/bin/bash echo 1 Hello     World echo 2 "Hello      World"       1 Hello World 2 Hello World #!/bin/bash echo 3 Hello  * World echo 4 "Hello  *  World"     3 Hello escape.sh gamefile hello1.sh hello2.sh myvar1.sh World 4 Hello * World

Escape characters What if want to output Hello “World”: use \ How about this #!/bin/bash echo 5 "Hello     \"World \" " 5 Hello "World" #!/bin/bash echo 6 "Hello     "World" "

Escape characters ", $, `, and \ are still interpreted by the shell as special characters, even when they're in double quotes #!/bin/bash X=10 echo "A quote is \", backslash is \\, backtick is \`." echo "A few spaces are ; dollar is \$. \$X is $X." A quote is ", backslash is \ , backtick is `. A few spaces are ; dollar is $. $X is 10.

Type of variables bash variables are untyped just character strings in essence depending on context, bash permits arithmetic operations and comparisons on variables the determining factor is whether the value of a variable contains only digits (i.e., 0-9) bash does not support floating point by itself, but can use other commands to handle floating point arithmetic (e.g., bc)

Operators Arithmetic operators: +, -, *, /, %, ++, -- Arithmetic relational operators: -lt (<), -gt (>), -le (<=), -ge (>=), -eq (==), -ne (!=) String comparison operators: s1 = s2, s1 != s2, s1 < s2, s1 > s2, #can also use letters #like –lt, -gt, -ne -n s1 #s1 is not null (contains one or more char.) -z s1 #s1 is null Logic operators (!, &&, ||)

Use let to do arithmetic operations Basic arithmetic operators: +, -, *, /, % #!/bin/bash let a=11 # Same as 'a=11' let a=a+5 # Equivalent to let "a = a + 5" # (Double quotes and spaces make it more readable) echo "11 + 5 = $a" # 16 let "a /= 4" # Equivalent to let "a = a / 4" echo "128 / 4 = $a" # 32 let "a -= 5" # Equivalent to let "a = a - 5" echo "32 - 5 = $a" # 27 let "a *= 10" # Equivalent to let "a = a * 10" echo "27 * 10 = $a" # 270 let "a %= 8“ # Equivalent to let "a = a % 8" echo "270 modulo 8 = $a (270 / 8 = 33, remainder $a)" # 6     

Use let to do arithmetic operations Self-increment/decrement: ++, -- #!/bin/bash # "let" permits C-style operators a=6 let a++ # C-style (post) increment echo "6++ = $a" # 6++ = 7 let a-- # C-style decrement echo "7-- = $a" # 7-- = 6 # note: ++a, etc., also allowed  

Use let to do arithmetic operations If no let ... #!/bin/bash a=2334 let "a += 1" echo "a = $a” a+=1 echo "a = $a“   $ ./arithmetic.sh a = 2335 a = 23351

Alternative to let: ((...)) the (( ... )) construct also permits arithmetic expansion and evaluation #!/bin/bash a=$(( 5 + 3 ))  # Set a to 5 + 3, or 8 (( a = 23 )) # Set a to 23 #+ with spaces on both sides of the "=". echo "a (initial value) = $a" # 23 (( a++ )) # Post-increment 'a', C-style. echo "a (after a++) = $a" # 24 (( a-- )) # Post-decrement 'a', C-style. echo "a (after a--) = $a" # 23 (( ++a )) # Pre-increment 'a', C-style. echo "a (after ++a) = $a" # 24 (( --a )) # Pre-decrement 'a', C-style. echo "a (after --a) = $a" # 23  

Conditions Let you decide whether to perform an action or not the decision is taken by evaluating an expression if ... then ... #!/bin/bash if [[ "foo“ = "foo“ ]]; then echo expression evaluated as true fi      if ... then ... else ... #!/bin/bash if [[ "foo“ = "foo" ]]; then echo expression evaluated as true else echo expression evaluated as false fi     

Conditions Conditions w/ variables #!/bin/bash V1=“foo” V2=“bar” if [[ “$V1“ = “$V2" ]]; then #”” is optional, but better use it to #avoid parsing error in case of #empty variables echo expression evaluated as true else echo expression evaluated as false fi     

Conditions if ... then ... elif ... #!/bin/bash V3="3" if [[ “$V3” = 1 ]]; then echo "expression evaluated as 1" elif [[ “$V3” = 2 ]]; then echo "expression evaluated as 2" else echo "expression evaluated as > 2" fi

Conditions Complex condition Sometimes you may see single brackets [...] for conditions alternative to double brackets [[ ... ]] but with more quirks older but maybe more portable #!/bin/bash #... if [[ “$varA” = 1 && (“$varB” = "t1" || “$varC” = "t2") ]]; then echo blahblahblah fi

Lab 2 DUE: 11:59pm, Tuesday, Mar 03, 2015 http://web.cse.ohio-state.edu/~zhengm/teaching/cse4251sp15/CSE4251%20Lab%202.htm DUE: 11:59pm, Tuesday, Mar 03, 2015