1 / 27

Shell Scripting

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 of echo and in fact is what is commonly used in programming and other scripting languages (C, C++, Perl) #!/bin/bash -f

donat
Download Presentation

Shell Scripting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Shell Scripting Printing; Loops and Logic

  2. printf: formatted print • printf is a more standardized method to print text to the screen. • It can be used instead of echo and in fact is what is commonly used in programming and other scripting languages (C, C++, Perl) #!/bin/bash -f printf “Hello world.\n” \n represents a newline a=`echo "Hello world." | wc | awk '{print $2}' ` printf "This phrase contains %d words\n" $a %./hello.sh Hello world. This phrase contains 2 words

  3. printf: format specifiers • %s String • %c ASCII character. • %d Decimal integer • %f Floating-point format • %E Scientific notation floating-point format

  4. #!/bin/bash -f printf “Hello world.\n” \n represents a newline a=`echo "Hello world." | wc | awk '{print $2}' ` printf "This phrase contains %5.2f words\n" $a

  5. Arithmetic • bash shell arithmetic resembles C programming language arithmetic. • In bash, the syntax $(( )) can be used to calculate arithmetic expressions or to set variables to complex arithmetic expressions %echo $(( 3+4 )) 7 %echo $(( x = 2 )) 2

  6. Basic Arithmetic Operators • shell arithmetic is integer only • + : addition • - : subtraction • * : multiplication • / : division • % : remainder or modulus %echo (( 10%3 )) 1 %echo $(( 10/3 )) 3

  7. Assignment Operators • = : set variable equal to value on right %x = 2; echo $x 2 • += : set variable equal to itself plus the value on right %x = 2; echo $(( x +=2 )) 4 • -= : set variable equal to itself minus the value on right %x = 2; echo $((x -= 2)) 0

  8. Assignment Operators • *= : set variable equal to itself times the value on right %x = 2; echo $((x *= 4)) 8 • /= : set variable equal to itself divided by value on right %x = 2; echo $((x/= 2)) 1 • %= : set variable equal to the remainder of itself divided by the value on the right %x = 4; echo $((x %= 3)) 1

  9. Unary Operations • A unary expression contains one operand and one operator • ++ : increment the operand by 1 • if ++ occurs after, $x++, the original value of the operand is used in the expression and then incremented • if ++ occurs before, ++$x, the incremented value of the operand is used in the expression • -- : decrement the operand by 1 • + : unary plus maintains the value of the operand, x=+x • - : unary minus negates the value of the operand, -1*x=-x • ! : logical negation evaluates if the operand is true (returns 1) or false (returns 0)

  10. Relational Operators • Returns 1 if true and 0 if false • All relational operators are left to right associative • < : test for less than • <= : test for less than or equal to • > : test for greater than • >= : test for greater than or equal to • == : test for equal to • != : test for not equal

  11. Boolean (Logical) Operators • Boolean operators return 1 for true and 0 for false • && : logical AND; tests that both expressions are true • left to right associative %echo $(( (3 < 4) && (10<15) )) 1 %echo $(( (3<4) && (10>15) )) 0 • || : logical OR ; tests that one or both of the expressions are true • left to right associative %echo $(( (3<4) || (10>15) )) 1 • ! : logical negation; tests that expression is true

  12. Bitwise Operators • Bitwise Operators treat scalars as 16-point bit binary values Example: 4019 equals 0000111110110011 • ~ : bitwise negation changes 0 to 1 and vice versa • & : bitwise AND • ^ : bitwise exclusive OR • | : bitwise OR • << : bitwise left shift • >> : bitwise right shift

  13. test • test: condition evaluation utility • common scripting tool that tests expressions and many details about files using a long list of flags • returns 0 if expression true and 1 if expression false or does not exist • two formats in bash scripting • test flag expression • [ flag expression ]

  14. useful flags return true (0) if • -d : expression is a directory • -f : expression is a regular file • -e : expression is any type of file • -w : expression is a writable file • -x : expression is an executable (file or directory) • -n : expression is a nonzero length string • -z : expression is a zeo length string if [ -e ~/SUMA.NEW.loc ] then commands else exit $? #unsuccessful execution of script fi

  15. String tests, true if • s1 = s2 : strings s1 and s2 are identical. • s1 != s2: strings s1 and s2 are not identical. • s1 < s2 : string s1 comes before s2 based on the ASCII value • s1 > s2 : string s1 comes after s2 based on the ASCII value • Algebraic integer tests, true if • n1 -eq n2 : integers n1 and n2 are algebraically equal. • n1 -ne n2 : integers n1 and n2 are not algebraically equal. • n1 -gt n2 : integer n1 is algebraically greater than the integer n2. • n1 -ge n2 : integer n1 is algebraically greater than or equal to the integer n2. • n1 -lt n2 : integer n1 is algebraically less than the integer n2. • n1 -le n2 : integer n1 is algebraically less than or equal to the integer n2.

  16. if/then/fi • If expression is true, then run the following. End the if command if [ $1 = "Heather” ] then printf "Hi %s. We were expecting you.\n"$1 fi

  17. if/then/else/fi • If expression is true, then run the first set of commands. Else if false, run the second set of commands. End the if command if [ $1 = "Heather” ] then printf "Hi %s. We were expecting you.\n"$1 else printf "Hi %s. Nice to meet you.\n" $1 fi

  18. if/then/elif/else/fi • If expression is true, then run the first set of commands. Else if a second expression is true, run the second set of commands. Else if neither is true, run a third set of commands. End the if command if [ $1 = "Heather” ] then printf "Hi %s. We were expecting you.\n"$1 elif [ $1 = "Andy"] printf "Hi %s. We were expecting you too.\n" $1 else printf "Hi %s. Nice to meet you.\n" $1 fi

  19. if/then/else/fi • If either of the expressions is true, then run the first set of commands. Else if the second expression is true, run the second set of commands. Else if nothing as been true, run a third set of commands. End the if command if [ $1 = "Heather” ] || [ $1 = “Andy” ] then printf "Hi %s. We were expecting you.\n"$1 elif [ $1 = "Andy"] printf "Hi %s. We were expecting you too.\n" $1 else printf "Hi %s. Nice to meet you.\n" $1 fi Would this script ever output “We were expecting you too”?

  20. do/done • do the following set of commands until done • in bash, this construct is used in conjunction with loop structures for, while, and until

  21. for • one of the most common loop structures is the for loop, which iterates over an array of objects • forx values in array, do this a="1 2 3 4 5” foriin $a do echo $(( ++i )) done What is the first integer printed to the screen?

  22. while • while: continues to loop as long as condition exited successfully #!/bin/sh cat << EOF > cities.d#Create the file cities.d 105.87 21.02 HANOI LM 282.95 -12.1 LIMA LM 178.42 -18.13 SUVA LM EOF while read clonclat city junk #setting the variables clonclat and city for each line do echo $city $clon $clat done <cities.d %sh –f junk.sh HANOI 105.87 21.02 LIMA 282.95 -12.1 SUVA 178.42 -18.13

  23. until • until: continues to loop as long as condition exits unsuccessfully • the until loop is used much less than the while loop #!/bin/bash myvar=0 until [ $myvar–eq 5 ] #until this expression is false do echo $myvar myvar=$(( $myvar + 1 )) done %sh –f junk.sh 0 1 2 3 4

  24. break • break: allows you to break out of a loop • can be used with a number to specify what do loop to break out of while condition1 # Outer loop, loop 2 do ... while condition2 # Inner loop, loop 1 do ... break 2 # Break out of outer loop done done ... # Execution continues here after break

  25. foreach : a tcsh command • the foreach command is a powerful way to iterate over files from the tcsh command line %foreachfile ( 828/*BHZ* ) #set variable file to each sac file foreach? echo $file foreach? set name = `echo $file | cut -f2 -d'/' ` foreach? set sta= `echo $name | cut -f1 -d'.' ` foreach? echo “copy $file to $sta.BHZ.SAC foreach? cp $file $sta.BHZ.SAC foreach? end 828/GAR.BHZ_00.D.1989.214:10.24.59 copy 828/GAR.BHZ_00.D.1989.214:10.24.59 to GAR.BHZ.SAC

  26. case in tcsh syntax foreach plane(0035.0 0050.0) set cnt=`expr $cnt + 1` switch ($cnt) case 1: set xpos=-5. set ypos=4.75 set min=-2.5 set max=2.5 breaksw case 2: set xpos=-6.6 set ypos=-3.5 set min=2.5 set max=7 breaksw endsw excessive amounts of GMT end

More Related