ChapThe Bourne Graham Glass aUNIX for ProgramUNIX for ProgramThird Edition, PearsonOriginal Notes bygypter 8Again Shelland King Ables, mmers and Users,mmers and Users,n Prentice Hall, 2003.y Michael WeeksyBourne AgainBourne Again● Typical shell on Linux sysWritten by Brian Fox●Written by Brian Fox ● of the Free Software Fo● Backwards Compatible wi● Bourne Shell info applie● Runs /etc/profile first, thenp,● At login, runs ~/.bash_p● At startup, runs ~/.bashn SHell (bash)n SHell (bash)stemsoundationith Bourne Shelles here, too n:profilehrcVariaVaria● Setting and accessing vsame as before–same as before– For example● numbers=”one two”echo $numbers●echo $numbersablesablesvariablesArraArra● Defining arraysdeclarea teams–declare -a teams– teams[0]=”Hawks”– teams[1]=”Falcons”M{}●Must use { } to access$ echo The ${teams[1]} playThe Falcons play footballThe Falcons play football.aysaysy football.AccessinAccessin●Access all entries with Find the number of ent●Find the number of ent{#array[@]}$ echo ${teams[*]}HkFlHawks Falcons$ echo I know of ${#teams[*$ ec o o o ${#tea s[I know of 2 teams.ng Arraysng Arrays[*] / [@]ries with {#array[*]} /ries with {#array[ ]} / *]} teams.]} tea sBuildinBuildin● OptionsAssign entries with num–Assign entries with num– Give list in parenthesis● Use \ to continue a line, $ declare -a list1$ li t1[0] "Th h "$ $$ list1[0]="Thrashers"$ list1[1]="Braves"$ list1[2]="Hawks"$ list1[3]=Falcons$ > $ T$ list1[3]=Falcons$ echo ${list1[*]}Thrashers Braves Hawks FalconsTFalconsgListsg Listsmbermberas neededdeclare -a list2list2=("Thrashers" "Braves" \ "Hawks" Falcons)echo ${list2[*]}Th h B H k F lThrashers Braves Hawks FalconsDeletinDeletin● Command unsetDelete an entry or entir●Delete an entry or entir$ echo ${list2[*]}Thrashers Braves Hawks FalcThrashers Braves Hawks Falc$ unset list2[1]$ echo ${list2[*]}Thrashers Hawks Falcons$ unset list2$ echo ${list2[*]}$${[]}$ng Listsng Listsre listre listconsconsAliaAlia● Command alias definesThat is put these inba●That is, put these in.baalias qubit=”ssh -l myacct qubalias squbit=”sftp myacct@qubalias squbit= sftp myacct@qub$ alias qubit="ssh -l cscmcw qubit.$li$ aliasalias ls='ls --color=auto'alias qubit='ssh -l cscmcw qubit.cs$$ [email protected]'s passwLast login: Tue Oct 16 17:43:26 20-bash-3.00$asesasess commandsashrcashrcit.cs.gsu.edu”bit cs gsu edu”bit.cs.gsu.educs.gsu.edu".gsu.edu'word: 07 from carmauxHistHist● Bash keeps track of theSo do other shells e g–So do other shells, e.g. ● $HISTSIZE defines # c● Command history showtorytorye commands you typeKorn CshKorn, Cshcommands to remember ws listHistory SHistory S● !! repeats last command● !34 repeats command #34● !gre repeats the last command– Such as grep ...● ^str1^str2 – repeats last command, subs$ mroe myfile-bash: mroe: command not foun$ ^ro^ormore myfile...ShortcutsShortcuts starting with grestituting str1 for str2ndBash ArBash Ar● Instead of expr, use (( o+++*/%–+, -, ++, --, , /, %– <=, >=, <, >, ==, !=, !, &● Declare an integer– declare -i intName–Integers are faster to ev–Integers are faster to evrithmeticrithmeticoperation ))&&, ||valuatevaluateArithmeticArithmetic#!/bin/bash## Demonstrate some Bash a# Demonstrate some Bash a#declareix 10declare -i x=10while (( x > 0 ))doecho x is $x(( x--))(( x))donecExamplec ExamplearithmeticarithmeticAnotherAnother #!/bin/bash# find max area x*y, where x+y=2# find max area xy, where xy2declare -i x=20declare -i y declare -i maxarea=-1dlideclare -i areawhile (( $x > 0 )); doy=20ywhile (( $y > 0 )); do(( area = $x * $y ))if (( area > maxarea && x + ythenthenmaxarea=$area fi(( y--))(( y))done(( x-- ))donedoneecho "Max area is $maxarea"ExampleExample2020y == 20 ))If stateIf state●Allows “elif” (else if) as #!/bin/bash##echo "enter your favorite class: "read favoriteread favorite# Use " " around $favorite so it kee# words together# words together.if [ "$favorite" == "Systems Programecho "good choice." lif [ "$f it " "3320" ] thelif [ "$favorite" == "3320" ]; thenecho "good choice." elseecho "what are you thinking?!" fiementementwell as “else”eps multiplemming" ]; thenRevRev● Bash overviewCompatible with Bourne●Compatible with Bourne●Allows arraysAllows arrays● Has a history mechanis● Handles arithmetic wel● Control structures are vviewviewe shelle shellsm (as do others)lvery similar to
View Full Document