shells, environment, scripting, and bash · typing commands into the bash shell and running a bash...
TRANSCRIPT
Shells,Environment,Scripting,andBash
(in80minutes[!])
PatPannuto/MarcusDarden
Q:Howdoesaprogramstart?
Q:Howdoesaprogramstart?Thejobsofashell
Spawn(launch)newprogramsHandleinputandoutputtoprogramsKillandcleanupoldprograms
Q:Howdoesaprogramstart?Thejobsofashell
Spawn(launch)newprogramsHandleinputandoutputtoprogramsKillandcleanupoldprograms
Whatshellshaveyouused?
Let'spokearoundhowthe[Desktop]shellworks
$cp/usr/share/applications/firefox.desktop~/Desktop/
$chmod+x~/Desktop/firefox.desktop
Let'spokearoundhowthe[Desktop]shellworks
$cp/usr/share/applications/firefox.desktop~/Desktop/
$chmod+x~/Desktop/firefox.desktop
Whatmakesfirefox.desktop work?
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
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?"
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
Where'sfirefox anyway?$firefox#Thisworks
$gcchello.c-ohello#Thisworks
$hello#Thisdoesn't
$./hello#Thisworks
YourenvironmentaffectsprogrambehaviorEvenshells!(they'reaprogramtoo)
Changingtheenvironmentwillchangeprogrambehavior
Inthiscase,howashellperformsthesearchforprograms
$PATH=$PATH:/home/username/#Assuming"hello"isinthisfolder
$hello#Nowthisworks!
$PATH=/home/username#Whatifyou'ddonethisinstead?
Alsosawabriefexampleofenvironmentvariablesinlastweek'shomework
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
Nowwhataboutscripting?
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
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
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
ClosingremarksTryoneoftheAdvancedExercises
We'reattheendofSegment1
Reminder:YoumustsubmittostaffatOH