Scripting and Programming
Created Tuesday 23 February 2021
Shell Scripting
So far, we've been doing everything on the command line. What if we want to do 2 or more things?
& - run another command SIMULTANEOUSLY
&& run another command only after the first has completed
successfully.
|| run another command after the first FAILS
; - run another command after the first has completed
regardless of outcome
Scripting Cont'd
Well, you could also put a bunch of commands in one file, one command per line, and then run that file.
LETS GO!
Bash Scripting
Start with a SHEBANG!
#!/bin/bash
(note, # is also the comment delimiter)
End by saving AND chmodding
chmod +x scriptname.sh
FULLPATH!
Note, they must be run with a FULLPATH!
NOT scriptname.sh
But /home/user/scriptname.sh
(which can be shortened to ./scriptname.sh )
This is for your own good ☺
Variables
Set them without $, use them with $ (NO SPACES)
thingtoecho=“Hey, this will be echoed” echo $thingtoecho
Or “read” them from standard input
read $yourname echo “Whattup $yourname”
Whitespace
Bash is strongly oriented toward:
Whitespace (Spaces and tabs) as "Delimiters"
i.e.
Generally,
john r marks
Means a john then an r then a marks
NOT
john r marks as a unit ... unless
Notes on Quotes
“Double Quotes” – Print contents, expand variables
'Single Quotes' - Print contents LITERALLY
`backticks` - Execute command, put contents in quotes* (dont use this ever)
(also, backslashes “literalize” special chars)
ALWAYS put double quotes around called variables (dollar signed ones)
GOOD IDEA to do curly braces, too (esp if your output isn't spaced normally)
Notes on Quotes (get our REPL on)
Consider the following terrible idea:
"Date" is a command. It means, give the date.
(And time. And is a very powerful command)
So, what do the following do?
date=“eh, whenever”
- echo “date” echo 'date' echo `date`
- echo “$date” echo '$date' echo `$date`
Note, never use backticks, use $(command)
That functionality is super cool — it replaces a "command" with the output it would give as if it were a variable;
But do it with $(command) — (dollar sign + Parentheses)
and note this is different from
${variablename} - {dollar sign + curly braces}
which is just a way to set off the name of the variable if you want your output to be unspaced.
and of course you'll probably want double quotes around these
and possible related output as well (man, so much to think about)
Obvious Use of Variables
- echo "Hey, so, what's your username?"
- read username
- echo "you know, while you're at it, might as well give me your password."
- read password
- echo "WOW, so your username is $username and your password is $password."
- echo "thanks, sucker!"
- # Internal storage
- echo "New entry:" >> "/home/class/ListOfSuckers.txt"
- echo "Username:$username" >> "/home/class/ListOfSuckers.txt"
- echo "Password:$password" >> "/home/class/ListOfSuckers.txt"
- # The below will add a newline for us, to keep them visually separated.
- echo "" >> "/home/class/ListOfSuckers.txt"
But, lets clean up and make this more portable
- suckerlist="/home/class/ListOfSuckers.txt"
- echo "Hey, so, what's your username?"
- read username
- echo "you know, while you're at it, might as well give me your password."
- read password
- echo "WOW, so your username is $username and your password is $password."
- echo "thanks, sucker!"
- # Internal storage
- echo "New entry:" >> "$suckerlist"
- echo "Username:$username" >> "$suckerlist"
- echo "Password:$password" >> "$suckerlist"
- # The below will add a newline for us, to keep them visually separated.
- echo "" >> "$suckerlist"
Special Variables-Environment
Environment Variables - "Built into the system"
Usually all caps and contain system/shell info
(don't make your variables all caps)
SHELL, HOME, PATH (?), LOGNAME etc
Special Variables - Arguments
$1 is first, $2 is second, and so on.
$# is number of args, $* is all of them, eg
Create a command
Make a file called apologize_to.sh
echo “I'm sorry about all the password stuff, $1”
Usage: apologize_to.sh "Your name"
(Consider if it will do last or first, and how?)
Making decisions.
Now you're playing with power. Seriously
- if condition
- then
- do this
- then
- elif
- then
- do the other thing
- then
- else
- do this thing
- fi
Expressing conditions
True = 0; False = 1 (or anything else)
Generally just use double brackets:
- if $password == "password" ; then
- echo "Wow. You officially have the worst password in the world."
- else
- echo "Well, at least you're not using the worst password in the
- fi
Conditions with commands
In Bash, sometimes there's a slick way to use conditions with commands.
The general rule is, if a command HAS a result, it's true, if not, it's false.
- if grep “fish” petlist.txt; then
- echo “Looks like you got a fish!”
- else
- echo “sorry, no fish here”
- fi
While
“While” is very similar to if; it keeps repeating
the loop while a condition is true.
(while + read + “cat pipe” or “<” )
is very good for reading files
For
for variable in (range or list)
do
— NOTE, for obeys general argument rules. e.g.
for 1 2 3 — thus you can do slickness like
for $(seq 1..20) or for $(./folder/*)
Functions
Remember aliases and ?
All related to FUNCTIONS
,and they're so general and powerful
.. they're kind of hard to talk about. You can think of them as
their own commands, or programs and whatnot
..and arguably the most powerful and robust languages in
the world are "functional"
function say_hi_to () {
name=$1
echo "Hello $name"
}
echo "Hello