shells, environment, scripting, and bash · typing commands into the bash shell and running a bash...

17
Shells, Environment, Scripting, and Bash (in 80 minutes[!]) Pat Pannuto / Marcus Darden

Upload: others

Post on 12-Oct-2020

22 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Shells, Environment, Scripting, and Bash · Typing commands into the bash shell and running a bash script are the same $ cat test.sh echo "hello" > test cat test true && echo "hello"

Shells,Environment,Scripting,andBash

(in80minutes[!])

PatPannuto/MarcusDarden

Page 2: Shells, Environment, Scripting, and Bash · Typing commands into the bash shell and running a bash script are the same $ cat test.sh echo "hello" > test cat test true && echo "hello"

Q:Howdoesaprogramstart?

Page 3: Shells, Environment, Scripting, and Bash · Typing commands into the bash shell and running a bash script are the same $ cat test.sh echo "hello" > test cat test true && echo "hello"

Q:Howdoesaprogramstart?Thejobsofashell

Spawn(launch)newprogramsHandleinputandoutputtoprogramsKillandcleanupoldprograms

Page 4: Shells, Environment, Scripting, and Bash · Typing commands into the bash shell and running a bash script are the same $ cat test.sh echo "hello" > test cat test true && echo "hello"

Q:Howdoesaprogramstart?Thejobsofashell

Spawn(launch)newprogramsHandleinputandoutputtoprogramsKillandcleanupoldprograms

Whatshellshaveyouused?

Page 5: Shells, Environment, Scripting, and Bash · Typing commands into the bash shell and running a bash script are the same $ cat test.sh echo "hello" > test cat test true && echo "hello"

Let'spokearoundhowthe[Desktop]shellworks

$cp/usr/share/applications/firefox.desktop~/Desktop/

$chmod+x~/Desktop/firefox.desktop

Page 6: Shells, Environment, Scripting, and Bash · Typing commands into the bash shell and running a bash script are the same $ cat test.sh echo "hello" > test cat test true && echo "hello"

Let'spokearoundhowthe[Desktop]shellworks

$cp/usr/share/applications/firefox.desktop~/Desktop/

$chmod+x~/Desktop/firefox.desktop

Whatmakesfirefox.desktop work?

Page 7: Shells, Environment, Scripting, and Bash · Typing commands into the bash shell and running a bash script are the same $ cat test.sh echo "hello" > test cat test true && echo "hello"

Let'spokearoundhowthe[Desktop]shellworks

$cp/usr/share/applications/firefox.desktop~/Desktop/

$chmod+x~/Desktop/firefox.desktop

Whatmakesfirefox.desktop work?

Howdoesthe[desktop]shell:Spawn(launch)newprogramsHandleinputandoutputtoprogramsKillandcleanupoldprograms

Page 8: Shells, Environment, Scripting, and Bash · Typing commands into the bash shell and running a bash script are the same $ cat test.sh echo "hello" > test cat test true && echo "hello"

Let'spokearoundhowthe[bash]shellworks

$firefox

<Ctrl-C>

$firefox&

$jobs

$fg

<Ctrl-Z>

$bg

$echo"hello">test

$cattest

$true&&echo"hello"

$false&&echo"nope"||echo"whaaaat?"

Page 9: Shells, Environment, Scripting, and Bash · Typing commands into the bash shell and running a bash script are the same $ cat test.sh echo "hello" > test cat test true && echo "hello"

Let'spokearoundhowthe[bash]shellworks

$firefox

<Ctrl-C>

$firefox&

$jobs

$fg

<Ctrl-Z>

$bg

$echo"hello">test

$cattest

$true&&echo"hello"

$false&&echo"nope"||echo"whaaaat?"

Howdoesthe[bash]shell:Spawn(launch)newprogramsHandleinputandoutputtoprogramsKillandcleanupoldprograms

Page 10: Shells, Environment, Scripting, and Bash · Typing commands into the bash shell and running a bash script are the same $ cat test.sh echo "hello" > test cat test true && echo "hello"

