cse 374 - week 3 (mon) scripting...cse 374 - week 3 (mon) scripting instructor: andrew hu tas: jim...
TRANSCRIPT
CSE 374 - Summer 2020
CSE 374 - Week 3 (Mon)Scripting
Instructor: Andrew Hu
TAs: Jim Limprasert, Kaelin Laundry, Keith Jiang, Nick Durand, Simon McFarlane, Yitian Hao
1
CSE 374 - Summer 2020
Administrivia
● Ex 5 is released today, first coding assignment
○ Start early!
● Spec is linked on the course website ("Ex 5 due" button)
● Autograded on Gradescope
● Any content that we don't cover today, we'll do on Wed
● No exercise for Wed
● HW 1 also released today, spec linked on course website2
CSE 374 - Summer 2020
Demo: Autograder Feedback
3
CSE 374 - Summer 2020
What is a Scripting Language?
● A language which automates the execution of tasks which
could alternatively be done manually, one at a time
● Bash is a shell, but also a scripting language
● Other languages used for scripting: Python, R, etc.
4
CSE 374 - Summer 2020
Simple Bash Script
● Simply put several bash commands in a text file
○ Convention is to end these files with .sh
● Execute by running sh myscript.sh
5
CSE 374 - Summer 2020
Interpreting Scripts (Sha-bang)
● Bash can run your scripts as if they were executables
○ Instead of sh myscript.sh
○ Just ./myscript.sh
● Add "#!" and the path to the interpreter as first line of script
○ #! /bin/sh (at the top of shell scripts)
○ #! /bin/python3 (at the top of a Python 3 script)
● # by itself starts a comment line
○ # This line will be ignored by Bash 6
CSE 374 - Summer 2020
Bash Permissions
● A file cannot be executed unless it has execute permissions
● All files support read, write, and execute permissions
● These permissions are different for the file's owner, group,
and others
● Change these permissions (modes) using chmod
○ e.g. chmod a+x myscript.sh "Let all users execute myscript.sh"
7
CSE 374 - Summer 2020
Demo: Straight Line Script, Permissions
8
CSE 374 - Summer 2020
Script Arguments
● Scripts can take arguments just like any other program
● Arguments are automatically named $1, $2, …
○ $1 is the first argument after the filename
● The script can accept any number of arguments (no limit)
9
CSE 374 - Summer 2020
Dealing with All Arguments
● Use $@ to get a space-separated list of all the arguments
● Use "$@"with quotes to make each argument quoted
○ Prevents args which were originally quoted from being read as multiple args
● To remove the first argument from $@, use shift
○ $1 goes away, $2 becomes $1, $3 becomes $2, etc.
○ shift n will apply shift n times
10
CSE 374 - Summer 2020
Demo: arglist, allargs, shift
11
CSE 374 - Summer 2020
Poll Question (PollEv.com/andrewhu)
What does ./script.sh foo "hear me out" output?
A. 1
B. 2
C. 3
D. 4
13
#! /bin/bash
shift
numargs $@
CSE 374 - Summer 2020
Control Flow
● It is possible to do conditionals and loops in bash
● However, the syntax is different!
● Beware of common pitfalls
○ Whitespace matters
○ if then fi
○ for in do done
○ while do done
14
CSE 374 - Summer 2020
If Statement
if ./myprogram args; then
command1 arg1 arg2 ...
command2 arg1 arg2 ...
...
fi
● Executes body if ./myprogram succeeds
○ i.e. if ./myprogram returns exit code 0
● The semicolon denotes the end of one command15
CSE 374 - Summer 2020
if grep -q -E 'myregex' file.txt; then
echo "Found it!"
fi
● Use the quiet (-q) option so your script doesn't print the
matches
If File Contains
16
CSE 374 - Summer 2020
Test Command
● test is a command that compares two arguments
● Check string equality○ test "$str1" == "$str2"
● Check if file exists○ test -f result.txt
● Check integer equality○ test $num -eq 0
● And many more17
CSE 374 - Summer 2020
Testing Conditions with If
● We can combine test with if
○ if test -f result.txt; then … fi
● Bash has handy syntax so we don't have to write out test
● Use [where you would say test
○ if [ -f result.txt ]; then … fi
● Be aware of the spaces around the brackets and the
semicolon
18
CSE 374 - Summer 2020
Demo: If Syntax
19
CSE 374 - Summer 2020
Which of these if statements has correct syntax?
20
if ("hello world" == $msg) then
echo "what a day"
fi
if "hello world" == "$msg"; then
echo "what a day"
fi
if ["hello world" == $msg]; then
echo "what a day"
fi
if [ "hello world" == "$msg" ]; then
echo "what a day"
fi
A. B.
D.C.
CSE 374 - Summer 2020
Bash Utility of the Day: wc
● Word Count
● Prints lines, words, and bytes in a file, along with the filename
● -c to print just the number of bytes (characters)
● If it reads from stdin, doesn't print filename
○ e.g. wc -c < file.txt outputs just "945"
● Aside: command substitution is allowed in double quotes
○ e.g. echo "$(wc -c < file.txt) bytes in this file"21
CSE 374 - Summer 2020
For Loop
● Bash for loops iterate over a list of strings
● Outputs:
22
for word in list of words
do
echo $word
done
list
of
words
CSE 374 - Summer 2020
Iterate Over Files
● Print every file in the current directory
for file in $(ls)
do
if [ -f $file ]; then
echo "$(wc -c < $file) bytes in $file"
fi
done
23
CSE 374 - Summer 2020
Demo: Looping Over Args
24
CSE 374 - Summer 2020
Aside: alias
● Create a globally visible one line command
○ alias cleantmp='rm -rf /tmp/*'
● To add them permanently, add that line to ~/.bashrc
25
CSE 374 - Summer 2020
Poll Question (PollEv.com/andrewhu)
How was the pace of this lecture?
A. Too fast
B. A little fast
C. About right
D. A little slow
E. Too slow
26