Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE4251 The Unix Programming Environment

Similar presentations


Presentation on theme: "CSE4251 The Unix Programming Environment"— Presentation transcript:

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

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
“#!” 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

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 $ ./hello1.sh Hello, World! $

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

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

10 Scope of variables you can set a variable via command line, but you must “export” it so that it’s visible to later scripts $ MY_VAR=hi $ ./myvar1.sh MY_VAR is: MY_VAR is: hi there $ export MY_VAR MY_VAR is: hi set in cmd line invisible to the script export inherited value shows up

11 Scope of variables you can set a variable via command line, but you must “export” it so that it’s visible to later scripts $ MY_VAR=hi $ ./myvar1.sh MY_VAR is: MY_VAR is: hi there $ export MY_VAR 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"

12 Scope of variables By default, the value defined in the script has no effect outside of the script (disappears after the script finishes) $ ./myvar1.sh MY_VAR is: hi MY_VAR is: hi there $ 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

13 Scope of variables To make a value defined in the script visible/usable outside of the script, source (. ) the script source (.) the script $ . ./myvar1.sh MY_VAR is: hi MY_VAR is: hi there $ echo $MY_VAR hi there “hi there” still there

14 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

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

16 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.

17 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)

18 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 (!, &&, ||)

19 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 " = $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 " = $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 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  

21 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

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

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

26 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

27 Lab 2 DUE: 11:59pm, Tuesday, Mar 03, 2015
DUE: 11:59pm, Tuesday, Mar 03, 2015


Download ppt "CSE4251 The Unix Programming Environment"

Similar presentations


Ads by Google