1 / 81

Lecture 5 – Scripting I

Lecture 5 – Scripting I. BASH Powershell. Intro. We’ve run shell commands …one at a time …by hand What if we wanted to do the same thing many times? Aliases (more of a shortcut) Script (for bigger jobs). Scripting. Allow user to invoke programs, move/edit files, etc

armen
Télécharger la présentation

Lecture 5 – Scripting I

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. Lecture 5 – Scripting I • BASH • Powershell

  2. Intro • We’ve run shell commands • …one at a time • …by hand • What if we wanted to do the same thing many times? • Aliases (more of a shortcut) • Script (for bigger jobs)

  3. Scripting • Allow user to invoke programs, move/edit files, etc • We already have this manually, so why do we care? • Make the invocation easier • It’s like a programming language, but a higher level • Permissions, files, etc

  4. Scripts • Interactive mode • “Real time” • We type stuff by hand • This is what we’ve been doing • Almost everything that we’ll mention for “file mode” scripting works here as well • File mode • We put stuff in a file • Windows: .ps1 • Linux: .sh

  5. Assumptions • We will not run in interactive mode • We will not have spaces in filenames • We will not use “special” characters

  6. Tokens (reviewed) • A sequence of characters delineated by another character sequence • For us, delineator is white space • The delineator divides character sequence into words

  7. Quoting • Single quotes • Every character quoted literally • Double quotes • Mostly the same as single • Some chars interpreted ($var) • Back quotes (left of ‘1’) • “Delayed expansion” • Interpreted as command, replaced by results

  8. I/O Redirection • stdin • Our terminal via keyboard • stdout • Our terminal • stderr • Our terminal

  9. I/O Redirection • Those are the default behaviors • Sometimes it is desirable to change this • Get input from file • Append output to file • Redirect output to another command’s input

  10. Redirect stdout • Command > FILE • Write to FILE • e.g., ls –l > myListing.txt • Problems with this?

  11. Redirect stdout • Command > FILE • Write to FILE • e.g., ls –l > myListing.txt • Problems with this? • It’s overwrites FILE, even if it already exists!

  12. Redirect stdout • Command >> FILE • Append to FILE • e.g., echo “Second Part” >> myFile.txt • Just like “> FILE” if FILE doesn’t exist

  13. Redirect stdin • Command < FILE • Read from FILE • e.g., myProgram < config.xml

  14. > Or < • If you know C++ • It’s backwards • If you don’t know C++ • Just remember it

  15. Piping • C1 | C2 • Output of C1 as input into C2 • e.g., ps –aux | grep vim • Indefinite piping • C1 | C2 | C3 | … | Cn

  16. BASH Variables • Declared 2 ways • ‘set’ command • Giving undeclared variable a value • Type is not required • Example testStr=“meh” echo $testStr • Note: no whitespace around ‘=‘

  17. BASH Variables • Can concatenate strings bigStr=MyFile.$testStr • String manipulation testStr=meh echo ${testStr#m} // eh echo ${testStr%h} // me • Note that variable didn’t change (wasn’t assignment)

  18. More examples file=myFile.txt echo ${file%txt}pdf // myFile.pdf

  19. More examples file=myFile.txt echo ${file%txt}pdf // myFile.pdf tmp=test echo ${tmp#e} // test why?

  20. More examples file=myFile.txt echo ${file%txt}pdf // myFile.pdf tmp=test echo ${tmp#e} // test why? Start and end…not middle!

  21. Even more examples file=myFile files=junk echo $file.txt // myFile.txt

  22. Even more examples file=myFile files=junk echo $file.txt // myFile.txt echo $files.txt // junk.txt

  23. Even more examples file=myFile files=junk echo $file.txt // myFile.txt echo $files.txt // junk.txt echo ${file}s.txt //myFiles.txt

  24. Control Structures • if-else-then • for • while • until • case

  25. Syntax: if if CONDITION; then CMDS; fi -------------------OR--------------------- if CONDITION then cmds elif CONDITION then cmds fi

  26. Example: if if [ -e /dev/sda1 ] then echo “Found it!” else echo “It’s missing!” fi

  27. Syntax: for for VAR in LIST; do CMD; done ------------OR----------- for VARIABLE in LIST do COMMANDS done

  28. Example: for #1 for number in 1 2 3 4 5 do echo $number done

  29. Example: for #2 for ((i=0; i<=10; i++)) do echo $i done

  30. Conditions [ EXPRESSION ]: true/false [ -e FILENAME ]: T if file exists [ $a –eq 7 ]: T if vara is number 7 [ $a = “five” ]: T if vara is string “five”

  31. Conditions [ ! –x FILENAME ]: T if FILENAME is not executable [ -e FN1 –o –d FN2 ]: T if FN1 exists or if FN2 is a directory [ -n $var –a $FN1 –ot $FN2 ]: T if var contains a string AND file name in FN1 is older than FN2’s file

  32. Example #1 for name in * do echo nm $name echo nm.txt ${name%.txt} echo nm.* ${name%.*} done What does this do?

  33. Example #2 for (( i=23; i < 250; i+=12 )) do echo i is $i done What does this do?

  34. Example #3 for i in * do mv $i `echo $i | sed –r ‘s/[0-9 ]+//’` done This is more complicated, and we haven’t learned all the pieces yet • Delayed expansion, sed, regex

  35. BASH scripts • We’re going to avoid interactive mode • Means we’ll be putting scripts in files

  36. Shebang • Every script file must start with shebang • Tells OS which shell to use • For us  #!/bin/bash • Must be at top of file

  37. Procedures • BASH procedures or shell functions • Same idea behind “regular” program functions • Must be declared before use

  38. Procedures procedure_name () { BODY } • Where are the params?

  39. Procedures procedure_name () { BODY } • Where are the params? • Hiding: $1, $2, etc

  40. Procedures procedure_name () { BODY } • Where are the params? • Hiding: $1, $2, etc • Where is return type?

  41. Procedures procedure_name () { BODY } • Where are the params? • Hiding: $1, $2, etc • Where is return type? • $? returns status code of last command • Look at RVH’s example script for other return values

  42. Procedures Example dotO () { for f in * do echo ${f%.o}.out done } echo Starting program dotO echo Ending program

  43. Executing scripts • ./myScript.sh1 • . myScript.sh2 • source myScript.sh2 • File must be executable • Forces script to run in current process

  44. Quoting (again) • Single quotes • Every character quoted literally • Double quotes • Mostly the same as single • Some chars interpreted ($var) • Back quotes (left of ‘1’) • “Delayed expansion” • Interpreted as command, replaced by results

  45. Script conventions • Sometimes it’s easier to move directories than have a long relative path • When script is done, we should put everything back just like we found it!

  46. Script conventions • How do we know where to go back to?

  47. Script conventions • How do we know where to go back to? • pushd • Push a path onto a stack and go there • popd • Pull the top path off the stack and go to the new top item

  48. Powershell • BASH is nifty, but sometimes we need Windows. • We used to do CMD • Now we do Powershell • Much more similar to BASH

  49. Invoking PS • Powershell • CLI • Powershell ISE • GUI

  50. Powershell – Help • Powershell is somewhat similar to BASH • You will still get somewhat lost • This is expected • It is also expected that you will work out the small details in lab

More Related