190 likes | 311 Views
Lecture 12: Bourne Shell ( ch 8). IT244 - Introduction to Linux / Unix Instructor: Bo Sheng. Expansion. History expansion !! Variable expansion echo $person echo ‘$person’ echo “$person’. Expansion. Tilde expansion echo ~/it244 echo ~ shengbo echo ~xx echo ~+ echo ~-. Expansion.
E N D
Lecture 12: Bourne Shell (ch 8) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng
Expansion • History expansion • !! • Variable expansion • echo $person • echo ‘$person’ • echo “$person’
Expansion • Tilde expansion • echo ~/it244 • echo ~shengbo • echo ~xx • echo ~+ • echo ~-
Expansion • Pathname/filename expansion • echo colors* • echo zzz* • echo “colors* $person” • echo ‘colors* $person’ • var=colors* • echo ‘$var’ • echo “$var” • echo $var
Expansion • Brace expansion • echo colors.{1,2} • mkdir test{1,2,3} • ls –F • Different from file reference • ls test[1-3] • mkdir quiz[1-3] • ls hello[5-6] • ls hello{5,6}
Expansion • Command substitution • $(command) • echo $(pwd) • mypwd=$(pwd)
Expansion • Arithmetic expansion • echo 1+1 • echo $((1+1)) • echo “there are $((60*60*24)) seconds in a day” • x=10;y=20 • echo $((x*2+y/4))
Expansion • Arithmetic expansion • cat > age_check >echo "how old are you?" >read age >echo "you'll be 60 in $((60-age)) years."
Expansion • Arithmetic expansion • wc –l colors.1 • wc –l colors.1 | cut –c1-2 • echo $(( $(wc –l colors.1 | cut –c1-2) + $(wc –l colors.2 | cut –c1-2) ))
Expansion • Order of expansion • tofile=“> log” • echo hello $tofile • a=hello* • echo “$a” • echo $a
Readline Completion • Command completion • ec[TAB] • bz[TAB] • bc[TAB] • Pathname completion • ls ~/it2[TAB]/hel[TAB] • Variable completion • echo $HO[TAB]
Aliases • Short names • alias myls=‘ls -a’ • No recursion • alias rm=‘rm –i’ • alias ls=‘ls -F’ • alias ll=‘ls –l’ • alias ls=‘ls -l’ • unaliasrm • alias ls=ls
Aliases • Inside quotes • echo $PWD • alias mypwd1=“pwd is $PWD” • alias mypwd2=‘pwd is $PWD’ • cd /home • mypwd1 • mypwd2
Re-execute and Edit Commands • Fix commands (fc) • fc -l • fc -l [first [last]] • fc –l 100 105 • fc –l 500 • fc –l echo ls • fc –l cat
Re-execute and Edit Commands • Fix commands (fc) • fc -s # • echo hello* • fc –l • fc -s # • fc -s hello=color #
Re-execute and Edit Commands • Exclamation point (!) • Table in Pg331 • !! • !498 • !-3 • !e • !?ch
Functions • What is a function? • function whoson() >{ >date >echo “Current Users:” >who >}
Functions • Compare a function to a script • Functions are stored in memory • Faster • No permissions • Functions are executed in the same shell • Remove a function • unset
Functions • A function and script with the same name • Shell executes the function first • Example echo “echo script”>my_script function my_script() { echo function } • A function and variable with the same name • unset removes the variable first