Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecture 6 Bash scripting I: - script introduction; - variables & operations - conditions CSE4251 The Unix Programming Environment 1."— Presentation transcript:

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

2 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

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

4 “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:~] $

5 “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

6 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 "

7 Variables Create a file based on user input #!/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 #!/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 Wrong: Correct:

8 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 "".

9 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

10 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] $./myvar1.sh MY_VAR is: hi MY_VAR is: hi there Scope of variables set in cmd line invisible to the script export inherited value shows up

11 #!/bin/bash echo "MY_VAR is: $MY_VAR" MY_VAR="hi there" echo "MY_VAR is: $MY_VAR" [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] $./myvar1.sh MY_VAR is: hi MY_VAR is: hi there Scope of variables Note: the 2 nd line is “hi there”, which implies that the inherited “hi” is overwritten by the “hi there” defined in the script! you can set a variable via command line, but you must “export” it so that it’s visible to later scripts

12 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 Scope of variables “hi there” is gone; “hi” is the value before running the script “hi there” defined in the script exists only when executing the script

13 To make a value defined in the script visible/usable outside of 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 Scope of variables “hi there” still there source (.) the script

14 double quotes (""): most characters (e.g., space, tab, *, etc) are not interpreted (i.e., they are taken literally) inside double quotes Escape characters #!/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

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

16 ", $, `, and \ are still interpreted by the shell as special characters, even when they're in double quotes Escape characters #!/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.

17 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) Type of variables

18 Arithmetic operators: +, -, *, /, %, ++, -- Arithmetic relational operators: -lt ( ), -le ( =), -eq (==), -ne (!=) String comparison operators: 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 (!, &&, ||) Operators

19 Basic arithmetic operators: +, -, *, /, % Use let to do arithmetic operations #!/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

20 Self-increment/decrement: ++, -- Use let to do arithmetic operations #!/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

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

22 the ((... )) construct also permits arithmetic expansion and evaluation Alternative to let: ((...)) #!/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

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

24 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

25 Conditions #!/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 if... then... elif...

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

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


Download ppt "Lecture 6 Bash scripting I: - script introduction; - variables & operations - conditions CSE4251 The Unix Programming Environment 1."

Similar presentations


Ads by Google