Where'sfirefox anyway?$firefox#Thisworks

$gcchello.c-ohello#Thisworks

$hello#Thisdoesn't

$./hello#Thisworks

YourenvironmentaffectsprogrambehaviorEvenshells!(they'reaprogramtoo)

Page 11: Shells, Environment, Scripting, and Bash · Typing commands into the bash shell and running a bash script are the same $ cat test.sh echo "hello" > test cat test true && echo "hello"

Changingtheenvironmentwillchangeprogrambehavior

Inthiscase,howashellperformsthesearchforprograms

$PATH=$PATH:/home/username/#Assuming"hello"isinthisfolder

$hello#Nowthisworks!

$PATH=/home/username#Whatifyou'ddonethisinstead?

Alsosawabriefexampleofenvironmentvariablesinlastweek'shomework

Page 12: Shells, Environment, Scripting, and Bash · Typing commands into the bash shell and running a bash script are the same $ cat test.sh echo "hello" > test cat test true && echo "hello"

Yourprogramscanusetheenvironmenttoo

#include<iostream>

usingnamespacestd;

intmain(intargc,char*argv[],char*envp[]){

cout<<"argc:"<<argc<<endl;

cout<<"envp[0]:"<<envp[0]<<endl;

//while(*envp++){//Trythisone,too!

//cout<<*envp<<endl;

//}

return0;

}

$./a.out

$HELLO=world./a.out

$lower=finemany=okaytoo./a.out

$exportIamPermanent=ish

$./a.out

$#Tryuncommentingthewhileloop,didyoufindthemissingones?

$./a.out|less#Thismayexplainsomeofthefunnycolors

Page 13: Shells, Environment, Scripting, and Bash · Typing commands into the bash shell and running a bash script are the same $ cat test.sh echo "hello" > test cat test true && echo "hello"

Nowwhataboutscripting?

Page 14: Shells, Environment, Scripting, and Bash · Typing commands into the bash shell and running a bash script are the same $ cat test.sh echo "hello" > test cat test true && echo "hello"

Nowwhataboutscripting?Surprise!You'vebeenscriptingthiswholetime!

Typingcommandsintothebashshellandrunningabashscriptarethesame

$cattest.sh

echo"hello">test

cattest

true&&echo"hello"

false&&echo"nope"||echo"whaaaat?"

$chmod+xtest.sh#Whatisthisdoing?

$./test.sh

Page 15: Shells, Environment, Scripting, and Bash · Typing commands into the bash shell and running a bash script are the same $ cat test.sh echo "hello" > test cat test true && echo "hello"

Nowwhataboutscripting?Surprise!You'vebeenscriptingthiswholetime!

Typingcommandsintothebashshellandrunningabashscriptarethesame

$cattest.sh

echo"hello">test

cattest

true&&echo"hello"

false&&echo"nope"||echo"whaaaat?"

$chmod+xtest.sh#Whatisthisdoing?

$./test.sh

Howtowriteabashscript?

TrythingsoutintheterminalCopythingsthatworkintoafile($history)RunthatfileRepeat

Page 16: Shells, Environment, Scripting, and Bash · Typing commands into the bash shell and running a bash script are the same $ cat test.sh echo "hello" > test cat test true && echo "hello"

Bashisold...Butuseful,especiallyforreallyshortthings

ButhasuglyandfinickysyntaxVARIABLE=test != VARIABLE=test :(

Butrunningprogramsisreallyeasy(it'swhatitwasbuiltforafterall)g++-O3-m32thread.olibinterrupt.atest1.cpp-ldl-otest1

./test1

ButdoingmuchmoreistrickyValidateprogramoutput( diff ?),whatifitvaries?Ruleofthumb:Morethan50-100lines,morethanashellscript

Page 17: Shells, Environment, Scripting, and Bash · Typing commands into the bash shell and running a bash script are the same $ cat test.sh echo "hello" > test cat test true && echo "hello"

ClosingremarksTryoneoftheAdvancedExercises

We'reattheendofSegment1

Reminder:YoumustsubmittostaffatOH