820 likes | 1k Views
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
E N D
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 • 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
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
Assumptions • We will not run in interactive mode • We will not have spaces in filenames • We will not use “special” characters
Tokens (reviewed) • A sequence of characters delineated by another character sequence • For us, delineator is white space • The delineator divides character sequence into words
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
I/O Redirection • stdin • Our terminal via keyboard • stdout • Our terminal • stderr • Our terminal
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
Redirect stdout • Command > FILE • Write to FILE • e.g., ls –l > myListing.txt • Problems with this?
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!
Redirect stdout • Command >> FILE • Append to FILE • e.g., echo “Second Part” >> myFile.txt • Just like “> FILE” if FILE doesn’t exist
Redirect stdin • Command < FILE • Read from FILE • e.g., myProgram < config.xml
> Or < • If you know C++ • It’s backwards • If you don’t know C++ • Just remember it
Piping • C1 | C2 • Output of C1 as input into C2 • e.g., ps –aux | grep vim • Indefinite piping • C1 | C2 | C3 | … | Cn
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 ‘=‘
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)
More examples file=myFile.txt echo ${file%txt}pdf // myFile.pdf
More examples file=myFile.txt echo ${file%txt}pdf // myFile.pdf tmp=test echo ${tmp#e} // test why?
More examples file=myFile.txt echo ${file%txt}pdf // myFile.pdf tmp=test echo ${tmp#e} // test why? Start and end…not middle!
Even more examples file=myFile files=junk echo $file.txt // myFile.txt
Even more examples file=myFile files=junk echo $file.txt // myFile.txt echo $files.txt // junk.txt
Even more examples file=myFile files=junk echo $file.txt // myFile.txt echo $files.txt // junk.txt echo ${file}s.txt //myFiles.txt
Control Structures • if-else-then • for • while • until • case
Syntax: if if CONDITION; then CMDS; fi -------------------OR--------------------- if CONDITION then cmds elif CONDITION then cmds fi
Example: if if [ -e /dev/sda1 ] then echo “Found it!” else echo “It’s missing!” fi
Syntax: for for VAR in LIST; do CMD; done ------------OR----------- for VARIABLE in LIST do COMMANDS done
Example: for #1 for number in 1 2 3 4 5 do echo $number done
Example: for #2 for ((i=0; i<=10; i++)) do echo $i done
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”
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
Example #1 for name in * do echo nm $name echo nm.txt ${name%.txt} echo nm.* ${name%.*} done What does this do?
Example #2 for (( i=23; i < 250; i+=12 )) do echo i is $i done What does this do?
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
BASH scripts • We’re going to avoid interactive mode • Means we’ll be putting scripts in files
Shebang • Every script file must start with shebang • Tells OS which shell to use • For us #!/bin/bash • Must be at top of file
Procedures • BASH procedures or shell functions • Same idea behind “regular” program functions • Must be declared before use
Procedures procedure_name () { BODY } • Where are the params?
Procedures procedure_name () { BODY } • Where are the params? • Hiding: $1, $2, etc
Procedures procedure_name () { BODY } • Where are the params? • Hiding: $1, $2, etc • Where is return type?
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
Procedures Example dotO () { for f in * do echo ${f%.o}.out done } echo Starting program dotO echo Ending program
Executing scripts • ./myScript.sh1 • . myScript.sh2 • source myScript.sh2 • File must be executable • Forces script to run in current process
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
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!
Script conventions • How do we know where to go back to?
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
Powershell • BASH is nifty, but sometimes we need Windows. • We used to do CMD • Now we do Powershell • Much more similar to BASH
Invoking PS • Powershell • CLI • Powershell ISE • GUI
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