| 1 |
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook V3.1//EN" [ |
| 2 |
|
| 3 |
|
| 4 |
<!ENTITY INDEX00 SYSTEM "INDEX00.sgml"> |
| 5 |
<!ENTITY TABEXP SYSTEM "TABEXP.sgml"> |
| 6 |
<!ENTITY ex1 SYSTEM "ex1.sh"> |
| 7 |
<!ENTITY ex1a SYSTEM "ex1a.sh"> |
| 8 |
<!ENTITY ex2 SYSTEM "ex2.sh"> |
| 9 |
<!ENTITY ex3 SYSTEM "ex3.sh"> |
| 10 |
<!ENTITY ex4 SYSTEM "ex4.sh"> |
| 11 |
<!ENTITY ex5 SYSTEM "ex5.sh"> |
| 12 |
<!ENTITY ex6 SYSTEM "ex6.sh"> |
| 13 |
<!ENTITY ex7 SYSTEM "ex7.sh"> |
| 14 |
<!ENTITY ex8 SYSTEM "ex8.sh"> |
| 15 |
<!ENTITY ex9 SYSTEM "ex9.sh"> |
| 16 |
<!ENTITY ex10 SYSTEM "ex10.sh"> |
| 17 |
<!ENTITY ex11 SYSTEM "ex11.sh"> |
| 18 |
<!ENTITY ex12 SYSTEM "ex12.sh"> |
| 19 |
<!ENTITY ex13 SYSTEM "ex13.sh"> |
| 20 |
<!ENTITY ex14 SYSTEM "ex14.sh"> |
| 21 |
<!ENTITY ex15 SYSTEM "ex15.sh"> |
| 22 |
<!ENTITY ex16 SYSTEM "ex16.sh"> |
| 23 |
<!ENTITY ex17 SYSTEM "ex17.sh"> |
| 24 |
<!ENTITY ex18 SYSTEM "ex18.sh"> |
| 25 |
<!ENTITY ex19 SYSTEM "ex19.sh"> |
| 26 |
<!ENTITY ex20 SYSTEM "ex20.sh"> |
| 27 |
<!ENTITY ex21 SYSTEM "ex21.sh"> |
| 28 |
<!ENTITY ex22 SYSTEM "ex22.sh"> |
| 29 |
<!ENTITY ex22a SYSTEM "ex22a.sh"> |
| 30 |
<!ENTITY ex23 SYSTEM "ex23.sh"> |
| 31 |
<!ENTITY ex24 SYSTEM "ex24.sh"> |
| 32 |
<!ENTITY ex25 SYSTEM "ex25.sh"> |
| 33 |
<!ENTITY ex26 SYSTEM "ex26.sh"> |
| 34 |
<!ENTITY ex26a SYSTEM "ex26a.sh"> |
| 35 |
<!ENTITY ex27 SYSTEM "ex27.sh"> |
| 36 |
<!ENTITY ex28 SYSTEM "ex28.sh"> |
| 37 |
<!ENTITY ex29 SYSTEM "ex29.sh"> |
| 38 |
<!ENTITY ex30 SYSTEM "ex30.sh"> |
| 39 |
<!ENTITY ex30a SYSTEM "ex30a.sh"> |
| 40 |
<!ENTITY ex31 SYSTEM "ex31.sh"> |
| 41 |
<!ENTITY ex32 SYSTEM "ex32.sh"> |
| 42 |
<!ENTITY ex33 SYSTEM "ex33.sh"> |
| 43 |
<!ENTITY ex33a SYSTEM "ex33a.sh"> |
| 44 |
<!ENTITY ex34 SYSTEM "ex34.sh"> |
| 45 |
<!ENTITY ex35 SYSTEM "ex35.sh"> |
| 46 |
<!ENTITY ex36 SYSTEM "ex36.sh"> |
| 47 |
<!ENTITY ex37 SYSTEM "ex37.sh"> |
| 48 |
<!ENTITY ex38 SYSTEM "ex38.sh"> |
| 49 |
<!ENTITY ex38bis SYSTEM "data-file"> |
| 50 |
<!ENTITY ex39 SYSTEM "ex39.sh"> |
| 51 |
<!ENTITY ex40 SYSTEM "ex40.sh"> |
| 52 |
<!ENTITY ex41 SYSTEM "ex41.sh"> |
| 53 |
<!ENTITY ex42 SYSTEM "ex42.sh"> |
| 54 |
<!ENTITY ex43 SYSTEM "ex43.sh"> |
| 55 |
<!ENTITY ex44 SYSTEM "ex44.sh"> |
| 56 |
<!ENTITY ex45 SYSTEM "ex45.sh"> |
| 57 |
<!ENTITY ex45a SYSTEM "ex45a.sh"> |
| 58 |
<!ENTITY ex46 SYSTEM "ex46.sh"> |
| 59 |
<!ENTITY ex47 SYSTEM "ex47.sh"> |
| 60 |
<!ENTITY ex48 SYSTEM "ex48.sh"> |
| 61 |
<!ENTITY ex49 SYSTEM "ex49.sh"> |
| 62 |
<!ENTITY ex50 SYSTEM "ex50.sh"> |
| 63 |
<!ENTITY ex51 SYSTEM "ex51.sh"> |
| 64 |
<!ENTITY ex52 SYSTEM "ex52.sh"> |
| 65 |
<!ENTITY ex53 SYSTEM "ex53.sh"> |
| 66 |
<!ENTITY ex54 SYSTEM "ex54.sh"> |
| 67 |
<!ENTITY ex55 SYSTEM "ex55.sh"> |
| 68 |
<!ENTITY ex56 SYSTEM "ex56.sh"> |
| 69 |
<!ENTITY ex56py SYSTEM "ex56py.sh"> |
| 70 |
<!ENTITY ex57 SYSTEM "ex57.sh"> |
| 71 |
<!ENTITY ex58 SYSTEM "ex58.sh"> |
| 72 |
<!ENTITY ex59 SYSTEM "ex59.sh"> |
| 73 |
<!ENTITY ex60 SYSTEM "ex60.sh"> |
| 74 |
<!ENTITY ex61 SYSTEM "ex61.sh"> |
| 75 |
<!ENTITY ex62 SYSTEM "ex62.sh"> |
| 76 |
<!ENTITY ex63 SYSTEM "ex63.sh"> |
| 77 |
<!ENTITY ex64 SYSTEM "ex64.sh"> |
| 78 |
<!ENTITY ex65 SYSTEM "ex65.sh"> |
| 79 |
<!ENTITY ex66 SYSTEM "ex66.sh"> |
| 80 |
<!ENTITY ex67 SYSTEM "ex67.sh"> |
| 81 |
<!ENTITY ex68 SYSTEM "ex68.sh"> |
| 82 |
<!ENTITY ex68a SYSTEM "ex68a.sh"> |
| 83 |
<!ENTITY ex69 SYSTEM "ex69.sh"> |
| 84 |
<!ENTITY ex70 SYSTEM "ex70.sh"> |
| 85 |
<!ENTITY ex71 SYSTEM "ex71.sh"> |
| 86 |
<!ENTITY ex71a SYSTEM "ex71a.sh"> |
| 87 |
<!ENTITY ex71b SYSTEM "ex71b.sh"> |
| 88 |
<!ENTITY ex71c SYSTEM "ex71c.sh"> |
| 89 |
<!ENTITY ex72 SYSTEM "ex72.sh"> |
| 90 |
<!ENTITY ex73 SYSTEM "ex73.sh"> |
| 91 |
<!ENTITY ex74 SYSTEM "ex74.sh"> |
| 92 |
<!ENTITY ex75 SYSTEM "ex75.sh"> |
| 93 |
<!ENTITY ex76 SYSTEM "ex76.sh"> |
| 94 |
<!ENTITY ex77 SYSTEM "ex77.sh"> |
| 95 |
<!ENTITY ex78 SYSTEM "ex78.sh"> |
| 96 |
<!ENTITY cards SYSTEM "cards.sh"> |
| 97 |
<!ENTITY andor SYSTEM "and-or.sh"> |
| 98 |
<!ENTITY lnum SYSTEM "line-number.sh"> |
| 99 |
<!ENTITY manview SYSTEM "manview.sh"> |
| 100 |
<!ENTITY rfe SYSTEM "rfe.sh"> |
| 101 |
<!ENTITY behead SYSTEM "behead.sh"> |
| 102 |
<!ENTITY encryptedpw SYSTEM "encryptedpw.sh"> |
| 103 |
<!ENTITY rpmcheck SYSTEM "rpm-check.sh"> |
| 104 |
<!ENTITY subshell SYSTEM "subshell.sh"> |
| 105 |
<!ENTITY lowercase SYSTEM "lowercase.sh"> |
| 106 |
<!ENTITY online SYSTEM "online.sh"> |
| 107 |
<!ENTITY reply SYSTEM "reply.sh"> |
| 108 |
<!ENTITY seconds SYSTEM "seconds.sh"> |
| 109 |
<!ENTITY numbers SYSTEM "numbers.sh"> |
| 110 |
<!ENTITY indref SYSTEM "ind-ref.sh"> |
| 111 |
<!ENTITY bubble SYSTEM "bubble.sh"> |
| 112 |
<!ENTITY paramsub SYSTEM "param-sub.sh"> |
| 113 |
<!ENTITY restricted SYSTEM "restricted.sh"> |
| 114 |
<!ENTITY pw SYSTEM "pw.sh"> |
| 115 |
<!ENTITY rn SYSTEM "rn.sh"> |
| 116 |
<!ENTITY coltotaler SYSTEM "col-totaler.sh"> |
| 117 |
<!ENTITY coltotaler2 SYSTEM "col-totaler2.sh"> |
| 118 |
<!ENTITY coltotaler3 SYSTEM "col-totaler3.sh"> |
| 119 |
<!ENTITY tmdin SYSTEM "timed-input.sh"> |
| 120 |
<!ENTITY fifo SYSTEM "fifo.sh"> |
| 121 |
<!ENTITY tree SYSTEM "tree.sh"> |
| 122 |
<!ENTITY tree2 SYSTEM "tree2.sh"> |
| 123 |
<!ENTITY secretpw SYSTEM "secret-pw.sh"> |
| 124 |
<!ENTITY stripc SYSTEM "strip-comments.sh"> |
| 125 |
<!ENTITY al SYSTEM "alias.sh"> |
| 126 |
<!ENTITY unal SYSTEM "unalias.sh"> |
| 127 |
<!ENTITY redir1 SYSTEM "redir1.sh"> |
| 128 |
<!ENTITY redir2 SYSTEM "redir2.sh"> |
| 129 |
<!ENTITY redir2a SYSTEM "redir2a.sh"> |
| 130 |
<!ENTITY redir3 SYSTEM "redir3.sh"> |
| 131 |
<!ENTITY redir4 SYSTEM "redir4.sh"> |
| 132 |
<!ENTITY redir4a SYSTEM "redir4a.sh"> |
| 133 |
<!ENTITY redir5 SYSTEM "redir5.sh"> |
| 134 |
<!ENTITY wipedir SYSTEM "wipedir.sh"> |
| 135 |
<!ENTITY grp SYSTEM "grp.sh"> |
| 136 |
<!ENTITY killprocess SYSTEM "kill-process.sh"> |
| 137 |
<!ENTITY killbyname SYSTEM "kill-byname.sh"> |
| 138 |
<!ENTITY strtest SYSTEM "str-test.sh"> |
| 139 |
<!ENTITY colm SYSTEM "colm.sh"> |
| 140 |
<!ENTITY lookup SYSTEM "lookup.sh"> |
| 141 |
<!ENTITY arglist SYSTEM "arglist.sh"> |
| 142 |
<!ENTITY rot13 SYSTEM "rot13.sh"> |
| 143 |
<!ENTITY rot13a SYSTEM "rot13a.sh"> |
| 144 |
<!ENTITY rot14 SYSTEM "rot14.sh"> |
| 145 |
<!ENTITY filecomp SYSTEM "file-comparison.sh"> |
| 146 |
<!ENTITY adddrv SYSTEM "add-drive.sh"> |
| 147 |
<!ENTITY whloopc SYSTEM "wh-loopc.sh"> |
| 148 |
<!ENTITY forloopc SYSTEM "for-loopc.sh"> |
| 149 |
<!ENTITY forloopcmd SYSTEM "for-loopcmd.sh"> |
| 150 |
<!ENTITY cvars SYSTEM "c-vars.sh"> |
| 151 |
<!ENTITY bingrep SYSTEM "bin-grep.sh"> |
| 152 |
<!ENTITY mailformat SYSTEM "mail-format.sh"> |
| 153 |
<!ENTITY symlinks SYSTEM "symlinks.sh"> |
| 154 |
<!ENTITY symlinks2 SYSTEM "symlinks2.sh"> |
| 155 |
<!ENTITY string SYSTEM "string.sh"> |
| 156 |
<!ENTITY nestedloop SYSTEM "nested-loop.sh"> |
| 157 |
<!ENTITY casecmd SYSTEM "case-cmd.sh"> |
| 158 |
<!ENTITY uns SYSTEM "unset.sh"> |
| 159 |
<!ENTITY base SYSTEM "base.sh"> |
| 160 |
<!ENTITY allprofs SYSTEM "allprofs.sh"> |
| 161 |
<!ENTITY pidid SYSTEM "pid-identifier.sh"> |
| 162 |
<!ENTITY constat SYSTEM "connect-stat.sh"> |
| 163 |
<!ENTITY subpit SYSTEM "subshell-pitfalls.sh"> |
| 164 |
<!ENTITY readredir SYSTEM "read-redir.sh"> |
| 165 |
<!ENTITY andlist2 SYSTEM "and-list2.sh"> |
| 166 |
<!ENTITY qfunction SYSTEM "q-function.sh"> |
| 167 |
<!ENTITY viewdata SYSTEM "viewdata.sh"> |
| 168 |
<!ENTITY VIEWDAT SYSTEM "VIEWDATA.BAT"> |
| 169 |
<!ENTITY what SYSTEM "what.sh"> |
| 170 |
<!ENTITY max SYSTEM "max.sh"> |
| 171 |
<!ENTITY max2 SYSTEM "max2.sh"> |
| 172 |
<!ENTITY findstring SYSTEM "findstring.sh"> |
| 173 |
<!ENTITY listglob SYSTEM "list-glob.sh"> |
| 174 |
<!ENTITY realname SYSTEM "realname.sh"> |
| 175 |
<!ENTITY escaped SYSTEM "escaped.sh"> |
| 176 |
<!ENTITY fileinfo SYSTEM "file-info.sh"> |
| 177 |
<!ENTITY fileinfo01 SYSTEM "file-info01.sh"> |
| 178 |
<!ENTITY weirdvars SYSTEM "weirdvars.sh"> |
| 179 |
<!ENTITY breaklevels SYSTEM "break-levels.sh"> |
| 180 |
<!ENTITY copycd SYSTEM "copy-cd.sh"> |
| 181 |
<!ENTITY arithops SYSTEM "arith-ops.sh"> |
| 182 |
<!ENTITY continuelevels SYSTEM "continue-nlevel.sh"> |
| 183 |
<!ENTITY timeout SYSTEM "timeout.sh"> |
| 184 |
<!ENTITY randomtest SYSTEM "random-test.sh"> |
| 185 |
<!ENTITY seedingrandom SYSTEM "seeding-random.sh"> |
| 186 |
<!ENTITY pattmatching SYSTEM "patt-matching.sh"> |
| 187 |
<!ENTITY isalpha SYSTEM "isalpha.sh"> |
| 188 |
<!ENTITY rnd SYSTEM "rnd.sh"> |
| 189 |
<!ENTITY du SYSTEM "Du.sh"> |
| 190 |
<!ENTITY refparams SYSTEM "ref-params.sh"> |
| 191 |
<!ENTITY indfunc SYSTEM "ind-func.sh"> |
| 192 |
<!ENTITY primes SYSTEM "primes.sh"> |
| 193 |
<!ENTITY primes2 SYSTEM "primes2.sh"> |
| 194 |
<!ENTITY vartrace SYSTEM "vartrace.sh"> |
| 195 |
<!ENTITY amiroot SYSTEM "am-i-root.sh"> |
| 196 |
<!ENTITY twodim SYSTEM "twodim.sh"> |
| 197 |
<!ENTITY arithtests SYSTEM "arith-tests.sh"> |
| 198 |
<!ENTITY incompat SYSTEM "incompat.sh"> |
| 199 |
<!ENTITY ifsh SYSTEM "ifs.sh"> |
| 200 |
<!ENTITY ifsempty SYSTEM "ifs-empty.sh"> |
| 201 |
<!ENTITY logevents SYSTEM "logevents.sh"> |
| 202 |
<!ENTITY keypress SYSTEM "keypress.sh"> |
| 203 |
<!ENTITY ddkeypress SYSTEM "dd-keypress.sh"> |
| 204 |
<!ENTITY emptyarray SYSTEM "empty-array.sh"> |
| 205 |
<!ENTITY length SYSTEM "length.sh"> |
| 206 |
<!ENTITY monthlypmt SYSTEM "monthlypmt.sh"> |
| 207 |
<!ENTITY derpm SYSTEM "de-rpm.sh"> |
| 208 |
<!ENTITY blotout SYSTEM "blot-out.sh"> |
| 209 |
<!ENTITY readr SYSTEM "read-r.sh"> |
| 210 |
<!ENTITY cryptoquote SYSTEM "crypto-quote.sh"> |
| 211 |
<!ENTITY erase SYSTEM "erase.sh"> |
| 212 |
<!ENTITY returntest SYSTEM "return-test.sh"> |
| 213 |
<!ENTITY daysbetween SYSTEM "days-between.sh"> |
| 214 |
<!ENTITY varmatch SYSTEM "var-match.sh"> |
| 215 |
<!ENTITY recurse SYSTEM "recurse.sh"> |
| 216 |
<!ENTITY assert SYSTEM "assert.sh"> |
| 217 |
<!ENTITY intorstring SYSTEM "int-or-string.sh"> |
| 218 |
<!ENTITY ramdisk SYSTEM "ramdisk.sh"> |
| 219 |
<!ENTITY m4 SYSTEM "m4.sh"> |
| 220 |
<!ENTITY idelete SYSTEM "idelete.sh"> |
| 221 |
<!ENTITY matchstring SYSTEM "match-string.sh"> |
| 222 |
<!ENTITY bashandperl SYSTEM "bashandperl.sh"> |
| 223 |
<!ENTITY cvt SYSTEM "cvt.sh"> |
| 224 |
<!ENTITY wf SYSTEM "wf.sh"> |
| 225 |
<!ENTITY wf2 SYSTEM "wf2.sh"> |
| 226 |
<!ENTITY hypot SYSTEM "hypotenuse.sh"> |
| 227 |
<!ENTITY random2 SYSTEM "random2.sh"> |
| 228 |
<!ENTITY altbc SYSTEM "alt-bc.sh"> |
| 229 |
<!ENTITY substringex SYSTEM "substring-extraction.sh"> |
| 230 |
<!ENTITY stupscr SYSTEM "stupid-script-tricks.sh"> |
| 231 |
<!ENTITY resistor SYSTEM "resistor-inventory.sh"> |
| 232 |
<!ENTITY stackex SYSTEM "stack.sh"> |
| 233 |
<!ENTITY gcd SYSTEM "gcd.sh"> |
| 234 |
<!ENTITY selfmailer SYSTEM "self-mailer.sh"> |
| 235 |
<!ENTITY collatz SYSTEM "collatz.sh"> |
| 236 |
<!ENTITY wstrings SYSTEM "wstrings.sh"> |
| 237 |
<!ENTITY multiplication SYSTEM "multiplication.sh"> |
| 238 |
<!ENTITY sumproduct SYSTEM "sum-product.sh"> |
| 239 |
<!ENTITY userlist SYSTEM "userlist.sh"> |
| 240 |
<!ENTITY bgloop SYSTEM "background-loop.sh"> |
| 241 |
<!ENTITY tout SYSTEM "t-out.sh"> |
| 242 |
<!ENTITY csubloop SYSTEM "csubloop.sh"> |
| 243 |
<!ENTITY arrfunc SYSTEM "array-function.sh"> |
| 244 |
<!ENTITY lifeslow SYSTEM "life.sh"> |
| 245 |
<!ENTITY commentblock SYSTEM "commentblock.sh"> |
| 246 |
<!ENTITY selfdocument SYSTEM "self-document.sh"> |
| 247 |
<!ENTITY hf SYSTEM "here-function.sh"> |
| 248 |
<!ENTITY fileintegrity SYSTEM "file-integrity.sh"> |
| 249 |
<!ENTITY readnovar SYSTEM "read-novar.sh"> |
| 250 |
<!ENTITY setpos SYSTEM "set-pos.sh"> |
| 251 |
<!ENTITY revposparams SYSTEM "revposparams.sh"> |
| 252 |
<!ENTITY badread SYSTEM "badread.sh"> |
| 253 |
<!ENTITY selfexec SYSTEM "self-exec.sh"> |
| 254 |
<!ENTITY selfdestruct SYSTEM "self-destruct.sh"> |
| 255 |
<!ENTITY reassignstdout SYSTEM "reassign-stdout.sh"> |
| 256 |
<!ENTITY upperconv SYSTEM "upperconv.sh"> |
| 257 |
<!ENTITY pbook SYSTEM "pb.sh"> |
| 258 |
<!ENTITY makedict SYSTEM "makedict.sh"> |
| 259 |
<!ENTITY missingkeyword SYSTEM "missing-keyword.sh"> |
| 260 |
<!ENTITY blankrename SYSTEM "blank-rename.sh"> |
| 261 |
<!ENTITY scriptdetector SYSTEM "script-detector.sh"> |
| 262 |
<!ENTITY hexconvert SYSTEM "hexconvert.sh"> |
| 263 |
<!ENTITY factr SYSTEM "factr.sh"> |
| 264 |
<!ENTITY cannon SYSTEM "cannon.sh"> |
| 265 |
<!ENTITY agram SYSTEM "agram.sh"> |
| 266 |
<!ENTITY agram2 SYSTEM "agram2.sh"> |
| 267 |
<!ENTITY poem SYSTEM "poem.sh"> |
| 268 |
<!ENTITY soundex SYSTEM "soundex.sh"> |
| 269 |
<!ENTITY tempfilename SYSTEM "tempfile-name.sh"> |
| 270 |
<!ENTITY unitconversion SYSTEM "unit-conversion.sh"> |
| 271 |
<!ENTITY usagemessage SYSTEM "usage-message.sh"> |
| 272 |
<!ENTITY colorecho SYSTEM "color-echo.sh"> |
| 273 |
<!ENTITY selfsource SYSTEM "self-source.sh"> |
| 274 |
<!ENTITY selfcopy SYSTEM "self-copy.sh"> |
| 275 |
<!ENTITY arrowdetect SYSTEM "arrow-detect.sh"> |
| 276 |
<!ENTITY paragraphspace SYSTEM "paragraph-space.sh"> |
| 277 |
<!ENTITY brokenlink SYSTEM "broken-link.sh"> |
| 278 |
<!ENTITY continuenex SYSTEM "continue-n.example"> |
| 279 |
<!ENTITY pickcard SYSTEM "pick-card.sh"> |
| 280 |
<!ENTITY copyarray SYSTEM "CopyArray.sh"> |
| 281 |
<!ENTITY directoryinfo SYSTEM "directory-info.sh"> |
| 282 |
<!ENTITY embarr SYSTEM "embedded-arrays.sh"> |
| 283 |
<!ENTITY generatescript SYSTEM "generate-script.sh"> |
| 284 |
<!ENTITY scriptarray SYSTEM "script-array.sh"> |
| 285 |
<!ENTITY randombetween SYSTEM "random-between.sh"> |
| 286 |
<!ENTITY arrayops SYSTEM "array-ops.sh"> |
| 287 |
<!ENTITY arraystrops SYSTEM "array-strops.sh"> |
| 288 |
<!ENTITY arrayappend SYSTEM "array-append.bash"> |
| 289 |
<!ENTITY arrayassign SYSTEM "array-assign.bash"> |
| 290 |
<!ENTITY lettercount SYSTEM "letter-count.sh"> |
| 291 |
<!ENTITY lettercount2 SYSTEM "letter-count2.sh"> |
| 292 |
<!ENTITY protectliteral SYSTEM "protect_literal.sh"> |
| 293 |
<!ENTITY unprotectliteral SYSTEM "unprotect_literal.sh"> |
| 294 |
<!ENTITY usbinst SYSTEM "usb.sh"> |
| 295 |
<!ENTITY speech0 SYSTEM "speech.sh"> |
| 296 |
<!ENTITY basicsreviewed SYSTEM "basics-reviewed.bash"> |
| 297 |
<!ENTITY readpipe SYSTEM "readpipe.sh"> |
| 298 |
<!ENTITY usrmnt SYSTEM "usrmnt.sh"> |
| 299 |
<!ENTITY stopwatch SYSTEM "sw.sh"> |
| 300 |
<!ENTITY dialog SYSTEM "dialog.sh"> |
| 301 |
<!ENTITY hellol SYSTEM "hello.sh"> |
| 302 |
<!ENTITY hanoi SYSTEM "hanoi.bash"> |
| 303 |
<!ENTITY hanoi2 SYSTEM "hanoi2.bash"> |
| 304 |
<!ENTITY hanoi2a SYSTEM "hanoi2a.bash"> |
| 305 |
<!ENTITY horserace SYSTEM "horserace.sh"> |
| 306 |
<!ENTITY remote SYSTEM "remote.bash"> |
| 307 |
<!ENTITY musicscr SYSTEM "music.sh"> |
| 308 |
<!ENTITY prependex SYSTEM "prepend.sh"> |
| 309 |
<!ENTITY setnewpw SYSTEM "setnew-passwd.sh"> |
| 310 |
<!ENTITY badop SYSTEM "bad-op.sh"> |
| 311 |
<!ENTITY dereferencecl SYSTEM "dereference.sh"> |
| 312 |
<!ENTITY archiveweblogs SYSTEM "archiveweblogs.sh"> |
| 313 |
<!ENTITY devtcp SYSTEM "dev-tcp.sh"> |
| 314 |
<!ENTITY multipleproc SYSTEM "multiple-processes.sh"> |
| 315 |
<!ENTITY funccmdlinearg SYSTEM "func-cmdlinearg.sh"> |
| 316 |
<!ENTITY isspammer SYSTEM "is-spammer.sh"> |
| 317 |
<!ENTITY isspammer2 SYSTEM "is_spammer.bash"> |
| 318 |
<!ENTITY wgetter2 SYSTEM "wgetter2.bash"> |
| 319 |
<!ENTITY exercisingdd SYSTEM "exercising-dd.sh"> |
| 320 |
<!ENTITY quotefetch SYSTEM "quote-fetch.sh"> |
| 321 |
<!ENTITY avoidsubshell SYSTEM "avoid-subshell.sh"> |
| 322 |
<!ENTITY loggingwrapper SYSTEM "logging-wrapper.sh"> |
| 323 |
<!ENTITY dictlookup SYSTEM "dict-lookup.sh"> |
| 324 |
<!ENTITY bashpodder SYSTEM "bashpodder.sh"> |
| 325 |
<!ENTITY drawbox SYSTEM "Draw-box.sh"> |
| 326 |
<!ENTITY testcgi SYSTEM "test-cgi.sh"> |
| 327 |
<!ENTITY spawnscr SYSTEM "spawn.sh"> |
| 328 |
<!ENTITY iscan SYSTEM "iscan.sh"> |
| 329 |
<!ENTITY ra2ogg SYSTEM "ra2ogg.sh"> |
| 330 |
<!ENTITY namesdata SYSTEM "names.data"> |
| 331 |
<!ENTITY hashlib SYSTEM "Hash.lib"> |
| 332 |
<!ENTITY hashexample SYSTEM "hash-example.sh"> |
| 333 |
<!ENTITY hashex2 SYSTEM "ha.sh"> |
| 334 |
<!ENTITY getoptsimple SYSTEM "getopt-simple.sh"> |
| 335 |
<!ENTITY usegetopt SYSTEM "UseGetOpt.sh"> |
| 336 |
<!ENTITY usegetopt2 SYSTEM "UseGetOpt-2.sh"> |
| 337 |
<!ENTITY usegetoptex SYSTEM "UseGetOpt-2"> |
| 338 |
<!ENTITY insertionsort SYSTEM "insertion-sort.bash"> |
| 339 |
<!ENTITY findsplit SYSTEM "find-splitpara.sh"> |
| 340 |
<!ENTITY prasc SYSTEM "pr-asc.sh"> |
| 341 |
<!ENTITY whx SYSTEM "whx.sh"> |
| 342 |
<!ENTITY fifteen SYSTEM "fifteen.sh"> |
| 343 |
<!ENTITY spamlookup SYSTEM "spam-lookup.sh"> |
| 344 |
<!ENTITY testsuite SYSTEM "test-suite.sh"> |
| 345 |
<!ENTITY mailboxgrep SYSTEM "mailbox_grep.sh"> |
| 346 |
<!ENTITY fc4upd SYSTEM "fc4upd.sh"> |
| 347 |
<!ENTITY ipscript SYSTEM "ip.sh"> |
| 348 |
<!ENTITY nightlybackup SYSTEM "nightly-backup.sh"> |
| 349 |
<!ENTITY echoparams SYSTEM "echo-params.sh"> |
| 350 |
<!ENTITY randstring SYSTEM "rand-string.sh"> |
| 351 |
<!ENTITY splitcopy SYSTEM "splitcopy.sh"> |
| 352 |
<!ENTITY padsw SYSTEM "pad.sh"> |
| 353 |
<!ENTITY soundcardon SYSTEM "soundcard-on.sh"> |
| 354 |
<!ENTITY recursiondemo SYSTEM "recursion-demo.sh"> |
| 355 |
<!ENTITY recursiondemo2 SYSTEM "recursion-def.sh"> |
| 356 |
<!ENTITY cwsolver SYSTEM "cw-solver.sh"> |
| 357 |
<!ENTITY progressbar SYSTEM "progress-bar.sh"> |
| 358 |
<!ENTITY progressbar2 SYSTEM "progress-bar2.sh"> |
| 359 |
<!ENTITY tohtml SYSTEM "tohtml.sh"> |
| 360 |
<!ENTITY datecalc SYSTEM "date-calc.sh"> |
| 361 |
<!ENTITY brownian SYSTEM "brownian.sh"> |
| 362 |
<!ENTITY fromsh SYSTEM "from.sh"> |
| 363 |
<!ENTITY fibo SYSTEM "fibo.sh"> |
| 364 |
<!ENTITY homework SYSTEM "homework.sh"> |
| 365 |
<!ENTITY arrchoice SYSTEM "arr-choice.sh"> |
| 366 |
<!ENTITY bashek SYSTEM "BashExtraKeys.sh"> |
| 367 |
<!ENTITY fetchaddress SYSTEM "fetch_address.sh"> |
| 368 |
<!ENTITY fetchaddress2 SYSTEM "fetch_address-2.sh"> |
| 369 |
<!ENTITY case4 SYSTEM "case4.sh"> |
| 370 |
<!ENTITY asciish SYSTEM "ascii.sh"> |
| 371 |
<!ENTITY ascii2sh SYSTEM "ascii2.sh"> |
| 372 |
<!ENTITY ascii3sh SYSTEM "ascii3.sh"> |
| 373 |
<!ENTITY petals SYSTEM "petals.sh"> |
| 374 |
<!ENTITY qky SYSTEM "qky.sh"> |
| 375 |
<!ENTITY maned SYSTEM "maned.sh"> |
| 376 |
<!ENTITY ktour SYSTEM "ktour.sh"> |
| 377 |
<!ENTITY msquare SYSTEM "msquare.sh"> |
| 378 |
<!ENTITY readn SYSTEM "read-N.sh"> |
| 379 |
<!ENTITY herecommsub SYSTEM "here-commsub.sh"> |
| 380 |
<!ENTITY negarray SYSTEM "neg-array.sh"> |
| 381 |
<!ENTITY negoffset SYSTEM "neg-offset.sh"> |
| 382 |
<!ENTITY lastpipeopt SYSTEM "lastpipe-option.sh"> |
| 383 |
<!ENTITY nim SYSTEM "nim.sh"> |
| 384 |
<!ENTITY psubp SYSTEM "psub.bash"> |
| 385 |
<!ENTITY base64 SYSTEM "base64.sh"> |
| 386 |
<!ENTITY bingo SYSTEM "bingo.sh"> |
| 387 |
<!ENTITY wrps SYSTEM "wr-ps.bash"> |
| 388 |
<!ENTITY ipaddresses SYSTEM "ip-addresses.sh"> |
| 389 |
<!ENTITY assocarrtest SYSTEM "assoc-arr-test.sh"> |
| 390 |
<!ENTITY stddev SYSTEM "sd.sh"> |
| 391 |
<!ENTITY samorse SYSTEM "sam.sh"> |
| 392 |
<!ENTITY backlight SYSTEM "backlight.sh"> |
| 393 |
<!ENTITY showallc SYSTEM "show-all-colors.sh"> |
| 394 |
<!ENTITY sedappend SYSTEM "sedappend.sh"> |
| 395 |
<!ENTITY gronsfeld SYSTEM "gronsfeld.bash"> |
| 396 |
<!ENTITY rpsdcard SYSTEM "rp.sdcard.sh"> |
| 397 |
<!ENTITY testexectime SYSTEM "test-execution-time.sh"> |
| 398 |
<!ENTITY gen0data SYSTEM "gen0"> |
| 399 |
<!ENTITY cdll SYSTEM "cdll"> |
| 400 |
<!ENTITY bashrc SYSTEM "bashrc"> |
| 401 |
<!ENTITY bashprof SYSTEM "bash-profile.snippet"> |
| 402 |
<!ENTITY opprectable SYSTEM "opprec-table.sgml"> |
| 403 |
]> |
| 404 |
|
| 405 |
<book> |
| 406 |
<bookinfo> |
| 407 |
<title>Advanced Bash-Scripting Guide</title> |
| 408 |
<subtitle>An in-depth exploration of the art of shell scripting</subtitle> |
| 409 |
|
| 410 |
|
| 411 |
<author> |
| 412 |
<firstname>Mendel</firstname> |
| 413 |
<surname>Cooper</surname> |
| 414 |
<affiliation> |
| 415 |
<orgname></orgname> |
| 416 |
<address><email>[email protected]</email></address> |
| 417 |
</affiliation> |
| 418 |
</author> |
| 419 |
|
| 420 |
|
| 421 |
<releaseinfo>10</releaseinfo> |
| 422 |
<pubdate>10 Mar 2014</pubdate> |
| 423 |
<isbn>978-1-4357-5219-1</isbn> |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
<revhistory> |
| 428 |
|
| 429 |
<revision> |
| 430 |
<revnumber>6.5</revnumber> |
| 431 |
<date>05 Apr 2012</date> |
| 432 |
<authorinitials>mc</authorinitials> |
| 433 |
<revremark>'TUNGSTENBERRY' release</revremark> |
| 434 |
</revision> |
| 435 |
|
| 436 |
<revision> |
| 437 |
<revnumber>6.6</revnumber> |
| 438 |
<date>27 Nov 2012</date> |
| 439 |
<authorinitials>mc</authorinitials> |
| 440 |
<revremark>'YTTERBIUMBERRY' release</revremark> |
| 441 |
</revision> |
| 442 |
|
| 443 |
<revision> |
| 444 |
<revnumber>10</revnumber> |
| 445 |
<date>10 Mar 2014</date> |
| 446 |
<authorinitials>mc</authorinitials> |
| 447 |
<revremark>'PUBLICDOMAIN' release</revremark> |
| 448 |
</revision> |
| 449 |
|
| 450 |
</revhistory> |
| 451 |
|
| 452 |
|
| 453 |
<abstract> |
| 454 |
|
| 455 |
<para>This tutorial assumes no previous knowledge of |
| 456 |
scripting or programming, yet progresses rapidly toward an |
| 457 |
intermediate/advanced level of instruction <emphasis>. . . all |
| 458 |
the while sneaking in little nuggets of <trademark |
| 459 |
class=registered>UNIX</trademark> wisdom and lore</emphasis>. It |
| 460 |
serves as a textbook, a manual for self-study, and as a reference and |
| 461 |
source of knowledge on shell scripting techniques. The exercises |
| 462 |
and heavily-commented examples invite active reader participation, |
| 463 |
under the premise that <userinput>the only way to really learn |
| 464 |
scripting is to write scripts</userinput>.</para> |
| 465 |
|
| 466 |
<para>This book is suitable for classroom use as a |
| 467 |
general introduction to programming concepts.</para> |
| 468 |
|
| 469 |
<para>This document is herewith granted to the Public Domain. |
| 470 |
<userinput>No copyright!</userinput></para> |
| 471 |
|
| 472 |
</abstract> |
| 473 |
</bookinfo> |
| 474 |
|
| 475 |
<dedication> |
| 476 |
<para>For Anita, the source of all the magic</para> |
| 477 |
</dedication> |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
<part label="Part 1" id="part1"> |
| 482 |
<title>Introduction</title> |
| 483 |
|
| 484 |
<partintro> |
| 485 |
|
| 486 |
<epigraph> |
| 487 |
<para>Script: <emphasis>A writing; a written |
| 488 |
document. [Obs.]</emphasis></para> |
| 489 |
<para>--<emphasis>Webster's Dictionary</emphasis>, 1913 ed.</para> |
| 490 |
</epigraph> |
| 491 |
|
| 492 |
<para><anchor id="whatsascript"></para> |
| 493 |
|
| 494 |
<para>The shell is a command interpreter. More than just the |
| 495 |
insulating layer between the operating system kernel and the user, |
| 496 |
it's also a fairly powerful programming language. A shell program, |
| 497 |
called a <firstterm>script</firstterm>, is an easy-to-use tool for |
| 498 |
building applications by <quote>gluing together</quote> system |
| 499 |
calls, tools, utilities, and compiled binaries. Virtually the |
| 500 |
entire repertoire of UNIX commands, utilities, and tools is |
| 501 |
available for invocation by a shell script. If that were |
| 502 |
not enough, internal shell commands, such as testing and loop |
| 503 |
constructs, lend additional power and flexibility to scripts. |
| 504 |
Shell scripts are especially well suited for administrative |
| 505 |
system tasks and other routine repetitive tasks not requiring the |
| 506 |
bells and whistles of a full-blown tightly structured programming |
| 507 |
language.</para> |
| 508 |
|
| 509 |
</partintro> |
| 510 |
|
| 511 |
<chapter id="why-shell"> |
| 512 |
<title>Shell Programming!</title> |
| 513 |
|
| 514 |
<epigraph> |
| 515 |
<para>No programming language is perfect. There is not even a single |
| 516 |
best language; there are only languages well suited or perhaps |
| 517 |
poorly suited for particular purposes.</para> |
| 518 |
<para>--Herbert Mayer</para> |
| 519 |
</epigraph> |
| 520 |
|
| 521 |
|
| 522 |
<para>A working knowledge of shell scripting is essential to anyone |
| 523 |
wishing to become reasonably proficient at system administration, |
| 524 |
even if they do not anticipate ever having to actually write a |
| 525 |
script. Consider that as a Linux machine boots up, it executes the |
| 526 |
shell scripts in <filename class="directory">/etc/rc.d</filename> |
| 527 |
to restore the system configuration and set up services. A detailed |
| 528 |
understanding of these startup scripts is important for analyzing |
| 529 |
the behavior of a system, and possibly modifying it.</para> |
| 530 |
|
| 531 |
<para>The craft of scripting is not hard to master, |
| 532 |
since scripts can be built in bite-sized sections and there |
| 533 |
is only a fairly small set of shell-specific operators and options |
| 534 |
|
| 535 |
<footnote><para>These are referred to as <link |
| 536 |
linkend="builtinref">builtins</link>, features internal to the |
| 537 |
shell.</para></footnote> |
| 538 |
|
| 539 |
to learn. The syntax is simple -- even austere -- similar to |
| 540 |
that of invoking and chaining together utilities at the command |
| 541 |
line, and there are only a few <quote>rules</quote> governing |
| 542 |
their use. Most short scripts work right the first time, and |
| 543 |
debugging even the longer ones is straightforward.</para> |
| 544 |
|
| 545 |
|
| 546 |
<para> |
| 547 |
<blockquote> |
| 548 |
<literallayout> |
| 549 |
In the early days of personal computing, the BASIC language enabled |
| 550 |
anyone reasonably computer proficient to write programs on an early |
| 551 |
generation of microcomputers. Decades later, the Bash scripting |
| 552 |
language enables anyone with a rudimentary knowledge of Linux or |
| 553 |
UNIX to do the same on modern machines. |
| 554 |
|
| 555 |
We now have miniaturized single-board computers with amazing |
| 556 |
capabilities, such as the <ulink url="http://www.raspberrypi.org/">Raspberry Pi</ulink>. |
| 557 |
Bash scripting provides a way to explore the capabilities of these |
| 558 |
fascinating devices. |
| 559 |
</literallayout> |
| 560 |
</blockquote> |
| 561 |
</para> |
| 562 |
|
| 563 |
|
| 564 |
<para>A shell script is a quick-and-dirty method of prototyping |
| 565 |
a complex application. Getting even a limited subset of |
| 566 |
the functionality to work in a script is often a useful |
| 567 |
first stage in project development. In this way, the structure |
| 568 |
of the application can be tested and tinkered with, and the |
| 569 |
major pitfalls found before proceeding to the final coding |
| 570 |
in <firstterm>C</firstterm>, <firstterm>C++</firstterm>, |
| 571 |
<firstterm>Java</firstterm>, <link linkend="perlref">Perl</link>, |
| 572 |
or <firstterm>Python</firstterm>.</para> |
| 573 |
|
| 574 |
<para>Shell scripting hearkens back to the classic UNIX philosophy |
| 575 |
of breaking complex projects into simpler subtasks, of chaining |
| 576 |
together components and utilities. Many consider this a better, |
| 577 |
or at least more esthetically pleasing approach to problem solving |
| 578 |
than using one of the new generation of high-powered all-in-one |
| 579 |
languages, such as <firstterm>Perl</firstterm>, which attempt to |
| 580 |
be all things to all people, but at the cost of forcing you to |
| 581 |
alter your thinking processes to fit the tool.</para> |
| 582 |
|
| 583 |
<para>According to <link linkend="mayerref">Herbert Mayer</link>, |
| 584 |
<quote>a useful language needs arrays, pointers, |
| 585 |
and a generic mechanism for building data structures.</quote> |
| 586 |
By these criteria, shell scripting falls somewhat short of being |
| 587 |
<quote>useful.</quote> Or, perhaps not. . . .</para> |
| 588 |
|
| 589 |
|
| 590 |
<sidebar> |
| 591 |
|
| 592 |
<para>When not to use shell scripts |
| 593 |
|
| 594 |
<itemizedlist> |
| 595 |
|
| 596 |
<listitem> |
| 597 |
<para>Resource-intensive tasks, especially where speed is |
| 598 |
a factor (sorting, hashing, recursion |
| 599 |
|
| 600 |
<footnote><para>Although <link linkend="recursionref0">recursion |
| 601 |
<emphasis>is</emphasis> possible in a shell script</link>, |
| 602 |
it tends to be slow and its implementation is often |
| 603 |
an <link linkend="fiboref">ugly kludge</link>. |
| 604 |
</para></footnote> |
| 605 |
|
| 606 |
...)</para> |
| 607 |
|
| 608 |
</listitem> <listitem> |
| 609 |
<para>Procedures involving heavy-duty math operations, |
| 610 |
especially floating point arithmetic, arbitrary |
| 611 |
precision calculations, or complex numbers (use |
| 612 |
<firstterm>C++</firstterm> or <firstterm>FORTRAN</firstterm> |
| 613 |
instead)</para> |
| 614 |
</listitem> <listitem> |
| 615 |
<para>Cross-platform portability required (use |
| 616 |
<firstterm>C</firstterm> or <firstterm>Java</firstterm> |
| 617 |
instead)</para> |
| 618 |
</listitem> <listitem> |
| 619 |
<para>Complex applications, where structured programming is |
| 620 |
a necessity (type-checking of variables, function |
| 621 |
prototypes, etc.)</para> |
| 622 |
</listitem> <listitem> |
| 623 |
<para>Mission-critical applications upon which you are betting the |
| 624 |
future of the company</para> |
| 625 |
</listitem> <listitem> |
| 626 |
<para>Situations where <emphasis>security</emphasis> is |
| 627 |
important, where you need to guarantee the integrity of |
| 628 |
your system and protect against intrusion, cracking, and |
| 629 |
vandalism</para> |
| 630 |
</listitem> <listitem> |
| 631 |
<para>Project consists of subcomponents with interlocking |
| 632 |
dependencies</para> |
| 633 |
</listitem> <listitem> |
| 634 |
<para>Extensive file operations required |
| 635 |
(<firstterm>Bash</firstterm> is limited to serial file access, |
| 636 |
and that only in a particularly clumsy and inefficient |
| 637 |
line-by-line fashion.)</para> |
| 638 |
</listitem> <listitem> |
| 639 |
<para>Need native support for multi-dimensional arrays</para> |
| 640 |
</listitem> <listitem> |
| 641 |
<para>Need data structures, such as linked lists or trees</para> |
| 642 |
</listitem> <listitem> |
| 643 |
<para>Need to generate / manipulate graphics or GUIs</para> |
| 644 |
</listitem> <listitem> |
| 645 |
<para>Need direct access to system hardware or |
| 646 |
external peripherals</para> |
| 647 |
</listitem> <listitem> |
| 648 |
<para>Need port or <link linkend="socketref">socket</link> |
| 649 |
I/O</para> |
| 650 |
</listitem> <listitem> |
| 651 |
<para>Need to use libraries or interface with legacy code</para> |
| 652 |
</listitem> <listitem> |
| 653 |
<para>Proprietary, closed-source applications (Shell scripts |
| 654 |
put the source code right out in the open for all the world |
| 655 |
to see.)</para> |
| 656 |
|
| 657 |
</listitem> |
| 658 |
</itemizedlist></para> |
| 659 |
|
| 660 |
<para>If any of the above applies, consider a more powerful scripting |
| 661 |
language -- perhaps <firstterm>Perl</firstterm>, |
| 662 |
<firstterm>Tcl</firstterm>, <firstterm>Python</firstterm>, |
| 663 |
<firstterm>Ruby</firstterm> -- or possibly a |
| 664 |
compiled language such as <firstterm>C</firstterm>, |
| 665 |
<firstterm>C++</firstterm>, or <firstterm>Java</firstterm>. Even |
| 666 |
then, prototyping the application as a shell script might still |
| 667 |
be a useful development step.</para> |
| 668 |
|
| 669 |
</sidebar> |
| 670 |
|
| 671 |
|
| 672 |
<para><anchor id="bashdef"></para> |
| 673 |
<para>We will be using <acronym>Bash</acronym>, an acronym |
| 674 |
|
| 675 |
<footnote><para>An <firstterm>acronym</firstterm> |
| 676 |
is an <emphasis>ersatz</emphasis> word formed by pasting |
| 677 |
together the initial letters of the words into a tongue-tripping |
| 678 |
phrase. This morally corrupt and pernicious practice |
| 679 |
deserves appropriately severe punishment. Public |
| 680 |
flogging suggests itself.</para></footnote> |
| 681 |
|
| 682 |
for <quote>Bourne-Again shell</quote> and a pun on Stephen Bourne's |
| 683 |
now classic <firstterm>Bourne</firstterm> shell. Bash has become |
| 684 |
a <foreignphrase>de facto</foreignphrase> standard for shell |
| 685 |
scripting on most flavors of UNIX. Most of the principles this |
| 686 |
book covers apply equally well to scripting with other shells, |
| 687 |
such as the <firstterm>Korn Shell</firstterm>, from which Bash |
| 688 |
derives some of its features, |
| 689 |
|
| 690 |
<footnote><para>Many of the features of <firstterm>ksh88</firstterm>, |
| 691 |
and even a few from the updated <firstterm>ksh93</firstterm> |
| 692 |
have been merged into Bash.</para></footnote> |
| 693 |
|
| 694 |
and the <firstterm>C Shell</firstterm> and its variants. (Note that |
| 695 |
<firstterm>C Shell</firstterm> programming is not recommended due to |
| 696 |
certain inherent problems, as pointed out in an October, 1993 <ulink |
| 697 |
url="http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/">Usenet |
| 698 |
post</ulink> by Tom Christiansen.) </para> |
| 699 |
|
| 700 |
<para>What follows is a tutorial on shell scripting. It relies |
| 701 |
heavily on examples to illustrate various features of the shell. |
| 702 |
The example scripts work -- they've been tested, insofar as |
| 703 |
possible -- and some of them are even useful in real life. The |
| 704 |
reader can play with the actual working code of the examples |
| 705 |
in the source archive (<filename>scriptname.sh</filename> or |
| 706 |
<filename>scriptname.bash</filename>), |
| 707 |
|
| 708 |
<footnote><para>By convention, user-written shell scripts |
| 709 |
that are Bourne shell compliant generally take a name with a |
| 710 |
<filename>.sh</filename> extension. System scripts, such as |
| 711 |
those found in <filename class="directory">/etc/rc.d</filename>, |
| 712 |
do not necessarily conform to this nomenclature.</para></footnote> |
| 713 |
|
| 714 |
give them <firstterm>execute</firstterm> permission |
| 715 |
(<userinput>chmod u+rx scriptname</userinput>), |
| 716 |
then run them to see what happens. Should the <ulink |
| 717 |
url="http://bash.deta.in/abs-guide-latest.tar.bz2">source |
| 718 |
archive</ulink> not be available, then cut-and-paste from the <ulink |
| 719 |
url="http://www.tldp.org/LDP/abs/abs-guide.html.tar.gz">HTML</ulink> or |
| 720 |
<ulink url="http://bash.deta.in/abs-guide.pdf">pdf</ulink> |
| 721 |
rendered versions. Be aware that some of the scripts presented here |
| 722 |
introduce features before they are explained, and this may require |
| 723 |
the reader to temporarily skip ahead for enlightenment.</para> |
| 724 |
|
| 725 |
<para>Unless otherwise noted, <ulink |
| 726 |
url="mailto:[email protected]">the author</ulink> of this |
| 727 |
book wrote the example scripts that follow.</para> |
| 728 |
|
| 729 |
<epigraph> |
| 730 |
<para>His countenance was bold and bashed not.</para> |
| 731 |
<para>--Edmund Spenser</para> |
| 732 |
</epigraph> |
| 733 |
|
| 734 |
</chapter> <!-- Why Shell Programming? --> |
| 735 |
|
| 736 |
|
| 737 |
<chapter id="sha-bang"> |
| 738 |
<title>Starting Off With a Sha-Bang</title> |
| 739 |
|
| 740 |
<epigraph> |
| 741 |
<para>Shell programming is a 1950s juke box . . .</para> |
| 742 |
<para>--Larry Wall</para> |
| 743 |
</epigraph> |
| 744 |
|
| 745 |
<para>In the simplest case, a script is nothing more than a list |
| 746 |
of system commands stored in a file. At the very least, this saves |
| 747 |
the effort of retyping that particular sequence of commands each |
| 748 |
time it is invoked.</para> |
| 749 |
|
| 750 |
<example id="ex1"> |
| 751 |
<title><firstterm>cleanup</firstterm>: A script to clean up log |
| 752 |
files in /var/log </title> <programlisting>&ex1;</programlisting> |
| 753 |
</example> |
| 754 |
|
| 755 |
<para>There is nothing unusual here, only a set of commands that |
| 756 |
could just as easily have been invoked one by one from the |
| 757 |
command-line on the console or in a terminal window. |
| 758 |
The advantages of placing the commands in a script go far beyond |
| 759 |
not having to retype them time and again. The script becomes a |
| 760 |
<firstterm>program</firstterm> -- a <emphasis>tool</emphasis> -- |
| 761 |
and it can easily be modified or customized for a particular |
| 762 |
application.</para> |
| 763 |
|
| 764 |
<example id="ex1a"> |
| 765 |
<title><firstterm>cleanup</firstterm>: An improved clean-up |
| 766 |
script</title> <programlisting>&ex1a;</programlisting> |
| 767 |
</example> |
| 768 |
|
| 769 |
<para>Now <emphasis>that's</emphasis> beginning to look like a real |
| 770 |
script. But we can go even farther . . .</para> |
| 771 |
|
| 772 |
<example id="ex2"> |
| 773 |
<title><firstterm>cleanup</firstterm>: An enhanced |
| 774 |
and generalized version of above scripts.</title> |
| 775 |
<programlisting>&ex2;</programlisting> |
| 776 |
</example> |
| 777 |
|
| 778 |
<para>Since you may not wish to wipe out the entire system log, |
| 779 |
this version of the script keeps the last section of the message |
| 780 |
log intact. You will constantly discover ways of fine-tuning |
| 781 |
previously written scripts for increased effectiveness.</para> |
| 782 |
|
| 783 |
<para><anchor id="shabangref">* * *</para> |
| 784 |
|
| 785 |
<para><anchor id="magnumref">The |
| 786 |
<firstterm><indexterm> |
| 787 |
<primary>sha-bang</primary> |
| 788 |
</indexterm> sha-bang</firstterm> |
| 789 |
(<token> |
| 790 |
<indexterm> |
| 791 |
<primary>#!</primary> |
| 792 |
</indexterm> #!</token>) |
| 793 |
|
| 794 |
<footnote><para>More commonly seen in the literature as |
| 795 |
<firstterm>she-bang</firstterm> or <firstterm>sh-bang</firstterm>. |
| 796 |
This derives from the concatenation of the tokens |
| 797 |
<firstterm>sharp</firstterm> (<token>#</token>) and |
| 798 |
<firstterm>bang</firstterm> (<token>!</token>).</para></footnote> |
| 799 |
|
| 800 |
at the head of a script tells your system that this file is a set |
| 801 |
of commands to be fed to the command interpreter indicated. The |
| 802 |
<token>#!</token> is actually a two-byte |
| 803 |
|
| 804 |
<footnote> |
| 805 |
<para>Some flavors of UNIX (those based on 4.2 BSD) |
| 806 |
allegedly take a four-byte magic number, requiring |
| 807 |
a blank after the <token>!</token> -- |
| 808 |
<userinput>#! /bin/sh</userinput>. <ulink |
| 809 |
url="http://www.in-ulm.de/~mascheck/various/shebang/#details"> |
| 810 |
According to Sven Mascheck</ulink> this is probably a myth.</para> |
| 811 |
</footnote> |
| 812 |
|
| 813 |
<indexterm> |
| 814 |
<primary>magic number</primary> |
| 815 |
</indexterm> |
| 816 |
<firstterm>magic number</firstterm>, a special marker that |
| 817 |
designates a file type, or in this case an executable shell |
| 818 |
script (type <userinput>man magic</userinput> for more |
| 819 |
details on this fascinating topic). Immediately following |
| 820 |
the <firstterm>sha-bang</firstterm> is a <firstterm>path |
| 821 |
name</firstterm>. This is the path to the program that interprets |
| 822 |
the commands in the script, whether it be a shell, a programming |
| 823 |
language, or a utility. This command interpreter then executes |
| 824 |
the commands in the script, starting at the top (the line |
| 825 |
following the <firstterm>sha-bang</firstterm> line), and ignoring |
| 826 |
comments. |
| 827 |
|
| 828 |
<footnote> |
| 829 |
<para>The <token>#!</token> line in a shell script |
| 830 |
will be the first thing the command interpreter |
| 831 |
(<command>sh</command> or <command>bash</command>) |
| 832 |
sees. Since this line begins with a <token>#</token>, |
| 833 |
it will be correctly interpreted as a comment when the |
| 834 |
command interpreter finally executes the script. The |
| 835 |
line has already served its purpose - calling the command |
| 836 |
interpreter.</para> |
| 837 |
<para>If, in fact, the script includes an |
| 838 |
<emphasis>extra</emphasis> <token>#!</token> line, then |
| 839 |
<command>bash</command> will interpret it as a comment. |
| 840 |
<programlisting>#!/bin/bash |
| 841 |
|
| 842 |
echo "Part 1 of script." |
| 843 |
a=1 |
| 844 |
|
| 845 |
#!/bin/bash |
| 846 |
# This does *not* launch a new script. |
| 847 |
|
| 848 |
echo "Part 2 of script." |
| 849 |
echo $a # Value of $a stays at 1.</programlisting></para> |
| 850 |
</footnote> |
| 851 |
|
| 852 |
</para> |
| 853 |
|
| 854 |
|
| 855 |
<para><programlisting>#!/bin/sh |
| 856 |
#!/bin/bash |
| 857 |
#!/usr/bin/perl |
| 858 |
#!/usr/bin/tcl |
| 859 |
#!/bin/sed -f |
| 860 |
#!/bin/awk -f</programlisting></para> |
| 861 |
|
| 862 |
<para>Each of the above script header lines calls a different command |
| 863 |
interpreter, be it <filename>/bin/sh</filename>, the default shell |
| 864 |
(<command>bash</command> in a Linux system) or otherwise. |
| 865 |
|
| 866 |
<footnote> |
| 867 |
<para>This allows some cute tricks.</para> |
| 868 |
<para><programlisting>#!/bin/rm |
| 869 |
# Self-deleting script. |
| 870 |
|
| 871 |
# Nothing much seems to happen when you run this... except that the file disappears. |
| 872 |
|
| 873 |
WHATEVER=85 |
| 874 |
|
| 875 |
echo "This line will never print (betcha!)." |
| 876 |
|
| 877 |
exit $WHATEVER # Doesn't matter. The script will not exit here. |
| 878 |
# Try an echo $? after script termination. |
| 879 |
# You'll get a 0, not a 85.</programlisting></para> |
| 880 |
<para>Also, try starting a <filename>README</filename> file with a |
| 881 |
<userinput>#!/bin/more</userinput>, and making it executable. |
| 882 |
The result is a self-listing documentation file. (A <link |
| 883 |
linkend="heredocref">here document</link> using |
| 884 |
<link linkend="catref">cat</link> is possibly a better alternative |
| 885 |
-- see <xref linkend="ex71">).</para> |
| 886 |
</footnote> |
| 887 |
|
| 888 |
Using <userinput>#!/bin/sh</userinput>, the default Bourne shell |
| 889 |
in most commercial variants of UNIX, makes the script <link |
| 890 |
linkend="portabilityissues">portable</link> to non-Linux machines, |
| 891 |
though you <link linkend="binsh">sacrifice Bash-specific |
| 892 |
features</link>. The script will, however, conform to the |
| 893 |
<acronym>POSIX</acronym> |
| 894 |
|
| 895 |
<footnote> |
| 896 |
<para><anchor id="posix2ref"><emphasis role="strong">P</emphasis>ortable |
| 897 |
<emphasis role="strong">O</emphasis>perating |
| 898 |
<emphasis role="strong">S</emphasis>ystem <emphasis |
| 899 |
role="bold">I</emphasis>nterface, an attempt to |
| 900 |
standardize UNI<emphasis role="strong">X</emphasis>-like |
| 901 |
OSes. The POSIX specifications are listed on the <ulink |
| 902 |
url="http://www.opengroup.org/onlinepubs/007904975/toc.htm">Open |
| 903 |
Group site</ulink>.</para> |
| 904 |
</footnote> |
| 905 |
|
| 906 |
<command>sh</command> standard.</para> |
| 907 |
|
| 908 |
<para>Note that the path given at the <quote>sha-bang</quote> must |
| 909 |
be correct, otherwise an error message -- usually <quote>Command |
| 910 |
not found.</quote> -- will be the only result of running the |
| 911 |
script. |
| 912 |
<footnote><para>To avoid this possibility, a script may begin |
| 913 |
with a <link linkend="envv2ref">#!/bin/env bash</link> |
| 914 |
<firstterm>sha-bang</firstterm> line. This may be |
| 915 |
useful on UNIX machines where <firstterm>bash</firstterm> |
| 916 |
is not located in <filename |
| 917 |
class="directory">/bin</filename></para></footnote> |
| 918 |
|
| 919 |
</para> |
| 920 |
|
| 921 |
<para><token>#!</token> can be omitted if the script consists only |
| 922 |
of a set of generic system commands, using no internal |
| 923 |
shell directives. The second example, above, requires the |
| 924 |
initial <token>#!</token>, since the variable assignment line, |
| 925 |
<userinput>lines=50</userinput>, uses a shell-specific construct. |
| 926 |
<footnote><para>If <firstterm>Bash</firstterm> is your default |
| 927 |
shell, then the <token>#!</token> isn't necessary at the |
| 928 |
beginning of a script. However, if launching a script from |
| 929 |
a different shell, such as <firstterm>tcsh</firstterm>, |
| 930 |
then you <emphasis>will</emphasis> need the |
| 931 |
<token>#!</token>.</para></footnote> |
| 932 |
Note again that <userinput>#!/bin/sh</userinput> invokes the default |
| 933 |
shell interpreter, which defaults to <filename>/bin/bash</filename> |
| 934 |
on a Linux machine.</para> |
| 935 |
|
| 936 |
|
| 937 |
<tip> |
| 938 |
<para>This tutorial encourages a modular approach |
| 939 |
to constructing a script. Make note of and collect |
| 940 |
<quote>boilerplate</quote> code snippets that might be useful |
| 941 |
in future scripts. Eventually you will build quite an extensive |
| 942 |
library of nifty routines. As an example, the following script |
| 943 |
prolog tests whether the script has been invoked with the correct |
| 944 |
number of parameters.</para> |
| 945 |
|
| 946 |
<para><programlisting>E_WRONG_ARGS=85 |
| 947 |
script_parameters="-a -h -m -z" |
| 948 |
# -a = all, -h = help, etc. |
| 949 |
|
| 950 |
if [ $# -ne $Number_of_expected_args ] |
| 951 |
then |
| 952 |
echo "Usage: `basename $0` $script_parameters" |
| 953 |
# `basename $0` is the script's filename. |
| 954 |
exit $E_WRONG_ARGS |
| 955 |
fi</programlisting> |
| 956 |
</para> |
| 957 |
|
| 958 |
<para>Many times, you will write a script that carries out one |
| 959 |
particular task. The first script in this chapter is an |
| 960 |
example. Later, it might occur to you to generalize |
| 961 |
the script to do other, similar tasks. Replacing the literal |
| 962 |
(<quote>hard-wired</quote>) constants by variables is a step in |
| 963 |
that direction, as is replacing repetitive code blocks by <link |
| 964 |
linkend="functionref">functions</link>.</para> |
| 965 |
|
| 966 |
</tip> |
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
<sect1 id="invoking"> |
| 971 |
<title>Invoking the script</title> |
| 972 |
|
| 973 |
<para>Having written the script, you can invoke it by <userinput>sh |
| 974 |
scriptname</userinput>, |
| 975 |
|
| 976 |
<footnote><para>Caution: invoking a <firstterm>Bash</firstterm> |
| 977 |
script by <userinput>sh scriptname</userinput> turns off |
| 978 |
Bash-specific extensions, and the script may therefore fail |
| 979 |
to execute.</para></footnote> |
| 980 |
|
| 981 |
or alternatively <userinput>bash scriptname</userinput>. (Not |
| 982 |
recommended is using <userinput>sh <scriptname</userinput>, |
| 983 |
since this effectively disables reading from |
| 984 |
<link linkend="stdinoutdef"><filename>stdin</filename></link> |
| 985 |
within the script.) Much more convenient is to make |
| 986 |
the script itself directly executable with a <link |
| 987 |
linkend="chmodref">chmod</link>. |
| 988 |
|
| 989 |
<variablelist> |
| 990 |
<varlistentry> |
| 991 |
<term>Either:</term> <listitem> |
| 992 |
<para><userinput>chmod 555 scriptname</userinput> (gives |
| 993 |
everyone read/execute permission) |
| 994 |
<footnote><para>A script needs <firstterm>read</firstterm>, as |
| 995 |
well as execute permission for it to run, since the shell |
| 996 |
needs to be able to read it.</para></footnote> |
| 997 |
</para> |
| 998 |
</listitem> |
| 999 |
</varlistentry> <varlistentry> |
| 1000 |
<term>or</term> <listitem> |
| 1001 |
<para><userinput>chmod +rx scriptname</userinput> (gives |
| 1002 |
everyone read/execute permission)</para> <para><userinput>chmod |
| 1003 |
u+rx scriptname</userinput> (gives only the |
| 1004 |
script owner read/execute permission)</para> |
| 1005 |
</listitem> |
| 1006 |
</varlistentry> |
| 1007 |
</variablelist> |
| 1008 |
</para> |
| 1009 |
|
| 1010 |
<para>Having made the script executable, you may now test it by |
| 1011 |
<userinput>./scriptname</userinput>. |
| 1012 |
|
| 1013 |
<footnote><para>Why not simply invoke the script with |
| 1014 |
<userinput>scriptname</userinput>? If the directory you |
| 1015 |
are in (<link linkend="pwdref">$PWD</link>) is where |
| 1016 |
<filename>scriptname</filename> is located, why doesn't |
| 1017 |
this work? This fails because, for security reasons, the |
| 1018 |
current directory (<filename class="directory">./</filename>) |
| 1019 |
is not by default included in a user's <link |
| 1020 |
linkend="pathref">$PATH</link>. It is therefore necessary to |
| 1021 |
explicitly invoke the script in the current directory with |
| 1022 |
a <userinput>./scriptname</userinput>.</para></footnote> |
| 1023 |
|
| 1024 |
If it begins with a <quote>sha-bang</quote> line, invoking the |
| 1025 |
script calls the correct command interpreter to run it.</para> |
| 1026 |
|
| 1027 |
<para>As a final step, after testing and debugging, |
| 1028 |
you would likely want to move it to <filename |
| 1029 |
class="directory">/usr/local/bin</filename> (as |
| 1030 |
<firstterm>root</firstterm>, of course), to make the script |
| 1031 |
available to yourself and all other users as a systemwide |
| 1032 |
executable. The script could then be invoked by simply typing |
| 1033 |
<command>scriptname</command> <keycap>[ENTER]</keycap> from the |
| 1034 |
command-line.</para> |
| 1035 |
|
| 1036 |
</sect1> <!-- Invoking the script --> |
| 1037 |
|
| 1038 |
|
| 1039 |
<sect1 id="prelimexer"> |
| 1040 |
<title>Preliminary Exercises</title> |
| 1041 |
|
| 1042 |
<orderedlist> |
| 1043 |
|
| 1044 |
<listitem> |
| 1045 |
<para>System administrators often write scripts to automate common |
| 1046 |
tasks. Give several instances where such scripts would be |
| 1047 |
useful.</para> |
| 1048 |
</listitem> |
| 1049 |
|
| 1050 |
<listitem> |
| 1051 |
<para>Write a script that upon invocation shows the |
| 1052 |
<link linkend="dateref">time and date</link>, <link |
| 1053 |
linkend="whoref">lists all logged-in users</link>, and gives |
| 1054 |
the system <link linkend="uptimeref">uptime</link>. The script |
| 1055 |
then <link linkend="ioredirref">saves this information</link> |
| 1056 |
to a logfile.</para> |
| 1057 |
</listitem> |
| 1058 |
|
| 1059 |
</orderedlist> |
| 1060 |
|
| 1061 |
</sect1> <!-- Preliminary Exercises --> |
| 1062 |
|
| 1063 |
</chapter> <!-- Starting Off With a Sha-Bang --> |
| 1064 |
|
| 1065 |
|
| 1066 |
</part> <!-- Part 1 (Introduction) --> |
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
<part label="Part 2" id="part2"> |
| 1071 |
<title>Basics</title> |
| 1072 |
|
| 1073 |
|
| 1074 |
<chapter id="special-chars"> |
| 1075 |
<title>Special Characters</title> |
| 1076 |
|
| 1077 |
<para>What makes a character <firstterm>special</firstterm>? |
| 1078 |
If it has a meaning beyond its |
| 1079 |
<firstterm>literal meaning</firstterm>, a <link |
| 1080 |
linkend="metameaningref">meta-meaning</link>, then we refer |
| 1081 |
to it as a <firstterm>special character</firstterm>. Along |
| 1082 |
with commands and <link linkend="keywordref">keywords</link>, |
| 1083 |
<firstterm>special characters</firstterm> are building blocks |
| 1084 |
of Bash scripts.</para> |
| 1085 |
|
| 1086 |
<variablelist id="scharlist"> |
| 1087 |
<title><anchor id="scharlist1">Special Characters Found In |
| 1088 |
Scripts and Elsewhere</title> |
| 1089 |
|
| 1090 |
<varlistentry><term><anchor id="hashmarkref"><token>#</token></term> |
| 1091 |
<indexterm> |
| 1092 |
<primary>#</primary> |
| 1093 |
</indexterm> |
| 1094 |
<indexterm> |
| 1095 |
<primary>special character</primary> |
| 1096 |
<secondary>#</secondary> |
| 1097 |
</indexterm> |
| 1098 |
<indexterm> |
| 1099 |
<primary>comment</primary> |
| 1100 |
</indexterm> |
| 1101 |
<listitem> |
| 1102 |
<formalpara><title>Comments</title> |
| 1103 |
<para>Lines beginning with a <token>#</token> |
| 1104 |
(with the exception of <link linkend="magnumref"> |
| 1105 |
<token>#!</token></link>) are comments and will |
| 1106 |
<emphasis>not</emphasis> be executed.</para> |
| 1107 |
</formalpara> |
| 1108 |
|
| 1109 |
<para><programlisting># This line is a comment.</programlisting></para> |
| 1110 |
|
| 1111 |
<para>Comments may also occur following the end of a command.</para> |
| 1112 |
|
| 1113 |
<para><programlisting>echo "A comment will follow." # Comment here. |
| 1114 |
# ^ Note whitespace before #</programlisting></para> |
| 1115 |
|
| 1116 |
<para><anchor id="wsbcomm"> Comments may also follow <link |
| 1117 |
linkend="whitespaceref">whitespace</link> at the beginning |
| 1118 |
of a line.</para> |
| 1119 |
<para> |
| 1120 |
<programlisting> # A tab precedes this comment.</programlisting> |
| 1121 |
</para> |
| 1122 |
|
| 1123 |
<para><anchor id="comminpipe">Comments may even be embedded |
| 1124 |
within a <link linkend="piperef">pipe</link>.</para> |
| 1125 |
<para> |
| 1126 |
<programlisting>initial=( `cat "$startfile" | sed -e '/#/d' | tr -d '\n' |\ |
| 1127 |
# Delete lines containing '#' comment character. |
| 1128 |
sed -e 's/\./\. /g' -e 's/_/_ /g'` ) |
| 1129 |
# Excerpted from life.sh script</programlisting> |
| 1130 |
</para> |
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
<caution><para>A command may not follow a comment on the |
| 1135 |
same line. There is no method of terminating the comment, |
| 1136 |
in order for <quote>live code</quote> to begin on the same |
| 1137 |
line. Use a new line for the next command.</para></caution> |
| 1138 |
|
| 1139 |
<note><para>Of course, a <link linkend="quotingref">quoted</link> |
| 1140 |
or an <link linkend="escp">escaped</link> <token>#</token> |
| 1141 |
in an <link linkend="echoref">echo</link> statement does |
| 1142 |
<emphasis>not</emphasis> begin a comment. Likewise, a |
| 1143 |
<token>#</token> appears in <link linkend="psub2">certain |
| 1144 |
parameter-substitution constructs</link> and in <link |
| 1145 |
linkend="numconstants"> numerical constant expressions</link>. |
| 1146 |
|
| 1147 |
<programlisting>echo "The # here does not begin a comment." |
| 1148 |
echo 'The # here does not begin a comment.' |
| 1149 |
echo The \# here does not begin a comment. |
| 1150 |
echo The # here begins a comment. |
| 1151 |
|
| 1152 |
echo ${PATH#*:} # Parameter substitution, not a comment. |
| 1153 |
echo $(( 2#101011 )) # Base conversion, not a comment. |
| 1154 |
|
| 1155 |
# Thanks, S.C.</programlisting> |
| 1156 |
|
| 1157 |
The standard <link linkend="quotingref">quoting and |
| 1158 |
escape</link> characters (" ' \) escape the #. |
| 1159 |
</para></note> |
| 1160 |
|
| 1161 |
<para>Certain <link linkend="psorex1">pattern matching |
| 1162 |
operations</link> also use the <token>#</token>.</para> |
| 1163 |
|
| 1164 |
</listitem> |
| 1165 |
</varlistentry> |
| 1166 |
|
| 1167 |
<varlistentry><term><anchor id="semicolonref"><token>;</token></term> |
| 1168 |
<indexterm> |
| 1169 |
<primary>;</primary> |
| 1170 |
</indexterm> |
| 1171 |
<indexterm> |
| 1172 |
<primary>special character</primary> |
| 1173 |
<secondary>;</secondary> |
| 1174 |
</indexterm> |
| 1175 |
<indexterm> |
| 1176 |
<primary>separator</primary> |
| 1177 |
</indexterm> |
| 1178 |
|
| 1179 |
<listitem> |
| 1180 |
<formalpara><title>Command separator [semicolon]</title> |
| 1181 |
<para>Permits putting two or more commands on the same |
| 1182 |
line.</para> |
| 1183 |
</formalpara> |
| 1184 |
|
| 1185 |
<para> |
| 1186 |
<programlisting>echo hello; echo there |
| 1187 |
|
| 1188 |
|
| 1189 |
if [ -x "$filename" ]; then # Note the space after the semicolon. |
| 1190 |
#+ ^^ |
| 1191 |
echo "File $filename exists."; cp $filename $filename.bak |
| 1192 |
else # ^^ |
| 1193 |
echo "File $filename not found."; touch $filename |
| 1194 |
fi; echo "File test complete."</programlisting> |
| 1195 |
</para> |
| 1196 |
|
| 1197 |
<para>Note that the <quote><token>;</token></quote> |
| 1198 |
<link linkend="findref0">sometimes needs to be |
| 1199 |
<firstterm>escaped</firstterm></link>.</para> |
| 1200 |
|
| 1201 |
</listitem> |
| 1202 |
</varlistentry> |
| 1203 |
|
| 1204 |
<varlistentry><term><token>;;</token></term> |
| 1205 |
<indexterm> |
| 1206 |
<primary>;;</primary> |
| 1207 |
</indexterm> |
| 1208 |
<indexterm> |
| 1209 |
<primary>special character</primary> |
| 1210 |
<secondary>case</secondary> |
| 1211 |
</indexterm> |
| 1212 |
<indexterm> |
| 1213 |
<primary>;;</primary> |
| 1214 |
</indexterm> |
| 1215 |
|
| 1216 |
<listitem> |
| 1217 |
<formalpara><title>Terminator in a <link |
| 1218 |
linkend="caseesac1">case</link> option [double semicolon]</title> |
| 1219 |
<para><anchor id="doublesemicolon"></para> |
| 1220 |
</formalpara> |
| 1221 |
|
| 1222 |
<para><programlisting>case "$variable" in |
| 1223 |
abc) echo "\$variable = abc" ;; |
| 1224 |
xyz) echo "\$variable = xyz" ;; |
| 1225 |
esac</programlisting></para> |
| 1226 |
|
| 1227 |
</listitem> |
| 1228 |
</varlistentry> |
| 1229 |
|
| 1230 |
|
| 1231 |
<varlistentry> |
| 1232 |
<term><token>;;&</token></term> |
| 1233 |
<term><token>;&</token></term> |
| 1234 |
<indexterm> |
| 1235 |
<primary>;;&</primary> |
| 1236 |
</indexterm> |
| 1237 |
<indexterm> |
| 1238 |
<primary>special character</primary> |
| 1239 |
<secondary>;;&</secondary> |
| 1240 |
</indexterm> |
| 1241 |
<indexterm> |
| 1242 |
<primary>;&</primary> |
| 1243 |
<secondary>case statement</secondary> |
| 1244 |
</indexterm> |
| 1245 |
<indexterm> |
| 1246 |
<primary>;&</primary> |
| 1247 |
</indexterm> |
| 1248 |
|
| 1249 |
<listitem><formalpara><title><link |
| 1250 |
linkend="ncterm">Terminators</link> |
| 1251 |
in a <firstterm>case</firstterm> option (<link |
| 1252 |
linkend="bash4ref">version 4+</link> of Bash).</title> |
| 1253 |
<para></para> |
| 1254 |
</formalpara> |
| 1255 |
</listitem> |
| 1256 |
</varlistentry> |
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
<varlistentry><term><token>.</token></term> |
| 1261 |
<indexterm> |
| 1262 |
<primary>.</primary> |
| 1263 |
</indexterm> |
| 1264 |
<indexterm> |
| 1265 |
<primary>special character</primary> |
| 1266 |
<secondary>.</secondary> |
| 1267 |
</indexterm> |
| 1268 |
<indexterm> |
| 1269 |
<primary>dot command</primary> |
| 1270 |
</indexterm> |
| 1271 |
<indexterm> |
| 1272 |
<primary>source</primary> |
| 1273 |
</indexterm> |
| 1274 |
|
| 1275 |
<listitem> |
| 1276 |
<para><anchor id="dotref"></para> |
| 1277 |
<formalpara><title><quote>dot</quote> command [period]</title> |
| 1278 |
<para>Equivalent to <link |
| 1279 |
linkend="sourceref">source</link> (see |
| 1280 |
<xref linkend="ex38">). This is a bash <link |
| 1281 |
linkend="builtinref">builtin</link>.</para> |
| 1282 |
</formalpara> |
| 1283 |
|
| 1284 |
</listitem> |
| 1285 |
</varlistentry> |
| 1286 |
|
| 1287 |
|
| 1288 |
<varlistentry><term><token>.</token></term> |
| 1289 |
<indexterm> |
| 1290 |
<primary>.</primary> |
| 1291 |
</indexterm> |
| 1292 |
<indexterm> |
| 1293 |
<primary>special character</primary> |
| 1294 |
<secondary>.</secondary> |
| 1295 |
</indexterm> |
| 1296 |
<indexterm> |
| 1297 |
<primary>filename</primary> |
| 1298 |
</indexterm> |
| 1299 |
<indexterm> |
| 1300 |
<primary>part of a filename</primary> |
| 1301 |
</indexterm> |
| 1302 |
|
| 1303 |
<listitem> |
| 1304 |
<formalpara><title><quote>dot</quote>, as a component of a filename</title> |
| 1305 |
<para>When working with filenames, a leading dot is the prefix |
| 1306 |
of a <quote>hidden</quote> file, a file that an |
| 1307 |
<link linkend="lsref">ls</link> will not normally show. |
| 1308 |
<screen><prompt>bash$ </prompt><userinput>touch .hidden-file</userinput> |
| 1309 |
<prompt>bash$ </prompt><userinput>ls -l</userinput> |
| 1310 |
<computeroutput>total 10 |
| 1311 |
-rw-r--r-- 1 bozo 4034 Jul 18 22:04 data1.addressbook |
| 1312 |
-rw-r--r-- 1 bozo 4602 May 25 13:58 data1.addressbook.bak |
| 1313 |
-rw-r--r-- 1 bozo 877 Dec 17 2000 employment.addressbook</computeroutput> |
| 1314 |
|
| 1315 |
|
| 1316 |
<prompt>bash$ </prompt><userinput>ls -al</userinput> |
| 1317 |
<computeroutput>total 14 |
| 1318 |
drwxrwxr-x 2 bozo bozo 1024 Aug 29 20:54 ./ |
| 1319 |
drwx------ 52 bozo bozo 3072 Aug 29 20:51 ../ |
| 1320 |
-rw-r--r-- 1 bozo bozo 4034 Jul 18 22:04 data1.addressbook |
| 1321 |
-rw-r--r-- 1 bozo bozo 4602 May 25 13:58 data1.addressbook.bak |
| 1322 |
-rw-r--r-- 1 bozo bozo 877 Dec 17 2000 employment.addressbook |
| 1323 |
-rw-rw-r-- 1 bozo bozo 0 Aug 29 20:54 .hidden-file</computeroutput> |
| 1324 |
</screen> |
| 1325 |
</para> |
| 1326 |
</formalpara> |
| 1327 |
|
| 1328 |
<para><anchor id="dotdirectory"></para> |
| 1329 |
<para>When considering directory names, <firstterm>a single |
| 1330 |
dot</firstterm> represents the current working directory, |
| 1331 |
and <firstterm>two dots</firstterm> denote the parent |
| 1332 |
directory.</para> |
| 1333 |
|
| 1334 |
<para> |
| 1335 |
<screen> |
| 1336 |
<prompt>bash$ </prompt><userinput>pwd</userinput> |
| 1337 |
<computeroutput>/home/bozo/projects</computeroutput> |
| 1338 |
|
| 1339 |
<prompt>bash$ </prompt><userinput>cd .</userinput> |
| 1340 |
<prompt>bash$ </prompt><userinput>pwd</userinput> |
| 1341 |
<computeroutput>/home/bozo/projects</computeroutput> |
| 1342 |
|
| 1343 |
<prompt>bash$ </prompt><userinput>cd ..</userinput> |
| 1344 |
<prompt>bash$ </prompt><userinput>pwd</userinput> |
| 1345 |
<computeroutput>/home/bozo/</computeroutput> |
| 1346 |
</screen> |
| 1347 |
</para> |
| 1348 |
|
| 1349 |
<para>The <firstterm>dot</firstterm> often appears as the |
| 1350 |
destination (directory) of a file movement command, |
| 1351 |
in this context meaning <firstterm>current |
| 1352 |
directory</firstterm>.</para> |
| 1353 |
|
| 1354 |
<para> |
| 1355 |
<screen> |
| 1356 |
<prompt>bash$ </prompt><userinput>cp /home/bozo/current_work/junk/* .</userinput> |
| 1357 |
</screen> |
| 1358 |
Copy all the <quote>junk</quote> files to |
| 1359 |
<link linkend="pwdref">$PWD</link>.</para> |
| 1360 |
|
| 1361 |
</listitem> |
| 1362 |
</varlistentry> |
| 1363 |
|
| 1364 |
|
| 1365 |
<varlistentry><term><token>.</token></term> |
| 1366 |
<indexterm> |
| 1367 |
<primary>.</primary> |
| 1368 |
</indexterm> |
| 1369 |
<indexterm> |
| 1370 |
<primary>special character</primary> |
| 1371 |
<secondary>.</secondary> |
| 1372 |
</indexterm> |
| 1373 |
<indexterm> |
| 1374 |
<primary>character match</primary> |
| 1375 |
</indexterm> |
| 1376 |
<indexterm> |
| 1377 |
<primary>match single character</primary> |
| 1378 |
</indexterm> |
| 1379 |
|
| 1380 |
<listitem> |
| 1381 |
<formalpara><title><quote>dot</quote> character match</title> |
| 1382 |
<para>When <link linkend="regexdot">matching |
| 1383 |
characters</link>, as part of a <link |
| 1384 |
linkend="regexref">regular expression</link>, a |
| 1385 |
<quote>dot</quote> <link linkend="regexdot">matches a |
| 1386 |
single character</link>.</para> |
| 1387 |
</formalpara> |
| 1388 |
|
| 1389 |
</listitem> |
| 1390 |
</varlistentry> |
| 1391 |
|
| 1392 |
|
| 1393 |
<varlistentry> |
| 1394 |
<term><token>"</token></term> |
| 1395 |
<listitem><formalpara><title><link linkend="dblquo">partial |
| 1396 |
quoting</link> [double quote]</title> |
| 1397 |
<para><emphasis>"STRING"</emphasis> preserves (from |
| 1398 |
interpretation) most of the special characters within |
| 1399 |
<emphasis>STRING</emphasis>. See <xref |
| 1400 |
linkend="quoting">.</para> |
| 1401 |
</formalpara> </listitem> |
| 1402 |
</varlistentry> |
| 1403 |
|
| 1404 |
<varlistentry> |
| 1405 |
<term><token>'</token></term> |
| 1406 |
<listitem><formalpara><title><link linkend="snglquo">full |
| 1407 |
quoting</link> [single quote]</title> |
| 1408 |
<para><emphasis>'STRING'</emphasis> preserves all special |
| 1409 |
characters within <emphasis>STRING</emphasis>. This is a |
| 1410 |
stronger form of quoting than <emphasis>"STRING"</emphasis>. |
| 1411 |
See <xref linkend="quoting">.</para> |
| 1412 |
</formalpara> </listitem> |
| 1413 |
</varlistentry> |
| 1414 |
|
| 1415 |
<varlistentry> |
| 1416 |
<term><token>,</token></term> |
| 1417 |
<listitem><formalpara><title><link linkend="commaop">comma |
| 1418 |
operator</link></title> |
| 1419 |
<para>The <firstterm>comma operator</firstterm> |
| 1420 |
|
| 1421 |
<footnote><para><anchor id="operatordef">An |
| 1422 |
<firstterm>operator</firstterm> is an agent that carries |
| 1423 |
out an <firstterm>operation</firstterm>. Some examples |
| 1424 |
are the common <link linkend="arops1">arithmetic |
| 1425 |
operators</link>, <command>+ - * /</command>. In |
| 1426 |
Bash, there is some overlap between the concepts |
| 1427 |
of <firstterm>operator</firstterm> and <link |
| 1428 |
linkend="keywordref">keyword</link>.</para></footnote> |
| 1429 |
|
| 1430 |
links together a |
| 1431 |
series of arithmetic operations. All are evaluated, |
| 1432 |
but only the last one is returned. |
| 1433 |
<programlisting>let "t2 = ((a = 9, 15 / 3))" |
| 1434 |
# Set "a = 9" and "t2 = 15 / 3"</programlisting> |
| 1435 |
</para> |
| 1436 |
</formalpara> |
| 1437 |
|
| 1438 |
<para><anchor id="commaop2">The <firstterm>comma</firstterm> |
| 1439 |
operator can also concatenate strings. |
| 1440 |
<programlisting>for file in /{,usr/}bin/*calc |
| 1441 |
# ^ Find all executable files ending in "calc" |
| 1442 |
#+ in /bin and /usr/bin directories. |
| 1443 |
do |
| 1444 |
if [ -x "$file" ] |
| 1445 |
then |
| 1446 |
echo $file |
| 1447 |
fi |
| 1448 |
done |
| 1449 |
|
| 1450 |
# /bin/ipcalc |
| 1451 |
# /usr/bin/kcalc |
| 1452 |
# /usr/bin/oidcalc |
| 1453 |
# /usr/bin/oocalc |
| 1454 |
|
| 1455 |
|
| 1456 |
# Thank you, Rory Winston, for pointing this out.</programlisting> |
| 1457 |
</para> |
| 1458 |
|
| 1459 |
</listitem> |
| 1460 |
|
| 1461 |
</varlistentry> |
| 1462 |
|
| 1463 |
<varlistentry> |
| 1464 |
<term><token>,</token></term> |
| 1465 |
<term><token>,</token></term> |
| 1466 |
<listitem> |
| 1467 |
<formalpara><title><link linkend="casemodparamsub">Lowercase |
| 1468 |
conversion</link> in <firstterm>parameter substitution</firstterm> |
| 1469 |
(added in <link |
| 1470 |
linkend="bash4ref">version 4</link> of Bash)</title> |
| 1471 |
<para></para></formalpara> |
| 1472 |
</listitem> |
| 1473 |
</varlistentry> |
| 1474 |
|
| 1475 |
|
| 1476 |
<varlistentry> |
| 1477 |
<term><token>\</token></term> |
| 1478 |
<listitem><formalpara><title><link linkend="escp">escape</link> [backslash]</title> |
| 1479 |
<para>A quoting mechanism for single characters.</para> |
| 1480 |
</formalpara> |
| 1481 |
|
| 1482 |
<para><userinput>\X</userinput> |
| 1483 |
<firstterm>escapes</firstterm> the character |
| 1484 |
<emphasis>X</emphasis>. This has the effect of |
| 1485 |
<quote>quoting</quote> <emphasis>X</emphasis>, equivalent |
| 1486 |
to <emphasis>'X'</emphasis>. The <token>\</token> may |
| 1487 |
be used to quote <token>"</token> and <token>'</token>, |
| 1488 |
so they are expressed literally.</para> |
| 1489 |
<para>See <xref linkend="quoting"> for an in-depth explanation |
| 1490 |
of escaped characters.</para> |
| 1491 |
</listitem> |
| 1492 |
</varlistentry> |
| 1493 |
|
| 1494 |
<varlistentry> |
| 1495 |
<term><token>/</token></term> |
| 1496 |
<listitem><formalpara><title>Filename path separator [forward slash]</title> |
| 1497 |
<para>Separates the components of a filename (as in |
| 1498 |
<filename>/home/bozo/projects/Makefile</filename>).</para> |
| 1499 |
</formalpara> |
| 1500 |
<para>This is also the division <link |
| 1501 |
linkend="arops1">arithmetic operator</link>.</para> |
| 1502 |
</listitem> |
| 1503 |
</varlistentry> |
| 1504 |
|
| 1505 |
<varlistentry> |
| 1506 |
<term><anchor id="backticksref"><token>`</token></term> |
| 1507 |
<listitem><formalpara><title><link |
| 1508 |
linkend="commandsubref">command substitution</link></title> |
| 1509 |
<para>The <command>`command`</command> construct makes |
| 1510 |
available the output of <command>command</command> |
| 1511 |
for assignment to a variable. This is also known as |
| 1512 |
<link linkend="backquotesref">backquotes</link> or |
| 1513 |
backticks.</para></formalpara> </listitem> |
| 1514 |
</varlistentry> |
| 1515 |
|
| 1516 |
<varlistentry><term><anchor id="colon0ref"><token>:</token></term> |
| 1517 |
<indexterm> |
| 1518 |
<primary>:</primary> |
| 1519 |
</indexterm> |
| 1520 |
<indexterm> |
| 1521 |
<primary>special character</primary> |
| 1522 |
<secondary>:</secondary> |
| 1523 |
</indexterm> |
| 1524 |
<indexterm> |
| 1525 |
<primary>null command</primary> |
| 1526 |
</indexterm> |
| 1527 |
<indexterm> |
| 1528 |
<primary>true</primary> |
| 1529 |
</indexterm> |
| 1530 |
<indexterm> |
| 1531 |
<primary>endless loop</primary> |
| 1532 |
</indexterm> |
| 1533 |
|
| 1534 |
<listitem> |
| 1535 |
<para><anchor id="nullref"></para> |
| 1536 |
<formalpara><title>null command [colon]</title> |
| 1537 |
<para>This is the shell equivalent of a |
| 1538 |
<quote>NOP</quote> (<replaceable>no op</replaceable>, a |
| 1539 |
do-nothing operation). It may be considered a synonym for |
| 1540 |
the shell builtin <link linkend="trueref">true</link>. The |
| 1541 |
<quote><token>:</token></quote> command is itself a |
| 1542 |
<firstterm>Bash</firstterm> <link |
| 1543 |
linkend="builtinref">builtin</link>, and its <link |
| 1544 |
linkend="exitstatusref">exit status</link> is |
| 1545 |
<firstterm>true</firstterm> |
| 1546 |
(<returnvalue>0</returnvalue>).</para> |
| 1547 |
</formalpara> |
| 1548 |
|
| 1549 |
<para><programlisting>: |
| 1550 |
echo $? # 0</programlisting></para> |
| 1551 |
|
| 1552 |
|
| 1553 |
<para>Endless loop:</para> |
| 1554 |
|
| 1555 |
<para><programlisting> |
| 1556 |
while : |
| 1557 |
do |
| 1558 |
operation-1 |
| 1559 |
operation-2 |
| 1560 |
... |
| 1561 |
operation-n |
| 1562 |
done |
| 1563 |
|
| 1564 |
# Same as: |
| 1565 |
# while true |
| 1566 |
# do |
| 1567 |
# ... |
| 1568 |
# done</programlisting> |
| 1569 |
</para> |
| 1570 |
|
| 1571 |
<para>Placeholder in if/then test:</para> |
| 1572 |
|
| 1573 |
<para><programlisting> |
| 1574 |
if condition |
| 1575 |
then : # Do nothing and branch ahead |
| 1576 |
else # Or else ... |
| 1577 |
take-some-action |
| 1578 |
fi</programlisting> |
| 1579 |
</para> |
| 1580 |
|
| 1581 |
<para>Provide a placeholder where a binary operation is |
| 1582 |
expected, see <xref linkend="arithops"> and <link |
| 1583 |
linkend="defparam">default parameters</link>.</para> |
| 1584 |
|
| 1585 |
<para><programlisting>: ${username=`whoami`} |
| 1586 |
# ${username=`whoami`} Gives an error without the leading : |
| 1587 |
# unless "username" is a command or builtin... |
| 1588 |
|
| 1589 |
: ${1?"Usage: $0 ARGUMENT"} # From "usage-message.sh example script.</programlisting> |
| 1590 |
</para> |
| 1591 |
|
| 1592 |
<para>Provide a placeholder where a command is expected in a |
| 1593 |
<link linkend="heredocref">here document</link>. See <xref |
| 1594 |
linkend="anonheredoc">.</para> |
| 1595 |
|
| 1596 |
<para>Evaluate string of variables using |
| 1597 |
<link linkend="paramsubref">parameter substitution</link> |
| 1598 |
(as in <xref linkend="ex6">). |
| 1599 |
|
| 1600 |
<programlisting>: ${HOSTNAME?} ${USER?} ${MAIL?} |
| 1601 |
# Prints error message |
| 1602 |
#+ if one or more of essential environmental variables not set.</programlisting> |
| 1603 |
</para> |
| 1604 |
|
| 1605 |
<para><command><link linkend="exprepl1">Variable expansion / substring |
| 1606 |
replacement</link></command>.</para> |
| 1607 |
|
| 1608 |
<para>In combination with the <token>></token> <link |
| 1609 |
linkend="ioredirref">redirection operator</link>, |
| 1610 |
truncates a file to zero length, without changing its |
| 1611 |
permissions. If the file did not previously exist, |
| 1612 |
creates it. |
| 1613 |
|
| 1614 |
<programlisting>: > data.xxx # File "data.xxx" now empty. |
| 1615 |
|
| 1616 |
# Same effect as cat /dev/null >data.xxx |
| 1617 |
# However, this does not fork a new process, since ":" is a builtin.</programlisting> |
| 1618 |
See also <xref linkend="ex12">.</para> |
| 1619 |
|
| 1620 |
<para>In combination with the <token>>></token> |
| 1621 |
redirection operator, has no effect on a pre-existing |
| 1622 |
target file (<userinput>: >> target_file</userinput>). |
| 1623 |
If the file did not previously exist, creates it.</para> |
| 1624 |
|
| 1625 |
<note><para><anchor id="regfileref">This applies to regular files, |
| 1626 |
not pipes, symlinks, and certain special files.</para></note> |
| 1627 |
|
| 1628 |
|
| 1629 |
<para>May be used to begin a comment line, although this is not |
| 1630 |
recommended. Using <token>#</token> for a comment turns |
| 1631 |
off error checking for the remainder of that line, so |
| 1632 |
almost anything may appear in a comment. However, |
| 1633 |
this is not the case with |
| 1634 |
<token>:</token>. |
| 1635 |
<programlisting>: This is a comment that generates an error, ( if [ $x -eq 3] ).</programlisting> |
| 1636 |
</para> |
| 1637 |
|
| 1638 |
<para>The <quote><token>:</token></quote> serves as a <link |
| 1639 |
linkend="fieldref">field</link> |
| 1640 |
separator, in <link |
| 1641 |
linkend="datafilesref1"><filename>/etc/passwd</filename></link>, |
| 1642 |
and in the <link linkend="pathref">$PATH</link> variable. |
| 1643 |
<screen><prompt>bash$ </prompt><userinput>echo $PATH</userinput> |
| 1644 |
<computeroutput>/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/sbin:/usr/sbin:/usr/games</computeroutput></screen> |
| 1645 |
</para> |
| 1646 |
|
| 1647 |
<para><anchor id="colonfname"></para> |
| 1648 |
<para>A <firstterm>colon</firstterm> is <link |
| 1649 |
linkend="fstrangeref">acceptable as a function name</link>. |
| 1650 |
<programlisting>:() |
| 1651 |
{ |
| 1652 |
echo "The name of this function is "$FUNCNAME" " |
| 1653 |
# Why use a colon as a function name? |
| 1654 |
# It's a way of obfuscating your code. |
| 1655 |
} |
| 1656 |
|
| 1657 |
: |
| 1658 |
|
| 1659 |
# The name of this function is :</programlisting> |
| 1660 |
|
| 1661 |
This is not <link linkend="portabilityissues">portable</link> |
| 1662 |
behavior, and therefore not a recommended practice. |
| 1663 |
In fact, more recent releases of Bash do not permit |
| 1664 |
this usage. An underscore <command>_</command> works, |
| 1665 |
though.</para> |
| 1666 |
|
| 1667 |
|
| 1668 |
<para><anchor id="coloninfunction"></para> |
| 1669 |
<para>A <firstterm>colon</firstterm> can serve |
| 1670 |
as a placeholder in an otherwise empty |
| 1671 |
function.</para> |
| 1672 |
<para><programlisting>not_empty () |
| 1673 |
{ |
| 1674 |
: |
| 1675 |
} # Contains a : (null command), and so is not empty.</programlisting></para> |
| 1676 |
|
| 1677 |
|
| 1678 |
</listitem> |
| 1679 |
</varlistentry> |
| 1680 |
|
| 1681 |
<varlistentry> |
| 1682 |
<term><anchor id="notref"><token>!</token></term> |
| 1683 |
<indexterm> |
| 1684 |
<primary>!</primary> |
| 1685 |
</indexterm> |
| 1686 |
<indexterm> |
| 1687 |
<primary>special character</primary> |
| 1688 |
<secondary>!</secondary> |
| 1689 |
</indexterm> |
| 1690 |
<indexterm> |
| 1691 |
<primary>not</primary> |
| 1692 |
<secondary>logical</secondary> |
| 1693 |
</indexterm> |
| 1694 |
<indexterm> |
| 1695 |
<primary>not</primary> |
| 1696 |
</indexterm> |
| 1697 |
|
| 1698 |
<listitem> |
| 1699 |
<formalpara><title>reverse (or negate) the sense of |
| 1700 |
a test or exit status [bang]</title> |
| 1701 |
|
| 1702 |
<para>The <token>!</token> operator inverts the <link |
| 1703 |
linkend="exitstatusref">exit status</link> |
| 1704 |
of the command to which it is applied (see |
| 1705 |
<xref linkend="negcond">). It also inverts |
| 1706 |
the meaning of a test operator. This can, for |
| 1707 |
example, change the sense of <firstterm>equal</firstterm> |
| 1708 |
( <link linkend="equalsignref">=</link> |
| 1709 |
) to <firstterm>not-equal</firstterm> ( != ). The |
| 1710 |
<token>!</token> operator is a Bash <link |
| 1711 |
linkend="keywordref">keyword</link>.</para> |
| 1712 |
|
| 1713 |
</formalpara> |
| 1714 |
<para>In a different context, the <token>!</token> |
| 1715 |
also appears in <link linkend="ivrref">indirect variable |
| 1716 |
references</link>.</para> |
| 1717 |
|
| 1718 |
<para>In yet another context, from the <firstterm>command |
| 1719 |
line</firstterm>, the <token>!</token> invokes the |
| 1720 |
Bash <firstterm>history mechanism</firstterm> (see <xref |
| 1721 |
linkend="histcommands">). Note that within a script, |
| 1722 |
the history mechanism is disabled.</para> |
| 1723 |
|
| 1724 |
</listitem> |
| 1725 |
</varlistentry> |
| 1726 |
|
| 1727 |
|
| 1728 |
<varlistentry> |
| 1729 |
<term><anchor id="asteriskref"><token>*</token></term> |
| 1730 |
<indexterm> |
| 1731 |
<primary>*</primary> |
| 1732 |
</indexterm> |
| 1733 |
<indexterm> |
| 1734 |
<primary>special character</primary> |
| 1735 |
<secondary>*</secondary> |
| 1736 |
</indexterm> |
| 1737 |
<indexterm> |
| 1738 |
<primary>wild card</primary> |
| 1739 |
<secondary>globbing</secondary> |
| 1740 |
</indexterm> |
| 1741 |
<indexterm> |
| 1742 |
<primary>regular expression</primary> |
| 1743 |
</indexterm> |
| 1744 |
|
| 1745 |
<listitem><formalpara><title>wild card [asterisk]</title> |
| 1746 |
|
| 1747 |
<para>The <token>*</token> character serves as a <quote>wild |
| 1748 |
card</quote> for filename expansion in |
| 1749 |
<link linkend="globbingref">globbing</link>. By itself, |
| 1750 |
it matches every filename in a given directory.</para> |
| 1751 |
</formalpara> |
| 1752 |
|
| 1753 |
<para> |
| 1754 |
<screen><prompt>bash$ </prompt><userinput>echo *</userinput> |
| 1755 |
<computeroutput>abs-book.sgml add-drive.sh agram.sh alias.sh</computeroutput> |
| 1756 |
</screen> |
| 1757 |
</para> |
| 1758 |
|
| 1759 |
<para><anchor id="asteriskref2"></para> |
| 1760 |
<para>The <token>*</token> also represents <link |
| 1761 |
linkend="asteriskreg">any number |
| 1762 |
(or zero) characters</link> in a <link |
| 1763 |
linkend="regexref">regular expression</link>.</para> |
| 1764 |
|
| 1765 |
</listitem> |
| 1766 |
</varlistentry> |
| 1767 |
|
| 1768 |
<varlistentry> |
| 1769 |
<term><token>*</token></term> |
| 1770 |
<indexterm> |
| 1771 |
<primary>*</primary> |
| 1772 |
</indexterm> |
| 1773 |
<indexterm> |
| 1774 |
<primary>special character</primary> |
| 1775 |
<secondary>*</secondary> |
| 1776 |
</indexterm> |
| 1777 |
<indexterm> |
| 1778 |
<primary>multiplication</primary> |
| 1779 |
<secondary>exponentiation</secondary> |
| 1780 |
</indexterm> |
| 1781 |
<indexterm> |
| 1782 |
<primary>arithmetic operator</primary> |
| 1783 |
</indexterm> |
| 1784 |
|
| 1785 |
<listitem><formalpara><title><link linkend="arops1">arithmetic operator</link></title> |
| 1786 |
|
| 1787 |
<para>In the context of arithmetic operations, the |
| 1788 |
<token>*</token> denotes multiplication.</para> |
| 1789 |
|
| 1790 |
</formalpara> |
| 1791 |
|
| 1792 |
<para><token>**</token> A double asterisk can represent the |
| 1793 |
<link linkend="exponentiationref">exponentiation</link> |
| 1794 |
operator or <link linkend="globstarref">extended |
| 1795 |
file-match</link> <firstterm>globbing</firstterm>.</para> |
| 1796 |
|
| 1797 |
</listitem> |
| 1798 |
</varlistentry> |
| 1799 |
|
| 1800 |
<varlistentry> |
| 1801 |
<term><token>?</token></term> |
| 1802 |
<indexterm> |
| 1803 |
<primary>?</primary> |
| 1804 |
</indexterm> |
| 1805 |
<indexterm> |
| 1806 |
<primary>special character</primary> |
| 1807 |
<secondary>?</secondary> |
| 1808 |
</indexterm> |
| 1809 |
<indexterm> |
| 1810 |
<primary>test</primary> |
| 1811 |
<secondary>operator</secondary> |
| 1812 |
</indexterm> |
| 1813 |
<indexterm> |
| 1814 |
<primary>test token</primary> |
| 1815 |
</indexterm> |
| 1816 |
|
| 1817 |
<listitem><formalpara><title>test operator</title> |
| 1818 |
<para>Within certain expressions, the <token>?</token> indicates |
| 1819 |
a test for a condition.</para> |
| 1820 |
</formalpara> |
| 1821 |
|
| 1822 |
<para><anchor id="cstrinary"></para> |
| 1823 |
<para>In a <link linkend="dblparens">double-parentheses |
| 1824 |
construct</link>, the <token>?</token> |
| 1825 |
can serve as an element of a C-style |
| 1826 |
<firstterm>trinary</firstterm> operator. |
| 1827 |
<footnote><para>This is more commonly known |
| 1828 |
as the <firstterm>ternary</firstterm> |
| 1829 |
operator. Unfortunately, <firstterm>ternary</firstterm> |
| 1830 |
is an ugly word. It doesn't roll off |
| 1831 |
the tongue, and it doesn't elucidate. It |
| 1832 |
obfuscates. <firstterm>Trinary</firstterm> is by far |
| 1833 |
the more elegant usage.</para></footnote> |
| 1834 |
</para> |
| 1835 |
|
| 1836 |
<para><varname>condition</varname><command>?</command><varname>result-if-true</varname><command>:</command><varname>result-if-false</varname></para> |
| 1837 |
|
| 1838 |
<para><programlisting>(( var0 = var1<98?9:21 )) |
| 1839 |
# ^ ^ |
| 1840 |
|
| 1841 |
# if [ "$var1" -lt 98 ] |
| 1842 |
# then |
| 1843 |
# var0=9 |
| 1844 |
# else |
| 1845 |
# var0=21 |
| 1846 |
# fi</programlisting></para> |
| 1847 |
|
| 1848 |
|
| 1849 |
<para>In a <link linkend="paramsubref">parameter |
| 1850 |
substitution</link> expression, the <token>?</token> |
| 1851 |
<link linkend="qerrmsg">tests whether a variable has been |
| 1852 |
set</link>.</para> |
| 1853 |
|
| 1854 |
</listitem> |
| 1855 |
</varlistentry> |
| 1856 |
|
| 1857 |
<varlistentry> |
| 1858 |
<term><anchor id="wildcardqu"><token>?</token></term> |
| 1859 |
<indexterm> |
| 1860 |
<primary>?</primary> |
| 1861 |
</indexterm> |
| 1862 |
<indexterm> |
| 1863 |
<primary>special character</primary> |
| 1864 |
<secondary>?</secondary> |
| 1865 |
</indexterm> |
| 1866 |
<indexterm> |
| 1867 |
<primary>wild card</primary> |
| 1868 |
<secondary>globbing</secondary> |
| 1869 |
</indexterm> |
| 1870 |
<indexterm> |
| 1871 |
<primary>regular expression</primary> |
| 1872 |
</indexterm> |
| 1873 |
|
| 1874 |
<listitem><formalpara><title>wild card</title> |
| 1875 |
|
| 1876 |
<para><anchor id="quexwc">The <token>?</token> character |
| 1877 |
serves as a single-character <quote>wild card</quote> |
| 1878 |
for filename expansion in <link |
| 1879 |
linkend="globbingref">globbing</link>, as well as <link |
| 1880 |
linkend="quexregex">representing one character</link> |
| 1881 |
in an <link linkend="extregex">extended regular |
| 1882 |
expression</link>.</para> |
| 1883 |
|
| 1884 |
</formalpara> |
| 1885 |
|
| 1886 |
</listitem> |
| 1887 |
</varlistentry> |
| 1888 |
|
| 1889 |
<varlistentry><term><token>$</token></term> |
| 1890 |
<indexterm> |
| 1891 |
<primary>$</primary> |
| 1892 |
</indexterm> |
| 1893 |
<indexterm> |
| 1894 |
<primary>special character</primary> |
| 1895 |
<secondary>$</secondary> |
| 1896 |
</indexterm> |
| 1897 |
<indexterm> |
| 1898 |
<primary>variable substitution</primary> |
| 1899 |
</indexterm> |
| 1900 |
<listitem> |
| 1901 |
<formalpara><title><link linkend="varsubn">Variable |
| 1902 |
substitution</link> (contents of a variable)</title> |
| 1903 |
<para> |
| 1904 |
<programlisting>var1=5 |
| 1905 |
var2=23skidoo |
| 1906 |
|
| 1907 |
echo $var1 # 5 |
| 1908 |
echo $var2 # 23skidoo</programlisting> |
| 1909 |
</para> |
| 1910 |
</formalpara> |
| 1911 |
|
| 1912 |
<para><anchor id="varprefixref"</para> |
| 1913 |
<para>A <token>$</token> prefixing a variable name |
| 1914 |
indicates the <firstterm>value</firstterm> the variable |
| 1915 |
holds.</para> |
| 1916 |
|
| 1917 |
</listitem> |
| 1918 |
</varlistentry> |
| 1919 |
|
| 1920 |
<varlistentry><term><token>$</token></term> |
| 1921 |
<indexterm> |
| 1922 |
<primary>$</primary> |
| 1923 |
</indexterm> |
| 1924 |
<indexterm> |
| 1925 |
<primary>special character</primary> |
| 1926 |
<secondary>$</secondary> |
| 1927 |
</indexterm> |
| 1928 |
<indexterm> |
| 1929 |
<primary>regular expression</primary> |
| 1930 |
</indexterm> |
| 1931 |
<indexterm> |
| 1932 |
<primary>end of line</primary> |
| 1933 |
</indexterm> |
| 1934 |
|
| 1935 |
<listitem> |
| 1936 |
<formalpara><title>end-of-line</title> |
| 1937 |
<para>In a <link linkend="regexref">regular expression</link>, a |
| 1938 |
<quote>$</quote> addresses the <link |
| 1939 |
linkend="dollarsignref">end of a line</link> of |
| 1940 |
text.</para> |
| 1941 |
</formalpara> |
| 1942 |
|
| 1943 |
</listitem> |
| 1944 |
</varlistentry> |
| 1945 |
|
| 1946 |
<varlistentry><term><token>${}</token></term> |
| 1947 |
<indexterm> |
| 1948 |
<primary>${}</primary> |
| 1949 |
</indexterm> |
| 1950 |
<indexterm> |
| 1951 |
<primary>special character</primary> |
| 1952 |
<secondary>${}</secondary> |
| 1953 |
</indexterm> |
| 1954 |
<indexterm> |
| 1955 |
<primary>parameter substitution</primary> |
| 1956 |
</indexterm> |
| 1957 |
<listitem> |
| 1958 |
<formalpara><title><link linkend="paramsubref">Parameter |
| 1959 |
substitution</link></title> |
| 1960 |
<para></para></formalpara> |
| 1961 |
</listitem> |
| 1962 |
</varlistentry> |
| 1963 |
|
| 1964 |
<varlistentry><term><token>$' ... '</token></term> |
| 1965 |
<indexterm> |
| 1966 |
<primary>$' ... '</primary> |
| 1967 |
</indexterm> |
| 1968 |
<indexterm> |
| 1969 |
<primary>special character</primary> |
| 1970 |
<secondary>$</secondary> |
| 1971 |
</indexterm> |
| 1972 |
<indexterm> |
| 1973 |
<primary>string expansion</primary> |
| 1974 |
</indexterm> |
| 1975 |
<indexterm> |
| 1976 |
<primary>string expansion</primary> |
| 1977 |
</indexterm> |
| 1978 |
|
| 1979 |
<listitem> |
| 1980 |
<formalpara><title><link linkend="strq">Quoted string |
| 1981 |
expansion</link></title> |
| 1982 |
<para>This construct expands single or multiple |
| 1983 |
escaped octal or hex values into ASCII |
| 1984 |
|
| 1985 |
<footnote> |
| 1986 |
<para><anchor id="asciidef"></para> |
| 1987 |
<para><command>A</command>merican |
| 1988 |
<command>S</command>tandard |
| 1989 |
<command>C</command>ode |
| 1990 |
for |
| 1991 |
<command>I</command>nformation |
| 1992 |
<command>I</command>nterchange. |
| 1993 |
This is a system for encoding text characters |
| 1994 |
(alphabetic, numeric, and a limited set of symbols) |
| 1995 |
as 7-bit numbers that can be stored and manipulated by |
| 1996 |
computers. Many of the ASCII characters are |
| 1997 |
represented on a standard keyboard.</para></footnote> |
| 1998 |
|
| 1999 |
or <link linkend="unicoderef">Unicode</link> |
| 2000 |
characters.</para> |
| 2001 |
</formalpara> |
| 2002 |
|
| 2003 |
</listitem> |
| 2004 |
</varlistentry> |
| 2005 |
|
| 2006 |
<varlistentry> |
| 2007 |
<term><token>$*</token></term> |
| 2008 |
<term><token>$@</token></term> |
| 2009 |
<indexterm> |
| 2010 |
<primary>$*</primary> |
| 2011 |
</indexterm> |
| 2012 |
<indexterm> |
| 2013 |
<primary>special character</primary> |
| 2014 |
<secondary>$*</secondary> |
| 2015 |
</indexterm> |
| 2016 |
<indexterm> |
| 2017 |
<primary>$@</primary> |
| 2018 |
<secondary>positional parameters</secondary> |
| 2019 |
</indexterm> |
| 2020 |
<indexterm> |
| 2021 |
<primary>$@</primary> |
| 2022 |
</indexterm> |
| 2023 |
|
| 2024 |
<listitem><formalpara><title><link linkend="appref">positional |
| 2025 |
parameters</link></title> |
| 2026 |
<para></para> |
| 2027 |
</formalpara> |
| 2028 |
</listitem> |
| 2029 |
</varlistentry> |
| 2030 |
|
| 2031 |
<varlistentry> |
| 2032 |
<term><token>$?</token></term> |
| 2033 |
<indexterm> |
| 2034 |
<primary>$?</primary> |
| 2035 |
</indexterm> |
| 2036 |
<indexterm> |
| 2037 |
<primary>special character</primary> |
| 2038 |
<secondary>?</secondary> |
| 2039 |
</indexterm> |
| 2040 |
<indexterm> |
| 2041 |
<primary>exit status</primary> |
| 2042 |
<secondary>variable</secondary> |
| 2043 |
</indexterm> |
| 2044 |
<indexterm> |
| 2045 |
<primary>exit status</primary> |
| 2046 |
</indexterm> |
| 2047 |
|
| 2048 |
<listitem><formalpara><title>exit status variable</title> |
| 2049 |
<para>The <link linkend="exsref">$? variable</link> |
| 2050 |
holds the <link linkend="exitstatusref">exit status</link> |
| 2051 |
of a command, a <link linkend="functionref">function</link>, |
| 2052 |
or of the script itself.</para> |
| 2053 |
</formalpara> |
| 2054 |
|
| 2055 |
</listitem> |
| 2056 |
</varlistentry> |
| 2057 |
|
| 2058 |
<varlistentry> |
| 2059 |
<term><anchor id="processidref"><token>$$</token></term> |
| 2060 |
<indexterm> |
| 2061 |
<primary>$$</primary> |
| 2062 |
</indexterm> |
| 2063 |
<indexterm> |
| 2064 |
<primary>special character</primary> |
| 2065 |
<secondary>$$</secondary> |
| 2066 |
</indexterm> |
| 2067 |
<indexterm> |
| 2068 |
<primary>process ID</primary> |
| 2069 |
<secondary>variable</secondary> |
| 2070 |
</indexterm> |
| 2071 |
<indexterm> |
| 2072 |
<primary>process ID</primary> |
| 2073 |
</indexterm> |
| 2074 |
|
| 2075 |
<listitem><formalpara><title>process ID variable</title> |
| 2076 |
<para>The <link linkend="proccid">$$ variable</link> |
| 2077 |
holds the <firstterm>process ID</firstterm> |
| 2078 |
<footnote> |
| 2079 |
<para><anchor id="processiddef"></para> |
| 2080 |
<para>A <firstterm>PID</firstterm>, or |
| 2081 |
<firstterm>process ID</firstterm>, is a number assigned |
| 2082 |
to a running process. The <firstterm>PID</firstterm>s |
| 2083 |
of running processes may be viewed with a <link |
| 2084 |
linkend="ppssref">ps</link> command. |
| 2085 |
</para> |
| 2086 |
<para><anchor id="processref"></para> |
| 2087 |
<para> |
| 2088 |
<userinput>Definition:</userinput> A |
| 2089 |
<firstterm>process</firstterm> is a currently |
| 2090 |
executing command (or program), sometimes referred |
| 2091 |
to as a <firstterm>job</firstterm>. </para> |
| 2092 |
</footnote> |
| 2093 |
of the script in which it appears.</para> |
| 2094 |
</formalpara> |
| 2095 |
|
| 2096 |
</listitem> |
| 2097 |
</varlistentry> |
| 2098 |
|
| 2099 |
|
| 2100 |
<varlistentry> |
| 2101 |
<term><anchor id="parensref"><token>()</token></term> |
| 2102 |
|
| 2103 |
<listitem> |
| 2104 |
<formalpara><title>command group</title> |
| 2105 |
<para><programlisting>(a=hello; echo $a)</programlisting></para> |
| 2106 |
</formalpara> |
| 2107 |
|
| 2108 |
<important> |
| 2109 |
|
| 2110 |
<para>A listing of commands within |
| 2111 |
<replaceable>parentheses</replaceable> starts a <link |
| 2112 |
linkend="subshellsref">subshell</link>.</para> |
| 2113 |
|
| 2114 |
<para>Variables inside parentheses, within the subshell, are not |
| 2115 |
visible to the rest of the script. The parent process, |
| 2116 |
the script, <link linkend="parvis">cannot read variables |
| 2117 |
created in the child process</link>, the subshell. |
| 2118 |
<programlisting>a=123 |
| 2119 |
( a=321; ) |
| 2120 |
|
| 2121 |
echo "a = $a" # a = 123 |
| 2122 |
# "a" within parentheses acts like a local variable.</programlisting></para> |
| 2123 |
</important> |
| 2124 |
|
| 2125 |
|
| 2126 |
<formalpara><title>array initialization</title> |
| 2127 |
<para> |
| 2128 |
<anchor id="arrayinit1"> |
| 2129 |
<programlisting>Array=(element1 element2 element3)</programlisting> |
| 2130 |
</para> |
| 2131 |
</formalpara> |
| 2132 |
</listitem> |
| 2133 |
|
| 2134 |
</varlistentry> |
| 2135 |
|
| 2136 |
<varlistentry> |
| 2137 |
<indexterm> |
| 2138 |
<primary>{xxx,yyy,zzz..}</primary> |
| 2139 |
</indexterm> |
| 2140 |
<indexterm> |
| 2141 |
<primary>special character</primary> |
| 2142 |
<secondary>{}</secondary> |
| 2143 |
</indexterm> |
| 2144 |
<indexterm> |
| 2145 |
<primary>brace expansion</primary> |
| 2146 |
</indexterm> |
| 2147 |
<term><token>{xxx,yyy,zzz,...}</token></term> |
| 2148 |
<listitem><formalpara><title>Brace expansion</title> |
| 2149 |
<para><anchor id="braceexpref"> |
| 2150 |
<programlisting>echo \"{These,words,are,quoted}\" # " prefix and suffix |
| 2151 |
# "These" "words" "are" "quoted" |
| 2152 |
|
| 2153 |
|
| 2154 |
cat {file1,file2,file3} > combined_file |
| 2155 |
# Concatenates the files file1, file2, and file3 into combined_file. |
| 2156 |
|
| 2157 |
cp file22.{txt,backup} |
| 2158 |
# Copies "file22.txt" to "file22.backup"</programlisting></para> |
| 2159 |
</formalpara> |
| 2160 |
|
| 2161 |
<para>A command may act upon a comma-separated list of file specs within |
| 2162 |
<replaceable>braces</replaceable>. |
| 2163 |
|
| 2164 |
<footnote><para>The shell does the <firstterm>brace |
| 2165 |
expansion</firstterm>. The command itself acts upon the |
| 2166 |
<emphasis>result</emphasis> of the expansion.</para></footnote> |
| 2167 |
|
| 2168 |
Filename expansion (<link linkend="globbingref">globbing</link>) |
| 2169 |
applies to the file specs between the braces.</para> |
| 2170 |
|
| 2171 |
<caution> |
| 2172 |
<para>No spaces allowed within the braces |
| 2173 |
<emphasis>unless</emphasis> the spaces are quoted or escaped.</para> |
| 2174 |
|
| 2175 |
<para><userinput>echo {file1,file2}\ :{\ A," B",' C'}</userinput></para> |
| 2176 |
<para><computeroutput>file1 : A file1 : B file1 : C file2 : A file2 : B file2 : C</computeroutput></para> |
| 2177 |
|
| 2178 |
</caution> |
| 2179 |
|
| 2180 |
</listitem> |
| 2181 |
</varlistentry> |
| 2182 |
|
| 2183 |
|
| 2184 |
<varlistentry> |
| 2185 |
<indexterm> |
| 2186 |
<primary>{a..z}</primary> |
| 2187 |
</indexterm> |
| 2188 |
<indexterm> |
| 2189 |
<primary>special character</primary> |
| 2190 |
<secondary>{}</secondary> |
| 2191 |
</indexterm> |
| 2192 |
<indexterm> |
| 2193 |
<primary>extended brace expansion</primary> |
| 2194 |
</indexterm> |
| 2195 |
<term><anchor id="braceexpref33"><token>{a..z}</token></term> |
| 2196 |
<listitem> |
| 2197 |
|
| 2198 |
<formalpara> |
| 2199 |
<title>Extended Brace expansion</title> |
| 2200 |
<para> |
| 2201 |
<programlisting>echo {a..z} # a b c d e f g h i j k l m n o p q r s t u v w x y z |
| 2202 |
# Echoes characters between a and z. |
| 2203 |
|
| 2204 |
echo {0..3} # 0 1 2 3 |
| 2205 |
# Echoes characters between 0 and 3. |
| 2206 |
|
| 2207 |
|
| 2208 |
base64_charset=( {A..Z} {a..z} {0..9} + / = ) |
| 2209 |
# Initializing an array, using extended brace expansion. |
| 2210 |
# From vladz's "base64.sh" example script.</programlisting> |
| 2211 |
</para> |
| 2212 |
</formalpara> |
| 2213 |
|
| 2214 |
<para>The <firstterm>{a..z}</firstterm> |
| 2215 |
<link linkend="braceexpref3">extended brace |
| 2216 |
expansion</link> construction is a feature introduced |
| 2217 |
in <link linkend="bash3ref">version 3</link> of |
| 2218 |
<firstterm>Bash</firstterm>.</para> |
| 2219 |
|
| 2220 |
</listitem> |
| 2221 |
</varlistentry> |
| 2222 |
|
| 2223 |
|
| 2224 |
|
| 2225 |
|
| 2226 |
<varlistentry> |
| 2227 |
<term><anchor id="codeblockref"><token>{}</token></term> |
| 2228 |
<indexterm> |
| 2229 |
<primary>{}</primary> |
| 2230 |
</indexterm> |
| 2231 |
<indexterm> |
| 2232 |
<primary>special character</primary> |
| 2233 |
<secondary>{}</secondary> |
| 2234 |
</indexterm> |
| 2235 |
<indexterm> |
| 2236 |
<primary>block of code</primary> |
| 2237 |
</indexterm> |
| 2238 |
|
| 2239 |
<listitem> |
| 2240 |
<formalpara><title>Block of code [curly brackets]</title> |
| 2241 |
<para>Also referred to as an <firstterm>inline group</firstterm>, |
| 2242 |
this construct, in effect, creates an <firstterm>anonymous |
| 2243 |
function</firstterm> (a function without a |
| 2244 |
name). However, unlike in a <quote>standard</quote> <link |
| 2245 |
linkend="functionref">function</link>, the variables |
| 2246 |
inside a code block remain visible to the remainder of the |
| 2247 |
script.</para></formalpara> |
| 2248 |
|
| 2249 |
<para> <screen><prompt>bash$ </prompt><userinput>{ local a; |
| 2250 |
a=123; }</userinput> |
| 2251 |
<computeroutput>bash: local: can only be used in a |
| 2252 |
function</computeroutput> |
| 2253 |
</screen> </para> |
| 2254 |
|
| 2255 |
<para><programlisting>a=123 |
| 2256 |
{ a=321; } |
| 2257 |
echo "a = $a" # a = 321 (value inside code block) |
| 2258 |
|
| 2259 |
# Thanks, S.C.</programlisting></para> |
| 2260 |
|
| 2261 |
|
| 2262 |
<para><anchor id="blockio"></para> |
| 2263 |
<para>The code block enclosed in braces may have <link |
| 2264 |
linkend="ioredirref">I/O redirected</link> to and from |
| 2265 |
it.</para> |
| 2266 |
|
| 2267 |
<example id="ex8"> |
| 2268 |
<title>Code blocks and I/O redirection</title> |
| 2269 |
<programlisting>&ex8;</programlisting> |
| 2270 |
</example> |
| 2271 |
|
| 2272 |
<para><anchor id="blockio2"></para> |
| 2273 |
<example id="rpmcheck"> |
| 2274 |
<title>Saving the output of a code block to a file</title> |
| 2275 |
<programlisting>&rpmcheck;</programlisting> |
| 2276 |
</example> |
| 2277 |
|
| 2278 |
<note><para>Unlike a command group within (parentheses), |
| 2279 |
as above, a code block enclosed by {braces} will |
| 2280 |
<emphasis>not</emphasis> normally launch a <link |
| 2281 |
linkend="subshellsref">subshell</link>. |
| 2282 |
|
| 2283 |
<footnote> |
| 2284 |
<para>Exception: a code block in braces as |
| 2285 |
part of a pipe <emphasis>may</emphasis> run as a |
| 2286 |
<link linkend="subshellsref">subshell</link>.</para> |
| 2287 |
|
| 2288 |
<para><programlisting>ls | { read firstline; read secondline; } |
| 2289 |
# Error. The code block in braces runs as a subshell, |
| 2290 |
#+ so the output of "ls" cannot be passed to variables within the block. |
| 2291 |
echo "First line is $firstline; second line is $secondline" # Won't work. |
| 2292 |
|
| 2293 |
# Thanks, S.C.</programlisting></para> |
| 2294 |
</footnote> |
| 2295 |
|
| 2296 |
</para> |
| 2297 |
|
| 2298 |
<para>It is possible to <link |
| 2299 |
linkend="iterationref">iterate</link> a code block |
| 2300 |
using a <link linkend="nododone">non-standard |
| 2301 |
<firstterm>for-loop</firstterm></link>.</para> |
| 2302 |
|
| 2303 |
</note> |
| 2304 |
|
| 2305 |
</listitem> |
| 2306 |
</varlistentry> |
| 2307 |
|
| 2308 |
<varlistentry> |
| 2309 |
<term><token>{}</token></term> |
| 2310 |
<listitem> |
| 2311 |
|
| 2312 |
<formalpara><title>placeholder for text</title> |
| 2313 |
<para>Used after <link linkend="xargscurlyref">xargs |
| 2314 |
<option>-i</option></link> (<firstterm>replace |
| 2315 |
strings</firstterm> option). The <token>{}</token> double |
| 2316 |
curly brackets are a placeholder for output text.</para> |
| 2317 |
</formalpara> |
| 2318 |
|
| 2319 |
<para><programlisting>ls . | xargs -i -t cp ./{} $1 |
| 2320 |
# ^^ ^^ |
| 2321 |
|
| 2322 |
# From "ex42.sh" (copydir.sh) example.</programlisting></para> |
| 2323 |
<para><anchor id="semicolonesc"></para> |
| 2324 |
|
| 2325 |
</listitem> |
| 2326 |
</varlistentry> |
| 2327 |
|
| 2328 |
|
| 2329 |
<varlistentry> |
| 2330 |
<term><token>{} \;</token></term> |
| 2331 |
<listitem> |
| 2332 |
|
| 2333 |
<formalpara><title>pathname</title> |
| 2334 |
<para>Mostly used in <link linkend="findref">find</link> |
| 2335 |
constructs. This is <emphasis>not</emphasis> a shell |
| 2336 |
<link linkend="builtinref">builtin</link>.</para> |
| 2337 |
</formalpara> |
| 2338 |
|
| 2339 |
<sidebar><para><anchor id="pathnameref"></para> |
| 2340 |
<para>Definition: A <firstterm>pathname</firstterm> |
| 2341 |
is a <firstterm>filename</firstterm> that includes the |
| 2342 |
complete <link linkend="pathref">path</link>. As an example, |
| 2343 |
<filename>/home/bozo/Notes/Thursday/schedule.txt</filename>. |
| 2344 |
This is sometimes referred to as the <firstterm>absolute |
| 2345 |
path</firstterm>.</para></sidebar> |
| 2346 |
|
| 2347 |
<note><para>The <quote><token>;</token></quote> ends |
| 2348 |
the <option>-exec</option> option of a |
| 2349 |
<command>find</command> command sequence. It needs |
| 2350 |
to be escaped to protect it from interpretation by the |
| 2351 |
shell.</para></note> |
| 2352 |
|
| 2353 |
</listitem> |
| 2354 |
</varlistentry> |
| 2355 |
|
| 2356 |
|
| 2357 |
|
| 2358 |
<varlistentry><term><anchor id="leftbracket"><token>[ ]</token></term> |
| 2359 |
<indexterm> |
| 2360 |
<primary>[]</primary> |
| 2361 |
</indexterm> |
| 2362 |
<indexterm> |
| 2363 |
<primary>special character</primary> |
| 2364 |
<secondary>[ ]</secondary> |
| 2365 |
</indexterm> |
| 2366 |
<indexterm> |
| 2367 |
<primary>test</primary> |
| 2368 |
</indexterm> |
| 2369 |
<listitem> |
| 2370 |
<formalpara><title>test</title> |
| 2371 |
<para><anchor id="bracktest"></para></formalpara> |
| 2372 |
<para><link linkend="ifthen">Test</link> expression between |
| 2373 |
<command>[ ]</command>. Note that <command>[</command> |
| 2374 |
is part of the shell <firstterm>builtin</firstterm> <link |
| 2375 |
linkend="ttestref">test</link> (and a synonym for it), |
| 2376 |
<emphasis>not</emphasis> a link to the external command |
| 2377 |
<filename>/usr/bin/test</filename>.</para> |
| 2378 |
</listitem> |
| 2379 |
</varlistentry> |
| 2380 |
|
| 2381 |
<varlistentry><term><token>[[ ]]</token></term> |
| 2382 |
<indexterm> |
| 2383 |
<primary>[[]]</primary> |
| 2384 |
</indexterm> |
| 2385 |
<indexterm> |
| 2386 |
<primary>special character</primary> |
| 2387 |
<secondary>[[ ]]</secondary> |
| 2388 |
</indexterm> |
| 2389 |
<indexterm> |
| 2390 |
<primary>test</primary> |
| 2391 |
</indexterm> |
| 2392 |
<listitem> |
| 2393 |
<formalpara><title>test</title> |
| 2394 |
<para></para></formalpara> |
| 2395 |
<para>Test expression between <token>[[ ]]</token>. More |
| 2396 |
flexible than the single-bracket <token>[ ]</token> test, |
| 2397 |
this is a shell <link |
| 2398 |
linkend="keywordref">keyword</link>.</para> <para>See the |
| 2399 |
discussion on the <link linkend="dblbrackets">[[ ... ]] |
| 2400 |
construct</link>.</para> |
| 2401 |
</listitem> |
| 2402 |
|
| 2403 |
</varlistentry> |
| 2404 |
|
| 2405 |
<varlistentry><term><token>[ ]</token></term> |
| 2406 |
<indexterm> |
| 2407 |
<primary>[ ]</primary> |
| 2408 |
</indexterm> |
| 2409 |
<indexterm> |
| 2410 |
<primary>special character</primary> |
| 2411 |
<secondary>array_element[ ]</secondary> |
| 2412 |
</indexterm> |
| 2413 |
<indexterm> |
| 2414 |
<primary>array element</primary> |
| 2415 |
</indexterm> |
| 2416 |
<listitem> |
| 2417 |
<formalpara><title>array element</title> |
| 2418 |
<para></para></formalpara> |
| 2419 |
<para>In the context of an <link linkend="arrayref">array</link>, |
| 2420 |
brackets set off the numbering of each element of that array. |
| 2421 |
<programlisting>Array[1]=slot_1 |
| 2422 |
echo ${Array[1]}</programlisting></para> |
| 2423 |
</listitem> |
| 2424 |
</varlistentry> |
| 2425 |
|
| 2426 |
<varlistentry><term><token>[ ]</token></term> |
| 2427 |
<indexterm> |
| 2428 |
<primary>[ ]</primary> |
| 2429 |
</indexterm> |
| 2430 |
<indexterm> |
| 2431 |
<primary>special character</primary> |
| 2432 |
<secondary>character range</secondary> |
| 2433 |
</indexterm> |
| 2434 |
<indexterm> |
| 2435 |
<primary>regular expression</primary> |
| 2436 |
</indexterm> |
| 2437 |
<listitem> |
| 2438 |
<formalpara><title>range of characters</title> |
| 2439 |
<para></para></formalpara> |
| 2440 |
<para>As part of a <link linkend="regexref">regular |
| 2441 |
expression</link>, brackets delineate a <link |
| 2442 |
linkend="bracketsref">range of characters</link> to |
| 2443 |
match.</para> |
| 2444 |
</listitem> |
| 2445 |
</varlistentry> |
| 2446 |
|
| 2447 |
<varlistentry><term><anchor id="bracketarith"><token>$[ ... ]</token></term> |
| 2448 |
<indexterm> |
| 2449 |
<primary>$[ ]</primary> |
| 2450 |
</indexterm> |
| 2451 |
<indexterm> |
| 2452 |
<primary>special character</primary> |
| 2453 |
<secondary>integer expansion</secondary> |
| 2454 |
</indexterm> |
| 2455 |
<indexterm> |
| 2456 |
<primary>integer arithmetic (obsolete)</primary> |
| 2457 |
</indexterm> |
| 2458 |
<listitem> |
| 2459 |
<formalpara><title>integer expansion</title> |
| 2460 |
<para></para></formalpara> |
| 2461 |
<para>Evaluate integer expression between |
| 2462 |
<token>$[ ]</token>. |
| 2463 |
<programlisting>a=3 |
| 2464 |
b=7 |
| 2465 |
|
| 2466 |
echo $[$a+$b] # 10 |
| 2467 |
echo $[$a*$b] # 21</programlisting></para> |
| 2468 |
|
| 2469 |
<para>Note that this usage is <emphasis>deprecated</emphasis>, |
| 2470 |
and has been replaced by the |
| 2471 |
<link linkend="dblparens">(( ... ))</link> construct.</para> |
| 2472 |
</listitem> |
| 2473 |
</varlistentry> |
| 2474 |
|
| 2475 |
|
| 2476 |
<varlistentry><term><token>(( ))</token></term> |
| 2477 |
<indexterm> |
| 2478 |
<primary>(( ))</primary> |
| 2479 |
</indexterm> |
| 2480 |
<indexterm> |
| 2481 |
<primary>special character</primary> |
| 2482 |
<secondary>(( ))</secondary> |
| 2483 |
</indexterm> |
| 2484 |
<indexterm> |
| 2485 |
<primary>integer comparison</primary> |
| 2486 |
</indexterm> |
| 2487 |
<listitem> |
| 2488 |
<formalpara><title>integer expansion</title> |
| 2489 |
<para></para></formalpara> |
| 2490 |
<para>Expand and evaluate integer expression between |
| 2491 |
<token>(( ))</token>.</para> |
| 2492 |
<para>See the discussion on the <link |
| 2493 |
linkend="dblparens">(( ... )) construct</link>.</para> |
| 2494 |
</listitem> |
| 2495 |
</varlistentry> |
| 2496 |
|
| 2497 |
|
| 2498 |
<varlistentry> |
| 2499 |
<term><token>></token> <token>&></token> <token>>&</token> <token>>></token> <token><</token> <token><></token></term> |
| 2500 |
<indexterm> |
| 2501 |
<primary>></primary> |
| 2502 |
</indexterm> |
| 2503 |
<indexterm> |
| 2504 |
<primary>>&</primary> |
| 2505 |
</indexterm> |
| 2506 |
<indexterm> |
| 2507 |
<primary>>></primary> |
| 2508 |
</indexterm> |
| 2509 |
<indexterm> |
| 2510 |
<primary><</primary> |
| 2511 |
</indexterm> |
| 2512 |
<indexterm> |
| 2513 |
<primary>special character</primary> |
| 2514 |
<secondary>></secondary> |
| 2515 |
</indexterm> |
| 2516 |
<indexterm> |
| 2517 |
<primary>special character</primary> |
| 2518 |
<secondary>>&</secondary> |
| 2519 |
</indexterm> |
| 2520 |
<indexterm> |
| 2521 |
<primary>special character</primary> |
| 2522 |
<secondary>>></secondary> |
| 2523 |
</indexterm> |
| 2524 |
<indexterm> |
| 2525 |
<primary>special character</primary> |
| 2526 |
<secondary><</secondary> |
| 2527 |
</indexterm> |
| 2528 |
<indexterm> |
| 2529 |
<primary>redirection</primary> |
| 2530 |
</indexterm> |
| 2531 |
|
| 2532 |
<listitem><formalpara><title><link linkend="ioredirref">redirection</link></title> |
| 2533 |
<para></para> |
| 2534 |
</formalpara> |
| 2535 |
|
| 2536 |
<para><userinput>scriptname >filename</userinput> redirects the output of |
| 2537 |
<filename>scriptname</filename> to file |
| 2538 |
<filename>filename</filename>. Overwrite |
| 2539 |
<filename>filename</filename> if it already exists.</para> |
| 2540 |
|
| 2541 |
<para><anchor id="redirouterror"></para> |
| 2542 |
<para><userinput>command &>filename</userinput> redirects |
| 2543 |
both the <link |
| 2544 |
linkend="stdinoutdef"><filename>stdout</filename></link> |
| 2545 |
and the |
| 2546 |
<filename>stderr</filename> of <filename>command</filename> |
| 2547 |
to <filename>filename</filename>.</para> |
| 2548 |
|
| 2549 |
|
| 2550 |
<note> |
| 2551 |
|
| 2552 |
<para> |
| 2553 |
<anchor id="devnullredirect"> |
| 2554 |
This is useful for suppressing output when |
| 2555 |
testing for a condition. For example, let us |
| 2556 |
test whether a certain command exists. |
| 2557 |
</para> |
| 2558 |
|
| 2559 |
<para> |
| 2560 |
<screen> |
| 2561 |
<prompt>bash$ </prompt><userinput>type bogus_command &>/dev/null</userinput> |
| 2562 |
<computeroutput></computeroutput> |
| 2563 |
|
| 2564 |
|
| 2565 |
<prompt>bash$ </prompt><userinput>echo $?</userinput> |
| 2566 |
<computeroutput>1</computeroutput> |
| 2567 |
</screen> |
| 2568 |
</para> |
| 2569 |
|
| 2570 |
<para>Or in a script:</para> |
| 2571 |
|
| 2572 |
<para><programlisting>command_test () { type "$1" &>/dev/null; } |
| 2573 |
# ^ |
| 2574 |
|
| 2575 |
cmd=rmdir # Legitimate command. |
| 2576 |
command_test $cmd; echo $? # 0 |
| 2577 |
|
| 2578 |
|
| 2579 |
cmd=bogus_command # Illegitimate command |
| 2580 |
command_test $cmd; echo $? # 1</programlisting></para> |
| 2581 |
|
| 2582 |
</note> |
| 2583 |
|
| 2584 |
|
| 2585 |
<para><anchor id="redirouterror2"></para> |
| 2586 |
<para><userinput>command >&2</userinput> redirects |
| 2587 |
<filename>stdout</filename> of <filename>command</filename> |
| 2588 |
to <filename>stderr</filename>.</para> |
| 2589 |
|
| 2590 |
<para><userinput>scriptname >>filename</userinput> appends |
| 2591 |
the output of <filename>scriptname</filename> |
| 2592 |
to file <filename>filename</filename>. If |
| 2593 |
<filename>filename</filename> does not already exist, |
| 2594 |
it is created.</para> |
| 2595 |
|
| 2596 |
<para><anchor id="redirrw"></para> |
| 2597 |
<para><userinput>[i]<>filename</userinput> |
| 2598 |
opens file <filename>filename</filename> for reading |
| 2599 |
and writing, and assigns <link linkend="fdref">file |
| 2600 |
descriptor</link> <token>i</token> to it. If |
| 2601 |
<filename>filename</filename> does not exist, it is |
| 2602 |
created.</para> |
| 2603 |
|
| 2604 |
|
| 2605 |
<formalpara><title><link linkend="processsubref">process substitution</link></title> |
| 2606 |
<para></para> |
| 2607 |
</formalpara> |
| 2608 |
|
| 2609 |
<para><userinput>(command)></userinput></para> |
| 2610 |
<para><userinput><(command)</userinput></para> |
| 2611 |
|
| 2612 |
|
| 2613 |
<para><link linkend="ltref">In a different context</link>, |
| 2614 |
the <quote><token><</token></quote> and |
| 2615 |
<quote><token>></token></quote> characters act |
| 2616 |
as <link linkend="scomparison1">string comparison |
| 2617 |
operators</link>.</para> |
| 2618 |
|
| 2619 |
<para><link linkend="intlt">In yet another context</link>, |
| 2620 |
the <quote><token><</token></quote> and |
| 2621 |
<quote><token>></token></quote> characters act |
| 2622 |
as <link linkend="icomparison1">integer comparison |
| 2623 |
operators</link>. See also <xref linkend="ex45">.</para> |
| 2624 |
|
| 2625 |
</listitem> |
| 2626 |
|
| 2627 |
|
| 2628 |
</varlistentry> |
| 2629 |
|
| 2630 |
<varlistentry> |
| 2631 |
<term><anchor id="heredocrrref"><token><<</token></term> |
| 2632 |
<listitem><formalpara><title>redirection used in a <link |
| 2633 |
linkend="heredocref">here document</link></title> |
| 2634 |
<para></para> |
| 2635 |
</formalpara> |
| 2636 |
</listitem> |
| 2637 |
</varlistentry> |
| 2638 |
|
| 2639 |
<varlistentry> |
| 2640 |
<term><anchor id="herestringref"><token><<<</token></term> |
| 2641 |
<listitem><formalpara><title>redirection used in a <link |
| 2642 |
linkend="herestringsref">here string</link></title> |
| 2643 |
<para></para> |
| 2644 |
</formalpara> |
| 2645 |
</listitem> |
| 2646 |
</varlistentry> |
| 2647 |
|
| 2648 |
<varlistentry> |
| 2649 |
<term><token><</token></term> |
| 2650 |
<term><token>></token></term> |
| 2651 |
<indexterm> |
| 2652 |
<primary><</primary> |
| 2653 |
</indexterm> |
| 2654 |
<indexterm> |
| 2655 |
<primary>special character</primary> |
| 2656 |
<secondary><</secondary> |
| 2657 |
</indexterm> |
| 2658 |
<indexterm> |
| 2659 |
<primary>></primary> |
| 2660 |
<secondary>ASCII comparison</secondary> |
| 2661 |
</indexterm> |
| 2662 |
<indexterm> |
| 2663 |
<primary>></primary> |
| 2664 |
</indexterm> |
| 2665 |
|
| 2666 |
<listitem><formalpara><title><link linkend="ltref">ASCII |
| 2667 |
comparison</link></title> |
| 2668 |
<para><programlisting>veg1=carrots |
| 2669 |
veg2=tomatoes |
| 2670 |
|
| 2671 |
if [[ "$veg1" < "$veg2" ]] |
| 2672 |
then |
| 2673 |
echo "Although $veg1 precede $veg2 in the dictionary," |
| 2674 |
echo -n "this does not necessarily imply anything " |
| 2675 |
echo "about my culinary preferences." |
| 2676 |
else |
| 2677 |
echo "What kind of dictionary are you using, anyhow?" |
| 2678 |
fi</programlisting></para> |
| 2679 |
</formalpara> |
| 2680 |
</listitem> |
| 2681 |
</varlistentry> |
| 2682 |
|
| 2683 |
<varlistentry> |
| 2684 |
<term><token>\<</token></term> |
| 2685 |
<term><token>\></token></term> |
| 2686 |
<indexterm> |
| 2687 |
<primary>\<</primary> |
| 2688 |
</indexterm> |
| 2689 |
<indexterm> |
| 2690 |
<primary>regular expression</primary> |
| 2691 |
<secondary>\<</secondary> |
| 2692 |
</indexterm> |
| 2693 |
<indexterm> |
| 2694 |
<primary>></primary> |
| 2695 |
<secondary>word boundary</secondary> |
| 2696 |
</indexterm> |
| 2697 |
<indexterm> |
| 2698 |
<primary>></primary> |
| 2699 |
</indexterm> |
| 2700 |
|
| 2701 |
<listitem><formalpara><title><link linkend="anglebrac">word |
| 2702 |
boundary</link> in a <link linkend="regexref">regular |
| 2703 |
expression</link></title> |
| 2704 |
<para></para> |
| 2705 |
</formalpara> |
| 2706 |
<para><prompt>bash$ </prompt><userinput>grep '\<the\>' textfile</userinput></para> |
| 2707 |
</listitem> |
| 2708 |
</varlistentry> |
| 2709 |
|
| 2710 |
<varlistentry> |
| 2711 |
<term><token>|</token></term> |
| 2712 |
<indexterm> |
| 2713 |
<primary>|</primary> |
| 2714 |
</indexterm> |
| 2715 |
<indexterm> |
| 2716 |
<primary>special character</primary> |
| 2717 |
<secondary>|</secondary> |
| 2718 |
</indexterm> |
| 2719 |
<indexterm> |
| 2720 |
<primary>pipe</primary> |
| 2721 |
</indexterm> |
| 2722 |
|
| 2723 |
<listitem> |
| 2724 |
<para><anchor id="piperef"></para> |
| 2725 |
<formalpara><title>pipe</title> |
| 2726 |
<para>Passes the output (<filename>stdout</filename>) |
| 2727 |
of a previous command to the input |
| 2728 |
(<filename>stdin</filename>) of the next one, or |
| 2729 |
to the shell. This is a method of chaining commands |
| 2730 |
together.</para> |
| 2731 |
</formalpara> |
| 2732 |
|
| 2733 |
<para> |
| 2734 |
<programlisting>echo ls -l | sh |
| 2735 |
# Passes the output of "echo ls -l" to the shell, |
| 2736 |
#+ with the same result as a simple "ls -l". |
| 2737 |
|
| 2738 |
|
| 2739 |
cat *.lst | sort | uniq |
| 2740 |
# Merges and sorts all ".lst" files, then deletes duplicate lines.</programlisting> |
| 2741 |
</para> |
| 2742 |
|
| 2743 |
<sidebar> |
| 2744 |
|
| 2745 |
<para> |
| 2746 |
A pipe, as a classic method of interprocess |
| 2747 |
communication, sends the <filename>stdout</filename> |
| 2748 |
of one <link linkend="processref">process</link> to the |
| 2749 |
<filename>stdin</filename> of another. In a typical case, |
| 2750 |
a command, such as <link linkend="catref">cat</link> or |
| 2751 |
<link linkend="echoref">echo</link>, pipes a stream of |
| 2752 |
data to a |
| 2753 |
<anchor id="filterdef"> |
| 2754 |
<firstterm>filter</firstterm>, a command that |
| 2755 |
transforms its input for processing. |
| 2756 |
<footnote><para> Even as in olden times a |
| 2757 |
<firstterm>philtre</firstterm> denoted a potion alleged |
| 2758 |
to have magical transformative powers, so does a UNIX |
| 2759 |
<firstterm>filter</firstterm> transform its target in |
| 2760 |
(roughly) analogous fashion. (The coder who comes up with a |
| 2761 |
<quote>love philtre</quote> that runs on a Linux machine |
| 2762 |
will likely win accolades and honors.)</para></footnote> |
| 2763 |
</para> |
| 2764 |
|
| 2765 |
<para> |
| 2766 |
<userinput>cat $filename1 $filename2 | grep $search_word</userinput> |
| 2767 |
</para> |
| 2768 |
|
| 2769 |
<para>For an interesting note on the complexity of using UNIX |
| 2770 |
pipes, see <ulink |
| 2771 |
url="http://www.faqs.org/faqs/unix-faq/faq/part3/">the UNIX FAQ, |
| 2772 |
Part 3</ulink>.</para> |
| 2773 |
|
| 2774 |
</sidebar> |
| 2775 |
|
| 2776 |
|
| 2777 |
<para><anchor id="ucref">The output of a command or commands |
| 2778 |
may be piped to a script. |
| 2779 |
|
| 2780 |
<programlisting>#!/bin/bash |
| 2781 |
# uppercase.sh : Changes input to uppercase. |
| 2782 |
|
| 2783 |
tr 'a-z' 'A-Z' |
| 2784 |
# Letter ranges must be quoted |
| 2785 |
#+ to prevent filename generation from single-letter filenames. |
| 2786 |
|
| 2787 |
exit 0</programlisting> |
| 2788 |
Now, let us pipe the output of <command>ls -l</command> to this |
| 2789 |
script. |
| 2790 |
|
| 2791 |
<screen><prompt>bash$ </prompt><userinput>ls -l | ./uppercase.sh</userinput> |
| 2792 |
<computeroutput>-RW-RW-R-- 1 BOZO BOZO 109 APR 7 19:49 1.TXT |
| 2793 |
-RW-RW-R-- 1 BOZO BOZO 109 APR 14 16:48 2.TXT |
| 2794 |
-RW-R--R-- 1 BOZO BOZO 725 APR 20 20:56 DATA-FILE</computeroutput> |
| 2795 |
</screen> |
| 2796 |
</para> |
| 2797 |
|
| 2798 |
<note> |
| 2799 |
|
| 2800 |
<para>The <filename>stdout</filename> of each process in |
| 2801 |
a pipe must be read as the <filename>stdin</filename> |
| 2802 |
of the next. If this is not the case, the data stream |
| 2803 |
will <firstterm>block</firstterm>, and the pipe will not |
| 2804 |
behave as expected. |
| 2805 |
<programlisting>cat file1 file2 | ls -l | sort |
| 2806 |
# The output from "cat file1 file2" disappears.</programlisting> |
| 2807 |
</para> |
| 2808 |
|
| 2809 |
<para>A pipe runs as a <link linkend="childref">child |
| 2810 |
process</link>, and therefore cannot alter script |
| 2811 |
variables. |
| 2812 |
<programlisting>variable="initial_value" |
| 2813 |
echo "new_value" | read variable |
| 2814 |
echo "variable = $variable" # variable = initial_value</programlisting> |
| 2815 |
</para> |
| 2816 |
|
| 2817 |
<para>If one of the commands in the pipe |
| 2818 |
aborts, this prematurely terminates execution of the |
| 2819 |
pipe. Called a <firstterm>broken pipe</firstterm>, this |
| 2820 |
condition sends a <replaceable>SIGPIPE</replaceable> <link |
| 2821 |
linkend="signald">signal</link>.</para> |
| 2822 |
|
| 2823 |
</note> |
| 2824 |
|
| 2825 |
|
| 2826 |
</listitem> |
| 2827 |
</varlistentry> |
| 2828 |
|
| 2829 |
<varlistentry> |
| 2830 |
<term><token>>|</token></term> |
| 2831 |
<indexterm> |
| 2832 |
<primary>>|</primary> |
| 2833 |
</indexterm> |
| 2834 |
<indexterm> |
| 2835 |
<primary>special character</primary> |
| 2836 |
<secondary>>|</secondary> |
| 2837 |
</indexterm> |
| 2838 |
<indexterm> |
| 2839 |
<primary>redirection</primary> |
| 2840 |
<secondary>force</secondary> |
| 2841 |
</indexterm> |
| 2842 |
<indexterm> |
| 2843 |
<primary>noclobber</primary> |
| 2844 |
</indexterm> |
| 2845 |
|
| 2846 |
<listitem><formalpara><title>force redirection (even if |
| 2847 |
the <link linkend="noclobberref">noclobber option</link> |
| 2848 |
is set)</title> |
| 2849 |
<para>This will forcibly overwrite an existing file.</para> |
| 2850 |
</formalpara> |
| 2851 |
</listitem> |
| 2852 |
</varlistentry> |
| 2853 |
|
| 2854 |
<varlistentry><term><token>||</token></term> |
| 2855 |
<indexterm> |
| 2856 |
<primary>||</primary> |
| 2857 |
</indexterm> |
| 2858 |
<indexterm> |
| 2859 |
<primary>special character</primary> |
| 2860 |
<secondary>||</secondary> |
| 2861 |
</indexterm> |
| 2862 |
<indexterm> |
| 2863 |
<primary>or</primary> |
| 2864 |
</indexterm> |
| 2865 |
<indexterm> |
| 2866 |
<primary>logical operator</primary> |
| 2867 |
</indexterm> |
| 2868 |
|
| 2869 |
<listitem> |
| 2870 |
<formalpara><title><link linkend="orref">OR logical operator</link></title> |
| 2871 |
<para>In a <link linkend="testconstructs1">test |
| 2872 |
construct</link>, the <token>||</token> operator causes |
| 2873 |
a return of <returnvalue>0</returnvalue> (success) if |
| 2874 |
<emphasis>either</emphasis> of the linked test conditions |
| 2875 |
is true.</para> |
| 2876 |
</formalpara> |
| 2877 |
|
| 2878 |
</listitem> |
| 2879 |
</varlistentry> |
| 2880 |
|
| 2881 |
|
| 2882 |
<varlistentry> |
| 2883 |
<term><anchor id="bgjob"><token>&</token></term> |
| 2884 |
<listitem><formalpara><title>Run job in background</title> |
| 2885 |
<para>A command followed by an <token>&</token> |
| 2886 |
will run in the background.</para> |
| 2887 |
</formalpara> |
| 2888 |
|
| 2889 |
<para> |
| 2890 |
<screen><prompt>bash$ </prompt><userinput>sleep 10 &</userinput> |
| 2891 |
<computeroutput>[1] 850</computeroutput> |
| 2892 |
<computeroutput>[1]+ Done sleep 10</computeroutput> |
| 2893 |
</screen> |
| 2894 |
</para> |
| 2895 |
|
| 2896 |
<para>Within a script, commands and even <link |
| 2897 |
linkend="forloopref1">loops</link> may run in the |
| 2898 |
background.</para> |
| 2899 |
|
| 2900 |
<para><anchor id="bgloop0"></para> |
| 2901 |
<example id="bgloop"> |
| 2902 |
<title>Running a loop in the background</title> |
| 2903 |
<programlisting>&bgloop;</programlisting> |
| 2904 |
</example> |
| 2905 |
|
| 2906 |
<caution><para>A command run in the background within a |
| 2907 |
script may cause the script to hang, waiting |
| 2908 |
for a keystroke. Fortunately, there is a <link |
| 2909 |
linkend="waithang">remedy</link> for this.</para></caution> |
| 2910 |
|
| 2911 |
</listitem> |
| 2912 |
</varlistentry> |
| 2913 |
|
| 2914 |
<varlistentry><term><anchor |
| 2915 |
id="logicaland"><token>&&</token></term> |
| 2916 |
<indexterm> |
| 2917 |
<primary>&&</primary> |
| 2918 |
</indexterm> |
| 2919 |
<indexterm> |
| 2920 |
<primary>special character</primary> |
| 2921 |
<secondary>&&</secondary> |
| 2922 |
</indexterm> |
| 2923 |
<indexterm> |
| 2924 |
<primary>and</primary> |
| 2925 |
</indexterm> |
| 2926 |
<indexterm> |
| 2927 |
<primary>logical operator</primary> |
| 2928 |
</indexterm> |
| 2929 |
|
| 2930 |
<listitem> |
| 2931 |
<formalpara><title><link linkend="logops1">AND logical |
| 2932 |
operator</link></title> |
| 2933 |
|
| 2934 |
<para>In a <link linkend="testconstructs1">test |
| 2935 |
construct</link>, the <token>&&</token> operator causes |
| 2936 |
a return of <returnvalue>0</returnvalue> (success) only if |
| 2937 |
<emphasis>both</emphasis> the linked test conditions |
| 2938 |
are true.</para> |
| 2939 |
|
| 2940 |
</formalpara> |
| 2941 |
|
| 2942 |
</listitem> |
| 2943 |
</varlistentry> |
| 2944 |
|
| 2945 |
<varlistentry> |
| 2946 |
<term><anchor id="dashref"><token>-</token></term> |
| 2947 |
<listitem><formalpara><title>option, prefix</title> |
| 2948 |
<para>Option flag for a command or filter. Prefix for |
| 2949 |
an operator. Prefix for a <link |
| 2950 |
linkend="defparam1">default parameter</link> |
| 2951 |
in <link linkend="paramsubref">parameter |
| 2952 |
substitution</link>.</para> |
| 2953 |
</formalpara> |
| 2954 |
<para><userinput>COMMAND -[Option1][Option2][...]</userinput></para> |
| 2955 |
<para><userinput>ls -al</userinput></para> |
| 2956 |
<para><userinput>sort -dfu $filename</userinput></para> |
| 2957 |
|
| 2958 |
|
| 2959 |
|
| 2960 |
<para> |
| 2961 |
<programlisting>if [ $file1 -ot $file2 ] |
| 2962 |
then # ^ |
| 2963 |
echo "File $file1 is older than $file2." |
| 2964 |
fi |
| 2965 |
|
| 2966 |
if [ "$a" -eq "$b" ] |
| 2967 |
then # ^ |
| 2968 |
echo "$a is equal to $b." |
| 2969 |
fi |
| 2970 |
|
| 2971 |
if [ "$c" -eq 24 -a "$d" -eq 47 ] |
| 2972 |
then # ^ ^ |
| 2973 |
echo "$c equals 24 and $d equals 47." |
| 2974 |
fi |
| 2975 |
|
| 2976 |
|
| 2977 |
param2=${param1:-$DEFAULTVAL} |
| 2978 |
# ^</programlisting> |
| 2979 |
</para> |
| 2980 |
|
| 2981 |
<para><anchor id="doubledashref"></para> |
| 2982 |
<para><command>--</command></para> |
| 2983 |
|
| 2984 |
<para>The <firstterm>double-dash</firstterm> |
| 2985 |
<option>--</option> prefixes <firstterm>long</firstterm> |
| 2986 |
(verbatim) options to commands.</para> |
| 2987 |
|
| 2988 |
<para><userinput>sort --ignore-leading-blanks</userinput></para> |
| 2989 |
|
| 2990 |
<para>Used with a <link linkend="builtinref">Bash |
| 2991 |
builtin</link>, it means the <firstterm>end of |
| 2992 |
options</firstterm> to that particular command.</para> |
| 2993 |
|
| 2994 |
<tip><para>This provides a handy means of removing |
| 2995 |
files whose <emphasis>names begin with a dash</emphasis>. |
| 2996 |
<screen><prompt>bash$ </prompt><userinput>ls -l</userinput> |
| 2997 |
<computeroutput>-rw-r--r-- 1 bozo bozo 0 Nov 25 12:29 -badname</computeroutput> |
| 2998 |
|
| 2999 |
|
| 3000 |
<prompt>bash$ </prompt><userinput>rm -- -badname</userinput> |
| 3001 |
|
| 3002 |
<prompt>bash$ </prompt><userinput>ls -l</userinput> |
| 3003 |
<computeroutput>total 0</computeroutput></screen> |
| 3004 |
</para></tip> |
| 3005 |
|
| 3006 |
<para>The <firstterm>double-dash</firstterm> is also used in |
| 3007 |
conjunction with <link linkend="setref">set</link>.</para> |
| 3008 |
|
| 3009 |
<para><userinput>set -- $variable</userinput> (as in <xref |
| 3010 |
linkend="setpos">)</para> |
| 3011 |
|
| 3012 |
|
| 3013 |
</listitem> |
| 3014 |
</varlistentry> |
| 3015 |
|
| 3016 |
<varlistentry> |
| 3017 |
<term><anchor id="dashref2"><token>-</token></term> |
| 3018 |
<indexterm> |
| 3019 |
<primary>-</primary> |
| 3020 |
</indexterm> |
| 3021 |
<indexterm> |
| 3022 |
<primary>special character</primary> |
| 3023 |
<secondary>-</secondary> |
| 3024 |
</indexterm> |
| 3025 |
<indexterm> |
| 3026 |
<primary>redirection</primary> |
| 3027 |
<secondary>from/to stdin/stdout</secondary> |
| 3028 |
</indexterm> |
| 3029 |
<listitem><formalpara><title>redirection from/to <filename>stdin</filename> or <filename>stdout</filename> [dash]</title> |
| 3030 |
<para><anchor id="coxex"></para> |
| 3031 |
</formalpara> |
| 3032 |
|
| 3033 |
<para> |
| 3034 |
<screen><prompt>bash$ </prompt><userinput>cat -</userinput> |
| 3035 |
<userinput>abc</userinput> |
| 3036 |
<computeroutput>abc</computeroutput> |
| 3037 |
|
| 3038 |
<computeroutput>...</computeroutput> |
| 3039 |
|
| 3040 |
<userinput>Ctl-D</userinput></screen> |
| 3041 |
</para> |
| 3042 |
|
| 3043 |
<para>As expected, <userinput>cat -</userinput> echoes |
| 3044 |
<filename>stdin</filename>, in this case keyboarded user input, |
| 3045 |
to <filename>stdout</filename>. But, does I/O redirection using |
| 3046 |
<command>-</command> have real-world applications?</para> |
| 3047 |
|
| 3048 |
<para><programlisting>(cd /source/directory && tar cf - . ) | (cd /dest/directory && tar xpvf -) |
| 3049 |
# Move entire file tree from one directory to another |
| 3050 |
# [courtesy Alan Cox <[email protected]>, with a minor change] |
| 3051 |
|
| 3052 |
# 1) cd /source/directory |
| 3053 |
# Source directory, where the files to be moved are. |
| 3054 |
# 2) && |
| 3055 |
# "And-list": if the 'cd' operation successful, |
| 3056 |
# then execute the next command. |
| 3057 |
# 3) tar cf - . |
| 3058 |
# The 'c' option 'tar' archiving command creates a new archive, |
| 3059 |
# the 'f' (file) option, followed by '-' designates the target file |
| 3060 |
# as stdout, and do it in current directory tree ('.'). |
| 3061 |
# 4) | |
| 3062 |
# Piped to ... |
| 3063 |
# 5) ( ... ) |
| 3064 |
# a subshell |
| 3065 |
# 6) cd /dest/directory |
| 3066 |
# Change to the destination directory. |
| 3067 |
# 7) && |
| 3068 |
# "And-list", as above |
| 3069 |
# 8) tar xpvf - |
| 3070 |
# Unarchive ('x'), preserve ownership and file permissions ('p'), |
| 3071 |
# and send verbose messages to stdout ('v'), |
| 3072 |
# reading data from stdin ('f' followed by '-'). |
| 3073 |
# |
| 3074 |
# Note that 'x' is a command, and 'p', 'v', 'f' are options. |
| 3075 |
# |
| 3076 |
# Whew! |
| 3077 |
|
| 3078 |
|
| 3079 |
|
| 3080 |
# More elegant than, but equivalent to: |
| 3081 |
# cd source/directory |
| 3082 |
# tar cf - . | (cd ../dest/directory; tar xpvf -) |
| 3083 |
# |
| 3084 |
# Also having same effect: |
| 3085 |
# cp -a /source/directory/* /dest/directory |
| 3086 |
# Or: |
| 3087 |
# cp -a /source/directory/* /source/directory/.[^.]* /dest/directory |
| 3088 |
# If there are hidden files in /source/directory. |
| 3089 |
</programlisting></para> |
| 3090 |
|
| 3091 |
<para><programlisting>bunzip2 -c linux-2.6.16.tar.bz2 | tar xvf - |
| 3092 |
# --uncompress tar file-- | --then pass it to "tar"-- |
| 3093 |
# If "tar" has not been patched to handle "bunzip2", |
| 3094 |
#+ this needs to be done in two discrete steps, using a pipe. |
| 3095 |
# The purpose of the exercise is to unarchive "bzipped" kernel source. |
| 3096 |
</programlisting></para> |
| 3097 |
|
| 3098 |
<para>Note that in this context the <quote>-</quote> is not |
| 3099 |
itself a Bash operator, but rather an option recognized by |
| 3100 |
certain UNIX utilities that write to |
| 3101 |
<filename>stdout</filename>, such as <command>tar</command>, |
| 3102 |
<command>cat</command>, etc.</para> |
| 3103 |
|
| 3104 |
|
| 3105 |
<para> |
| 3106 |
<screen><prompt>bash$ </prompt><userinput>echo "whatever" | cat -</userinput> |
| 3107 |
<computeroutput>whatever</computeroutput> </screen> |
| 3108 |
</para> |
| 3109 |
|
| 3110 |
|
| 3111 |
<para>Where a filename is expected, |
| 3112 |
<replaceable>-</replaceable> redirects output to |
| 3113 |
<filename>stdout</filename> (sometimes seen with |
| 3114 |
<userinput>tar cf</userinput>), or accepts input from |
| 3115 |
<filename>stdin</filename>, rather than from a file. |
| 3116 |
<anchor id="filterdash"> |
| 3117 |
This is a method of using a file-oriented utility as a |
| 3118 |
filter in a pipe.</para> |
| 3119 |
|
| 3120 |
<para> |
| 3121 |
<screen><prompt>bash$ </prompt><userinput>file</userinput> |
| 3122 |
<computeroutput>Usage: file [-bciknvzL] [-f namefile] [-m magicfiles] file...</computeroutput> |
| 3123 |
</screen> |
| 3124 |
|
| 3125 |
By itself on the command-line, <link |
| 3126 |
linkend="fileref">file</link> fails with an error message. |
| 3127 |
</para> |
| 3128 |
|
| 3129 |
<para> |
| 3130 |
Add a <quote>-</quote> for a more useful result. This causes the |
| 3131 |
shell to await user input. |
| 3132 |
|
| 3133 |
<screen> |
| 3134 |
<prompt>bash$ </prompt><userinput>file -</userinput> |
| 3135 |
<userinput>abc</userinput> |
| 3136 |
<computeroutput>standard input: ASCII text</computeroutput> |
| 3137 |
|
| 3138 |
|
| 3139 |
|
| 3140 |
<prompt>bash$ </prompt><userinput>file -</userinput> |
| 3141 |
<userinput>#!/bin/bash</userinput> |
| 3142 |
<computeroutput>standard input: Bourne-Again shell script text executable</computeroutput> |
| 3143 |
</screen> |
| 3144 |
|
| 3145 |
Now the command accepts input from <filename>stdin</filename> |
| 3146 |
and analyzes it. |
| 3147 |
</para> |
| 3148 |
|
| 3149 |
<para>The <quote>-</quote> can be used to pipe |
| 3150 |
<filename>stdout</filename> to other commands. This permits |
| 3151 |
such stunts as <link linkend="prependref">prepending lines |
| 3152 |
to a file</link>.</para> |
| 3153 |
|
| 3154 |
<para>Using <link linkend="diffref">diff</link> to |
| 3155 |
compare a file with a <emphasis>section</emphasis> |
| 3156 |
of another:</para> |
| 3157 |
|
| 3158 |
<para><userinput>grep Linux file1 | diff file2 -</userinput></para> |
| 3159 |
|
| 3160 |
|
| 3161 |
<para>Finally, a real-world example using |
| 3162 |
<replaceable>-</replaceable> with <link |
| 3163 |
linkend="tarref">tar</link>.</para> |
| 3164 |
|
| 3165 |
<example id="ex58"> |
| 3166 |
<title>Backup of all files changed in last day</title> |
| 3167 |
<programlisting>&ex58;</programlisting> |
| 3168 |
</example> |
| 3169 |
|
| 3170 |
<caution> |
| 3171 |
|
| 3172 |
<para>Filenames beginning with |
| 3173 |
<quote>-</quote> may cause problems when coupled with the |
| 3174 |
<quote>-</quote> redirection operator. A script should |
| 3175 |
check for this and add an appropriate prefix to such |
| 3176 |
filenames, for example <filename>./-FILENAME</filename>, |
| 3177 |
<filename>$PWD/-FILENAME</filename>, or |
| 3178 |
<filename>$PATHNAME/-FILENAME</filename>.</para> |
| 3179 |
|
| 3180 |
<para>If the value of a variable begins with a |
| 3181 |
<replaceable>-</replaceable>, this may likewise create |
| 3182 |
problems. |
| 3183 |
<programlisting>var="-n" |
| 3184 |
echo $var |
| 3185 |
# Has the effect of "echo -n", and outputs nothing.</programlisting> |
| 3186 |
</para> |
| 3187 |
</caution> |
| 3188 |
|
| 3189 |
</listitem> |
| 3190 |
</varlistentry> |
| 3191 |
|
| 3192 |
|
| 3193 |
<varlistentry> |
| 3194 |
<term><token>-</token></term> |
| 3195 |
<listitem><formalpara><title>previous working directory</title> |
| 3196 |
<para>A <command>cd -</command> command changes to the |
| 3197 |
previous working directory. This uses the |
| 3198 |
<link linkend="oldpwd">$OLDPWD</link> <link |
| 3199 |
linkend="envref">environmental variable</link>.</para> |
| 3200 |
</formalpara> |
| 3201 |
<caution><para>Do not confuse the <quote>-</quote> used in this |
| 3202 |
sense with the <quote>-</quote> redirection |
| 3203 |
operator just discussed. The interpretation of the |
| 3204 |
<quote>-</quote> depends on the context in which it |
| 3205 |
appears.</para></caution> |
| 3206 |
</listitem> |
| 3207 |
</varlistentry> |
| 3208 |
|
| 3209 |
<varlistentry> |
| 3210 |
<term><token>-</token></term> |
| 3211 |
<listitem><formalpara><title>Minus</title> |
| 3212 |
<para>Minus sign in an <link linkend="arops1">arithmetic |
| 3213 |
operation</link>.</para> |
| 3214 |
</formalpara> |
| 3215 |
</listitem> |
| 3216 |
</varlistentry> |
| 3217 |
|
| 3218 |
<varlistentry> |
| 3219 |
<term><token>=</token></term> |
| 3220 |
<listitem><formalpara><title>Equals</title> |
| 3221 |
<para><link linkend="eqref">Assignment operator</link> |
| 3222 |
<programlisting>a=28 |
| 3223 |
echo $a # 28</programlisting></para> |
| 3224 |
</formalpara> |
| 3225 |
<para>In a <link linkend="equalsignref">different context</link>, |
| 3226 |
the <quote><token>=</token></quote> is a <link |
| 3227 |
linkend="scomparison1">string comparison</link> |
| 3228 |
operator.</para> |
| 3229 |
</listitem> |
| 3230 |
</varlistentry> |
| 3231 |
|
| 3232 |
<varlistentry> |
| 3233 |
<term><token>+</token></term> |
| 3234 |
<listitem><formalpara><title>Plus</title> |
| 3235 |
<para>Addition <link linkend="arops1">arithmetic |
| 3236 |
operator</link>.</para> |
| 3237 |
</formalpara> |
| 3238 |
<para>In a <link linkend="plusref">different context</link>, |
| 3239 |
the <token>+</token> is a <link linkend="regexp">Regular |
| 3240 |
Expression</link> operator.</para> |
| 3241 |
</listitem> |
| 3242 |
</varlistentry> |
| 3243 |
|
| 3244 |
<varlistentry> |
| 3245 |
<term><token>+</token></term> |
| 3246 |
<listitem><formalpara><title>Option</title> |
| 3247 |
<para>Option flag for a command or filter.</para> |
| 3248 |
</formalpara> |
| 3249 |
<para>Certain commands and <link |
| 3250 |
linkend="builtinref">builtins</link> use the |
| 3251 |
<option>+</option> to enable certain options and the |
| 3252 |
<option>-</option> to disable them. In <link |
| 3253 |
linkend="paramsubref">parameter substitution</link>, |
| 3254 |
the <option>+</option> prefixes an <link linkend="paramaltv"> |
| 3255 |
alternate value</link> that a variable expands to.</para> |
| 3256 |
</listitem> |
| 3257 |
</varlistentry> |
| 3258 |
|
| 3259 |
<varlistentry> |
| 3260 |
<term><anchor id="modulo00"><token>%</token></term> |
| 3261 |
<listitem><formalpara><title><link linkend="moduloref">modulo</link></title> |
| 3262 |
<para>Modulo (remainder of a division) <link linkend="arops1">arithmetic |
| 3263 |
operation</link>.</para> |
| 3264 |
</formalpara> |
| 3265 |
<para><programlisting>let "z = 5 % 3" |
| 3266 |
echo $z # 2</programlisting></para> |
| 3267 |
<para>In a <link linkend="pctpatref">different context</link>, |
| 3268 |
the <token>%</token> is a <link linkend="psub2">pattern |
| 3269 |
matching</link> operator.</para> |
| 3270 |
</listitem> |
| 3271 |
</varlistentry> |
| 3272 |
|
| 3273 |
<varlistentry> |
| 3274 |
<term><anchor id="tilderef"><token>~</token></term> |
| 3275 |
<listitem><formalpara><title>home directory [tilde]</title> |
| 3276 |
|
| 3277 |
<para>This corresponds to the <link |
| 3278 |
linkend="homedirref">$HOME</link> internal variable. |
| 3279 |
|
| 3280 |
<filename>~bozo</filename> is bozo's home directory, |
| 3281 |
and <command>ls ~bozo</command> lists the contents of it. |
| 3282 |
<token>~/</token> is the current user's home directory, |
| 3283 |
and <command>ls ~/</command> lists the contents of it. |
| 3284 |
|
| 3285 |
<screen><prompt>bash$ </prompt><userinput>echo ~bozo</userinput> |
| 3286 |
<computeroutput>/home/bozo</computeroutput> |
| 3287 |
|
| 3288 |
<prompt>bash$ </prompt><userinput>echo ~</userinput> |
| 3289 |
<computeroutput>/home/bozo</computeroutput> |
| 3290 |
|
| 3291 |
<prompt>bash$ </prompt><userinput>echo ~/</userinput> |
| 3292 |
<computeroutput>/home/bozo/</computeroutput> |
| 3293 |
|
| 3294 |
<prompt>bash$ </prompt><userinput>echo ~:</userinput> |
| 3295 |
<computeroutput>/home/bozo:</computeroutput> |
| 3296 |
|
| 3297 |
<prompt>bash$ </prompt><userinput>echo ~nonexistent-user</userinput> |
| 3298 |
<computeroutput>~nonexistent-user</computeroutput> |
| 3299 |
</screen> |
| 3300 |
</para> |
| 3301 |
|
| 3302 |
</formalpara> |
| 3303 |
</listitem> |
| 3304 |
</varlistentry> |
| 3305 |
|
| 3306 |
<varlistentry> |
| 3307 |
<term><anchor id="workingdirref"><token>~+</token></term> |
| 3308 |
<listitem><formalpara><title>current working directory</title> |
| 3309 |
<para>This corresponds to the <link |
| 3310 |
linkend="pwdref">$PWD</link> internal variable.</para> |
| 3311 |
</formalpara> |
| 3312 |
</listitem> |
| 3313 |
</varlistentry> |
| 3314 |
|
| 3315 |
<varlistentry> |
| 3316 |
<term><anchor id="prevworkingdir"><token>~-</token></term> |
| 3317 |
<listitem><formalpara><title>previous working directory</title> |
| 3318 |
<para>This corresponds to the <link |
| 3319 |
linkend="oldpwd">$OLDPWD</link> internal variable.</para> |
| 3320 |
</formalpara> |
| 3321 |
</listitem> |
| 3322 |
</varlistentry> |
| 3323 |
|
| 3324 |
<varlistentry> |
| 3325 |
<term><token>=~</token></term> |
| 3326 |
<listitem><formalpara><title><link linkend="regexmatchref">regular |
| 3327 |
expression match</link></title> |
| 3328 |
<para>This operator was introduced with <link |
| 3329 |
linkend="bash3ref">version 3</link> of Bash.</para> |
| 3330 |
</formalpara> |
| 3331 |
</listitem> |
| 3332 |
</varlistentry> |
| 3333 |
|
| 3334 |
<varlistentry><term><anchor id="beglineref"><token>^</token></term> |
| 3335 |
<indexterm> |
| 3336 |
<primary>^</primary> |
| 3337 |
</indexterm> |
| 3338 |
<indexterm> |
| 3339 |
<primary>special character</primary> |
| 3340 |
<secondary>^</secondary> |
| 3341 |
</indexterm> |
| 3342 |
<indexterm> |
| 3343 |
<primary>regular expression</primary> |
| 3344 |
</indexterm> |
| 3345 |
<indexterm> |
| 3346 |
<primary>beginning of line</primary> |
| 3347 |
</indexterm> |
| 3348 |
<indexterm> |
| 3349 |
<primary>uppercase modification</primary> |
| 3350 |
<secondary>parameter substitution</secondary> |
| 3351 |
</indexterm> |
| 3352 |
|
| 3353 |
<listitem> |
| 3354 |
<formalpara><title>beginning-of-line</title> |
| 3355 |
<para>In a <link linkend="regexref">regular expression</link>, a |
| 3356 |
<quote>^</quote> addresses the <link |
| 3357 |
linkend="caretref">beginning of a line</link> of text.</para> |
| 3358 |
</formalpara> |
| 3359 |
|
| 3360 |
</listitem> |
| 3361 |
</varlistentry> |
| 3362 |
|
| 3363 |
<varlistentry> |
| 3364 |
<term><token>^</token></term> |
| 3365 |
<term><token>^^</token></term> |
| 3366 |
<listitem> |
| 3367 |
<formalpara><title><link linkend="casemodparamsub">Uppercase |
| 3368 |
conversion</link> in <firstterm>parameter substitution</firstterm> |
| 3369 |
(added in <link |
| 3370 |
linkend="bash4ref">version 4</link> of Bash)</title> |
| 3371 |
<para></para></formalpara> |
| 3372 |
</listitem> |
| 3373 |
</varlistentry> |
| 3374 |
|
| 3375 |
<varlistentry> |
| 3376 |
<term><anchor id="controlcharref">Control Characters</term> |
| 3377 |
<listitem> |
| 3378 |
<formalpara><title> change the behavior of the |
| 3379 |
terminal or text display.</title> |
| 3380 |
|
| 3381 |
<para>A control character is a <keycap>CONTROL</keycap> |
| 3382 |
+ <keycap>key</keycap> combination (pressed |
| 3383 |
simultaneously). |
| 3384 |
|
| 3385 |
A control character may also |
| 3386 |
be written in <firstterm>octal</firstterm> or |
| 3387 |
<firstterm>hexadecimal</firstterm> notation, |
| 3388 |
following an <firstterm>escape</firstterm>.</para> |
| 3389 |
</formalpara> |
| 3390 |
<para>Control characters are not normally useful inside a |
| 3391 |
script.</para> |
| 3392 |
|
| 3393 |
|
| 3394 |
<itemizedlist id="ctlchar"> |
| 3395 |
|
| 3396 |
<listitem> |
| 3397 |
<para><userinput>Ctl-A</userinput></para> |
| 3398 |
<para>Moves cursor to beginning of line of text |
| 3399 |
(on the command-line).</para> |
| 3400 |
</listitem> |
| 3401 |
|
| 3402 |
<listitem> |
| 3403 |
<para><userinput>Ctl-B</userinput></para> |
| 3404 |
<para><userinput>Backspace</userinput> |
| 3405 |
(nondestructive).</para> |
| 3406 |
</listitem> |
| 3407 |
|
| 3408 |
<listitem> |
| 3409 |
<para><anchor id="ctlcref"></para> |
| 3410 |
<para><userinput>Ctl-C</userinput></para> |
| 3411 |
<para><userinput>Break</userinput>. |
| 3412 |
Terminate a foreground job.</para> |
| 3413 |
</listitem> |
| 3414 |
|
| 3415 |
<listitem> |
| 3416 |
|
| 3417 |
<para><anchor id="ctldref"></para> |
| 3418 |
<para><userinput>Ctl-D</userinput></para> |
| 3419 |
<para><firstterm>Log out</firstterm> from a shell (similar to |
| 3420 |
<link linkend="exitcommandref">exit</link>).</para> |
| 3421 |
<para><userinput>EOF</userinput> (end-of-file). This also |
| 3422 |
terminates input from <filename>stdin</filename>.</para> |
| 3423 |
<para>When typing text on the console or in an |
| 3424 |
<firstterm>xterm</firstterm> window, |
| 3425 |
<userinput>Ctl-D</userinput> erases the character under the |
| 3426 |
cursor. When there are no characters present, |
| 3427 |
<userinput>Ctl-D</userinput> logs out of the session, as |
| 3428 |
expected. In an <firstterm>xterm</firstterm> window, |
| 3429 |
this has the effect of closing the window.</para> |
| 3430 |
</listitem> |
| 3431 |
|
| 3432 |
<listitem> |
| 3433 |
<para><userinput>Ctl-E</userinput></para> |
| 3434 |
<para>Moves cursor to end of line of text |
| 3435 |
(on the command-line).</para> |
| 3436 |
</listitem> |
| 3437 |
|
| 3438 |
<listitem> |
| 3439 |
<para><userinput>Ctl-F</userinput></para> |
| 3440 |
<para>Moves cursor forward one character position |
| 3441 |
(on the command-line).</para> |
| 3442 |
</listitem> |
| 3443 |
|
| 3444 |
<listitem> |
| 3445 |
<para><anchor id="ctlgref"></para> |
| 3446 |
<para><userinput>Ctl-G</userinput></para> |
| 3447 |
<para><userinput>BEL</userinput>. On some |
| 3448 |
old-time teletype terminals, this would actually ring |
| 3449 |
a bell. In an <firstterm>xterm</firstterm> it might |
| 3450 |
beep.</para> |
| 3451 |
</listitem> |
| 3452 |
|
| 3453 |
<listitem> |
| 3454 |
<para><anchor id="ctlhref"></para> |
| 3455 |
<para><userinput>Ctl-H</userinput></para> |
| 3456 |
<para><userinput>Rubout</userinput> (destructive backspace). |
| 3457 |
Erases characters the cursor backs over while |
| 3458 |
backspacing.</para> |
| 3459 |
<para> |
| 3460 |
<programlisting>#!/bin/bash |
| 3461 |
# Embedding Ctl-H in a string. |
| 3462 |
|
| 3463 |
a="^H^H" # Two Ctl-H's -- backspaces |
| 3464 |
# ctl-V ctl-H, using vi/vim |
| 3465 |
echo "abcdef" # abcdef |
| 3466 |
echo |
| 3467 |
echo -n "abcdef$a " # abcd f |
| 3468 |
# Space at end ^ ^ Backspaces twice. |
| 3469 |
echo |
| 3470 |
echo -n "abcdef$a" # abcdef |
| 3471 |
# No space at end ^ Doesn't backspace (why?). |
| 3472 |
# Results may not be quite as expected. |
| 3473 |
echo; echo |
| 3474 |
|
| 3475 |
# Constantin Hagemeier suggests trying: |
| 3476 |
# a=$'\010\010' |
| 3477 |
# a=$'\b\b' |
| 3478 |
# a=$'\x08\x08' |
| 3479 |
# But, this does not change the results. |
| 3480 |
|
| 3481 |
######################################## |
| 3482 |
|
| 3483 |
# Now, try this. |
| 3484 |
|
| 3485 |
rubout="^H^H^H^H^H" # 5 x Ctl-H. |
| 3486 |
|
| 3487 |
echo -n "12345678" |
| 3488 |
sleep 2 |
| 3489 |
echo -n "$rubout" |
| 3490 |
sleep 2</programlisting> |
| 3491 |
</para> |
| 3492 |
</listitem> |
| 3493 |
|
| 3494 |
<listitem> |
| 3495 |
<para><userinput>Ctl-I</userinput></para> |
| 3496 |
<para><userinput>Horizontal tab</userinput>.</para> |
| 3497 |
</listitem> |
| 3498 |
|
| 3499 |
<listitem> |
| 3500 |
<para><anchor id="ctljref"></para> |
| 3501 |
<para><userinput>Ctl-J</userinput></para> |
| 3502 |
<para><userinput>Newline</userinput> (line feed). |
| 3503 |
In a script, may also be expressed in octal notation -- |
| 3504 |
'\012' or in hexadecimal -- '\x0a'.</para> |
| 3505 |
</listitem> |
| 3506 |
|
| 3507 |
<listitem> |
| 3508 |
<para><userinput>Ctl-K</userinput></para> |
| 3509 |
<para><userinput>Vertical tab</userinput>.</para> |
| 3510 |
<para>When typing text on the console or in an |
| 3511 |
<firstterm>xterm</firstterm> window, |
| 3512 |
<userinput>Ctl-K</userinput> erases from the character |
| 3513 |
under the cursor to end of line. Within a script, |
| 3514 |
<userinput>Ctl-K</userinput> may behave differently, |
| 3515 |
as in Lee Lee Maschmeyer's example, below.</para> |
| 3516 |
</listitem> |
| 3517 |
|
| 3518 |
<listitem> |
| 3519 |
<para><userinput>Ctl-L</userinput></para> |
| 3520 |
<para><userinput>Formfeed</userinput> (clear the terminal |
| 3521 |
screen). In a terminal, this has the same effect as the |
| 3522 |
<link linkend="clearref">clear</link> command. When sent |
| 3523 |
to a printer, a <userinput>Ctl-L</userinput> causes |
| 3524 |
an advance to end of the paper sheet.</para> |
| 3525 |
</listitem> |
| 3526 |
|
| 3527 |
<listitem> |
| 3528 |
<para><anchor id="ctlmref"></para> |
| 3529 |
<para><userinput>Ctl-M</userinput></para> |
| 3530 |
<para><userinput>Carriage return</userinput>.</para> |
| 3531 |
<para> |
| 3532 |
<programlisting>#!/bin/bash |
| 3533 |
# Thank you, Lee Maschmeyer, for this example. |
| 3534 |
|
| 3535 |
read -n 1 -s -p \ |
| 3536 |
$'Control-M leaves cursor at beginning of this line. Press Enter. \x0d' |
| 3537 |
# Of course, '0d' is the hex equivalent of Control-M. |
| 3538 |
echo >&2 # The '-s' makes anything typed silent, |
| 3539 |
#+ so it is necessary to go to new line explicitly. |
| 3540 |
|
| 3541 |
read -n 1 -s -p $'Control-J leaves cursor on next line. \x0a' |
| 3542 |
# '0a' is the hex equivalent of Control-J, linefeed. |
| 3543 |
echo >&2 |
| 3544 |
|
| 3545 |
### |
| 3546 |
|
| 3547 |
read -n 1 -s -p $'And Control-K\x0bgoes straight down.' |
| 3548 |
echo >&2 # Control-K is vertical tab. |
| 3549 |
|
| 3550 |
# A better example of the effect of a vertical tab is: |
| 3551 |
|
| 3552 |
var=$'\x0aThis is the bottom line\x0bThis is the top line\x0a' |
| 3553 |
echo "$var" |
| 3554 |
# This works the same way as the above example. However: |
| 3555 |
echo "$var" | col |
| 3556 |
# This causes the right end of the line to be higher than the left end. |
| 3557 |
# It also explains why we started and ended with a line feed -- |
| 3558 |
#+ to avoid a garbled screen. |
| 3559 |
|
| 3560 |
# As Lee Maschmeyer explains: |
| 3561 |
# -------------------------- |
| 3562 |
# In the [first vertical tab example] . . . the vertical tab |
| 3563 |
#+ makes the printing go straight down without a carriage return. |
| 3564 |
# This is true only on devices, such as the Linux console, |
| 3565 |
#+ that can't go "backward." |
| 3566 |
# The real purpose of VT is to go straight UP, not down. |
| 3567 |
# It can be used to print superscripts on a printer. |
| 3568 |
# The col utility can be used to emulate the proper behavior of VT. |
| 3569 |
|
| 3570 |
exit 0</programlisting> |
| 3571 |
</para> |
| 3572 |
</listitem> |
| 3573 |
|
| 3574 |
<listitem> |
| 3575 |
<para><userinput>Ctl-N</userinput></para> |
| 3576 |
<para>Erases a line of text recalled from |
| 3577 |
<firstterm>history buffer</firstterm> |
| 3578 |
<footnote><para>Bash stores a list of commands |
| 3579 |
previously issued from the command-line |
| 3580 |
in a <firstterm>buffer</firstterm>, or |
| 3581 |
memory space, for recall with the <link |
| 3582 |
linkend="builtinref">builtin</link> |
| 3583 |
<firstterm>history</firstterm> |
| 3584 |
commands.</para></footnote> (on the |
| 3585 |
command-line).</para> |
| 3586 |
</listitem> |
| 3587 |
|
| 3588 |
<listitem> |
| 3589 |
<para><userinput>Ctl-O</userinput></para> |
| 3590 |
<para>Issues a <firstterm>newline</firstterm> |
| 3591 |
(on the command-line).</para> |
| 3592 |
</listitem> |
| 3593 |
|
| 3594 |
<listitem> |
| 3595 |
<para><userinput>Ctl-P</userinput></para> |
| 3596 |
<para>Recalls last command from <firstterm>history |
| 3597 |
buffer</firstterm> (on the command-line).</para> |
| 3598 |
</listitem> |
| 3599 |
|
| 3600 |
<listitem> |
| 3601 |
<para><userinput>Ctl-Q</userinput></para> |
| 3602 |
<para>Resume (<userinput>XON</userinput>).</para> |
| 3603 |
<para>This resumes <filename>stdin</filename> in a terminal.</para> |
| 3604 |
</listitem> |
| 3605 |
|
| 3606 |
<listitem> |
| 3607 |
<para><userinput>Ctl-R</userinput></para> |
| 3608 |
<para>Backwards search for text in <firstterm>history |
| 3609 |
buffer</firstterm> |
| 3610 |
(on the command-line).</para> |
| 3611 |
</listitem> |
| 3612 |
|
| 3613 |
<listitem> |
| 3614 |
<para><userinput>Ctl-S</userinput></para> |
| 3615 |
<para>Suspend (<userinput>XOFF</userinput>).</para> |
| 3616 |
<para>This freezes <filename>stdin</filename> in a terminal. |
| 3617 |
(Use Ctl-Q to restore input.)</para> |
| 3618 |
</listitem> |
| 3619 |
|
| 3620 |
<listitem> |
| 3621 |
<para><userinput>Ctl-T</userinput></para> |
| 3622 |
<para>Reverses the position of the character the cursor |
| 3623 |
is on with the previous character (on the |
| 3624 |
command-line).</para> |
| 3625 |
</listitem> |
| 3626 |
|
| 3627 |
<listitem> |
| 3628 |
<para><userinput>Ctl-U</userinput></para> |
| 3629 |
<para>Erase a line of input, from the cursor backward to |
| 3630 |
beginning of line. In some settings, |
| 3631 |
<userinput>Ctl-U</userinput> erases the entire |
| 3632 |
line of input, <emphasis>regardless of cursor |
| 3633 |
position</emphasis>.</para> |
| 3634 |
</listitem> |
| 3635 |
|
| 3636 |
<listitem> |
| 3637 |
<para><userinput>Ctl-V</userinput></para> |
| 3638 |
<para>When inputting text, <userinput>Ctl-V</userinput> |
| 3639 |
permits inserting control characters. For example, the |
| 3640 |
following two are equivalent: |
| 3641 |
<programlisting>echo -e '\x0a' |
| 3642 |
echo <Ctl-V><Ctl-J></programlisting></para> |
| 3643 |
<para><userinput>Ctl-V</userinput> is primarily useful from |
| 3644 |
within a text editor.</para> |
| 3645 |
</listitem> |
| 3646 |
|
| 3647 |
<listitem> |
| 3648 |
<para><userinput>Ctl-W</userinput></para> |
| 3649 |
<para>When typing text on the console or in an xterm window, |
| 3650 |
<userinput>Ctl-W</userinput> erases from the character |
| 3651 |
under the cursor backwards to the first instance of |
| 3652 |
<link linkend="whitespaceref">whitespace</link>. In |
| 3653 |
some settings, <userinput>Ctl-W</userinput> erases |
| 3654 |
backwards to first non-alphanumeric character.</para> |
| 3655 |
</listitem> |
| 3656 |
|
| 3657 |
<listitem> |
| 3658 |
<para><userinput>Ctl-X</userinput></para> |
| 3659 |
<para>In certain word processing programs, |
| 3660 |
<firstterm>Cuts</firstterm> highlighted text |
| 3661 |
and copies to <firstterm>clipboard</firstterm>.</para> |
| 3662 |
</listitem> |
| 3663 |
|
| 3664 |
<listitem> |
| 3665 |
<para><userinput>Ctl-Y</userinput></para> |
| 3666 |
<para><firstterm>Pastes</firstterm> back text previously |
| 3667 |
erased (with <userinput>Ctl-U</userinput> or |
| 3668 |
<userinput>Ctl-W</userinput>).</para> |
| 3669 |
</listitem> |
| 3670 |
|
| 3671 |
<listitem> |
| 3672 |
<para><userinput>Ctl-Z</userinput></para> |
| 3673 |
<para><firstterm>Pauses</firstterm> a foreground job.</para> |
| 3674 |
<para><firstterm>Substitute</firstterm> operation in certain |
| 3675 |
word processing applications.</para> |
| 3676 |
<para><userinput>EOF</userinput> (end-of-file) character |
| 3677 |
in the MSDOS filesystem.</para> |
| 3678 |
</listitem> |
| 3679 |
|
| 3680 |
</itemizedlist> |
| 3681 |
|
| 3682 |
|
| 3683 |
|
| 3684 |
</listitem> |
| 3685 |
</varlistentry> |
| 3686 |
|
| 3687 |
<varlistentry> |
| 3688 |
<term><anchor id="whitespaceref">Whitespace</term> |
| 3689 |
<listitem> |
| 3690 |
<formalpara> |
| 3691 |
<title>functions as a separator between commands and/or |
| 3692 |
variables.</title> |
| 3693 |
<para>Whitespace consists of either |
| 3694 |
<firstterm>spaces</firstterm>, |
| 3695 |
<firstterm>tabs</firstterm>, <firstterm>blank |
| 3696 |
lines</firstterm>, or any combination thereof. |
| 3697 |
|
| 3698 |
<footnote><para>A linefeed (<firstterm>newline</firstterm>) |
| 3699 |
is also a whitespace character. This explains |
| 3700 |
why a <firstterm>blank line</firstterm>, |
| 3701 |
consisting only of a linefeed, is considered |
| 3702 |
whitespace.</para></footnote> |
| 3703 |
|
| 3704 |
In some contexts, such as <link linkend="wsbad">variable |
| 3705 |
assignment</link>, whitespace is not permitted, and |
| 3706 |
results in a syntax error.</para> |
| 3707 |
</formalpara> |
| 3708 |
|
| 3709 |
<para>Blank lines have no effect on the action of a script, |
| 3710 |
and are therefore useful for visually separating functional |
| 3711 |
sections.</para> |
| 3712 |
|
| 3713 |
<para><link linkend="ifsref">$IFS</link>, the special variable |
| 3714 |
separating <firstterm>fields</firstterm> of input to certain |
| 3715 |
commands. It defaults to whitespace.</para> |
| 3716 |
|
| 3717 |
<sidebar><para> |
| 3718 |
<anchor id="fieldref"><userinput>Definition:</userinput> |
| 3719 |
A <firstterm>field</firstterm> is a discrete chunk of data |
| 3720 |
expressed as a string of consecutive characters. |
| 3721 |
Separating each field from adjacent fields is either |
| 3722 |
<firstterm>whitespace</firstterm> or some other designated |
| 3723 |
character (often determined by the <token>$IFS</token>). |
| 3724 |
In some contexts, a field may be called a |
| 3725 |
<firstterm>record</firstterm>. |
| 3726 |
</para></sidebar> |
| 3727 |
|
| 3728 |
<para><anchor id="quotingws"></para> |
| 3729 |
<para>To preserve <firstterm>whitespace</firstterm> |
| 3730 |
within a string or in a variable, use <link |
| 3731 |
linkend="quotingref">quoting</link>.</para> |
| 3732 |
|
| 3733 |
<para>UNIX <link linkend="filterdef">filters</link> |
| 3734 |
can target and operate on <firstterm>whitespace</firstterm> |
| 3735 |
using the <link linkend="posixref">POSIX</link> character class |
| 3736 |
<link linkend="wsposix">[:space:]</link>.</para> |
| 3737 |
|
| 3738 |
</listitem> |
| 3739 |
</varlistentry> |
| 3740 |
|
| 3741 |
|
| 3742 |
</variablelist> |
| 3743 |
|
| 3744 |
</chapter> <!-- Special characters used in shell scripts --> |
| 3745 |
|
| 3746 |
|
| 3747 |
|
| 3748 |
<chapter id="variables"> |
| 3749 |
<title>Introduction to Variables and Parameters</title> |
| 3750 |
|
| 3751 |
<para><firstterm>Variables</firstterm> are how programming and |
| 3752 |
scripting languages represent data. A variable is nothing |
| 3753 |
more than a <firstterm>label</firstterm>, a name assigned to a |
| 3754 |
location or set of locations in computer memory holding an item |
| 3755 |
of data.</para> |
| 3756 |
|
| 3757 |
<para>Variables appear in arithmetic operations and manipulation of |
| 3758 |
quantities, and in string parsing.</para> |
| 3759 |
|
| 3760 |
|
| 3761 |
<sect1 id="varsubn"> |
| 3762 |
<title>Variable Substitution</title> |
| 3763 |
|
| 3764 |
<para>The <firstterm>name</firstterm> of a variable is a placeholder |
| 3765 |
for its <firstterm>value</firstterm>, the data it holds. |
| 3766 |
Referencing (retrieving) its value is called |
| 3767 |
<firstterm>variable substitution</firstterm>.</para> |
| 3768 |
|
| 3769 |
<variablelist id="dollarsign"> |
| 3770 |
|
| 3771 |
<varlistentry> |
| 3772 |
<term><token>$</token></term> <indexterm> |
| 3773 |
<primary>$</primary> |
| 3774 |
</indexterm> <indexterm> |
| 3775 |
<primary>variable</primary> <secondary>$</secondary> |
| 3776 |
</indexterm> <indexterm> |
| 3777 |
<primary>variable</primary> |
| 3778 |
<secondary>substitution</secondary> |
| 3779 |
</indexterm> |
| 3780 |
|
| 3781 |
<listitem> |
| 3782 |
|
| 3783 |
<para><anchor id="varnameval"></para> |
| 3784 |
<para>Let us carefully distinguish between the |
| 3785 |
<firstterm>name</firstterm> of a variable |
| 3786 |
and its <firstterm>value</firstterm>. If |
| 3787 |
<userinput>variable1</userinput> is the name of a |
| 3788 |
variable, then <userinput>$variable1</userinput> |
| 3789 |
is a reference to its <firstterm>value</firstterm>, |
| 3790 |
the data item it contains. |
| 3791 |
|
| 3792 |
<footnote> |
| 3793 |
|
| 3794 |
<para><anchor id="lvalueref">Technically, the |
| 3795 |
<firstterm>name</firstterm> of a variable is called an |
| 3796 |
<firstterm>lvalue</firstterm>, meaning that it appears |
| 3797 |
on the <emphasis>left</emphasis> side of an assignment |
| 3798 |
statment, as in <userinput>VARIABLE=23</userinput>. |
| 3799 |
A variable's <firstterm>value</firstterm> is |
| 3800 |
an <firstterm>rvalue</firstterm>, meaning that |
| 3801 |
it appears on the <emphasis>right</emphasis> |
| 3802 |
side of an assignment statement, as in |
| 3803 |
<userinput>VAR2=$VARIABLE</userinput>.</para> |
| 3804 |
|
| 3805 |
<para><anchor id="pointerref">A variable's |
| 3806 |
<firstterm>name</firstterm> is, in fact, |
| 3807 |
a <firstterm>reference</firstterm>, a |
| 3808 |
<firstterm>pointer</firstterm> to the memory |
| 3809 |
location(s) where the actual data associated with |
| 3810 |
that variable is kept.</para> |
| 3811 |
|
| 3812 |
</footnote> |
| 3813 |
|
| 3814 |
|
| 3815 |
</para> |
| 3816 |
|
| 3817 |
<para> |
| 3818 |
<screen><prompt>bash$ </prompt><userinput>variable1=23</userinput> |
| 3819 |
|
| 3820 |
|
| 3821 |
<prompt>bash$ </prompt><userinput>echo variable1</userinput> |
| 3822 |
<computeroutput>variable1</computeroutput> |
| 3823 |
|
| 3824 |
<prompt>bash$ </prompt><userinput>echo $variable1</userinput> |
| 3825 |
<computeroutput>23</computeroutput></screen> |
| 3826 |
</para> |
| 3827 |
|
| 3828 |
|
| 3829 |
<para>The only times a variable appears <quote>naked</quote> |
| 3830 |
-- without the <token>$</token> prefix -- is when |
| 3831 |
declared or assigned, when <firstterm>unset</firstterm>, |
| 3832 |
when <link linkend="exportref">exported</link>, |
| 3833 |
in an arithmetic expression within <link |
| 3834 |
linkend="dblparens">double parentheses |
| 3835 |
(( ... ))</link>, or in the special case of a variable |
| 3836 |
representing a <link linkend="signald">signal</link> |
| 3837 |
(see <xref linkend="ex76">). Assignment may be with an |
| 3838 |
<token>=</token> (as in <parameter>var1=27</parameter>), |
| 3839 |
in a <link linkend="readref">read</link> statement, |
| 3840 |
and at the head of a loop (<parameter>for var2 in 1 |
| 3841 |
2 3</parameter>).</para> |
| 3842 |
|
| 3843 |
|
| 3844 |
<para><anchor id="dblquo">Enclosing a referenced value in |
| 3845 |
<firstterm>double quotes</firstterm> (<token>" ... "</token>) |
| 3846 |
does not interfere with variable substitution. This is |
| 3847 |
called <firstterm>partial quoting</firstterm>, sometimes |
| 3848 |
referred to as <quote>weak quoting.</quote> <anchor |
| 3849 |
id="snglquo">Using single quotes (<token>' ... '</token>) |
| 3850 |
causes the variable name to be used literally, and no |
| 3851 |
substitution will take place. This is <firstterm>full |
| 3852 |
quoting</firstterm>, sometimes referred to as 'strong |
| 3853 |
quoting.' See <xref linkend="quoting"> for a |
| 3854 |
detailed discussion.</para> |
| 3855 |
|
| 3856 |
<para>Note that <userinput>$variable</userinput> is actually a |
| 3857 |
simplified form of |
| 3858 |
<userinput>${variable}</userinput>. In contexts |
| 3859 |
where the <userinput>$variable</userinput> syntax |
| 3860 |
causes an error, the longer form may work (see <xref |
| 3861 |
linkend="Parameter-Substitution">, below).</para> |
| 3862 |
|
| 3863 |
<para><anchor id="varunsetting"></para> |
| 3864 |
<example id="ex9"> |
| 3865 |
<title>Variable assignment and substitution</title> |
| 3866 |
<programlisting>&ex9;</programlisting> |
| 3867 |
</example> |
| 3868 |
|
| 3869 |
<caution> |
| 3870 |
|
| 3871 |
<para><anchor id="uninitvar1"></para> |
| 3872 |
<para>An uninitialized variable has a |
| 3873 |
<quote>null</quote> value -- no assigned value at all |
| 3874 |
(<emphasis>not</emphasis> zero!). |
| 3875 |
|
| 3876 |
<programlisting>if [ -z "$unassigned" ] |
| 3877 |
then |
| 3878 |
echo "\$unassigned is NULL." |
| 3879 |
fi # $unassigned is NULL.</programlisting></para> |
| 3880 |
|
| 3881 |
<para>Using a variable before |
| 3882 |
assigning a value to it may cause problems. |
| 3883 |
It is nevertheless possible to perform arithmetic operations |
| 3884 |
on an uninitialized variable. |
| 3885 |
|
| 3886 |
<programlisting>echo "$uninitialized" # (blank line) |
| 3887 |
let "uninitialized += 5" # Add 5 to it. |
| 3888 |
echo "$uninitialized" # 5 |
| 3889 |
|
| 3890 |
# Conclusion: |
| 3891 |
# An uninitialized variable has no value, |
| 3892 |
#+ however it evaluates as 0 in an arithmetic operation.</programlisting> |
| 3893 |
|
| 3894 |
See also <xref linkend="selfsource">.</para> |
| 3895 |
|
| 3896 |
</caution> |
| 3897 |
|
| 3898 |
</listitem> |
| 3899 |
</varlistentry> |
| 3900 |
</variablelist> |
| 3901 |
</sect1> <!-- Variable Substitution --> |
| 3902 |
|
| 3903 |
|
| 3904 |
<sect1 id="varassignment"> |
| 3905 |
<title>Variable Assignment</title> |
| 3906 |
|
| 3907 |
<variablelist> |
| 3908 |
<varlistentry> |
| 3909 |
<term><anchor id="eqref"><token>=</token></term> |
| 3910 |
<indexterm> |
| 3911 |
<primary>=</primary> |
| 3912 |
</indexterm> |
| 3913 |
<indexterm> |
| 3914 |
<primary>variable</primary> |
| 3915 |
<secondary>assignment</secondary> |
| 3916 |
</indexterm> |
| 3917 |
<listitem><para>the assignment operator (<emphasis>no space before |
| 3918 |
and after</emphasis>)</para> |
| 3919 |
|
| 3920 |
|
| 3921 |
<caution> |
| 3922 |
<para>Do not confuse this with <link |
| 3923 |
linkend="equalsignref">=</link> and |
| 3924 |
<link linkend="equalref">-eq</link>, which |
| 3925 |
<link linkend="ifthen">test</link>, |
| 3926 |
rather than assign!</para> |
| 3927 |
|
| 3928 |
<para>Note that <token>=</token> can be either |
| 3929 |
an <firstterm>assignment</firstterm> or a |
| 3930 |
<firstterm>test</firstterm> operator, depending on |
| 3931 |
context.</para> |
| 3932 |
</caution> |
| 3933 |
|
| 3934 |
|
| 3935 |
<para><anchor id="ex15_0"></para> |
| 3936 |
<example id="ex15"> |
| 3937 |
<title>Plain Variable Assignment</title> |
| 3938 |
<programlisting>&ex15;</programlisting> |
| 3939 |
</example> |
| 3940 |
|
| 3941 |
<para><anchor id="ex16_0"></para> |
| 3942 |
<example id="ex16"> |
| 3943 |
<title>Variable Assignment, plain and fancy</title> |
| 3944 |
<programlisting>&ex16;</programlisting> |
| 3945 |
</example> |
| 3946 |
|
| 3947 |
<para><anchor id="commandsubref0"></para> |
| 3948 |
|
| 3949 |
<para>Variable assignment using the <firstterm>$(...)</firstterm> |
| 3950 |
mechanism (a newer method than <link |
| 3951 |
linkend="backquotesref">backquotes</link>). This is |
| 3952 |
likewise a form of <link linkend="commandsubref">command |
| 3953 |
substitution</link>.</para> |
| 3954 |
|
| 3955 |
<para><programlisting># From /etc/rc.d/rc.local |
| 3956 |
R=$(cat /etc/redhat-release) |
| 3957 |
arch=$(uname -m)</programlisting></para> |
| 3958 |
</listitem> |
| 3959 |
</varlistentry> |
| 3960 |
</variablelist> |
| 3961 |
|
| 3962 |
</sect1> <!-- Variable Assignment --> |
| 3963 |
|
| 3964 |
<sect1 id="untyped"> |
| 3965 |
<title>Bash Variables Are Untyped</title> |
| 3966 |
|
| 3967 |
<para><anchor id="bvuntyped"></para> |
| 3968 |
<para>Unlike many other programming languages, Bash does not segregate |
| 3969 |
its variables by <quote>type.</quote> Essentially, <emphasis>Bash |
| 3970 |
variables are character strings</emphasis>, but, depending on |
| 3971 |
context, Bash permits arithmetic operations and comparisons on |
| 3972 |
variables. The determining factor is whether the value of a |
| 3973 |
variable contains only digits.</para> |
| 3974 |
|
| 3975 |
<example id="intorstring"> |
| 3976 |
<title>Integer or string?</title> |
| 3977 |
<programlisting>&intorstring;</programlisting> |
| 3978 |
</example> |
| 3979 |
|
| 3980 |
<para>Untyped variables are both a blessing and a curse. They permit |
| 3981 |
more flexibility in scripting and make it easier to grind out |
| 3982 |
lines of code (and give you enough rope to hang yourself!). |
| 3983 |
However, they likewise permit subtle errors to creep in |
| 3984 |
and encourage sloppy programming habits.</para> |
| 3985 |
|
| 3986 |
<para>To lighten the burden of keeping track of variable |
| 3987 |
types in a script, Bash <emphasis>does</emphasis> permit |
| 3988 |
<link linkend="declareref">declaring</link> variables.</para> |
| 3989 |
|
| 3990 |
</sect1> <!-- Bash Variables Are Untyped--> |
| 3991 |
|
| 3992 |
|
| 3993 |
<sect1 id="othertypesv"> |
| 3994 |
<title>Special Variable Types</title> |
| 3995 |
|
| 3996 |
<variablelist> |
| 3997 |
|
| 3998 |
<varlistentry> |
| 3999 |
<term><replaceable>Local variables</replaceable></term> |
| 4000 |
<indexterm> |
| 4001 |
<primary>variable</primary> |
| 4002 |
<secondary>local</secondary> |
| 4003 |
</indexterm> |
| 4004 |
|
| 4005 |
<listitem><para>Variables <link |
| 4006 |
linkend="scoperef">visible</link> only within a <link |
| 4007 |
linkend="codeblockref">code block</link> or function (see |
| 4008 |
also <link linkend="localref">local variables</link> in |
| 4009 |
<link linkend="functionref">functions</link>)</para> |
| 4010 |
</listitem> |
| 4011 |
</varlistentry> |
| 4012 |
|
| 4013 |
<varlistentry> |
| 4014 |
<term><anchor id="envref"><replaceable>Environmental variables</replaceable></term> |
| 4015 |
<indexterm> |
| 4016 |
<primary>variable</primary> |
| 4017 |
<secondary>environmental</secondary> |
| 4018 |
</indexterm> |
| 4019 |
<listitem> |
| 4020 |
|
| 4021 |
<para>Variables that affect the behavior of the shell and |
| 4022 |
user interface</para> |
| 4023 |
|
| 4024 |
<note> |
| 4025 |
|
| 4026 |
<para>In a more general context, each <link |
| 4027 |
linkend="processref">process</link> has an |
| 4028 |
<quote>environment</quote>, that is, a group of |
| 4029 |
variables that the process may reference. In this sense, |
| 4030 |
the shell behaves like any other process.</para> |
| 4031 |
|
| 4032 |
<para>Every time a shell starts, it creates shell variables that |
| 4033 |
correspond to its own environmental variables. Updating |
| 4034 |
or adding new environmental variables causes the |
| 4035 |
shell to update its environment, and all the shell's |
| 4036 |
<firstterm>child processes</firstterm> (the commands it |
| 4037 |
executes) inherit this environment.</para> |
| 4038 |
|
| 4039 |
</note> |
| 4040 |
|
| 4041 |
<caution> |
| 4042 |
<para>The space allotted to the environment is limited. |
| 4043 |
Creating too many environmental variables or ones that use up |
| 4044 |
excessive space may cause problems.</para> |
| 4045 |
|
| 4046 |
<para> |
| 4047 |
<screen> |
| 4048 |
<prompt>bash$ </prompt><userinput>eval "`seq 10000 | sed -e 's/.*/export var&=ZZZZZZZZZZZZZZ/'`"</userinput> |
| 4049 |
|
| 4050 |
<prompt>bash$ </prompt><userinput>du</userinput> |
| 4051 |
<computeroutput>bash: /usr/bin/du: Argument list too long</computeroutput> |
| 4052 |
</screen> |
| 4053 |
</para> |
| 4054 |
|
| 4055 |
<para>Note: this <quote>error</quote> has been fixed, as of |
| 4056 |
kernel version 2.6.23.</para> |
| 4057 |
<para>(Thank you, Stéphane Chazelas for the clarification, |
| 4058 |
and for providing the above example.)</para> |
| 4059 |
</caution> |
| 4060 |
|
| 4061 |
<para>If a script sets environmental variables, they need to be |
| 4062 |
<quote>exported,</quote> that is, reported to the |
| 4063 |
<firstterm>environment</firstterm> local to |
| 4064 |
the script. This is the function of the <link |
| 4065 |
linkend="exportref">export</link> command.</para> |
| 4066 |
|
| 4067 |
<anchor id="childref"> |
| 4068 |
<note> |
| 4069 |
<para>A script can <command>export</command> variables only |
| 4070 |
to child <link linkend="processref">processes</link>, |
| 4071 |
that is, only to commands or processes which that |
| 4072 |
particular script initiates. A script invoked from |
| 4073 |
the command-line <replaceable>cannot</replaceable> |
| 4074 |
export variables back to the command-line environment. |
| 4075 |
<emphasis><link linkend="forkref">Child processes</link> |
| 4076 |
cannot export variables back to the parent processes that |
| 4077 |
spawned them.</emphasis></para> |
| 4078 |
<para><anchor id="childref2"><userinput>Definition:</userinput> |
| 4079 |
A <firstterm>child process</firstterm> is a |
| 4080 |
subprocess launched by another process, its <link |
| 4081 |
linkend="parentref">parent</link>.</para> |
| 4082 |
</note> |
| 4083 |
|
| 4084 |
|
| 4085 |
</listitem> |
| 4086 |
</varlistentry> |
| 4087 |
|
| 4088 |
<varlistentry> |
| 4089 |
<term><anchor id="posparamref1"><replaceable>Positional parameters</replaceable></term> |
| 4090 |
<indexterm> |
| 4091 |
<primary>parameter</primary> |
| 4092 |
<secondary>positional</secondary> |
| 4093 |
</indexterm> |
| 4094 |
<listitem> |
| 4095 |
<para>Arguments passed to the script from the command |
| 4096 |
line |
| 4097 |
<footnote><para>Note that <link |
| 4098 |
linkend="passedargs"><firstterm>functions</firstterm> |
| 4099 |
also take positional parameters</link>.</para></footnote> |
| 4100 |
: <varname>$0</varname>, <varname>$1</varname>, |
| 4101 |
<varname>$2</varname>, <varname>$3</varname> . . .</para> |
| 4102 |
|
| 4103 |
<para><anchor id="scrnameparam"><varname>$0</varname> is |
| 4104 |
the name of the script itself, |
| 4105 |
<varname>$1</varname> is the first argument, |
| 4106 |
<varname>$2</varname> the second, <varname>$3</varname> |
| 4107 |
the third, and so forth. |
| 4108 |
|
| 4109 |
<footnote> |
| 4110 |
<para><anchor id="arg0">The process calling the |
| 4111 |
script sets the <varname>$0</varname> parameter. By |
| 4112 |
convention, this parameter is the name of the script. See |
| 4113 |
the <link linkend="manref">manpage</link> (manual page) |
| 4114 |
for <command>execv</command>.</para> |
| 4115 |
<para>From the <firstterm>command-line</firstterm>, however, |
| 4116 |
<varname>$0</varname> is the name of the shell. |
| 4117 |
<screen><prompt>bash$ </prompt><userinput>echo $0</userinput> |
| 4118 |
<computeroutput>bash</computeroutput> |
| 4119 |
|
| 4120 |
<prompt>tcsh% </prompt><userinput>echo $0</userinput> |
| 4121 |
<computeroutput>tcsh</computeroutput></screen></para> |
| 4122 |
</footnote> |
| 4123 |
|
| 4124 |
<anchor id="bracketnotation"> |
| 4125 |
After <varname>$9</varname>, the arguments must be enclosed |
| 4126 |
in brackets, for example, <varname>${10}</varname>, |
| 4127 |
<varname>${11}</varname>, <varname>${12}</varname>.</para> |
| 4128 |
|
| 4129 |
<para>The special variables <link linkend="appref">$* and $@</link> |
| 4130 |
denote <emphasis>all</emphasis> the positional parameters.</para> |
| 4131 |
|
| 4132 |
<example id="ex17"> |
| 4133 |
<title>Positional Parameters</title> |
| 4134 |
<programlisting>&ex17;</programlisting> |
| 4135 |
</example> |
| 4136 |
|
| 4137 |
<para><firstterm>Bracket notation</firstterm> for positional |
| 4138 |
parameters leads to a fairly simple way of referencing |
| 4139 |
the <emphasis>last</emphasis> argument passed to a |
| 4140 |
script on the command-line. This also requires <link |
| 4141 |
linkend="varrefnew">indirect referencing</link>.</para> |
| 4142 |
|
| 4143 |
<para><anchor id="lastargref"></para> |
| 4144 |
<para><programlisting>args=$# # Number of args passed. |
| 4145 |
lastarg=${!args} |
| 4146 |
# Note: This is an *indirect reference* to $args ... |
| 4147 |
|
| 4148 |
|
| 4149 |
# Or: lastarg=${!#} (Thanks, Chris Monson.) |
| 4150 |
# This is an *indirect reference* to the $# variable. |
| 4151 |
# Note that lastarg=${!$#} doesn't work. |
| 4152 |
</programlisting></para> |
| 4153 |
|
| 4154 |
|
| 4155 |
<para>Some scripts can perform different operations, |
| 4156 |
depending on which name they are invoked with. For this |
| 4157 |
to work, the script needs to check <varname>$0</varname>, |
| 4158 |
the name it was invoked by. |
| 4159 |
|
| 4160 |
<footnote><para>If the the script is <link |
| 4161 |
linkend="sourceref">sourced</link> or <link |
| 4162 |
linkend="symlinkref">symlinked</link>, then |
| 4163 |
this will not work. It is safer to check <link |
| 4164 |
linkend="bashsourceref">$BASH_Source</link>.</para></footnote> |
| 4165 |
|
| 4166 |
There must also exist symbolic links to all the alternate |
| 4167 |
names of the script. See <xref linkend="hellol">.</para> |
| 4168 |
|
| 4169 |
<para><anchor id="nullvar"></para> |
| 4170 |
<tip><para>If a script expects a command-line parameter |
| 4171 |
but is invoked without one, this may cause a <firstterm>null |
| 4172 |
variable assignment</firstterm>, generally an undesirable |
| 4173 |
result. One way to prevent this is to append an extra |
| 4174 |
character to both sides of the assignment statement using |
| 4175 |
the expected positional parameter. </para></tip> |
| 4176 |
|
| 4177 |
<programlisting>variable1_=$1_ # Rather than variable1=$1 |
| 4178 |
# This will prevent an error, even if positional parameter is absent. |
| 4179 |
|
| 4180 |
critical_argument01=$variable1_ |
| 4181 |
|
| 4182 |
# The extra character can be stripped off later, like so. |
| 4183 |
variable1=${variable1_/_/} |
| 4184 |
# Side effects only if $variable1_ begins with an underscore. |
| 4185 |
# This uses one of the parameter substitution templates discussed later. |
| 4186 |
# (Leaving out the replacement pattern results in a deletion.) |
| 4187 |
|
| 4188 |
# A more straightforward way of dealing with this is |
| 4189 |
#+ to simply test whether expected positional parameters have been passed. |
| 4190 |
if [ -z $1 ] |
| 4191 |
then |
| 4192 |
exit $E_MISSING_POS_PARAM |
| 4193 |
fi |
| 4194 |
|
| 4195 |
|
| 4196 |
# However, as Fabian Kreutz points out, |
| 4197 |
#+ the above method may have unexpected side-effects. |
| 4198 |
# A better method is parameter substitution: |
| 4199 |
# ${1:-$DefaultVal} |
| 4200 |
# See the "Parameter Substition" section |
| 4201 |
#+ in the "Variables Revisited" chapter. |
| 4202 |
</programlisting> |
| 4203 |
|
| 4204 |
<para>---</para> |
| 4205 |
|
| 4206 |
<example id="ex18"> |
| 4207 |
<title><firstterm>wh</firstterm>, <firstterm> |
| 4208 |
whois</firstterm> domain name lookup</title> |
| 4209 |
<programlisting>&ex18;</programlisting> |
| 4210 |
</example> |
| 4211 |
|
| 4212 |
<para>---</para> |
| 4213 |
|
| 4214 |
<para><anchor id="shiftref"></para> |
| 4215 |
<para> |
| 4216 |
<indexterm> |
| 4217 |
<primary>shift</primary> |
| 4218 |
</indexterm> |
| 4219 |
<indexterm> |
| 4220 |
<primary>command</primary> |
| 4221 |
<secondary>shift</secondary> |
| 4222 |
</indexterm> |
| 4223 |
The <command>shift</command> command reassigns the positional |
| 4224 |
parameters, in effect shifting them to the left one notch.</para> |
| 4225 |
|
| 4226 |
<para><varname>$1</varname> <--- <varname>$2</varname>, <varname>$2</varname> <--- <varname>$3</varname>, <varname>$3</varname> <--- <varname>$4</varname>, etc.</para> |
| 4227 |
|
| 4228 |
<para>The old <varname>$1</varname> disappears, but |
| 4229 |
<emphasis><varname>$0</varname> (the script name) |
| 4230 |
does not change</emphasis>. If you use a large number of |
| 4231 |
positional parameters to a script, <command>shift</command> |
| 4232 |
lets you access those past <literal>10</literal>, although |
| 4233 |
<link linkend="bracketnotation">{bracket} notation</link> |
| 4234 |
also permits this.</para> |
| 4235 |
|
| 4236 |
<example id="ex19"> |
| 4237 |
<title>Using <firstterm>shift</firstterm></title> |
| 4238 |
<programlisting>&ex19;</programlisting> |
| 4239 |
</example> |
| 4240 |
|
| 4241 |
<para>The <command>shift</command> command can take a numerical |
| 4242 |
parameter indicating how many positions to shift.</para> |
| 4243 |
|
| 4244 |
<para><programlisting>#!/bin/bash |
| 4245 |
# shift-past.sh |
| 4246 |
|
| 4247 |
shift 3 # Shift 3 positions. |
| 4248 |
# n=3; shift $n |
| 4249 |
# Has the same effect. |
| 4250 |
|
| 4251 |
echo "$1" |
| 4252 |
|
| 4253 |
exit 0 |
| 4254 |
|
| 4255 |
# ======================== # |
| 4256 |
|
| 4257 |
|
| 4258 |
$ sh shift-past.sh 1 2 3 4 5 |
| 4259 |
4 |
| 4260 |
|
| 4261 |
# However, as Eleni Fragkiadaki, points out, |
| 4262 |
#+ attempting a 'shift' past the number of |
| 4263 |
#+ positional parameters ($#) returns an exit status of 1, |
| 4264 |
#+ and the positional parameters themselves do not change. |
| 4265 |
# This means possibly getting stuck in an endless loop. . . . |
| 4266 |
# For example: |
| 4267 |
# until [ -z "$1" ] |
| 4268 |
# do |
| 4269 |
# echo -n "$1 " |
| 4270 |
# shift 20 # If less than 20 pos params, |
| 4271 |
# done #+ then loop never ends! |
| 4272 |
# |
| 4273 |
# When in doubt, add a sanity check. . . . |
| 4274 |
# shift 20 || break |
| 4275 |
# ^^^^^^^^</programlisting></para> |
| 4276 |
|
| 4277 |
<note><para>The <command>shift</command> command works in a similar |
| 4278 |
fashion on parameters passed to a <link |
| 4279 |
linkend="functionref">function</link>. See <xref |
| 4280 |
linkend="multiplication">.</para></note> |
| 4281 |
|
| 4282 |
</listitem> |
| 4283 |
</varlistentry> |
| 4284 |
</variablelist> |
| 4285 |
|
| 4286 |
</sect1> <!-- Special Variable Types --> |
| 4287 |
|
| 4288 |
|
| 4289 |
</chapter> <!-- Variables --> |
| 4290 |
|
| 4291 |
|
| 4292 |
|
| 4293 |
<chapter id="quoting"> |
| 4294 |
<title>Quoting</title> |
| 4295 |
|
| 4296 |
<para><anchor id="quotingref"></para> |
| 4297 |
|
| 4298 |
<indexterm> |
| 4299 |
<primary>"</primary> |
| 4300 |
</indexterm> |
| 4301 |
<indexterm> |
| 4302 |
<primary>special character</primary> |
| 4303 |
<secondary>"</secondary> |
| 4304 |
</indexterm> |
| 4305 |
<indexterm> |
| 4306 |
<primary>'</primary> |
| 4307 |
</indexterm> |
| 4308 |
<indexterm> |
| 4309 |
<primary>special character</primary> |
| 4310 |
<secondary>'</secondary> |
| 4311 |
</indexterm> |
| 4312 |
<indexterm> |
| 4313 |
<primary>quote</primary> |
| 4314 |
</indexterm> |
| 4315 |
<indexterm> |
| 4316 |
<primary>\</primary> |
| 4317 |
</indexterm> |
| 4318 |
<indexterm> |
| 4319 |
<primary>special character</primary> |
| 4320 |
<secondary>\</secondary> |
| 4321 |
</indexterm> |
| 4322 |
<indexterm> |
| 4323 |
<primary>escape</primary> |
| 4324 |
</indexterm> |
| 4325 |
<para>Quoting means just that, bracketing a string in quotes. This |
| 4326 |
has the effect of protecting <link linkend="scharlist1">special |
| 4327 |
characters</link> in the string from reinterpretation |
| 4328 |
or expansion by the shell or shell script. (A character |
| 4329 |
is <quote>special</quote> if it has an interpretation |
| 4330 |
other than its literal meaning. For example, the <link |
| 4331 |
linkend="asteriskref">asterisk *</link> represents |
| 4332 |
a <firstterm>wild card</firstterm> character in |
| 4333 |
<link linkend="globbingref">globbing</link> and <link |
| 4334 |
linkend="regexref">Regular Expressions</link>).</para> |
| 4335 |
|
| 4336 |
<para> |
| 4337 |
<screen><prompt>bash$ </prompt><userinput>ls -l [Vv]*</userinput> |
| 4338 |
<computeroutput>-rw-rw-r-- 1 bozo bozo 324 Apr 2 15:05 VIEWDATA.BAT |
| 4339 |
-rw-rw-r-- 1 bozo bozo 507 May 4 14:25 vartrace.sh |
| 4340 |
-rw-rw-r-- 1 bozo bozo 539 Apr 14 17:11 viewdata.sh |
| 4341 |
</computeroutput> |
| 4342 |
|
| 4343 |
<prompt>bash$ </prompt><userinput>ls -l '[Vv]*'</userinput> |
| 4344 |
<computeroutput>ls: [Vv]*: No such file or directory</computeroutput></screen> |
| 4345 |
</para> |
| 4346 |
|
| 4347 |
<para><anchor id="quotingdef"></para> |
| 4348 |
<sidebar><para>In everyday speech or writing, when we |
| 4349 |
<quote>quote</quote> a phrase, we set it apart and give it special |
| 4350 |
meaning. In a Bash script, when we <firstterm>quote</firstterm> a |
| 4351 |
string, we set it apart and protect its <firstterm>literal</firstterm> |
| 4352 |
meaning.</para></sidebar> |
| 4353 |
|
| 4354 |
<para>Certain programs and utilities reinterpret or expand |
| 4355 |
special characters in a quoted string. An important use of |
| 4356 |
quoting is protecting a command-line parameter from the shell, |
| 4357 |
but still letting the calling program expand it.</para> |
| 4358 |
|
| 4359 |
<para> |
| 4360 |
<screen><prompt>bash$ </prompt><userinput>grep '[Ff]irst' *.txt</userinput> |
| 4361 |
<computeroutput>file1.txt:This is the first line of file1.txt. |
| 4362 |
file2.txt:This is the First line of file2.txt.</computeroutput></screen> |
| 4363 |
</para> |
| 4364 |
|
| 4365 |
<para>Note that the unquoted <userinput>grep [Ff]irst *.txt</userinput> |
| 4366 |
works under the Bash shell. |
| 4367 |
<footnote><para>Unless there is a file named |
| 4368 |
<filename>first</filename> in the current working directory. Yet |
| 4369 |
another reason to <firstterm>quote</firstterm>. (Thank you, Harald |
| 4370 |
Koenig, for pointing this out.</para></footnote> |
| 4371 |
</para> |
| 4372 |
|
| 4373 |
|
| 4374 |
<para>Quoting can also suppress <link linkend="echoref">echo's</link> |
| 4375 |
<quote>appetite</quote> for newlines.</para> |
| 4376 |
|
| 4377 |
<para> |
| 4378 |
<screen><prompt>bash$ </prompt><userinput>echo $(ls -l)</userinput> |
| 4379 |
<computeroutput>total 8 -rw-rw-r-- 1 bo bo 13 Aug 21 12:57 t.sh -rw-rw-r-- 1 bo bo 78 Aug 21 12:57 u.sh</computeroutput> |
| 4380 |
|
| 4381 |
|
| 4382 |
<prompt>bash$ </prompt><userinput>echo "$(ls -l)"</userinput> |
| 4383 |
<computeroutput>total 8 |
| 4384 |
-rw-rw-r-- 1 bo bo 13 Aug 21 12:57 t.sh |
| 4385 |
-rw-rw-r-- 1 bo bo 78 Aug 21 12:57 u.sh</computeroutput></screen> |
| 4386 |
</para> |
| 4387 |
|
| 4388 |
|
| 4389 |
<sect1 id="quotingvar"> |
| 4390 |
<title>Quoting Variables</title> |
| 4391 |
|
| 4392 |
|
| 4393 |
<para>When referencing a variable, it is generally advisable to |
| 4394 |
enclose its name in double quotes. |
| 4395 |
This prevents reinterpretation of all special characters within |
| 4396 |
the quoted string -- except <token>$</token>, <token>`</token> |
| 4397 |
(backquote), and <token>\</token> (escape). |
| 4398 |
|
| 4399 |
<footnote> |
| 4400 |
|
| 4401 |
<para><anchor id="quotingbsl"></para> |
| 4402 |
<para>Encapsulating <quote>!</quote> within double |
| 4403 |
quotes gives an error when used <emphasis>from the command |
| 4404 |
line</emphasis>. This is interpreted as a <link |
| 4405 |
linkend="histcommands">history command</link>. Within a script, |
| 4406 |
though, this problem does not occur, since the Bash history |
| 4407 |
mechanism is disabled then.</para> |
| 4408 |
|
| 4409 |
<para>Of more concern is the <emphasis>apparently</emphasis> |
| 4410 |
inconsistent behavior of <replaceable>\</replaceable> |
| 4411 |
within double quotes, and especially following an |
| 4412 |
<command>echo -e</command> command.</para> |
| 4413 |
|
| 4414 |
<para> |
| 4415 |
<screen> |
| 4416 |
<prompt>bash$ </prompt><userinput>echo hello\!</userinput> |
| 4417 |
<computeroutput>hello!</computeroutput> |
| 4418 |
<prompt>bash$ </prompt><userinput>echo "hello\!"</userinput> |
| 4419 |
<computeroutput>hello\!</computeroutput> |
| 4420 |
|
| 4421 |
|
| 4422 |
<prompt>bash$ </prompt><userinput>echo \</userinput> |
| 4423 |
<computeroutput>></computeroutput> |
| 4424 |
<prompt>bash$ </prompt><userinput>echo "\"</userinput> |
| 4425 |
<computeroutput>></computeroutput> |
| 4426 |
<prompt>bash$ </prompt><userinput>echo \a</userinput> |
| 4427 |
<computeroutput>a</computeroutput> |
| 4428 |
<prompt>bash$ </prompt><userinput>echo "\a"</userinput> |
| 4429 |
<computeroutput>\a</computeroutput> |
| 4430 |
|
| 4431 |
|
| 4432 |
<prompt>bash$ </prompt><userinput>echo x\ty</userinput> |
| 4433 |
<computeroutput>xty</computeroutput> |
| 4434 |
<prompt>bash$ </prompt><userinput>echo "x\ty"</userinput> |
| 4435 |
<computeroutput>x\ty</computeroutput> |
| 4436 |
|
| 4437 |
<prompt>bash$ </prompt><userinput>echo -e x\ty</userinput> |
| 4438 |
<computeroutput>xty</computeroutput> |
| 4439 |
<prompt>bash$ </prompt><userinput>echo -e "x\ty"</userinput> |
| 4440 |
<computeroutput>x y</computeroutput> |
| 4441 |
</screen> |
| 4442 |
</para> |
| 4443 |
|
| 4444 |
<para>Double quotes following an <firstterm>echo</firstterm> |
| 4445 |
<emphasis>sometimes</emphasis> escape |
| 4446 |
<replaceable>\</replaceable>. Moreover, the |
| 4447 |
<option>-e</option> option to <firstterm>echo</firstterm> |
| 4448 |
causes the <quote>\t</quote> to be interpreted as a |
| 4449 |
<firstterm>tab</firstterm>.</para> |
| 4450 |
|
| 4451 |
<para>(Thank you, Wayne Pollock, for pointing this out, and Geoff |
| 4452 |
Lee and Daniel Barclay for explaining it.) </para> |
| 4453 |
|
| 4454 |
</footnote> |
| 4455 |
|
| 4456 |
Keeping <token>$</token> as a special character within |
| 4457 |
double quotes permits referencing a quoted variable |
| 4458 |
(<replaceable>"$variable"</replaceable>), that is, replacing the |
| 4459 |
variable with its value (see <xref linkend="ex9">, above).</para> |
| 4460 |
|
| 4461 |
<para><anchor id="wsquo"></para> |
| 4462 |
<para>Use double quotes to prevent word splitting. |
| 4463 |
|
| 4464 |
<footnote><para><anchor id="wsplitref"><quote>Word |
| 4465 |
splitting,</quote> in this context, means dividing |
| 4466 |
a character string into separate and discrete |
| 4467 |
arguments.</para></footnote> |
| 4468 |
|
| 4469 |
An argument enclosed in double quotes presents |
| 4470 |
itself as a single word, even if it contains <link |
| 4471 |
linkend="whitespaceref">whitespace</link> separators.</para> |
| 4472 |
|
| 4473 |
|
| 4474 |
<para><anchor id="varsplitting"></para> |
| 4475 |
<para><programlisting>List="one two three" |
| 4476 |
|
| 4477 |
for a in $List # Splits the variable in parts at whitespace. |
| 4478 |
do |
| 4479 |
echo "$a" |
| 4480 |
done |
| 4481 |
# one |
| 4482 |
# two |
| 4483 |
# three |
| 4484 |
|
| 4485 |
echo "---" |
| 4486 |
|
| 4487 |
for a in "$List" # Preserves whitespace in a single variable. |
| 4488 |
do # ^ ^ |
| 4489 |
echo "$a" |
| 4490 |
done |
| 4491 |
# one two three</programlisting></para> |
| 4492 |
|
| 4493 |
<para>A more elaborate example:</para> |
| 4494 |
|
| 4495 |
<para><programlisting>variable1="a variable containing five words" |
| 4496 |
COMMAND This is $variable1 # Executes COMMAND with 7 arguments: |
| 4497 |
# "This" "is" "a" "variable" "containing" "five" "words" |
| 4498 |
|
| 4499 |
COMMAND "This is $variable1" # Executes COMMAND with 1 argument: |
| 4500 |
# "This is a variable containing five words" |
| 4501 |
|
| 4502 |
|
| 4503 |
variable2="" # Empty. |
| 4504 |
|
| 4505 |
COMMAND $variable2 $variable2 $variable2 |
| 4506 |
# Executes COMMAND with no arguments. |
| 4507 |
COMMAND "$variable2" "$variable2" "$variable2" |
| 4508 |
# Executes COMMAND with 3 empty arguments. |
| 4509 |
COMMAND "$variable2 $variable2 $variable2" |
| 4510 |
# Executes COMMAND with 1 argument (2 spaces). |
| 4511 |
|
| 4512 |
# Thanks, Stéphane Chazelas. |
| 4513 |
</programlisting></para> |
| 4514 |
|
| 4515 |
|
| 4516 |
<tip><para>Enclosing the arguments to an <command>echo</command> |
| 4517 |
statement in double quotes is necessary only when word splitting |
| 4518 |
or preservation of <link linkend="whitespaceref">whitespace</link> |
| 4519 |
is an issue.</para></tip> |
| 4520 |
|
| 4521 |
<example id="weirdvars"> |
| 4522 |
<title>Echoing Weird Variables</title> |
| 4523 |
<programlisting>&weirdvars;</programlisting> |
| 4524 |
</example> |
| 4525 |
|
| 4526 |
<para>Single quotes (<token>' '</token>) operate similarly to double |
| 4527 |
quotes, but do not permit referencing variables, since |
| 4528 |
the special meaning of <token>$</token> is turned off. |
| 4529 |
Within single quotes, <emphasis>every</emphasis> special |
| 4530 |
character except <token>'</token> gets interpreted literally. |
| 4531 |
Consider single quotes (<quote>full quoting</quote>) to be a |
| 4532 |
stricter method of quoting than double quotes (<quote>partial |
| 4533 |
quoting</quote>).</para> |
| 4534 |
|
| 4535 |
<note><para>Since even the escape character (<token>\</token>) |
| 4536 |
gets a literal interpretation within single quotes, trying to |
| 4537 |
enclose a single quote within single quotes will not yield the |
| 4538 |
expected result. |
| 4539 |
<programlisting>echo "Why can't I write 's between single quotes" |
| 4540 |
|
| 4541 |
echo |
| 4542 |
|
| 4543 |
# The roundabout method. |
| 4544 |
echo 'Why can'\''t I write '"'"'s between single quotes' |
| 4545 |
# |-------| |----------| |-----------------------| |
| 4546 |
# Three single-quoted strings, with escaped and quoted single quotes between. |
| 4547 |
|
| 4548 |
# This example courtesy of Stéphane Chazelas.</programlisting> |
| 4549 |
</para></note> |
| 4550 |
|
| 4551 |
</sect1> <!-- Quoting Variables --> |
| 4552 |
|
| 4553 |
|
| 4554 |
|
| 4555 |
<sect1 id="escapingsection"> |
| 4556 |
<title>Escaping</title> |
| 4557 |
|
| 4558 |
<para><anchor id="escp"><firstterm>Escaping</firstterm> is a method |
| 4559 |
of quoting single characters. The <token>escape</token> |
| 4560 |
(<token>\</token>) preceding a character tells the shell to |
| 4561 |
interpret that character literally.</para> |
| 4562 |
|
| 4563 |
<caution><para>With certain commands and utilities, such as <link |
| 4564 |
linkend="echoref">echo</link> and <link |
| 4565 |
linkend="sedref">sed</link>, escaping a character may have the |
| 4566 |
opposite effect - it can toggle on a special meaning for that |
| 4567 |
character.</para></caution> |
| 4568 |
|
| 4569 |
<variablelist id="specialmeanings"> |
| 4570 |
<title><anchor id="spm">Special meanings of certain |
| 4571 |
escaped characters</title> |
| 4572 |
|
| 4573 |
<varlistentry> |
| 4574 |
<term>used with <command>echo</command> and |
| 4575 |
<command>sed</command></term> |
| 4576 |
<listitem><para></para></listitem> |
| 4577 |
</varlistentry> |
| 4578 |
|
| 4579 |
<varlistentry><term><token>\n</token></term> |
| 4580 |
<indexterm> |
| 4581 |
<primary>\n</primary> |
| 4582 |
</indexterm> |
| 4583 |
<indexterm> |
| 4584 |
<primary>escaped character</primary> |
| 4585 |
<secondary>\n</secondary> |
| 4586 |
</indexterm> |
| 4587 |
<indexterm> |
| 4588 |
<primary>newline</primary> |
| 4589 |
</indexterm> |
| 4590 |
<listitem><para>means newline</para> |
| 4591 |
</listitem> |
| 4592 |
</varlistentry> |
| 4593 |
|
| 4594 |
<varlistentry><term><token>\r</token></term> |
| 4595 |
<indexterm> |
| 4596 |
<primary>\r</primary> |
| 4597 |
</indexterm> |
| 4598 |
<indexterm> |
| 4599 |
<primary>escaped character</primary> |
| 4600 |
<secondary>\r</secondary> |
| 4601 |
</indexterm> |
| 4602 |
<indexterm> |
| 4603 |
<primary>carriage return</primary> |
| 4604 |
</indexterm> |
| 4605 |
<listitem><para>means return</para> |
| 4606 |
</listitem> |
| 4607 |
</varlistentry> |
| 4608 |
|
| 4609 |
<varlistentry><term><token>\t</token></term> |
| 4610 |
<indexterm> |
| 4611 |
<primary>\t</primary> |
| 4612 |
</indexterm> |
| 4613 |
<indexterm> |
| 4614 |
<primary>escaped character</primary> |
| 4615 |
<secondary>\t</secondary> |
| 4616 |
</indexterm> |
| 4617 |
<indexterm> |
| 4618 |
<primary>tabulation</primary> |
| 4619 |
</indexterm> |
| 4620 |
<listitem><para>means tab</para> |
| 4621 |
</listitem> |
| 4622 |
</varlistentry> |
| 4623 |
|
| 4624 |
<varlistentry><term><token>\v</token></term> |
| 4625 |
<indexterm> |
| 4626 |
<primary>\v</primary> |
| 4627 |
</indexterm> |
| 4628 |
<indexterm> |
| 4629 |
<primary>escaped character</primary> |
| 4630 |
<secondary>\v</secondary> |
| 4631 |
</indexterm> |
| 4632 |
<indexterm> |
| 4633 |
<primary>vertical tabulation</primary> |
| 4634 |
</indexterm> |
| 4635 |
<listitem><para> means vertical tab</para> |
| 4636 |
</listitem> |
| 4637 |
</varlistentry> |
| 4638 |
|
| 4639 |
<varlistentry><term><token>\b</token></term> |
| 4640 |
<indexterm> |
| 4641 |
<primary>\b</primary> |
| 4642 |
</indexterm> |
| 4643 |
<indexterm> |
| 4644 |
<primary>escaped character</primary> |
| 4645 |
<secondary>\b</secondary> |
| 4646 |
</indexterm> |
| 4647 |
<indexterm> |
| 4648 |
<primary>backspace</primary> |
| 4649 |
</indexterm> |
| 4650 |
<listitem><para>means backspace</para> |
| 4651 |
</listitem> |
| 4652 |
</varlistentry> |
| 4653 |
|
| 4654 |
<varlistentry><term><token>\a</token></term> |
| 4655 |
<indexterm> |
| 4656 |
<primary>\a</primary> |
| 4657 |
</indexterm> |
| 4658 |
<indexterm> |
| 4659 |
<primary>escaped character</primary> |
| 4660 |
<secondary>\a</secondary> |
| 4661 |
</indexterm> |
| 4662 |
<indexterm> |
| 4663 |
<primary>alert</primary> |
| 4664 |
</indexterm> |
| 4665 |
<indexterm> |
| 4666 |
<primary>beep</primary> |
| 4667 |
</indexterm> |
| 4668 |
<indexterm> |
| 4669 |
<primary>flash</primary> |
| 4670 |
</indexterm> |
| 4671 |
<listitem><para>means <firstterm>alert</firstterm> (beep or flash)</para> |
| 4672 |
</listitem> |
| 4673 |
</varlistentry> |
| 4674 |
|
| 4675 |
<varlistentry><term><token>\0xx</token></term> |
| 4676 |
<indexterm> |
| 4677 |
<primary>\0xx</primary> |
| 4678 |
</indexterm> |
| 4679 |
<indexterm> |
| 4680 |
<primary>escaped character</primary> |
| 4681 |
<secondary>\0nn</secondary> |
| 4682 |
</indexterm> |
| 4683 |
<indexterm> |
| 4684 |
<primary>octal ASCII</primary> |
| 4685 |
</indexterm> |
| 4686 |
<listitem><para><anchor id="octalref">translates to the |
| 4687 |
octal <link linkend="asciidef">ASCII</link> |
| 4688 |
equivalent of <replaceable>0nn</replaceable>, where |
| 4689 |
<replaceable>nn</replaceable> is a string of digits</para> |
| 4690 |
|
| 4691 |
<important> |
| 4692 |
<para><anchor id="strq"></para> |
| 4693 |
<para>The <userinput>$' ... '</userinput> |
| 4694 |
<link linkend="quotingref">quoted</link> string-expansion |
| 4695 |
construct is a mechanism that uses escaped octal or hex values |
| 4696 |
to assign ASCII characters to variables, e.g., |
| 4697 |
<command>quote=$'\042'</command>.</para> |
| 4698 |
</important> |
| 4699 |
|
| 4700 |
<example id="escaped"> |
| 4701 |
<title>Escaped Characters</title> |
| 4702 |
<programlisting>&escaped;</programlisting> |
| 4703 |
</example> |
| 4704 |
|
| 4705 |
<para>A more elaborate example:</para> |
| 4706 |
<example id="bashek"> |
| 4707 |
<title>Detecting key-presses</title> |
| 4708 |
<programlisting>&bashek;</programlisting> |
| 4709 |
</example> |
| 4710 |
|
| 4711 |
<para>See also <xref linkend="ex77">.</para> |
| 4712 |
|
| 4713 |
</listitem> |
| 4714 |
</varlistentry> |
| 4715 |
|
| 4716 |
<varlistentry><term><token>\"</token></term> |
| 4717 |
<indexterm> |
| 4718 |
<primary>\"</primary> |
| 4719 |
</indexterm> |
| 4720 |
<indexterm> |
| 4721 |
<primary>escaped character</primary> |
| 4722 |
<secondary>\"</secondary> |
| 4723 |
</indexterm> |
| 4724 |
<indexterm> |
| 4725 |
<primary>quote</primary> |
| 4726 |
</indexterm> |
| 4727 |
<listitem><para> gives the quote its literal meaning</para> |
| 4728 |
<para><programlisting>echo "Hello" # Hello |
| 4729 |
echo "\"Hello\" ... he said." # "Hello" ... he said.</programlisting></para> |
| 4730 |
</listitem> |
| 4731 |
</varlistentry> |
| 4732 |
|
| 4733 |
<varlistentry><term><token>\$</token></term> |
| 4734 |
<indexterm> |
| 4735 |
<primary>\$</primary> |
| 4736 |
</indexterm> |
| 4737 |
<indexterm> |
| 4738 |
<primary>escaped character</primary> |
| 4739 |
<secondary>\$</secondary> |
| 4740 |
</indexterm> |
| 4741 |
<indexterm> |
| 4742 |
<primary>dollar</primary> |
| 4743 |
</indexterm> |
| 4744 |
<listitem><para>gives the dollar sign its literal meaning |
| 4745 |
(variable name following <token>\$</token> will not be |
| 4746 |
referenced)</para> |
| 4747 |
<para><programlisting>echo "\$variable01" # $variable01 |
| 4748 |
echo "The book cost \$7.98." # The book cost $7.98.</programlisting></para> |
| 4749 |
</listitem> |
| 4750 |
</varlistentry> |
| 4751 |
|
| 4752 |
<varlistentry><term><token>\\</token></term> |
| 4753 |
<indexterm> |
| 4754 |
<primary>\\</primary> |
| 4755 |
</indexterm> |
| 4756 |
<indexterm> |
| 4757 |
<primary>escaped character</primary> |
| 4758 |
<secondary>\\</secondary> |
| 4759 |
</indexterm> |
| 4760 |
<indexterm> |
| 4761 |
<primary>double backslash</primary> |
| 4762 |
</indexterm> |
| 4763 |
<listitem><para>gives the backslash its literal meaning</para> |
| 4764 |
<para><programlisting>echo "\\" # Results in \ |
| 4765 |
|
| 4766 |
# Whereas . . . |
| 4767 |
|
| 4768 |
echo "\" # Invokes secondary prompt from the command-line. |
| 4769 |
# In a script, gives an error message. |
| 4770 |
|
| 4771 |
# However . . . |
| 4772 |
|
| 4773 |
echo '\' # Results in \</programlisting></para> |
| 4774 |
|
| 4775 |
</listitem> |
| 4776 |
</varlistentry> |
| 4777 |
</variablelist> |
| 4778 |
|
| 4779 |
<note> |
| 4780 |
<para>The behavior of <token>\</token> depends on whether |
| 4781 |
it is escaped, <link linkend="snglquo">strong-quoted</link>, |
| 4782 |
<link linkend="dblquo">weak-quoted</link>, or appearing within |
| 4783 |
<link linkend="commandsubref">command substitution</link> or a |
| 4784 |
<link linkend="heredocref">here document</link>. |
| 4785 |
|
| 4786 |
<programlisting> # Simple escaping and quoting |
| 4787 |
echo \z # z |
| 4788 |
echo \\z # \z |
| 4789 |
echo '\z' # \z |
| 4790 |
echo '\\z' # \\z |
| 4791 |
echo "\z" # \z |
| 4792 |
echo "\\z" # \z |
| 4793 |
|
| 4794 |
# Command substitution |
| 4795 |
echo `echo \z` # z |
| 4796 |
echo `echo \\z` # z |
| 4797 |
echo `echo \\\z` # \z |
| 4798 |
echo `echo \\\\z` # \z |
| 4799 |
echo `echo \\\\\\z` # \z |
| 4800 |
echo `echo \\\\\\\z` # \\z |
| 4801 |
echo `echo "\z"` # \z |
| 4802 |
echo `echo "\\z"` # \z |
| 4803 |
|
| 4804 |
# Here document |
| 4805 |
cat <<EOF |
| 4806 |
\z |
| 4807 |
EOF # \z |
| 4808 |
|
| 4809 |
cat <<EOF |
| 4810 |
\\z |
| 4811 |
EOF # \z |
| 4812 |
|
| 4813 |
# These examples supplied by Stéphane Chazelas.</programlisting> |
| 4814 |
</para> |
| 4815 |
|
| 4816 |
<para>Elements of a string assigned to a variable may be escaped, but |
| 4817 |
the escape character alone may not be assigned to a variable. |
| 4818 |
<programlisting>variable=\ |
| 4819 |
echo "$variable" |
| 4820 |
# Will not work - gives an error message: |
| 4821 |
# test.sh: : command not found |
| 4822 |
# A "naked" escape cannot safely be assigned to a variable. |
| 4823 |
# |
| 4824 |
# What actually happens here is that the "\" escapes the newline and |
| 4825 |
#+ the effect is variable=echo "$variable" |
| 4826 |
#+ invalid variable assignment |
| 4827 |
|
| 4828 |
variable=\ |
| 4829 |
23skidoo |
| 4830 |
echo "$variable" # 23skidoo |
| 4831 |
# This works, since the second line |
| 4832 |
#+ is a valid variable assignment. |
| 4833 |
|
| 4834 |
variable=\ |
| 4835 |
# \^ escape followed by space |
| 4836 |
echo "$variable" # space |
| 4837 |
|
| 4838 |
variable=\\ |
| 4839 |
echo "$variable" # \ |
| 4840 |
|
| 4841 |
variable=\\\ |
| 4842 |
echo "$variable" |
| 4843 |
# Will not work - gives an error message: |
| 4844 |
# test.sh: \: command not found |
| 4845 |
# |
| 4846 |
# First escape escapes second one, but the third one is left "naked", |
| 4847 |
#+ with same result as first instance, above. |
| 4848 |
|
| 4849 |
variable=\\\\ |
| 4850 |
echo "$variable" # \\ |
| 4851 |
# Second and fourth escapes escaped. |
| 4852 |
# This is o.k.</programlisting> |
| 4853 |
</para> |
| 4854 |
|
| 4855 |
</note> |
| 4856 |
|
| 4857 |
|
| 4858 |
|
| 4859 |
<para>Escaping a space can prevent word splitting in a command's argument list. |
| 4860 |
<programlisting>file_list="/bin/cat /bin/gzip /bin/more /usr/bin/less /usr/bin/emacs-20.7" |
| 4861 |
# List of files as argument(s) to a command. |
| 4862 |
|
| 4863 |
# Add two files to the list, and list all. |
| 4864 |
ls -l /usr/X11R6/bin/xsetroot /sbin/dump $file_list |
| 4865 |
|
| 4866 |
echo "-------------------------------------------------------------------------" |
| 4867 |
|
| 4868 |
# What happens if we escape a couple of spaces? |
| 4869 |
ls -l /usr/X11R6/bin/xsetroot\ /sbin/dump\ $file_list |
| 4870 |
# Error: the first three files concatenated into a single argument to 'ls -l' |
| 4871 |
# because the two escaped spaces prevent argument (word) splitting.</programlisting> |
| 4872 |
</para> |
| 4873 |
|
| 4874 |
|
| 4875 |
<para><anchor id="escnewline"></para> |
| 4876 |
<para>The <token>escape</token> also provides a means of writing a |
| 4877 |
multi-line command. Normally, each separate line constitutes |
| 4878 |
a different command, but an <token>escape</token> at the end |
| 4879 |
of a line <emphasis>escapes the newline character</emphasis>, |
| 4880 |
and the command sequence continues on to the next line.</para> |
| 4881 |
<para><programlisting>(cd /source/directory && tar cf - . ) | \ |
| 4882 |
(cd /dest/directory && tar xpvf -) |
| 4883 |
# Repeating Alan Cox's directory tree copy command, |
| 4884 |
# but split into two lines for increased legibility. |
| 4885 |
|
| 4886 |
# As an alternative: |
| 4887 |
tar cf - -C /source/directory . | |
| 4888 |
tar xpvf - -C /dest/directory |
| 4889 |
# See note below. |
| 4890 |
# (Thanks, Stéphane Chazelas.)</programlisting> |
| 4891 |
|
| 4892 |
<note><para>If a script line ends with a <token>|</token>, a pipe |
| 4893 |
character, then a <token>\</token>, an escape, is not strictly |
| 4894 |
necessary. It is, however, good programming practice to always |
| 4895 |
escape the end of a line of code that continues to the |
| 4896 |
following line.</para></note></para> |
| 4897 |
|
| 4898 |
<para><programlisting>echo "foo |
| 4899 |
bar" |
| 4900 |
#foo |
| 4901 |
#bar |
| 4902 |
|
| 4903 |
echo |
| 4904 |
|
| 4905 |
echo 'foo |
| 4906 |
bar' # No difference yet. |
| 4907 |
#foo |
| 4908 |
#bar |
| 4909 |
|
| 4910 |
echo |
| 4911 |
|
| 4912 |
echo foo\ |
| 4913 |
bar # Newline escaped. |
| 4914 |
#foobar |
| 4915 |
|
| 4916 |
echo |
| 4917 |
|
| 4918 |
echo "foo\ |
| 4919 |
bar" # Same here, as \ still interpreted as escape within weak quotes. |
| 4920 |
#foobar |
| 4921 |
|
| 4922 |
echo |
| 4923 |
|
| 4924 |
echo 'foo\ |
| 4925 |
bar' # Escape character \ taken literally because of strong quoting. |
| 4926 |
#foo\ |
| 4927 |
#bar |
| 4928 |
|
| 4929 |
# Examples suggested by Stéphane Chazelas.</programlisting></para> |
| 4930 |
|
| 4931 |
</sect1> <!-- Escaping --> |
| 4932 |
|
| 4933 |
|
| 4934 |
</chapter> <!-- Quoting --> |
| 4935 |
|
| 4936 |
|
| 4937 |
<chapter id="exit-status"> |
| 4938 |
<title>Exit and Exit Status</title> |
| 4939 |
|
| 4940 |
<epigraph> |
| 4941 |
<para>... there are dark corners in the Bourne shell, and people use all |
| 4942 |
of them.</para> |
| 4943 |
<para>--Chet Ramey</para> |
| 4944 |
</epigraph> |
| 4945 |
|
| 4946 |
<para><anchor id="exitcommandref">The |
| 4947 |
<command> |
| 4948 |
<indexterm> |
| 4949 |
<primary>exit</primary> |
| 4950 |
</indexterm> |
| 4951 |
<indexterm> |
| 4952 |
<primary>command</primary> |
| 4953 |
<secondary>exit</secondary> |
| 4954 |
</indexterm> |
| 4955 |
exit |
| 4956 |
</command> |
| 4957 |
command terminates a script, just as in a <command>C</command> |
| 4958 |
program. It can also return a value, which is available to the |
| 4959 |
script's parent process.</para> |
| 4960 |
|
| 4961 |
|
| 4962 |
<para><anchor id="exitstatusref">Every command returns an |
| 4963 |
<firstterm> |
| 4964 |
<indexterm> |
| 4965 |
<primary>exit status</primary> |
| 4966 |
</indexterm> |
| 4967 |
exit status |
| 4968 |
</firstterm> |
| 4969 |
(sometimes referred to as a |
| 4970 |
<firstterm> |
| 4971 |
<indexterm> |
| 4972 |
<primary>return status</primary> |
| 4973 |
</indexterm> |
| 4974 |
return status |
| 4975 |
</firstterm> or <firstterm>exit code</firstterm>). |
| 4976 |
<anchor id="exitsuccess"> |
| 4977 |
A successful command returns a <returnvalue>0</returnvalue>, while |
| 4978 |
an unsuccessful one returns a <returnvalue>non-zero</returnvalue> |
| 4979 |
value that usually can be interpreted as an <firstterm>error |
| 4980 |
code</firstterm>. Well-behaved UNIX commands, programs, and |
| 4981 |
utilities return a <returnvalue>0</returnvalue> exit code upon |
| 4982 |
successful completion, though there are some exceptions.</para> |
| 4983 |
|
| 4984 |
<para><anchor id="functxstr"></para> |
| 4985 |
<para>Likewise, <link linkend="functionref">functions</link> |
| 4986 |
within a script and the script itself return an exit |
| 4987 |
status. The last command executed in the function or |
| 4988 |
script determines the exit status. Within a script, an |
| 4989 |
<userinput>exit <replaceable>nnn</replaceable></userinput> |
| 4990 |
command may be used to deliver an |
| 4991 |
<returnvalue><replaceable>nnn</replaceable></returnvalue> |
| 4992 |
exit status to the shell |
| 4993 |
(<returnvalue><replaceable>nnn</replaceable></returnvalue> |
| 4994 |
must be an integer in the <returnvalue>0</returnvalue> - |
| 4995 |
<returnvalue>255</returnvalue> range).</para> |
| 4996 |
|
| 4997 |
<note> |
| 4998 |
|
| 4999 |
<para>When a script ends with an <command>exit</command> that has |
| 5000 |
no parameter, the exit status of the script is the exit status |
| 5001 |
of the last command executed in the script (previous to the |
| 5002 |
<command>exit</command>).</para> |
| 5003 |
|
| 5004 |
<para><programlisting>#!/bin/bash |
| 5005 |
|
| 5006 |
COMMAND_1 |
| 5007 |
|
| 5008 |
. . . |
| 5009 |
|
| 5010 |
COMMAND_LAST |
| 5011 |
|
| 5012 |
# Will exit with status of last command. |
| 5013 |
|
| 5014 |
exit</programlisting></para> |
| 5015 |
|
| 5016 |
<para>The equivalent of a bare <command>exit</command> is |
| 5017 |
<command>exit $?</command> or even just omitting the |
| 5018 |
<command>exit</command>.</para> |
| 5019 |
|
| 5020 |
<para><programlisting>#!/bin/bash |
| 5021 |
|
| 5022 |
COMMAND_1 |
| 5023 |
|
| 5024 |
. . . |
| 5025 |
|
| 5026 |
COMMAND_LAST |
| 5027 |
|
| 5028 |
# Will exit with status of last command. |
| 5029 |
|
| 5030 |
exit $?</programlisting></para> |
| 5031 |
|
| 5032 |
<para><programlisting>#!/bin/bash |
| 5033 |
|
| 5034 |
COMMAND1 |
| 5035 |
|
| 5036 |
. . . |
| 5037 |
|
| 5038 |
COMMAND_LAST |
| 5039 |
|
| 5040 |
# Will exit with status of last command.</programlisting></para> |
| 5041 |
|
| 5042 |
</note> |
| 5043 |
|
| 5044 |
<para><anchor id="exsref"></para> |
| 5045 |
|
| 5046 |
<para> |
| 5047 |
<varname> |
| 5048 |
<indexterm> |
| 5049 |
<primary>$?</primary> |
| 5050 |
</indexterm> <indexterm> |
| 5051 |
<primary>variable</primary> <secondary>$?</secondary> |
| 5052 |
</indexterm> $?</varname> reads the exit status of the last |
| 5053 |
command executed. After a function returns, |
| 5054 |
<varname>$?</varname> gives the exit status of the last |
| 5055 |
command executed in the function. This is Bash's way of giving |
| 5056 |
functions a <quote>return value.</quote> |
| 5057 |
|
| 5058 |
<footnote><para>In those instances when there is no <link |
| 5059 |
linkend="returnref">return</link> |
| 5060 |
terminating the function.</para></footnote> |
| 5061 |
</para> |
| 5062 |
|
| 5063 |
<para><anchor id="pipeex">Following the execution of a <link |
| 5064 |
linkend="piperef">pipe</link>, a <varname>$?</varname> |
| 5065 |
gives the exit status of the last command executed.</para> |
| 5066 |
|
| 5067 |
|
| 5068 |
<para>After a script terminates, a <varname>$?</varname> from the |
| 5069 |
command-line gives the exit status of the script, that is, the |
| 5070 |
last command executed in the script, which is, by convention, |
| 5071 |
<userinput>0</userinput> on success or an integer in the |
| 5072 |
range <returnvalue>1 - 255</returnvalue> on error.</para> |
| 5073 |
|
| 5074 |
<example id="ex5"> |
| 5075 |
<title>exit / exit status</title> |
| 5076 |
<programlisting>&ex5;</programlisting> |
| 5077 |
</example> |
| 5078 |
|
| 5079 |
<para><link linkend="xstatvarref">$?</link> is especially useful |
| 5080 |
for testing the result of a command in a script (see <xref |
| 5081 |
linkend="filecomp"> and <xref linkend="lookup">).</para> |
| 5082 |
|
| 5083 |
<note> |
| 5084 |
<para>The <link linkend="notref">!</link>, the <firstterm>logical |
| 5085 |
not</firstterm> qualifier, reverses the outcome of a test or |
| 5086 |
command, and this affects its <link linkend="exitstatusref">exit |
| 5087 |
status</link>. |
| 5088 |
|
| 5089 |
<example id="negcond"> |
| 5090 |
<title>Negating a condition using <token>!</token></title> |
| 5091 |
<programlisting>true # The "true" builtin. |
| 5092 |
echo "exit status of \"true\" = $?" # 0 |
| 5093 |
|
| 5094 |
! true |
| 5095 |
echo "exit status of \"! true\" = $?" # 1 |
| 5096 |
# Note that the "!" needs a space between it and the command. |
| 5097 |
# !true leads to a "command not found" error |
| 5098 |
# |
| 5099 |
# The '!' operator prefixing a command invokes the Bash history mechanism. |
| 5100 |
|
| 5101 |
true |
| 5102 |
!true |
| 5103 |
# No error this time, but no negation either. |
| 5104 |
# It just repeats the previous command (true). |
| 5105 |
|
| 5106 |
|
| 5107 |
# =========================================================== # |
| 5108 |
# Preceding a _pipe_ with ! inverts the exit status returned. |
| 5109 |
ls | bogus_command # bash: bogus_command: command not found |
| 5110 |
echo $? # 127 |
| 5111 |
|
| 5112 |
! ls | bogus_command # bash: bogus_command: command not found |
| 5113 |
echo $? # 0 |
| 5114 |
# Note that the ! does not change the execution of the pipe. |
| 5115 |
# Only the exit status changes. |
| 5116 |
# =========================================================== # |
| 5117 |
|
| 5118 |
# Thanks, Stéphane Chazelas and Kristopher Newsome.</programlisting> |
| 5119 |
</example> |
| 5120 |
|
| 5121 |
</para> |
| 5122 |
</note> |
| 5123 |
|
| 5124 |
|
| 5125 |
<caution><para>Certain exit status codes have <link |
| 5126 |
linkend="exitcodesref">reserved meanings</link> and should not |
| 5127 |
be user-specified in a script. </para></caution> |
| 5128 |
|
| 5129 |
|
| 5130 |
</chapter> <!-- Exit and Exit status --> |
| 5131 |
|
| 5132 |
|
| 5133 |
<chapter id="tests"> |
| 5134 |
<title>Tests</title> |
| 5135 |
|
| 5136 |
<para><anchor id="ifthen"></para> |
| 5137 |
|
| 5138 |
<indexterm> |
| 5139 |
<primary>if</primary> |
| 5140 |
</indexterm> |
| 5141 |
<indexterm> |
| 5142 |
<primary>test</primary> |
| 5143 |
<secondary>if</secondary> |
| 5144 |
</indexterm> |
| 5145 |
<indexterm> |
| 5146 |
<primary>then</primary> |
| 5147 |
</indexterm> |
| 5148 |
<indexterm> |
| 5149 |
<primary>else</primary> |
| 5150 |
</indexterm> |
| 5151 |
<indexterm> |
| 5152 |
<primary>else if</primary> |
| 5153 |
</indexterm> |
| 5154 |
<indexterm> |
| 5155 |
<primary>elif</primary> |
| 5156 |
</indexterm> |
| 5157 |
|
| 5158 |
<para>Every reasonably complete programming language can test |
| 5159 |
for a condition, then act according to the result of the |
| 5160 |
test. Bash has the <link linkend="ttestref">test</link> |
| 5161 |
command, various <link linkend="dblbrackets">bracket</link> |
| 5162 |
and <link linkend="dblparenstst">parenthesis</link> operators, |
| 5163 |
and the <command>if/then</command> construct.</para> |
| 5164 |
|
| 5165 |
<sect1 id="testconstructs"> |
| 5166 |
<title>Test Constructs</title> |
| 5167 |
|
| 5168 |
<para><anchor id="testconstructs1"></para> |
| 5169 |
|
| 5170 |
<itemizedlist id="testingref"> |
| 5171 |
|
| 5172 |
<listitem> |
| 5173 |
<para>An <command>if/then</command> construct tests whether the |
| 5174 |
<link linkend="exitstatusref">exit status</link> of a list |
| 5175 |
of commands is <returnvalue>0</returnvalue> (since 0 means |
| 5176 |
<quote>success</quote> by UNIX convention), and if so, executes |
| 5177 |
one or more commands.</para> |
| 5178 |
</listitem> |
| 5179 |
|
| 5180 |
<listitem> |
| 5181 |
<para>There exists a dedicated command called <command> |
| 5182 |
[</command> (<link linkend="leftbracket">left bracket</link> |
| 5183 |
special character). It is a synonym for <command>test</command>, |
| 5184 |
and a <link linkend="builtinref">builtin</link> for efficiency |
| 5185 |
reasons. This command considers its arguments as comparison |
| 5186 |
expressions or file tests and returns an exit status corresponding |
| 5187 |
to the result of the comparison (0 for true, 1 for false).</para> |
| 5188 |
</listitem> |
| 5189 |
|
| 5190 |
<listitem> |
| 5191 |
<para>With version 2.02, Bash introduced the <link |
| 5192 |
linkend="dblbrackets">[[ ... ]]</link> <firstterm>extended |
| 5193 |
test command</firstterm>, which performs comparisons |
| 5194 |
in a manner more familiar to programmers from other |
| 5195 |
languages. Note that <command>[[</command> is a <link |
| 5196 |
linkend="keywordref">keyword</link>, not a command.</para> |
| 5197 |
|
| 5198 |
<para>Bash sees <userinput>[[ $a -lt $b ]]</userinput> as a |
| 5199 |
single element, which returns an exit status.</para> |
| 5200 |
</listitem> |
| 5201 |
|
| 5202 |
<listitem> |
| 5203 |
<para><anchor id="dblparenstst"></para> |
| 5204 |
<para>The <link linkend="dblparens">(( ... ))</link> and <link |
| 5205 |
linkend="letref">let ...</link> constructs return an |
| 5206 |
<link linkend="exitstatusref">exit status</link>, |
| 5207 |
<emphasis>according to whether the arithmetic expressions they |
| 5208 |
evaluate expand to a non-zero value</emphasis>. These |
| 5209 |
<link linkend="arithexpref">arithmetic-expansion</link> |
| 5210 |
constructs may therefore be used to perform <link |
| 5211 |
linkend="icomparison1">arithmetic comparisons</link>.</para> |
| 5212 |
|
| 5213 |
<para> |
| 5214 |
<programlisting>(( 0 && 1 )) # Logical AND |
| 5215 |
echo $? # 1 *** |
| 5216 |
# And so ... |
| 5217 |
let "num = (( 0 && 1 ))" |
| 5218 |
echo $num # 0 |
| 5219 |
# But ... |
| 5220 |
let "num = (( 0 && 1 ))" |
| 5221 |
echo $? # 1 *** |
| 5222 |
|
| 5223 |
|
| 5224 |
(( 200 || 11 )) # Logical OR |
| 5225 |
echo $? # 0 *** |
| 5226 |
# ... |
| 5227 |
let "num = (( 200 || 11 ))" |
| 5228 |
echo $num # 1 |
| 5229 |
let "num = (( 200 || 11 ))" |
| 5230 |
echo $? # 0 *** |
| 5231 |
|
| 5232 |
|
| 5233 |
(( 200 | 11 )) # Bitwise OR |
| 5234 |
echo $? # 0 *** |
| 5235 |
# ... |
| 5236 |
let "num = (( 200 | 11 ))" |
| 5237 |
echo $num # 203 |
| 5238 |
let "num = (( 200 | 11 ))" |
| 5239 |
echo $? # 0 *** |
| 5240 |
|
| 5241 |
# The "let" construct returns the same exit status |
| 5242 |
#+ as the double-parentheses arithmetic expansion.</programlisting> |
| 5243 |
</para> |
| 5244 |
|
| 5245 |
<caution><para><anchor id="arxs">Again, note that the |
| 5246 |
<firstterm>exit status</firstterm> of an arithmetic expression |
| 5247 |
is <emphasis>not</emphasis> an error value. |
| 5248 |
<programlisting>var=-2 && (( var+=2 )) |
| 5249 |
echo $? # 1 |
| 5250 |
|
| 5251 |
var=-2 && (( var+=2 )) && echo $var |
| 5252 |
# Will not echo $var!</programlisting> |
| 5253 |
</para></caution> |
| 5254 |
|
| 5255 |
</listitem> |
| 5256 |
|
| 5257 |
<listitem> |
| 5258 |
<para><anchor id="ifgrepref"></para> |
| 5259 |
|
| 5260 |
<para>An <command>if</command> can test any command, not just |
| 5261 |
conditions enclosed within brackets.</para> |
| 5262 |
|
| 5263 |
|
| 5264 |
<para><programlisting>if cmp a b &> /dev/null # Suppress output. |
| 5265 |
then echo "Files a and b are identical." |
| 5266 |
else echo "Files a and b differ." |
| 5267 |
fi |
| 5268 |
|
| 5269 |
# The very useful "if-grep" construct: |
| 5270 |
# ----------------------------------- |
| 5271 |
if grep -q Bash file |
| 5272 |
then echo "File contains at least one occurrence of Bash." |
| 5273 |
fi |
| 5274 |
|
| 5275 |
word=Linux |
| 5276 |
letter_sequence=inu |
| 5277 |
if echo "$word" | grep -q "$letter_sequence" |
| 5278 |
# The "-q" option to grep suppresses output. |
| 5279 |
then |
| 5280 |
echo "$letter_sequence found in $word" |
| 5281 |
else |
| 5282 |
echo "$letter_sequence not found in $word" |
| 5283 |
fi |
| 5284 |
|
| 5285 |
|
| 5286 |
if COMMAND_WHOSE_EXIT_STATUS_IS_0_UNLESS_ERROR_OCCURRED |
| 5287 |
then echo "Command succeeded." |
| 5288 |
else echo "Command failed." |
| 5289 |
fi</programlisting> |
| 5290 |
</para> |
| 5291 |
</listitem> |
| 5292 |
|
| 5293 |
<listitem> |
| 5294 |
|
| 5295 |
|
| 5296 |
<para><emphasis>These last two examples |
| 5297 |
courtesy of Stéphane Chazelas.</emphasis></para> |
| 5298 |
|
| 5299 |
</listitem> |
| 5300 |
|
| 5301 |
|
| 5302 |
</itemizedlist> |
| 5303 |
|
| 5304 |
|
| 5305 |
|
| 5306 |
<example id="ex10"> |
| 5307 |
<title>What is truth?</title> |
| 5308 |
<programlisting>&ex10;</programlisting> |
| 5309 |
</example> |
| 5310 |
|
| 5311 |
<formalpara><title>Exercise</title> |
| 5312 |
<para>Explain the behavior of <xref linkend="ex10">, above.</para> |
| 5313 |
</formalpara> |
| 5314 |
|
| 5315 |
<para><anchor id="elseref"><programlisting>if [ condition-true ] |
| 5316 |
then |
| 5317 |
command 1 |
| 5318 |
command 2 |
| 5319 |
... |
| 5320 |
else # Or else ... |
| 5321 |
# Adds default code block executing if original condition tests false. |
| 5322 |
command 3 |
| 5323 |
command 4 |
| 5324 |
... |
| 5325 |
fi</programlisting> |
| 5326 |
</para> |
| 5327 |
|
| 5328 |
<note> |
| 5329 |
<para>When <firstterm>if</firstterm> and <firstterm>then</firstterm> |
| 5330 |
are on same line in a condition test, a semicolon must |
| 5331 |
terminate the <firstterm>if</firstterm> statement. Both |
| 5332 |
<firstterm>if</firstterm> and <firstterm>then</firstterm> |
| 5333 |
are <link linkend="keywordref">keywords</link>. Keywords (or |
| 5334 |
commands) begin statements, and before a new statement on the |
| 5335 |
same line begins, the old one must terminate.</para> |
| 5336 |
|
| 5337 |
<para><programlisting>if [ -x "$filename" ]; then</programlisting></para> |
| 5338 |
</note> |
| 5339 |
|
| 5340 |
<variablelist id="elifref"> |
| 5341 |
<title><anchor id="elifref1">Else if and elif</title> |
| 5342 |
<varlistentry> |
| 5343 |
<term><token>elif</token></term> |
| 5344 |
<listitem> |
| 5345 |
<para><userinput>elif</userinput> is a contraction |
| 5346 |
for <firstterm>else if</firstterm>. The effect is to nest an |
| 5347 |
inner <token>if/then</token> construct within an outer |
| 5348 |
one.</para> |
| 5349 |
|
| 5350 |
<para><programlisting>if [ condition1 ] |
| 5351 |
then |
| 5352 |
command1 |
| 5353 |
command2 |
| 5354 |
command3 |
| 5355 |
elif [ condition2 ] |
| 5356 |
# Same as else if |
| 5357 |
then |
| 5358 |
command4 |
| 5359 |
command5 |
| 5360 |
else |
| 5361 |
default-command |
| 5362 |
fi</programlisting> |
| 5363 |
</para> |
| 5364 |
</listitem> |
| 5365 |
</varlistentry> |
| 5366 |
</variablelist> |
| 5367 |
|
| 5368 |
<para> |
| 5369 |
<indexterm> |
| 5370 |
<primary>test</primary> |
| 5371 |
</indexterm> |
| 5372 |
<indexterm> |
| 5373 |
<primary>test</primary> |
| 5374 |
<secondary>test</secondary> |
| 5375 |
</indexterm> |
| 5376 |
<indexterm> |
| 5377 |
<primary>[</primary> |
| 5378 |
</indexterm> |
| 5379 |
<indexterm> |
| 5380 |
<primary>special character</primary> |
| 5381 |
<secondary>[</secondary> |
| 5382 |
</indexterm> |
| 5383 |
<indexterm> |
| 5384 |
<primary>]</primary> |
| 5385 |
</indexterm> |
| 5386 |
<indexterm> |
| 5387 |
<primary>special character</primary> |
| 5388 |
<secondary>]</secondary> |
| 5389 |
</indexterm> |
| 5390 |
<anchor id="ifref2"> |
| 5391 |
The <userinput>if test condition-true</userinput> construct is the |
| 5392 |
exact equivalent of <userinput>if [ condition-true ]</userinput>. |
| 5393 |
As it happens, the left bracket, <command>[</command> , is a |
| 5394 |
<firstterm>token</firstterm> |
| 5395 |
|
| 5396 |
<footnote><para><anchor id="tokenref">A |
| 5397 |
<firstterm>token</firstterm> is a symbol or short |
| 5398 |
string with a special meaning attached to it (a <link |
| 5399 |
linkend="metameaningref">meta-meaning</link>). In Bash, |
| 5400 |
certain tokens, such as <command>[</command> and <link |
| 5401 |
linkend="dotref">. (dot-command)</link>, may expand to |
| 5402 |
<firstterm>keywords</firstterm> and commands.</para></footnote> |
| 5403 |
|
| 5404 |
which invokes the <command>test</command> command. The closing |
| 5405 |
right bracket, <command>]</command> , in an if/test should not |
| 5406 |
therefore be strictly necessary, however newer versions of Bash |
| 5407 |
require it.</para> |
| 5408 |
|
| 5409 |
<para><anchor id="ttestref"></para> |
| 5410 |
<note><para>The <command>test</command> command is a Bash <link |
| 5411 |
linkend="builtinref">builtin</link> which tests file |
| 5412 |
types and compares strings. Therefore, in a Bash script, |
| 5413 |
<command>test</command> does <emphasis>not</emphasis> call |
| 5414 |
the external <filename>/usr/bin/test</filename> binary, |
| 5415 |
which is part of the <firstterm>sh-utils</firstterm> |
| 5416 |
package. Likewise, <command>[</command> does not call |
| 5417 |
<filename>/usr/bin/[</filename>, which is linked to |
| 5418 |
<filename>/usr/bin/test</filename>.</para> |
| 5419 |
|
| 5420 |
<para> |
| 5421 |
<screen> |
| 5422 |
<prompt>bash$ </prompt><userinput>type test</userinput> |
| 5423 |
<computeroutput>test is a shell builtin</computeroutput> |
| 5424 |
<prompt>bash$ </prompt><userinput>type '['</userinput> |
| 5425 |
<computeroutput>[ is a shell builtin</computeroutput> |
| 5426 |
<prompt>bash$ </prompt><userinput>type '[['</userinput> |
| 5427 |
<computeroutput>[[ is a shell keyword</computeroutput> |
| 5428 |
<prompt>bash$ </prompt><userinput>type ']]'</userinput> |
| 5429 |
<computeroutput>]] is a shell keyword</computeroutput> |
| 5430 |
<prompt>bash$ </prompt><userinput>type ']'</userinput> |
| 5431 |
<computeroutput>bash: type: ]: not found</computeroutput> |
| 5432 |
</screen> |
| 5433 |
</para> |
| 5434 |
|
| 5435 |
<para><anchor id="usrbintest"></para> |
| 5436 |
<para>If, for some reason, you wish to use |
| 5437 |
<filename>/usr/bin/test</filename> in a Bash script, |
| 5438 |
then specify it by full pathname.</para> |
| 5439 |
|
| 5440 |
</note> |
| 5441 |
|
| 5442 |
|
| 5443 |
<example id="ex11"> |
| 5444 |
<title>Equivalence of <firstterm>test</firstterm>, |
| 5445 |
<filename>/usr/bin/test</filename>, <token>[ ]</token>, |
| 5446 |
and <filename>/usr/bin/[</filename></title> |
| 5447 |
<programlisting>&ex11;</programlisting> |
| 5448 |
</example> |
| 5449 |
|
| 5450 |
|
| 5451 |
<indexterm> |
| 5452 |
<primary>test</primary> |
| 5453 |
</indexterm> |
| 5454 |
<indexterm> |
| 5455 |
<primary>test</primary> |
| 5456 |
<secondary>test</secondary> |
| 5457 |
</indexterm> |
| 5458 |
<indexterm> |
| 5459 |
<primary>[[</primary> |
| 5460 |
</indexterm> |
| 5461 |
<indexterm> |
| 5462 |
<primary>special character</primary> |
| 5463 |
<secondary>[[</secondary> |
| 5464 |
</indexterm> |
| 5465 |
<indexterm> |
| 5466 |
<primary>]]</primary> |
| 5467 |
</indexterm> |
| 5468 |
<indexterm> |
| 5469 |
<primary>special character</primary> |
| 5470 |
<secondary>]]</secondary> |
| 5471 |
</indexterm> |
| 5472 |
|
| 5473 |
|
| 5474 |
<sidebar> |
| 5475 |
|
| 5476 |
<para><anchor id="dblbrackets">The <token>[[ ]]</token> construct |
| 5477 |
is the more versatile Bash version of <token>[ ]</token>. This |
| 5478 |
is the <firstterm>extended test command</firstterm>, adopted from |
| 5479 |
<firstterm>ksh88</firstterm>.</para> |
| 5480 |
|
| 5481 |
<para>* * *</para> |
| 5482 |
|
| 5483 |
<para>No filename expansion or word splitting takes place |
| 5484 |
between <token>[[</token> and <token>]]</token>, but there is |
| 5485 |
parameter expansion and command substitution. |
| 5486 |
|
| 5487 |
<programlisting>file=/etc/passwd |
| 5488 |
|
| 5489 |
if [[ -e $file ]] |
| 5490 |
then |
| 5491 |
echo "Password file exists." |
| 5492 |
fi</programlisting> |
| 5493 |
</para> |
| 5494 |
|
| 5495 |
|
| 5496 |
<para>Using the <command>[[ ... ]]</command> test construct, |
| 5497 |
rather than <command>[ ... ]</command> can prevent many |
| 5498 |
logic errors in scripts. For example, the <token>&&</token>, |
| 5499 |
<token>||</token>, <token><</token>, and <token>></token> |
| 5500 |
operators work within a <token>[[ ]]</token> test, despite |
| 5501 |
giving an error within a <token>[ ]</token> construct.</para> |
| 5502 |
|
| 5503 |
|
| 5504 |
<para><anchor id="dblbraev"></para> |
| 5505 |
<para><firstterm>Arithmetic evaluation</firstterm> of octal / |
| 5506 |
hexadecimal constants takes place automatically within a |
| 5507 |
<token>[[ ... ]]</token> construct. |
| 5508 |
<programlisting># [[ Octal and hexadecimal evaluation ]] |
| 5509 |
# Thank you, Moritz Gronbach, for pointing this out. |
| 5510 |
|
| 5511 |
|
| 5512 |
decimal=15 |
| 5513 |
octal=017 # = 15 (decimal) |
| 5514 |
hex=0x0f # = 15 (decimal) |
| 5515 |
|
| 5516 |
if [ "$decimal" -eq "$octal" ] |
| 5517 |
then |
| 5518 |
echo "$decimal equals $octal" |
| 5519 |
else |
| 5520 |
echo "$decimal is not equal to $octal" # 15 is not equal to 017 |
| 5521 |
fi # Doesn't evaluate within [ single brackets ]! |
| 5522 |
|
| 5523 |
|
| 5524 |
if [[ "$decimal" -eq "$octal" ]] |
| 5525 |
then |
| 5526 |
echo "$decimal equals $octal" # 15 equals 017 |
| 5527 |
else |
| 5528 |
echo "$decimal is not equal to $octal" |
| 5529 |
fi # Evaluates within [[ double brackets ]]! |
| 5530 |
|
| 5531 |
if [[ "$decimal" -eq "$hex" ]] |
| 5532 |
then |
| 5533 |
echo "$decimal equals $hex" # 15 equals 0x0f |
| 5534 |
else |
| 5535 |
echo "$decimal is not equal to $hex" |
| 5536 |
fi # [[ $hexadecimal ]] also evaluates!</programlisting> |
| 5537 |
</para> |
| 5538 |
|
| 5539 |
</sidebar> |
| 5540 |
|
| 5541 |
|
| 5542 |
<note> |
| 5543 |
<para>Following an <command>if</command>, neither the |
| 5544 |
<command>test</command> command nor the test brackets ( [ ] or [[ ]] ) |
| 5545 |
are strictly necessary. |
| 5546 |
|
| 5547 |
<programlisting>dir=/home/bozo |
| 5548 |
|
| 5549 |
if cd "$dir" 2>/dev/null; then # "2>/dev/null" hides error message. |
| 5550 |
echo "Now in $dir." |
| 5551 |
else |
| 5552 |
echo "Can't change to $dir." |
| 5553 |
fi</programlisting> |
| 5554 |
|
| 5555 |
The "if COMMAND" construct returns the exit status of COMMAND. |
| 5556 |
</para> |
| 5557 |
|
| 5558 |
|
| 5559 |
<para>Similarly, a condition within test brackets may stand alone |
| 5560 |
without an <command>if</command>, when used in combination |
| 5561 |
with a <link linkend="listconsref">list construct</link>. |
| 5562 |
|
| 5563 |
<programlisting>var1=20 |
| 5564 |
var2=22 |
| 5565 |
[ "$var1" -ne "$var2" ] && echo "$var1 is not equal to $var2" |
| 5566 |
|
| 5567 |
home=/home/bozo |
| 5568 |
[ -d "$home" ] || echo "$home directory does not exist."</programlisting></para> |
| 5569 |
</note> |
| 5570 |
|
| 5571 |
|
| 5572 |
<indexterm> |
| 5573 |
<primary>test</primary> |
| 5574 |
</indexterm> |
| 5575 |
<indexterm> |
| 5576 |
<primary>test</primary> |
| 5577 |
<secondary>test</secondary> |
| 5578 |
</indexterm> |
| 5579 |
<indexterm> |
| 5580 |
<primary>((</primary> |
| 5581 |
</indexterm> |
| 5582 |
<indexterm> |
| 5583 |
<primary>special character</primary> |
| 5584 |
<secondary>))</secondary> |
| 5585 |
</indexterm> |
| 5586 |
<indexterm> |
| 5587 |
<primary>((</primary> |
| 5588 |
</indexterm> |
| 5589 |
<indexterm> |
| 5590 |
<primary>special character</primary> |
| 5591 |
<secondary>))</secondary> |
| 5592 |
</indexterm> |
| 5593 |
|
| 5594 |
<para><anchor id="dblprx">The <link linkend="dblparens">(( )) |
| 5595 |
construct</link> expands and evaluates an arithmetic |
| 5596 |
expression. If the expression evaluates as zero, it returns |
| 5597 |
an <link linkend="exitstatusref">exit status</link> of |
| 5598 |
<returnvalue>1</returnvalue>, or <quote>false</quote>. A non-zero |
| 5599 |
expression returns an exit status of <returnvalue>0</returnvalue>, |
| 5600 |
or <quote>true</quote>. This is in marked contrast to using |
| 5601 |
the <command>test</command> and <token>[ ]</token> constructs |
| 5602 |
previously discussed.</para> |
| 5603 |
|
| 5604 |
<example id="arithtests"> |
| 5605 |
<title>Arithmetic Tests using <token>(( ))</token></title> |
| 5606 |
<programlisting>&arithtests;</programlisting> |
| 5607 |
</example> |
| 5608 |
|
| 5609 |
</sect1> <!-- Test Constructs --> |
| 5610 |
|
| 5611 |
|
| 5612 |
<sect1 id="fto"> |
| 5613 |
<title>File test operators</title> |
| 5614 |
|
| 5615 |
<variablelist> |
| 5616 |
<title><anchor id="rtif">Returns true if...</title> |
| 5617 |
<varlistentry> |
| 5618 |
<term><token>-e</token></term> |
| 5619 |
<listitem><para>file exists</para></listitem> |
| 5620 |
</varlistentry> |
| 5621 |
<varlistentry> |
| 5622 |
<term><token>-a</token></term> |
| 5623 |
<listitem><para>file exists</para> |
| 5624 |
<para>This is identical in effect to <token>-e</token>. |
| 5625 |
It has been <quote>deprecated,</quote> |
| 5626 |
|
| 5627 |
<footnote><para> |
| 5628 |
Per the 1913 edition of <emphasis>Webster's |
| 5629 |
Dictionary</emphasis>: |
| 5630 |
<programlisting>Deprecate |
| 5631 |
... |
| 5632 |
|
| 5633 |
To pray against, as an evil; |
| 5634 |
to seek to avert by prayer; |
| 5635 |
to desire the removal of; |
| 5636 |
to seek deliverance from; |
| 5637 |
to express deep regret for; |
| 5638 |
to disapprove of strongly.</programlisting> |
| 5639 |
</para></footnote> |
| 5640 |
|
| 5641 |
and its use is |
| 5642 |
discouraged.</para></listitem> |
| 5643 |
</varlistentry> |
| 5644 |
<varlistentry> |
| 5645 |
<term><anchor id="regularfile"><token>-f</token></term> |
| 5646 |
<listitem><para>file is a <replaceable>regular</replaceable> |
| 5647 |
file (not a directory or <link linkend="devfileref">device |
| 5648 |
file</link>)</para></listitem> |
| 5649 |
</varlistentry> |
| 5650 |
<varlistentry> |
| 5651 |
<term><token>-s</token></term> |
| 5652 |
<listitem><para>file is not zero size</para></listitem> |
| 5653 |
</varlistentry> |
| 5654 |
<varlistentry> |
| 5655 |
<term><token>-d</token></term> |
| 5656 |
<listitem><para>file is a directory</para></listitem> |
| 5657 |
</varlistentry> |
| 5658 |
|
| 5659 |
<varlistentry> |
| 5660 |
<term><token>-b</token></term> |
| 5661 |
<listitem> |
| 5662 |
<para>file is a <link linkend="blockdevref">block |
| 5663 |
device</link></para> |
| 5664 |
<para><anchor id="blockdevtest"></para> |
| 5665 |
</listitem> |
| 5666 |
</varlistentry> |
| 5667 |
|
| 5668 |
<varlistentry> |
| 5669 |
<term><token>-c</token></term> |
| 5670 |
<listitem><para><anchor id="chardevtest">file is a <link |
| 5671 |
linkend="chardevref">character device</link></para> |
| 5672 |
<para><programlisting>device0="/dev/sda2" # / (root directory) |
| 5673 |
if [ -b "$device0" ] |
| 5674 |
then |
| 5675 |
echo "$device0 is a block device." |
| 5676 |
fi |
| 5677 |
|
| 5678 |
# /dev/sda2 is a block device. |
| 5679 |
|
| 5680 |
|
| 5681 |
|
| 5682 |
device1="/dev/ttyS1" # PCMCIA modem card. |
| 5683 |
if [ -c "$device1" ] |
| 5684 |
then |
| 5685 |
echo "$device1 is a character device." |
| 5686 |
fi |
| 5687 |
|
| 5688 |
# /dev/ttyS1 is a character device.</programlisting></para> |
| 5689 |
</listitem> |
| 5690 |
</varlistentry> |
| 5691 |
|
| 5692 |
<varlistentry> |
| 5693 |
<term><token>-p</token></term> |
| 5694 |
<listitem> |
| 5695 |
<para>file is a <link linkend="piperef">pipe</link></para> |
| 5696 |
<para><programlisting>function show_input_type() |
| 5697 |
{ |
| 5698 |
[ -p /dev/fd/0 ] && echo PIPE || echo STDIN |
| 5699 |
} |
| 5700 |
|
| 5701 |
show_input_type "Input" # STDIN |
| 5702 |
echo "Input" | show_input_type # PIPE |
| 5703 |
|
| 5704 |
# This example courtesy of Carl Anderson.</programlisting></para> |
| 5705 |
</listitem> |
| 5706 |
</varlistentry> |
| 5707 |
|
| 5708 |
<varlistentry> |
| 5709 |
<term><token>-h</token></term> |
| 5710 |
<listitem><para>file is a <link linkend="symlinkref">symbolic |
| 5711 |
link</link></para></listitem> |
| 5712 |
</varlistentry> |
| 5713 |
<varlistentry> |
| 5714 |
<term><token>-L</token></term> |
| 5715 |
<listitem><para>file is a symbolic link</para></listitem> |
| 5716 |
</varlistentry> |
| 5717 |
<varlistentry> |
| 5718 |
<term><token>-S</token></term> |
| 5719 |
<listitem><para>file is a <link linkend="socketref">socket</link></para></listitem> |
| 5720 |
</varlistentry> |
| 5721 |
<varlistentry> |
| 5722 |
<term><token>-t</token></term> |
| 5723 |
<listitem> |
| 5724 |
|
| 5725 |
<para><anchor id="termtest">file (<link |
| 5726 |
linkend="fdref">descriptor</link>) is |
| 5727 |
associated with a terminal device</para> |
| 5728 |
|
| 5729 |
<para>This test option <link linkend="ii2test"> may be used |
| 5730 |
to check</link> whether the <filename>stdin</filename> |
| 5731 |
<userinput>[ -t 0 ]</userinput> or |
| 5732 |
<filename>stdout</filename> <userinput>[ -t 1 ]</userinput> |
| 5733 |
in a given script is a terminal.</para> |
| 5734 |
|
| 5735 |
</listitem> |
| 5736 |
</varlistentry> |
| 5737 |
<varlistentry> |
| 5738 |
<term><token>-r</token></term> |
| 5739 |
<listitem><para>file has read permission (<emphasis>for the |
| 5740 |
user running the test</emphasis>)</para></listitem> |
| 5741 |
</varlistentry> |
| 5742 |
<varlistentry> |
| 5743 |
<term><token>-w</token></term> |
| 5744 |
<listitem><para>file has write permission (for the user running |
| 5745 |
the test)</para></listitem> |
| 5746 |
</varlistentry> |
| 5747 |
<varlistentry> |
| 5748 |
<term><token>-x</token></term> |
| 5749 |
<listitem><para>file has execute permission (for the user running |
| 5750 |
the test)</para></listitem> |
| 5751 |
</varlistentry> |
| 5752 |
|
| 5753 |
<varlistentry> |
| 5754 |
<term><token>-g</token></term> |
| 5755 |
<listitem> |
| 5756 |
<para>set-group-id (sgid) flag set on file or directory</para> |
| 5757 |
<para>If a directory has the <replaceable>sgid</replaceable> |
| 5758 |
flag set, then a file created within that directory belongs |
| 5759 |
to the group that owns the directory, not necessarily to |
| 5760 |
the group of the user who created the file. This may be |
| 5761 |
useful for a directory shared by a workgroup.</para> |
| 5762 |
</listitem> |
| 5763 |
</varlistentry> |
| 5764 |
|
| 5765 |
<varlistentry> |
| 5766 |
<term><token>-u</token></term> |
| 5767 |
<listitem> |
| 5768 |
<para><anchor id="suidref"></para> |
| 5769 |
<para>set-user-id (suid) flag set on file</para> |
| 5770 |
<para>A binary owned by <firstterm>root</firstterm> |
| 5771 |
with <replaceable>set-user-id</replaceable> flag set |
| 5772 |
runs with <firstterm>root</firstterm> privileges, even |
| 5773 |
when an ordinary user invokes it. |
| 5774 |
|
| 5775 |
<footnote><para>Be aware that <firstterm>suid</firstterm> |
| 5776 |
binaries may open security holes. The |
| 5777 |
<firstterm>suid</firstterm> flag has no effect on |
| 5778 |
shell scripts.</para></footnote> |
| 5779 |
|
| 5780 |
This is useful for executables (such as |
| 5781 |
<command>pppd</command> and <command>cdrecord</command>) |
| 5782 |
that need to access system hardware. Lacking the |
| 5783 |
<firstterm>suid</firstterm> flag, these binaries could not |
| 5784 |
be invoked by a <firstterm>non-root</firstterm> user.</para> |
| 5785 |
|
| 5786 |
<para> |
| 5787 |
<screen> |
| 5788 |
<computeroutput>-rwsr-xr-t 1 root 178236 Oct 2 2000 /usr/sbin/pppd</computeroutput> |
| 5789 |
</screen> |
| 5790 |
</para> |
| 5791 |
|
| 5792 |
<para>A file with the <replaceable>suid</replaceable> |
| 5793 |
flag set shows an <firstterm>s</firstterm> in its |
| 5794 |
permissions.</para> |
| 5795 |
|
| 5796 |
</listitem> |
| 5797 |
</varlistentry> |
| 5798 |
|
| 5799 |
<varlistentry> |
| 5800 |
<term><token>-k</token></term> |
| 5801 |
<listitem> |
| 5802 |
|
| 5803 |
<para><replaceable>sticky bit</replaceable> set</para> |
| 5804 |
|
| 5805 |
<para>Commonly known as the <firstterm>sticky bit,</firstterm> |
| 5806 |
the <firstterm>save-text-mode</firstterm> flag is a special |
| 5807 |
type of file permission. If a file has this flag set, |
| 5808 |
that file will be kept in cache memory, for quicker access. |
| 5809 |
<footnote><para>On Linux systems, the sticky |
| 5810 |
bit is no longer used for files, only on |
| 5811 |
directories.</para></footnote> |
| 5812 |
If set on a directory, it restricts write permission. |
| 5813 |
Setting the sticky bit adds a <firstterm>t</firstterm> |
| 5814 |
to the permissions on the file or directory listing. |
| 5815 |
This restricts altering or deleting specific files |
| 5816 |
in that directory to the owner of those files.</para> |
| 5817 |
|
| 5818 |
<para> |
| 5819 |
<screen> |
| 5820 |
<computeroutput>drwxrwxrwt 7 root 1024 May 19 21:26 tmp/</computeroutput> |
| 5821 |
</screen> |
| 5822 |
</para> |
| 5823 |
|
| 5824 |
<para>If a user does not own a directory that has the sticky |
| 5825 |
bit set, but has write permission in that directory, she |
| 5826 |
can only delete those files that she owns in it. This |
| 5827 |
keeps users from inadvertently overwriting or deleting |
| 5828 |
each other's files in a publicly accessible directory, |
| 5829 |
such as <filename class="directory">/tmp</filename>. |
| 5830 |
(The <firstterm>owner</firstterm> of the directory or |
| 5831 |
<firstterm>root</firstterm> can, of course, delete or |
| 5832 |
rename files there.)</para> |
| 5833 |
|
| 5834 |
</listitem> |
| 5835 |
</varlistentry> |
| 5836 |
<varlistentry> |
| 5837 |
<term><token>-O</token></term> |
| 5838 |
<listitem><para>you are owner of file</para></listitem> |
| 5839 |
</varlistentry> |
| 5840 |
<varlistentry> |
| 5841 |
<term><token>-G</token></term> |
| 5842 |
<listitem><para>group-id of file same as yours</para></listitem> |
| 5843 |
</varlistentry> |
| 5844 |
<varlistentry> |
| 5845 |
<term><token>-N</token></term> |
| 5846 |
<listitem><para>file modified since it was last read</para></listitem> |
| 5847 |
</varlistentry> |
| 5848 |
<varlistentry> |
| 5849 |
<term><token>f1 -nt f2</token></term> |
| 5850 |
<listitem><para>file <replaceable>f1</replaceable> is newer than |
| 5851 |
<replaceable>f2</replaceable></para></listitem> |
| 5852 |
</varlistentry> |
| 5853 |
<varlistentry> |
| 5854 |
<term><token>f1 -ot f2</token></term> |
| 5855 |
<listitem><para>file <replaceable>f1</replaceable> is older than |
| 5856 |
<replaceable>f2</replaceable></para></listitem> |
| 5857 |
</varlistentry> |
| 5858 |
<varlistentry> |
| 5859 |
<term><token>f1 -ef f2</token></term> |
| 5860 |
<listitem><para>files <replaceable>f1</replaceable> and |
| 5861 |
<replaceable>f2</replaceable> are hard links to the same |
| 5862 |
file</para></listitem> |
| 5863 |
</varlistentry> |
| 5864 |
<varlistentry> |
| 5865 |
<term><token>!</token></term> |
| 5866 |
<listitem><para><quote>not</quote> -- reverses the sense of the |
| 5867 |
tests above (returns true if condition absent).</para></listitem> |
| 5868 |
</varlistentry> |
| 5869 |
</variablelist> |
| 5870 |
|
| 5871 |
<example id="brokenlink"> |
| 5872 |
<title>Testing for broken links</title> |
| 5873 |
<programlisting>&brokenlink;</programlisting> |
| 5874 |
</example> |
| 5875 |
|
| 5876 |
<para><xref linkend="cookies">, <xref linkend="bingrep">, |
| 5877 |
<xref linkend="fileinfo">, <xref linkend="ramdisk">, and <xref |
| 5878 |
linkend="mailformat"> also illustrate uses of the file test |
| 5879 |
operators.</para> |
| 5880 |
|
| 5881 |
|
| 5882 |
</sect1> <!-- File test operators --> |
| 5883 |
|
| 5884 |
<sect1 id="comparison-ops"> |
| 5885 |
<title>Other Comparison Operators</title> |
| 5886 |
|
| 5887 |
<para>A <firstterm>binary</firstterm> comparison operator |
| 5888 |
compares two variables or quantities. <emphasis>Note |
| 5889 |
that integer and string comparison use a different set of |
| 5890 |
operators.</emphasis></para> |
| 5891 |
|
| 5892 |
<variablelist id="icomparison"> |
| 5893 |
<title><anchor id="icomparison1">integer comparison</title> |
| 5894 |
|
| 5895 |
<varlistentry> |
| 5896 |
<term><anchor id="equalref"><token>-eq</token></term> |
| 5897 |
<listitem> |
| 5898 |
<para>is equal to</para> |
| 5899 |
<para><userinput>if [ "$a" -eq "$b" ]</userinput></para> |
| 5900 |
</listitem> |
| 5901 |
</varlistentry> |
| 5902 |
|
| 5903 |
<varlistentry> |
| 5904 |
<term><anchor id="nequalref"><token>-ne</token></term> |
| 5905 |
<listitem> |
| 5906 |
<para>is not equal to</para> |
| 5907 |
<para><userinput>if [ "$a" -ne "$b" ]</userinput></para> |
| 5908 |
</listitem> |
| 5909 |
</varlistentry> |
| 5910 |
|
| 5911 |
<varlistentry> |
| 5912 |
<term><anchor id="gt0ref"><token>-gt</token></term> |
| 5913 |
<listitem> |
| 5914 |
<para>is greater than</para> |
| 5915 |
<para><userinput>if [ "$a" -gt "$b" ]</userinput></para> |
| 5916 |
</listitem> |
| 5917 |
</varlistentry> |
| 5918 |
|
| 5919 |
<varlistentry> |
| 5920 |
<term><anchor id="ge0ref"><token>-ge</token></term> |
| 5921 |
<listitem> |
| 5922 |
<para>is greater than or equal to</para> |
| 5923 |
<para><userinput>if [ "$a" -ge "$b" ]</userinput></para> |
| 5924 |
</listitem> |
| 5925 |
</varlistentry> |
| 5926 |
|
| 5927 |
<varlistentry> |
| 5928 |
<term><anchor id="lt0ref"><token>-lt</token></term> |
| 5929 |
<listitem> |
| 5930 |
<para>is less than</para> |
| 5931 |
<para><userinput>if [ "$a" -lt "$b" ]</userinput></para> |
| 5932 |
</listitem> |
| 5933 |
</varlistentry> |
| 5934 |
|
| 5935 |
<varlistentry> |
| 5936 |
<term><anchor id="le0ref"><token>-le</token></term> |
| 5937 |
<listitem> |
| 5938 |
<para>is less than or equal to</para> |
| 5939 |
<para><userinput>if [ "$a" -le "$b" ]</userinput></para> |
| 5940 |
</listitem> |
| 5941 |
</varlistentry> |
| 5942 |
|
| 5943 |
<varlistentry> |
| 5944 |
<term><anchor id="intlt"><token><</token></term> |
| 5945 |
<listitem> |
| 5946 |
<para>is less than (within <link linkend="dblparens">double |
| 5947 |
parentheses</link>)</para> |
| 5948 |
<para><userinput>(("$a" < "$b"))</userinput></para> |
| 5949 |
</listitem> |
| 5950 |
</varlistentry> |
| 5951 |
|
| 5952 |
<varlistentry> |
| 5953 |
<term><anchor id="lteq"><token><=</token></term> |
| 5954 |
<listitem> |
| 5955 |
<para>is less than or equal to (within double parentheses)</para> |
| 5956 |
<para><userinput>(("$a" <= "$b"))</userinput></para> |
| 5957 |
</listitem> |
| 5958 |
</varlistentry> |
| 5959 |
|
| 5960 |
<varlistentry> |
| 5961 |
<term><anchor id="intgt"><token>></token></term> |
| 5962 |
<listitem> |
| 5963 |
<para>is greater than (within double parentheses)</para> |
| 5964 |
<para><userinput>(("$a" > "$b"))</userinput></para> |
| 5965 |
</listitem> |
| 5966 |
</varlistentry> |
| 5967 |
|
| 5968 |
<varlistentry> |
| 5969 |
<term><anchor id="gteq"><token>>=</token></term> |
| 5970 |
<listitem> |
| 5971 |
<para>is greater than or equal to (within double parentheses)</para> |
| 5972 |
<para><userinput>(("$a" >= "$b"))</userinput></para> |
| 5973 |
</listitem> |
| 5974 |
</varlistentry> |
| 5975 |
|
| 5976 |
</variablelist> |
| 5977 |
|
| 5978 |
<variablelist id="scomparison"> |
| 5979 |
<title><anchor id="scomparison1">string comparison</title> |
| 5980 |
<varlistentry> |
| 5981 |
<term><token>=</token></term> |
| 5982 |
<listitem> |
| 5983 |
<para><anchor id="equalsignref"></para> |
| 5984 |
<para>is equal to</para> |
| 5985 |
<para><userinput>if [ "$a" = "$b" ]</userinput></para> |
| 5986 |
|
| 5987 |
<caution><para>Note the <link |
| 5988 |
linkend="whitespaceref">whitespace</link> |
| 5989 |
framing the <command>=</command>.</para> |
| 5990 |
<para><userinput>if [ "$a"="$b" ]</userinput> is |
| 5991 |
<emphasis>not</emphasis> equivalent to the |
| 5992 |
above.</para></caution> |
| 5993 |
|
| 5994 |
</listitem> |
| 5995 |
</varlistentry> |
| 5996 |
|
| 5997 |
<varlistentry> |
| 5998 |
<term><anchor id="scomparison2"><token>==</token></term> |
| 5999 |
<listitem> |
| 6000 |
<para>is equal to</para> |
| 6001 |
<para><userinput>if [ "$a" == "$b" ]</userinput></para> |
| 6002 |
<para>This is a synonym for <token>=</token>.</para> |
| 6003 |
|
| 6004 |
<note><para> |
| 6005 |
The <token>==</token> comparison operator behaves differently |
| 6006 |
within a <link linkend="dblbrackets">double-brackets</link> |
| 6007 |
test than within single brackets. |
| 6008 |
<programlisting>[[ $a == z* ]] # True if $a starts with an "z" (pattern matching). |
| 6009 |
[[ $a == "z*" ]] # True if $a is equal to z* (literal matching). |
| 6010 |
|
| 6011 |
[ $a == z* ] # File globbing and word splitting take place. |
| 6012 |
[ "$a" == "z*" ] # True if $a is equal to z* (literal matching). |
| 6013 |
|
| 6014 |
# Thanks, Stéphane Chazelas</programlisting> |
| 6015 |
</para></note> |
| 6016 |
|
| 6017 |
</listitem> |
| 6018 |
</varlistentry> |
| 6019 |
<varlistentry> |
| 6020 |
<term><anchor id="notequal"><token>!=</token></term> |
| 6021 |
<listitem> |
| 6022 |
<para>is not equal to</para> |
| 6023 |
<para><userinput>if [ "$a" != "$b" ]</userinput></para> |
| 6024 |
<para>This operator uses pattern matching within a <link |
| 6025 |
linkend="dblbrackets">[[ ... ]]</link> construct.</para> |
| 6026 |
</listitem> |
| 6027 |
</varlistentry> |
| 6028 |
|
| 6029 |
<varlistentry> |
| 6030 |
<term><anchor id="ltref"><token><</token></term> |
| 6031 |
<listitem> |
| 6032 |
<para>is less than, in <link |
| 6033 |
linkend="asciidef">ASCII</link> alphabetical |
| 6034 |
order</para> |
| 6035 |
<para><userinput>if [[ "$a" < "$b" ]]</userinput></para> |
| 6036 |
<para><userinput>if [ "$a" \< "$b" ]</userinput></para> |
| 6037 |
<para>Note that the <quote><</quote> needs to be |
| 6038 |
<link linkend="escp">escaped</link> within a |
| 6039 |
<userinput>[ ]</userinput> construct.</para> |
| 6040 |
</listitem> |
| 6041 |
</varlistentry> |
| 6042 |
|
| 6043 |
<varlistentry> |
| 6044 |
<term><anchor id="gtref"><token>></token></term> |
| 6045 |
<listitem> |
| 6046 |
<para>is greater than, in ASCII alphabetical order</para> |
| 6047 |
<para><userinput>if [[ "$a" > "$b" ]]</userinput></para> |
| 6048 |
<para><userinput>if [ "$a" \> "$b" ]</userinput></para> |
| 6049 |
<para>Note that the <quote>></quote> needs to be |
| 6050 |
escaped within a <userinput>[ ]</userinput> construct.</para> |
| 6051 |
<para>See <xref linkend="bubble"> for an application of this |
| 6052 |
comparison operator.</para> |
| 6053 |
</listitem> |
| 6054 |
</varlistentry> |
| 6055 |
|
| 6056 |
<varlistentry> |
| 6057 |
<term><anchor id="stringnull"><token>-z</token></term> |
| 6058 |
<listitem> |
| 6059 |
<para>string is <firstterm>null</firstterm>, |
| 6060 |
that is, has zero length</para> |
| 6061 |
<para><programlisting> String='' # Zero-length ("null") string variable. |
| 6062 |
|
| 6063 |
if [ -z "$String" ] |
| 6064 |
then |
| 6065 |
echo "\$String is null." |
| 6066 |
else |
| 6067 |
echo "\$String is NOT null." |
| 6068 |
fi # $String is null.</programlisting></para> |
| 6069 |
</listitem> |
| 6070 |
</varlistentry> |
| 6071 |
|
| 6072 |
<varlistentry> |
| 6073 |
<term><anchor id="stringnotnull"><token>-n</token></term> |
| 6074 |
<listitem> |
| 6075 |
<para>string is not <firstterm>null.</firstterm></para> |
| 6076 |
|
| 6077 |
<caution><para>The <userinput>-n</userinput> test |
| 6078 |
requires that the string be quoted within the |
| 6079 |
test brackets. Using an unquoted string with |
| 6080 |
<firstterm>! -z</firstterm>, or even just the |
| 6081 |
unquoted string alone within test brackets (see <xref |
| 6082 |
linkend="strtest">) normally works, however, this is |
| 6083 |
an unsafe practice. <emphasis>Always</emphasis> quote |
| 6084 |
a tested string. |
| 6085 |
<footnote><para>As S.C. points out, in a compound test, |
| 6086 |
even quoting the string variable might not |
| 6087 |
suffice. <userinput>[ -n "$string" -o "$a" = "$b" ]</userinput> |
| 6088 |
may cause an error with some versions of Bash if |
| 6089 |
<varname>$string</varname> is empty. The safe way |
| 6090 |
is to append an extra character to possibly empty variables, |
| 6091 |
<userinput>[ "x$string" != x -o "x$a" = "x$b" ]</userinput> |
| 6092 |
(the <quote>x's</quote> cancel out).</para></footnote> |
| 6093 |
</para></caution> |
| 6094 |
</listitem> |
| 6095 |
</varlistentry> |
| 6096 |
|
| 6097 |
|
| 6098 |
|
| 6099 |
</variablelist> |
| 6100 |
|
| 6101 |
<example id="ex13"> |
| 6102 |
<title>Arithmetic and string comparisons</title> |
| 6103 |
<programlisting>&ex13;</programlisting> |
| 6104 |
</example> |
| 6105 |
|
| 6106 |
<example id="strtest"> |
| 6107 |
<title>Testing whether a string is <firstterm>null</firstterm></title> |
| 6108 |
<programlisting>&strtest;</programlisting> |
| 6109 |
</example> |
| 6110 |
|
| 6111 |
<example id="ex14"> |
| 6112 |
<title><firstterm>zmore</firstterm></title> |
| 6113 |
<programlisting>&ex14;</programlisting> |
| 6114 |
</example> |
| 6115 |
|
| 6116 |
<variablelist id="ccomparison"> |
| 6117 |
<title><anchor id="ccomparison1">compound comparison</title> |
| 6118 |
|
| 6119 |
<varlistentry> |
| 6120 |
<term><anchor id="compoundand"><token>-a</token></term> |
| 6121 |
<listitem> |
| 6122 |
<para>logical and</para> |
| 6123 |
<para><replaceable>exp1 -a exp2</replaceable> returns true if |
| 6124 |
<emphasis>both</emphasis> exp1 and exp2 are true.</para> |
| 6125 |
</listitem> |
| 6126 |
</varlistentry> |
| 6127 |
|
| 6128 |
<varlistentry> |
| 6129 |
<term><anchor id="compoundor"><token>-o</token></term> |
| 6130 |
<listitem> |
| 6131 |
<para>logical or </para> |
| 6132 |
<para><replaceable>exp1 -o exp2</replaceable> returns |
| 6133 |
true if either exp1 <emphasis>or</emphasis> exp2 is |
| 6134 |
true.</para> |
| 6135 |
</listitem> |
| 6136 |
</varlistentry> |
| 6137 |
</variablelist> |
| 6138 |
|
| 6139 |
<para> |
| 6140 |
These are similar to the Bash comparison operators |
| 6141 |
<command>&&</command> and <command>||</command>, used |
| 6142 |
within <link linkend="dblbrackets">double brackets</link>. |
| 6143 |
<programlisting>[[ condition1 && condition2 ]]</programlisting> |
| 6144 |
</para> |
| 6145 |
|
| 6146 |
<para> |
| 6147 |
The <command>-o</command> and <command>-a</command> operators |
| 6148 |
work with the <link linkend="ttestref">test</link> command or |
| 6149 |
occur within single test brackets. |
| 6150 |
<programlisting>if [ "$expr1" -a "$expr2" ] |
| 6151 |
then |
| 6152 |
echo "Both expr1 and expr2 are true." |
| 6153 |
else |
| 6154 |
echo "Either expr1 or expr2 is false." |
| 6155 |
fi</programlisting> |
| 6156 |
</para> |
| 6157 |
|
| 6158 |
<caution> |
| 6159 |
<para>But, as <emphasis>rihad</emphasis> points out: |
| 6160 |
<programlisting>[ 1 -eq 1 ] && [ -n "`echo true 1>&2`" ] # true |
| 6161 |
[ 1 -eq 2 ] && [ -n "`echo true 1>&2`" ] # (no output) |
| 6162 |
# ^^^^^^^ False condition. So far, everything as expected. |
| 6163 |
|
| 6164 |
# However ... |
| 6165 |
[ 1 -eq 2 -a -n "`echo true 1>&2`" ] # true |
| 6166 |
# ^^^^^^^ False condition. So, why "true" output? |
| 6167 |
|
| 6168 |
# Is it because both condition clauses within brackets evaluate? |
| 6169 |
[[ 1 -eq 2 && -n "`echo true 1>&2`" ]] # (no output) |
| 6170 |
# No, that's not it. |
| 6171 |
|
| 6172 |
# Apparently && and || "short-circuit" while -a and -o do not.</programlisting> |
| 6173 |
</para> |
| 6174 |
</caution> |
| 6175 |
|
| 6176 |
<para>Refer to <xref linkend="andor">, <xref linkend="twodim">, |
| 6177 |
and <xref linkend="whx"> to see compound comparison operators |
| 6178 |
in action.</para> |
| 6179 |
|
| 6180 |
</sect1> <!-- Comparison operators (binary) --> |
| 6181 |
|
| 6182 |
<sect1 id="nestedifthen"> |
| 6183 |
<title>Nested <replaceable>if/then</replaceable> Condition Tests</title> |
| 6184 |
|
| 6185 |
<para>Condition tests using the <replaceable>if/then</replaceable> |
| 6186 |
construct may be nested. The net result is equivalent to using the |
| 6187 |
<link linkend="logops1"><firstterm>&&</firstterm></link> compound |
| 6188 |
comparison operator.</para> |
| 6189 |
|
| 6190 |
<para><programlisting>a=3 |
| 6191 |
|
| 6192 |
if [ "$a" -gt 0 ] |
| 6193 |
then |
| 6194 |
if [ "$a" -lt 5 ] |
| 6195 |
then |
| 6196 |
echo "The value of \"a\" lies somewhere between 0 and 5." |
| 6197 |
fi |
| 6198 |
fi |
| 6199 |
|
| 6200 |
# Same result as: |
| 6201 |
|
| 6202 |
if [ "$a" -gt 0 ] && [ "$a" -lt 5 ] |
| 6203 |
then |
| 6204 |
echo "The value of \"a\" lies somewhere between 0 and 5." |
| 6205 |
fi</programlisting></para> |
| 6206 |
|
| 6207 |
<para><xref linkend="cards"> and <xref linkend="backlight"> |
| 6208 |
demonstrate nested <replaceable>if/then</replaceable> condition |
| 6209 |
tests.</para> |
| 6210 |
|
| 6211 |
</sect1> <!-- Nested if/then Tests --> |
| 6212 |
|
| 6213 |
<sect1 id="testtest"> |
| 6214 |
<title>Testing Your Knowledge of Tests</title> |
| 6215 |
|
| 6216 |
<para>The systemwide <filename>xinitrc</filename> file can be used |
| 6217 |
to launch the X server. This file contains quite a number |
| 6218 |
of <firstterm>if/then</firstterm> tests. The following |
| 6219 |
is excerpted from an <quote>ancient</quote> version of |
| 6220 |
<filename>xinitrc</filename> (<firstterm>Red Hat 7.1</firstterm>, |
| 6221 |
or thereabouts).</para> |
| 6222 |
|
| 6223 |
<para><programlisting>if [ -f $HOME/.Xclients ]; then |
| 6224 |
exec $HOME/.Xclients |
| 6225 |
elif [ -f /etc/X11/xinit/Xclients ]; then |
| 6226 |
exec /etc/X11/xinit/Xclients |
| 6227 |
else |
| 6228 |
# failsafe settings. Although we should never get here |
| 6229 |
# (we provide fallbacks in Xclients as well) it can't hurt. |
| 6230 |
xclock -geometry 100x100-5+5 & |
| 6231 |
xterm -geometry 80x50-50+150 & |
| 6232 |
if [ -f /usr/bin/netscape -a -f /usr/share/doc/HTML/index.html ]; then |
| 6233 |
netscape /usr/share/doc/HTML/index.html & |
| 6234 |
fi |
| 6235 |
fi</programlisting></para> |
| 6236 |
|
| 6237 |
<para>Explain the <firstterm>test</firstterm> constructs in the |
| 6238 |
above snippet, then examine an updated version of the |
| 6239 |
file, <filename>/etc/X11/xinit/xinitrc</filename>, and |
| 6240 |
analyze the <firstterm>if/then</firstterm> test constructs |
| 6241 |
there. You may need to refer ahead to the discussions of <link |
| 6242 |
linkend="grepref">grep</link>, <link linkend="sedref">sed</link>, |
| 6243 |
and <link linkend="regexref">regular expressions</link>.</para> |
| 6244 |
|
| 6245 |
|
| 6246 |
</sect1> <!-- Testing Your Knowledge of Tests --> |
| 6247 |
|
| 6248 |
</chapter> <!-- Tests --> |
| 6249 |
|
| 6250 |
|
| 6251 |
|
| 6252 |
<chapter id="operations"> |
| 6253 |
<title>Operations and Related Topics</title> |
| 6254 |
|
| 6255 |
|
| 6256 |
<sect1 id="ops"> |
| 6257 |
<title>Operators</title> |
| 6258 |
|
| 6259 |
<variablelist id="asnop"> |
| 6260 |
<title><anchor id="asnop1">assignment</title> |
| 6261 |
|
| 6262 |
<varlistentry> |
| 6263 |
<term><replaceable>variable assignment</replaceable></term> |
| 6264 |
<listitem><para>Initializing or changing the value of a variable</para> |
| 6265 |
</listitem> |
| 6266 |
</varlistentry> |
| 6267 |
|
| 6268 |
<varlistentry> |
| 6269 |
<indexterm> |
| 6270 |
<primary>=</primary> |
| 6271 |
</indexterm> |
| 6272 |
<indexterm> |
| 6273 |
<primary>operation</primary> |
| 6274 |
<secondary>=</secondary> |
| 6275 |
</indexterm> |
| 6276 |
<term>=</term> |
| 6277 |
<listitem> |
| 6278 |
<para>All-purpose assignment operator, which works for both |
| 6279 |
arithmetic and string assignments.</para> |
| 6280 |
|
| 6281 |
<para> |
| 6282 |
<programlisting>var=27 |
| 6283 |
category=minerals # No spaces allowed after the "=".</programlisting> |
| 6284 |
</para> |
| 6285 |
|
| 6286 |
<caution> |
| 6287 |
<para>Do not confuse the <quote>=</quote> assignment |
| 6288 |
operator with the <link linkend="equalsignref">= test |
| 6289 |
operator</link>.</para> |
| 6290 |
|
| 6291 |
<para> |
| 6292 |
<programlisting># = as a test operator |
| 6293 |
|
| 6294 |
if [ "$string1" = "$string2" ] |
| 6295 |
then |
| 6296 |
command |
| 6297 |
fi |
| 6298 |
|
| 6299 |
# if [ "X$string1" = "X$string2" ] is safer, |
| 6300 |
#+ to prevent an error message should one of the variables be empty. |
| 6301 |
# (The prepended "X" characters cancel out.)</programlisting> |
| 6302 |
</para> |
| 6303 |
</caution> |
| 6304 |
|
| 6305 |
|
| 6306 |
</listitem> |
| 6307 |
</varlistentry> |
| 6308 |
</variablelist> |
| 6309 |
|
| 6310 |
<indexterm> |
| 6311 |
<primary>expr</primary> |
| 6312 |
</indexterm> |
| 6313 |
<indexterm> |
| 6314 |
<primary>command</primary> |
| 6315 |
<secondary>expr</secondary> |
| 6316 |
</indexterm> |
| 6317 |
<indexterm> |
| 6318 |
<primary>let</primary> |
| 6319 |
</indexterm> |
| 6320 |
<indexterm> |
| 6321 |
<primary>command</primary> |
| 6322 |
<secondary>let</secondary> |
| 6323 |
</indexterm> |
| 6324 |
|
| 6325 |
|
| 6326 |
<variablelist id="arops"> |
| 6327 |
<title><anchor id="arops1">arithmetic operators</title> |
| 6328 |
|
| 6329 |
<varlistentry> |
| 6330 |
<term><token>+</token></term> |
| 6331 |
<indexterm> |
| 6332 |
<primary>+</primary> |
| 6333 |
</indexterm> |
| 6334 |
<indexterm> |
| 6335 |
<primary>operation</primary> |
| 6336 |
<secondary>+</secondary> |
| 6337 |
</indexterm> |
| 6338 |
<indexterm> |
| 6339 |
<primary>addition</primary> |
| 6340 |
</indexterm> |
| 6341 |
<indexterm> |
| 6342 |
<primary>plus</primary> |
| 6343 |
</indexterm> |
| 6344 |
<listitem><para>plus</para> |
| 6345 |
</listitem> |
| 6346 |
</varlistentry> |
| 6347 |
|
| 6348 |
<varlistentry> |
| 6349 |
<term><token>-</token></term> |
| 6350 |
<indexterm> |
| 6351 |
<primary>-</primary> |
| 6352 |
</indexterm> |
| 6353 |
<indexterm> |
| 6354 |
<primary>operation</primary> |
| 6355 |
<secondary>-</secondary> |
| 6356 |
</indexterm> |
| 6357 |
<indexterm> |
| 6358 |
<primary>subtraction</primary> |
| 6359 |
</indexterm> |
| 6360 |
<indexterm> |
| 6361 |
<primary>minus</primary> |
| 6362 |
</indexterm> |
| 6363 |
<listitem><para>minus</para> |
| 6364 |
</listitem> |
| 6365 |
</varlistentry> |
| 6366 |
|
| 6367 |
<varlistentry> |
| 6368 |
<term><token>*</token></term> |
| 6369 |
<indexterm> |
| 6370 |
<primary>*</primary> |
| 6371 |
</indexterm> |
| 6372 |
<indexterm> |
| 6373 |
<primary>operation</primary> |
| 6374 |
<secondary>*</secondary> |
| 6375 |
</indexterm> |
| 6376 |
<indexterm> |
| 6377 |
<primary>multiplication</primary> |
| 6378 |
</indexterm> |
| 6379 |
<listitem><para>multiplication</para> |
| 6380 |
</listitem> |
| 6381 |
</varlistentry> |
| 6382 |
|
| 6383 |
<varlistentry> |
| 6384 |
<term><token>/</token></term> |
| 6385 |
<indexterm> |
| 6386 |
<primary>/</primary> |
| 6387 |
</indexterm> |
| 6388 |
<indexterm> |
| 6389 |
<primary>operation</primary> |
| 6390 |
<secondary>/</secondary> |
| 6391 |
</indexterm> |
| 6392 |
<indexterm> |
| 6393 |
<primary>division</primary> |
| 6394 |
</indexterm> |
| 6395 |
<listitem><para>division</para> |
| 6396 |
</listitem> |
| 6397 |
</varlistentry> |
| 6398 |
|
| 6399 |
<varlistentry> |
| 6400 |
<term><anchor id="exponentiationref"><token>**</token></term> |
| 6401 |
<indexterm> |
| 6402 |
<primary>**</primary> |
| 6403 |
</indexterm> |
| 6404 |
<indexterm> |
| 6405 |
<primary>operation</primary> |
| 6406 |
<secondary>**</secondary> |
| 6407 |
</indexterm> |
| 6408 |
<indexterm> |
| 6409 |
<primary>exponentiation</primary> |
| 6410 |
</indexterm> |
| 6411 |
<listitem> |
| 6412 |
<para>exponentiation</para> |
| 6413 |
|
| 6414 |
<para> |
| 6415 |
<programlisting># Bash, version 2.02, introduced the "**" exponentiation operator. |
| 6416 |
|
| 6417 |
let "z=5**3" # 5 * 5 * 5 |
| 6418 |
echo "z = $z" # z = 125</programlisting> |
| 6419 |
</para> |
| 6420 |
|
| 6421 |
</listitem> |
| 6422 |
</varlistentry> |
| 6423 |
|
| 6424 |
<varlistentry> |
| 6425 |
<term><anchor id="moduloref"><token>%</token></term> |
| 6426 |
<indexterm> |
| 6427 |
<primary>%</primary> |
| 6428 |
</indexterm> |
| 6429 |
<indexterm> |
| 6430 |
<primary>operation</primary> |
| 6431 |
<secondary>%</secondary> |
| 6432 |
</indexterm> |
| 6433 |
<indexterm> |
| 6434 |
<primary>modulo</primary> |
| 6435 |
</indexterm> |
| 6436 |
<listitem> |
| 6437 |
|
| 6438 |
<para>modulo, or mod (returns the |
| 6439 |
<firstterm>remainder</firstterm> of an integer division |
| 6440 |
operation)</para> |
| 6441 |
|
| 6442 |
<para> |
| 6443 |
<screen><prompt>bash$ </prompt><userinput>expr 5 % 3</userinput> |
| 6444 |
<computeroutput>2</computeroutput> |
| 6445 |
</screen> |
| 6446 |
<emphasis>5/3 = 1, with remainder 2</emphasis> |
| 6447 |
</para> |
| 6448 |
|
| 6449 |
<para>This operator finds use in, among other things, |
| 6450 |
generating numbers within a specific range (see <xref |
| 6451 |
linkend="ex21"> and <xref linkend="randomtest">) and |
| 6452 |
formatting program output (see <xref linkend="qfunction"> and |
| 6453 |
<xref linkend="collatz">). It can even be used to generate |
| 6454 |
prime numbers, (see <xref linkend="primes">). Modulo turns |
| 6455 |
up surprisingly often in numerical recipes.</para> |
| 6456 |
|
| 6457 |
<example id="gcd"> |
| 6458 |
<title>Greatest common divisor</title> |
| 6459 |
<programlisting>&gcd;</programlisting> |
| 6460 |
</example> |
| 6461 |
|
| 6462 |
</listitem> |
| 6463 |
</varlistentry> |
| 6464 |
|
| 6465 |
<varlistentry> |
| 6466 |
<term><anchor id="arithopscomb"><token>+=</token></term> |
| 6467 |
<indexterm> |
| 6468 |
<primary>+=</primary> |
| 6469 |
</indexterm> |
| 6470 |
<indexterm> |
| 6471 |
<primary>operation</primary> |
| 6472 |
<secondary>+=</secondary> |
| 6473 |
</indexterm> |
| 6474 |
<indexterm> |
| 6475 |
<primary>plus-equal</primary> |
| 6476 |
</indexterm> |
| 6477 |
<listitem> |
| 6478 |
<para><firstterm>plus-equal</firstterm> (increment variable |
| 6479 |
by a constant) |
| 6480 |
|
| 6481 |
<footnote><para>In a different context, <command>+=</command> can |
| 6482 |
serve as a <firstterm>string concatenation</firstterm> |
| 6483 |
operator. This can be useful for <link |
| 6484 |
linkend="pathappend">modifying <firstterm>environmental |
| 6485 |
variables</firstterm></link>.</para></footnote> |
| 6486 |
|
| 6487 |
</para> |
| 6488 |
|
| 6489 |
<para><userinput>let "var += 5"</userinput> results in |
| 6490 |
<parameter>var</parameter> being incremented by |
| 6491 |
<literal>5</literal>.</para> |
| 6492 |
|
| 6493 |
</listitem> |
| 6494 |
</varlistentry> |
| 6495 |
|
| 6496 |
<varlistentry> |
| 6497 |
<term><token>-=</token></term> |
| 6498 |
<indexterm> |
| 6499 |
<primary>-=</primary> |
| 6500 |
</indexterm> |
| 6501 |
<indexterm> |
| 6502 |
<primary>operation</primary> |
| 6503 |
<secondary>-=</secondary> |
| 6504 |
</indexterm> |
| 6505 |
<indexterm> |
| 6506 |
<primary>minus-equal</primary> |
| 6507 |
</indexterm> |
| 6508 |
<listitem><para><firstterm>minus-equal</firstterm> (decrement |
| 6509 |
variable by a constant)</para> |
| 6510 |
</listitem> |
| 6511 |
</varlistentry> |
| 6512 |
|
| 6513 |
<varlistentry> |
| 6514 |
<term><token>*=</token></term> |
| 6515 |
<indexterm> |
| 6516 |
<primary>*=</primary> |
| 6517 |
</indexterm> |
| 6518 |
<indexterm> |
| 6519 |
<primary>operation</primary> |
| 6520 |
<secondary>*=</secondary> |
| 6521 |
</indexterm> |
| 6522 |
<indexterm> |
| 6523 |
<primary>times-equal</primary> |
| 6524 |
</indexterm> |
| 6525 |
<listitem><para><firstterm>times-equal</firstterm> (multiply |
| 6526 |
variable by a constant)</para> |
| 6527 |
<para><userinput>let "var *= 4"</userinput> results in <parameter>var</parameter> |
| 6528 |
being multiplied by <literal>4</literal>.</para> |
| 6529 |
</listitem> |
| 6530 |
</varlistentry> |
| 6531 |
|
| 6532 |
<varlistentry> |
| 6533 |
<term><token>/=</token></term> |
| 6534 |
<indexterm> |
| 6535 |
<primary>/=</primary> |
| 6536 |
</indexterm> |
| 6537 |
<indexterm> |
| 6538 |
<primary>operation</primary> |
| 6539 |
<secondary>/=</secondary> |
| 6540 |
</indexterm> |
| 6541 |
<indexterm> |
| 6542 |
<primary>slash-equal</primary> |
| 6543 |
</indexterm> |
| 6544 |
<listitem><para><firstterm>slash-equal</firstterm> (divide |
| 6545 |
variable by a constant)</para> |
| 6546 |
</listitem> |
| 6547 |
</varlistentry> |
| 6548 |
|
| 6549 |
<varlistentry> |
| 6550 |
<term><token>%=</token></term> |
| 6551 |
<indexterm> |
| 6552 |
<primary>%=</primary> |
| 6553 |
</indexterm> |
| 6554 |
<indexterm> |
| 6555 |
<primary>operation</primary> |
| 6556 |
<secondary>%=</secondary> |
| 6557 |
</indexterm> |
| 6558 |
<indexterm> |
| 6559 |
<primary>mod-equal</primary> |
| 6560 |
</indexterm> |
| 6561 |
<listitem><para><firstterm>mod-equal</firstterm> |
| 6562 |
(<firstterm>remainder</firstterm> |
| 6563 |
of dividing variable by a constant)</para> |
| 6564 |
|
| 6565 |
<para><emphasis>Arithmetic operators often occur in an |
| 6566 |
<link linkend="exprref">expr</link> or <link |
| 6567 |
linkend="letref">let</link> expression.</emphasis></para> |
| 6568 |
|
| 6569 |
<example id="arithops"> |
| 6570 |
<title>Using Arithmetic Operations</title> |
| 6571 |
<programlisting>&arithops;</programlisting> |
| 6572 |
</example> |
| 6573 |
|
| 6574 |
</listitem> |
| 6575 |
</varlistentry> |
| 6576 |
|
| 6577 |
</variablelist> |
| 6578 |
|
| 6579 |
<para><anchor id="intvarref"></para> |
| 6580 |
<note> |
| 6581 |
<para>Integer variables in older versions of Bash were signed |
| 6582 |
<firstterm>long</firstterm> (32-bit) integers, in the range of |
| 6583 |
-2147483648 to 2147483647. An operation that took a variable |
| 6584 |
outside these limits gave an erroneous result.</para> |
| 6585 |
|
| 6586 |
<para> |
| 6587 |
<programlisting>echo $BASH_VERSION # 1.14 |
| 6588 |
|
| 6589 |
a=2147483646 |
| 6590 |
echo "a = $a" # a = 2147483646 |
| 6591 |
let "a+=1" # Increment "a". |
| 6592 |
echo "a = $a" # a = 2147483647 |
| 6593 |
let "a+=1" # increment "a" again, past the limit. |
| 6594 |
echo "a = $a" # a = -2147483648 |
| 6595 |
# ERROR: out of range, |
| 6596 |
# + and the leftmost bit, the sign bit, |
| 6597 |
# + has been set, making the result negative.</programlisting> |
| 6598 |
</para> |
| 6599 |
|
| 6600 |
<para>As of version >= 2.05b, Bash supports 64-bit integers.</para> |
| 6601 |
</note> |
| 6602 |
|
| 6603 |
<caution> |
| 6604 |
|
| 6605 |
<para><anchor id="nofloatingpoint"></para> |
| 6606 |
<para>Bash does not understand floating point arithmetic. It |
| 6607 |
treats numbers containing a decimal point as strings.</para> |
| 6608 |
|
| 6609 |
<para> |
| 6610 |
<programlisting>a=1.5 |
| 6611 |
|
| 6612 |
let "b = $a + 1.3" # Error. |
| 6613 |
# t2.sh: let: b = 1.5 + 1.3: syntax error in expression |
| 6614 |
# (error token is ".5 + 1.3") |
| 6615 |
|
| 6616 |
echo "b = $b" # b=1</programlisting> |
| 6617 |
</para> |
| 6618 |
|
| 6619 |
<para>Use <link linkend="bcref">bc</link> in scripts that that need floating |
| 6620 |
point calculations or math library functions.</para></caution> |
| 6621 |
|
| 6622 |
|
| 6623 |
|
| 6624 |
<formalpara><title>bitwise operators</title> |
| 6625 |
<para>The bitwise operators seldom make an appearance in shell scripts. |
| 6626 |
Their chief use seems to be manipulating and testing values read |
| 6627 |
from ports or <link linkend="socketref">sockets</link>. <quote>Bit |
| 6628 |
flipping</quote> is more relevant to compiled languages, such |
| 6629 |
as C and C++, which provide direct access to system |
| 6630 |
hardware. However, see <emphasis>vladz's</emphasis> |
| 6631 |
ingenious use of bitwise operators in his |
| 6632 |
<firstterm>base64.sh</firstterm> (<xref linkend="base64">) |
| 6633 |
script. </para></formalpara> |
| 6634 |
|
| 6635 |
<variablelist id="bitwsops"> |
| 6636 |
<title><anchor id="bitwsops1">bitwise operators</title> |
| 6637 |
|
| 6638 |
<varlistentry> |
| 6639 |
<term><token><<</token></term> |
| 6640 |
<indexterm> |
| 6641 |
<primary><<</primary> |
| 6642 |
</indexterm> |
| 6643 |
<indexterm> |
| 6644 |
<primary>operation</primary> |
| 6645 |
<secondary><<</secondary> |
| 6646 |
</indexterm> |
| 6647 |
<indexterm> |
| 6648 |
<primary>left shift</primary> |
| 6649 |
</indexterm> |
| 6650 |
<listitem><para>bitwise left shift (multiplies by <literal>2</literal> |
| 6651 |
for each shift position)</para> |
| 6652 |
</listitem> |
| 6653 |
</varlistentry> |
| 6654 |
|
| 6655 |
<varlistentry> |
| 6656 |
<term><token><<=</token></term> |
| 6657 |
<indexterm> |
| 6658 |
<primary><<=</primary> |
| 6659 |
</indexterm> |
| 6660 |
<indexterm> |
| 6661 |
<primary>operation</primary> |
| 6662 |
<secondary><<=</secondary> |
| 6663 |
</indexterm> |
| 6664 |
<indexterm> |
| 6665 |
<primary>left-shift-equal</primary> |
| 6666 |
</indexterm> |
| 6667 |
<listitem><para><firstterm>left-shift-equal</firstterm></para> |
| 6668 |
<para><userinput>let "var <<= 2"</userinput> results in <parameter>var</parameter> |
| 6669 |
left-shifted <literal>2</literal> bits (multiplied by <literal>4</literal>)</para> |
| 6670 |
</listitem> |
| 6671 |
</varlistentry> |
| 6672 |
|
| 6673 |
<varlistentry> |
| 6674 |
<term><token>>></token></term> |
| 6675 |
<indexterm> |
| 6676 |
<primary>>></primary> |
| 6677 |
</indexterm> |
| 6678 |
<indexterm> |
| 6679 |
<primary>operation</primary> |
| 6680 |
<secondary>>></secondary> |
| 6681 |
</indexterm> |
| 6682 |
<indexterm> |
| 6683 |
<primary>right shift</primary> |
| 6684 |
</indexterm> |
| 6685 |
<listitem><para>bitwise right shift (divides by <literal>2</literal> |
| 6686 |
for each shift position)</para> |
| 6687 |
</listitem> |
| 6688 |
</varlistentry> |
| 6689 |
|
| 6690 |
<varlistentry> |
| 6691 |
<term><token>>>=</token></term> |
| 6692 |
<indexterm> |
| 6693 |
<primary>>>=</primary> |
| 6694 |
</indexterm> |
| 6695 |
<indexterm> |
| 6696 |
<primary>operation</primary> |
| 6697 |
<secondary>>>=</secondary> |
| 6698 |
</indexterm> |
| 6699 |
<indexterm> |
| 6700 |
<primary>right-shift-equal</primary> |
| 6701 |
</indexterm> |
| 6702 |
<listitem><para><firstterm>right-shift-equal</firstterm> |
| 6703 |
(inverse of <token><<=</token>)</para> |
| 6704 |
</listitem> |
| 6705 |
</varlistentry> |
| 6706 |
|
| 6707 |
<varlistentry> |
| 6708 |
<term><token>&</token></term> |
| 6709 |
<indexterm> |
| 6710 |
<primary>&</primary> |
| 6711 |
</indexterm> |
| 6712 |
<indexterm> |
| 6713 |
<primary>operation</primary> |
| 6714 |
<secondary>&</secondary> |
| 6715 |
</indexterm> |
| 6716 |
<indexterm> |
| 6717 |
<primary>AND</primary> |
| 6718 |
<secondary>bitwise</secondary> |
| 6719 |
</indexterm> |
| 6720 |
<listitem><para>bitwise AND</para> |
| 6721 |
</listitem> |
| 6722 |
</varlistentry> |
| 6723 |
|
| 6724 |
<varlistentry> |
| 6725 |
<term><token>&=</token></term> |
| 6726 |
<indexterm> |
| 6727 |
<primary>&=</primary> |
| 6728 |
</indexterm> |
| 6729 |
<indexterm> |
| 6730 |
<primary>operation</primary> |
| 6731 |
<secondary>&=</secondary> |
| 6732 |
</indexterm> |
| 6733 |
<indexterm> |
| 6734 |
<primary>and-equal</primary> |
| 6735 |
</indexterm> |
| 6736 |
<listitem><para>bitwise <firstterm>AND-equal</firstterm></para> |
| 6737 |
</listitem> |
| 6738 |
</varlistentry> |
| 6739 |
|
| 6740 |
<varlistentry> |
| 6741 |
<term><token>|</token></term> |
| 6742 |
<indexterm> |
| 6743 |
<primary>|</primary> |
| 6744 |
</indexterm> |
| 6745 |
<indexterm> |
| 6746 |
<primary>operation</primary> |
| 6747 |
<secondary>|</secondary> |
| 6748 |
</indexterm> |
| 6749 |
<indexterm> |
| 6750 |
<primary>OR</primary> |
| 6751 |
<secondary>bitwise</secondary> |
| 6752 |
</indexterm> |
| 6753 |
<listitem><para>bitwise OR</para> |
| 6754 |
</listitem> |
| 6755 |
</varlistentry> |
| 6756 |
|
| 6757 |
<varlistentry> |
| 6758 |
<term><token>|=</token></term> |
| 6759 |
<indexterm> |
| 6760 |
<primary>|=</primary> |
| 6761 |
</indexterm> |
| 6762 |
<indexterm> |
| 6763 |
<primary>operation</primary> |
| 6764 |
<secondary>|=</secondary> |
| 6765 |
</indexterm> |
| 6766 |
<indexterm> |
| 6767 |
<primary>OR-equal</primary> |
| 6768 |
</indexterm> |
| 6769 |
<listitem><para>bitwise <firstterm>OR-equal</firstterm></para> |
| 6770 |
</listitem> |
| 6771 |
</varlistentry> |
| 6772 |
|
| 6773 |
<varlistentry> |
| 6774 |
<term><token>~</token></term> |
| 6775 |
<indexterm> |
| 6776 |
<primary>~</primary> |
| 6777 |
</indexterm> |
| 6778 |
<indexterm> |
| 6779 |
<primary>operation</primary> |
| 6780 |
<secondary>~</secondary> |
| 6781 |
</indexterm> |
| 6782 |
<indexterm> |
| 6783 |
<primary>negate</primary> |
| 6784 |
</indexterm> |
| 6785 |
<listitem><para>bitwise NOT</para> |
| 6786 |
</listitem> |
| 6787 |
</varlistentry> |
| 6788 |
|
| 6789 |
<varlistentry> |
| 6790 |
<term><token>^</token></term> |
| 6791 |
<indexterm> |
| 6792 |
<primary>^</primary> |
| 6793 |
</indexterm> |
| 6794 |
<indexterm> |
| 6795 |
<primary>operation</primary> |
| 6796 |
<secondary>^</secondary> |
| 6797 |
</indexterm> |
| 6798 |
<indexterm> |
| 6799 |
<primary>XOR</primary> |
| 6800 |
</indexterm> |
| 6801 |
<listitem><para>bitwise XOR</para> |
| 6802 |
</listitem> |
| 6803 |
</varlistentry> |
| 6804 |
|
| 6805 |
<varlistentry> |
| 6806 |
<term><token>^=</token></term> |
| 6807 |
<indexterm> |
| 6808 |
<primary>^=</primary> |
| 6809 |
</indexterm> |
| 6810 |
<indexterm> |
| 6811 |
<primary>operation</primary> |
| 6812 |
<secondary>^=</secondary> |
| 6813 |
</indexterm> |
| 6814 |
<indexterm> |
| 6815 |
<primary>XOR-equal</primary> |
| 6816 |
</indexterm> |
| 6817 |
<listitem><para>bitwise <firstterm>XOR-equal</firstterm></para> |
| 6818 |
</listitem> |
| 6819 |
</varlistentry> |
| 6820 |
|
| 6821 |
</variablelist> |
| 6822 |
|
| 6823 |
|
| 6824 |
<variablelist id="logops"> |
| 6825 |
<title><anchor id="logops1">logical (boolean) operators</title> |
| 6826 |
|
| 6827 |
<varlistentry> |
| 6828 |
<term><token>!</token></term> |
| 6829 |
<indexterm> |
| 6830 |
<primary>!</primary> |
| 6831 |
</indexterm> |
| 6832 |
<indexterm> |
| 6833 |
<primary>operator</primary> |
| 6834 |
<secondary>!</secondary> |
| 6835 |
</indexterm> |
| 6836 |
<indexterm> |
| 6837 |
<primary>NOT</primary> |
| 6838 |
</indexterm> |
| 6839 |
<listitem><para>NOT</para> |
| 6840 |
<para><programlisting>if [ ! -f $FILENAME ] |
| 6841 |
then |
| 6842 |
...</programlisting></para> |
| 6843 |
</listitem> |
| 6844 |
</varlistentry> |
| 6845 |
|
| 6846 |
<varlistentry> |
| 6847 |
<term><token>&&</token></term> |
| 6848 |
<indexterm> |
| 6849 |
<primary>&&</primary> |
| 6850 |
</indexterm> |
| 6851 |
<indexterm> |
| 6852 |
<primary>operator</primary> |
| 6853 |
<secondary>&&</secondary> |
| 6854 |
</indexterm> |
| 6855 |
<indexterm> |
| 6856 |
<primary>AND</primary> |
| 6857 |
<secondary>logical</secondary> |
| 6858 |
</indexterm> |
| 6859 |
<listitem><para>AND</para> |
| 6860 |
<para><programlisting>if [ $condition1 ] && [ $condition2 ] |
| 6861 |
# Same as: if [ $condition1 -a $condition2 ] |
| 6862 |
# Returns true if both condition1 and condition2 hold true... |
| 6863 |
|
| 6864 |
if [[ $condition1 && $condition2 ]] # Also works. |
| 6865 |
# Note that && operator not permitted <emphasis>inside brackets</emphasis> |
| 6866 |
#+ of [ ... ] construct.</programlisting></para> |
| 6867 |
|
| 6868 |
<note><para><token>&&</token> may also be used, depending on context, |
| 6869 |
in an <link linkend="listconsref">and list</link> |
| 6870 |
to concatenate commands.</para></note> |
| 6871 |
|
| 6872 |
</listitem> |
| 6873 |
</varlistentry> |
| 6874 |
|
| 6875 |
<varlistentry> |
| 6876 |
<term><anchor id="orref"><token>||</token></term> |
| 6877 |
<indexterm> |
| 6878 |
<primary>||</primary> |
| 6879 |
</indexterm> |
| 6880 |
<indexterm> |
| 6881 |
<primary>operator</primary> |
| 6882 |
<secondary>||</secondary> |
| 6883 |
</indexterm> |
| 6884 |
<indexterm> |
| 6885 |
<primary>OR</primary> |
| 6886 |
<secondary>logical</secondary> |
| 6887 |
</indexterm> |
| 6888 |
<listitem><para>OR</para> |
| 6889 |
<para><programlisting>if [ $condition1 ] || [ $condition2 ] |
| 6890 |
# Same as: if [ $condition1 -o $condition2 ] |
| 6891 |
# Returns true if either condition1 or condition2 holds true... |
| 6892 |
|
| 6893 |
if [[ $condition1 || $condition2 ]] # Also works. |
| 6894 |
# Note that || operator not permitted <emphasis>inside brackets</emphasis> |
| 6895 |
#+ of a [ ... ] construct.</programlisting></para> |
| 6896 |
|
| 6897 |
<note><para>Bash tests the <link linkend="exitstatusref">exit |
| 6898 |
status</link> of each statement linked with a logical |
| 6899 |
operator.</para></note> |
| 6900 |
|
| 6901 |
<example id="andor"> |
| 6902 |
<title>Compound Condition Tests Using && and ||</title> |
| 6903 |
<programlisting>&andor;</programlisting> |
| 6904 |
</example> |
| 6905 |
|
| 6906 |
<para>The <token>&&</token> and <token>||</token> operators also |
| 6907 |
find use in an arithmetic context.</para> |
| 6908 |
|
| 6909 |
<para> |
| 6910 |
<screen><prompt>bash$ </prompt><userinput>echo $(( 1 && 2 )) $((3 && 0)) $((4 || 0)) $((0 || 0))</userinput> |
| 6911 |
<computeroutput>1 0 1 0</computeroutput> |
| 6912 |
</screen> |
| 6913 |
</para> |
| 6914 |
|
| 6915 |
</listitem> |
| 6916 |
</varlistentry> |
| 6917 |
|
| 6918 |
</variablelist> |
| 6919 |
|
| 6920 |
<variablelist id="miscop"> |
| 6921 |
<title><anchor id="miscop1">miscellaneous operators</title> |
| 6922 |
|
| 6923 |
<varlistentry> |
| 6924 |
<term><anchor id="commaop"><token>,</token></term> |
| 6925 |
<indexterm> |
| 6926 |
<primary>,</primary> |
| 6927 |
</indexterm> |
| 6928 |
<indexterm> |
| 6929 |
<primary>operation</primary> |
| 6930 |
<secondary>,</secondary> |
| 6931 |
</indexterm> |
| 6932 |
<indexterm> |
| 6933 |
<primary>linking</primary> |
| 6934 |
</indexterm> |
| 6935 |
<listitem> |
| 6936 |
<para>Comma operator</para> |
| 6937 |
<para>The <command>comma operator</command> chains together |
| 6938 |
two or more arithmetic operations. All the operations are |
| 6939 |
evaluated (with possible <firstterm>side |
| 6940 |
effects</firstterm>. |
| 6941 |
<footnote><para><firstterm>Side effects</firstterm> |
| 6942 |
are, of course, unintended -- and usually undesirable -- |
| 6943 |
consequences.</para></footnote> |
| 6944 |
</para> |
| 6945 |
|
| 6946 |
<para> |
| 6947 |
<programlisting>let "t1 = ((5 + 3, 7 - 1, 15 - 4))" |
| 6948 |
echo "t1 = $t1" ^^^^^^ # t1 = 11 |
| 6949 |
# Here t1 is set to the result of the last operation. Why? |
| 6950 |
|
| 6951 |
let "t2 = ((a = 9, 15 / 3))" # Set "a" and calculate "t2". |
| 6952 |
echo "t2 = $t2 a = $a" # t2 = 5 a = 9</programlisting> |
| 6953 |
</para> |
| 6954 |
|
| 6955 |
<para>The comma operator finds use mainly in <link |
| 6956 |
linkend="forloopref1">for loops</link>. See <xref |
| 6957 |
linkend="forloopc">.</para> |
| 6958 |
|
| 6959 |
</listitem> |
| 6960 |
|
| 6961 |
</varlistentry> |
| 6962 |
</variablelist> |
| 6963 |
|
| 6964 |
</sect1> <!-- Operators --> |
| 6965 |
|
| 6966 |
<sect1 id="Numerical-Constants"> |
| 6967 |
<title>Numerical Constants</title> |
| 6968 |
|
| 6969 |
<para><anchor id="numconstants">A shell script interprets a number |
| 6970 |
as decimal (base 10), unless that number has a |
| 6971 |
special prefix or notation. A number preceded by a |
| 6972 |
<replaceable>0</replaceable> is <replaceable>octal</replaceable> |
| 6973 |
(base 8). A number preceded by <replaceable>0x</replaceable> |
| 6974 |
is <replaceable>hexadecimal</replaceable> (base 16). A number |
| 6975 |
with an embedded <replaceable>#</replaceable> evaluates as |
| 6976 |
<replaceable>BASE#NUMBER</replaceable> (with range and notational |
| 6977 |
restrictions).</para> |
| 6978 |
|
| 6979 |
<example id="numbers"> |
| 6980 |
<title>Representation of numerical constants</title> |
| 6981 |
<programlisting>&numbers;</programlisting> |
| 6982 |
</example> |
| 6983 |
|
| 6984 |
</sect1> <!-- Numerical-Constants --> |
| 6985 |
|
| 6986 |
<sect1 id="dblparens"> |
| 6987 |
<title>The Double-Parentheses Construct</title> |
| 6988 |
|
| 6989 |
<para><anchor id="dblparensref"></para> |
| 6990 |
|
| 6991 |
<para>Similar to the <link linkend="letref">let</link> command, |
| 6992 |
the <command>(( ... ))</command> construct permits |
| 6993 |
arithmetic expansion and evaluation. In its simplest |
| 6994 |
form, <userinput>a=$(( 5 + 3 ))</userinput> would set |
| 6995 |
<userinput>a</userinput> to <userinput>5 + 3</userinput>, or |
| 6996 |
<userinput>8</userinput>. However, this double-parentheses |
| 6997 |
construct is also a mechanism for allowing C-style |
| 6998 |
manipulation of variables in Bash, for example, |
| 6999 |
<varname>(( var++ ))</varname>.</para> |
| 7000 |
|
| 7001 |
<para><anchor id="plusplusref"></para> |
| 7002 |
|
| 7003 |
<example id="cvars"> |
| 7004 |
<title>C-style manipulation of variables</title> |
| 7005 |
<programlisting>&cvars;</programlisting> |
| 7006 |
</example> |
| 7007 |
|
| 7008 |
<para>See also <xref linkend="forloopc"> and <xref |
| 7009 |
linkend="numbers">.</para> |
| 7010 |
|
| 7011 |
</sect1> <!-- The Double-Parentheses Construct --> |
| 7012 |
|
| 7013 |
<sect1 id="opprecedence"> |
| 7014 |
<title>Operator Precedence</title> |
| 7015 |
|
| 7016 |
<para><anchor id="opprecedence1"></para> |
| 7017 |
|
| 7018 |
<para> |
| 7019 |
In a script, operations execute in order of |
| 7020 |
<firstterm>precedence</firstterm>: the higher precedence operations |
| 7021 |
execute <emphasis>before</emphasis> the lower precedence ones. |
| 7022 |
<footnote><para><firstterm>Precedence</firstterm>, in this context, |
| 7023 |
has approximately the same meaning as |
| 7024 |
<firstterm>priority</firstterm></para></footnote> |
| 7025 |
</para> |
| 7026 |
|
| 7027 |
&opprectable; |
| 7028 |
|
| 7029 |
<para>In practice, all you really need to remember is the |
| 7030 |
following:</para> |
| 7031 |
|
| 7032 |
<itemizedlist> |
| 7033 |
<listitem> |
| 7034 |
<para>The <quote>My Dear Aunt Sally</quote> mantra (<emphasis>multiply, |
| 7035 |
divide, add, subtract</emphasis>) for the familiar <link |
| 7036 |
linkend="arops1">arithmetic operations</link>.</para> |
| 7037 |
</listitem> |
| 7038 |
<listitem> |
| 7039 |
<para>The <firstterm>compound</firstterm> logical operators, |
| 7040 |
<command>&&</command>, <command>||</command>, <command>-a</command>, |
| 7041 |
and <command>-o</command> have low precedence.</para> |
| 7042 |
</listitem> |
| 7043 |
<listitem> |
| 7044 |
<para>The order of evaluation of equal-precedence operators is |
| 7045 |
usually <firstterm>left-to-right</firstterm>.</para> |
| 7046 |
</listitem> |
| 7047 |
</itemizedlist> |
| 7048 |
|
| 7049 |
<para>Now, let's utilize our knowledge of operator precedence to |
| 7050 |
analyze a couple of lines from the |
| 7051 |
<filename>/etc/init.d/functions file</filename>, as found in |
| 7052 |
the <firstterm>Fedora Core</firstterm> Linux distro.</para> |
| 7053 |
|
| 7054 |
<para><programlisting>while [ -n "$remaining" -a "$retry" -gt 0 ]; do |
| 7055 |
|
| 7056 |
# This looks rather daunting at first glance. |
| 7057 |
|
| 7058 |
|
| 7059 |
# Separate the conditions: |
| 7060 |
while [ -n "$remaining" -a "$retry" -gt 0 ]; do |
| 7061 |
# --condition 1-- ^^ --condition 2- |
| 7062 |
|
| 7063 |
# If variable "$remaining" is not zero length |
| 7064 |
#+ AND (-a) |
| 7065 |
#+ variable "$retry" is greater-than zero |
| 7066 |
#+ then |
| 7067 |
#+ the [ expresion-within-condition-brackets ] returns success (0) |
| 7068 |
#+ and the while-loop executes an iteration. |
| 7069 |
# ============================================================== |
| 7070 |
# Evaluate "condition 1" and "condition 2" ***before*** |
| 7071 |
#+ ANDing them. Why? Because the AND (-a) has a lower precedence |
| 7072 |
#+ than the -n and -gt operators, |
| 7073 |
#+ and therefore gets evaluated *last*. |
| 7074 |
|
| 7075 |
################################################################# |
| 7076 |
|
| 7077 |
if [ -f /etc/sysconfig/i18n -a -z "${NOLOCALE:-}" ] ; then |
| 7078 |
|
| 7079 |
|
| 7080 |
# Again, separate the conditions: |
| 7081 |
if [ -f /etc/sysconfig/i18n -a -z "${NOLOCALE:-}" ] ; then |
| 7082 |
# --condition 1--------- ^^ --condition 2----- |
| 7083 |
|
| 7084 |
# If file "/etc/sysconfig/i18n" exists |
| 7085 |
#+ AND (-a) |
| 7086 |
#+ variable $NOLOCALE is zero length |
| 7087 |
#+ then |
| 7088 |
#+ the [ test-expresion-within-condition-brackets ] returns success (0) |
| 7089 |
#+ and the commands following execute. |
| 7090 |
# |
| 7091 |
# As before, the AND (-a) gets evaluated *last* |
| 7092 |
#+ because it has the lowest precedence of the operators within |
| 7093 |
#+ the test brackets. |
| 7094 |
# ============================================================== |
| 7095 |
# Note: |
| 7096 |
# ${NOLOCALE:-} is a parameter expansion that seems redundant. |
| 7097 |
# But, if $NOLOCALE has not been declared, it gets set to *null*, |
| 7098 |
#+ in effect declaring it. |
| 7099 |
# This makes a difference in some contexts.</programlisting></para> |
| 7100 |
|
| 7101 |
<tip> |
| 7102 |
<para>To avoid confusion or error in a complex sequence of test |
| 7103 |
operators, break up the sequence into bracketed sections. |
| 7104 |
<programlisting>if [ "$v1" -gt "$v2" -o "$v1" -lt "$v2" -a -e "$filename" ] |
| 7105 |
# Unclear what's going on here... |
| 7106 |
|
| 7107 |
if [[ "$v1" -gt "$v2" ]] || [[ "$v1" -lt "$v2" ]] && [[ -e "$filename" ]] |
| 7108 |
# Much better -- the condition tests are grouped in logical sections.</programlisting> |
| 7109 |
</para> |
| 7110 |
</tip> |
| 7111 |
|
| 7112 |
</sect1> <!-- Operator Precedence --> |
| 7113 |
|
| 7114 |
</chapter> <!-- Operations --> |
| 7115 |
|
| 7116 |
</part> <!-- Part 2 (Basics) --> |
| 7117 |
|
| 7118 |
|
| 7119 |
|
| 7120 |
<part label="Part 3" id="part3"> |
| 7121 |
<title>Beyond the Basics</title> |
| 7122 |
|
| 7123 |
|
| 7124 |
<chapter id="variables2"> |
| 7125 |
<title>Another Look at Variables</title> |
| 7126 |
|
| 7127 |
<para>Used properly, variables can add power and flexibility |
| 7128 |
to scripts. This requires learning their subtleties and |
| 7129 |
nuances.</para> |
| 7130 |
|
| 7131 |
|
| 7132 |
<sect1 id="internalvariables"> |
| 7133 |
<title>Internal Variables</title> |
| 7134 |
|
| 7135 |
<variablelist id="internalvariables1"> |
| 7136 |
|
| 7137 |
<varlistentry> |
| 7138 |
<term><replaceable><link |
| 7139 |
linkend="builtinref">Builtin</link> variables:</replaceable></term> |
| 7140 |
<listitem><para>variables affecting bash script behavior</para> |
| 7141 |
</listitem> |
| 7142 |
</varlistentry> |
| 7143 |
|
| 7144 |
<varlistentry> |
| 7145 |
<term><anchor id="bashvarref"><varname>$BASH</varname></term> |
| 7146 |
<indexterm> |
| 7147 |
<primary>$BASH</primary> |
| 7148 |
</indexterm> |
| 7149 |
<indexterm> |
| 7150 |
<primary>variable</primary> |
| 7151 |
<secondary>$BASH</secondary> |
| 7152 |
</indexterm> |
| 7153 |
<indexterm> |
| 7154 |
<primary>path to bash</primary> |
| 7155 |
</indexterm> |
| 7156 |
<listitem><para>The path to the <firstterm>Bash</firstterm> |
| 7157 |
binary itself |
| 7158 |
<screen><prompt>bash$ </prompt><userinput>echo $BASH</userinput> |
| 7159 |
<computeroutput>/bin/bash</computeroutput></screen> |
| 7160 |
</para> |
| 7161 |
|
| 7162 |
</listitem> |
| 7163 |
</varlistentry> |
| 7164 |
|
| 7165 |
<varlistentry> |
| 7166 |
<term><anchor |
| 7167 |
id="bashenvref"><varname>$BASH_ENV</varname></term> |
| 7168 |
<indexterm> |
| 7169 |
<primary>$BASH_ENV</primary> |
| 7170 |
</indexterm> |
| 7171 |
<indexterm> |
| 7172 |
<primary>variable</primary> |
| 7173 |
<secondary>$BASH_ENV</secondary> |
| 7174 |
</indexterm> |
| 7175 |
<listitem><para>An <link linkend="envref">environmental |
| 7176 |
variable</link> pointing to a Bash startup file to be read |
| 7177 |
when a script is invoked</para> |
| 7178 |
</listitem> |
| 7179 |
</varlistentry> |
| 7180 |
|
| 7181 |
|
| 7182 |
<varlistentry> |
| 7183 |
<term><anchor id="bashsubshellref"><varname>$BASH_SUBSHELL</varname></term> |
| 7184 |
<indexterm> |
| 7185 |
<primary>$BASH_SUBSHELL</primary> |
| 7186 |
</indexterm> |
| 7187 |
<indexterm> |
| 7188 |
<primary>variable</primary> |
| 7189 |
<secondary>subshell</secondary> |
| 7190 |
</indexterm> |
| 7191 |
<listitem> |
| 7192 |
<para>A variable indicating the <link |
| 7193 |
linkend="subshellsref">subshell</link> level. This is a |
| 7194 |
new addition to Bash, <link linkend="bash3ref">version 3</link>.</para> |
| 7195 |
<para>See <xref linkend="subshell"> for usage.</para> |
| 7196 |
</listitem> |
| 7197 |
</varlistentry> |
| 7198 |
|
| 7199 |
|
| 7200 |
<varlistentry> |
| 7201 |
<term><anchor |
| 7202 |
id="bashpidref"><varname>$BASHPID</varname></term> |
| 7203 |
<indexterm> |
| 7204 |
<primary>$BASHPID</primary> |
| 7205 |
</indexterm> |
| 7206 |
<indexterm> |
| 7207 |
<primary>variable</primary> |
| 7208 |
<secondary>process ID</secondary> |
| 7209 |
</indexterm> |
| 7210 |
<listitem><para><firstterm>Process ID</firstterm> |
| 7211 |
of the current instance of Bash. This is not the same as the |
| 7212 |
<link linkend="proccid">$$</link> variable, but it often |
| 7213 |
gives the same result.</para> |
| 7214 |
<para> |
| 7215 |
<screen> |
| 7216 |
<prompt>bash4$ </prompt><userinput>echo $$</userinput> |
| 7217 |
<computeroutput>11015</computeroutput> |
| 7218 |
|
| 7219 |
|
| 7220 |
<prompt>bash4$ </prompt><userinput>echo $BASHPID</userinput> |
| 7221 |
<computeroutput>11015</computeroutput> |
| 7222 |
|
| 7223 |
|
| 7224 |
<prompt>bash4$ </prompt><userinput>ps ax | grep bash4</userinput> |
| 7225 |
<computeroutput>11015 pts/2 R 0:00 bash4</computeroutput> |
| 7226 |
</screen> |
| 7227 |
</para> |
| 7228 |
<para><anchor id="bashpidref2">But ...</para> |
| 7229 |
<para><programlisting> |
| 7230 |
#!/bin/bash4 |
| 7231 |
|
| 7232 |
echo "\$\$ outside of subshell = $$" # 9602 |
| 7233 |
echo "\$BASH_SUBSHELL outside of subshell = $BASH_SUBSHELL" # 0 |
| 7234 |
echo "\$BASHPID outside of subshell = $BASHPID" # 9602 |
| 7235 |
|
| 7236 |
echo |
| 7237 |
|
| 7238 |
( echo "\$\$ inside of subshell = $$" # 9602 |
| 7239 |
echo "\$BASH_SUBSHELL inside of subshell = $BASH_SUBSHELL" # 1 |
| 7240 |
echo "\$BASHPID inside of subshell = $BASHPID" ) # 9603 |
| 7241 |
# Note that $$ returns PID of parent process. |
| 7242 |
</programlisting></para> |
| 7243 |
</listitem> |
| 7244 |
</varlistentry> |
| 7245 |
|
| 7246 |
|
| 7247 |
<varlistentry> |
| 7248 |
<term><varname>$BASH_VERSINFO[n]</varname></term> |
| 7249 |
<indexterm> |
| 7250 |
<primary>$BASH_VERSINFO</primary> |
| 7251 |
</indexterm> |
| 7252 |
<indexterm> |
| 7253 |
<primary>variable</primary> |
| 7254 |
<secondary>version information</secondary> |
| 7255 |
</indexterm> |
| 7256 |
<listitem> |
| 7257 |
<para>A 6-element <link linkend="arrayref">array</link> |
| 7258 |
containing version information about the installed release |
| 7259 |
of Bash. This is similar to <varname>$BASH_VERSION</varname>, |
| 7260 |
below, but a bit more detailed.</para> |
| 7261 |
<para> |
| 7262 |
<programlisting># Bash version info: |
| 7263 |
|
| 7264 |
for n in 0 1 2 3 4 5 |
| 7265 |
do |
| 7266 |
echo "BASH_VERSINFO[$n] = ${BASH_VERSINFO[$n]}" |
| 7267 |
done |
| 7268 |
|
| 7269 |
# BASH_VERSINFO[0] = 3 # Major version no. |
| 7270 |
# BASH_VERSINFO[1] = 00 # Minor version no. |
| 7271 |
# BASH_VERSINFO[2] = 14 # Patch level. |
| 7272 |
# BASH_VERSINFO[3] = 1 # Build version. |
| 7273 |
# BASH_VERSINFO[4] = release # Release status. |
| 7274 |
# BASH_VERSINFO[5] = i386-redhat-linux-gnu # Architecture |
| 7275 |
# (same as $MACHTYPE).</programlisting> |
| 7276 |
</para> |
| 7277 |
|
| 7278 |
</listitem> |
| 7279 |
</varlistentry> |
| 7280 |
|
| 7281 |
<varlistentry> |
| 7282 |
<term><varname>$BASH_VERSION</varname></term> |
| 7283 |
<indexterm> |
| 7284 |
<primary>$BASH_VERSION</primary> |
| 7285 |
</indexterm> |
| 7286 |
<indexterm> |
| 7287 |
<primary>variable</primary> |
| 7288 |
<secondary>$BASH_VERSION</secondary> |
| 7289 |
</indexterm> |
| 7290 |
<listitem><para>The version of Bash installed on the system</para> |
| 7291 |
|
| 7292 |
<para> |
| 7293 |
<screen><prompt>bash$ </prompt><userinput>echo $BASH_VERSION</userinput> |
| 7294 |
<computeroutput>3.2.25(1)-release</computeroutput> |
| 7295 |
</screen> |
| 7296 |
</para> |
| 7297 |
|
| 7298 |
<para> |
| 7299 |
<screen><prompt>tcsh% </prompt><userinput>echo $BASH_VERSION</userinput> |
| 7300 |
<computeroutput>BASH_VERSION: Undefined variable.</computeroutput> |
| 7301 |
</screen> |
| 7302 |
</para> |
| 7303 |
|
| 7304 |
<para>Checking $BASH_VERSION is a good method of determining which |
| 7305 |
shell is running. <link linkend="shellvarref">$SHELL</link> |
| 7306 |
does not necessarily give the correct answer.</para> |
| 7307 |
|
| 7308 |
</listitem> |
| 7309 |
</varlistentry> |
| 7310 |
|
| 7311 |
|
| 7312 |
<varlistentry> |
| 7313 |
<term><anchor id="cdpathref"><varname>$CDPATH</varname></term> |
| 7314 |
<indexterm> |
| 7315 |
<primary>$CDPATH</primary> |
| 7316 |
</indexterm> |
| 7317 |
<indexterm> |
| 7318 |
<primary>variable</primary> |
| 7319 |
<secondary>$CDPATH</secondary> |
| 7320 |
</indexterm> |
| 7321 |
<indexterm> |
| 7322 |
<primary>cd path</primary> |
| 7323 |
</indexterm> |
| 7324 |
<indexterm> |
| 7325 |
<primary>cd</primary> |
| 7326 |
<secondary>path</secondary> |
| 7327 |
</indexterm> |
| 7328 |
<listitem><para>A colon-separated list of search paths |
| 7329 |
available to the <link linkend="cdref">cd</link> |
| 7330 |
command, similar in function to the <link |
| 7331 |
linkend="pathref">$PATH</link> variable for binaries. |
| 7332 |
The <varname>$CDPATH</varname> variable may be set in the |
| 7333 |
local <link |
| 7334 |
linkend="bashrc"><filename>~/.bashrc</filename></link> |
| 7335 |
file.</para> |
| 7336 |
|
| 7337 |
<para> |
| 7338 |
<screen> |
| 7339 |
<prompt>bash$ </prompt><userinput>cd bash-doc</userinput> |
| 7340 |
<computeroutput>bash: cd: bash-doc: No such file or directory</computeroutput> |
| 7341 |
|
| 7342 |
|
| 7343 |
<prompt>bash$ </prompt><userinput>CDPATH=/usr/share/doc</userinput> |
| 7344 |
<prompt>bash$ </prompt><userinput>cd bash-doc</userinput> |
| 7345 |
<computeroutput>/usr/share/doc/bash-doc</computeroutput> |
| 7346 |
|
| 7347 |
|
| 7348 |
<prompt>bash$ </prompt><userinput>echo $PWD</userinput> |
| 7349 |
<computeroutput>/usr/share/doc/bash-doc</computeroutput> |
| 7350 |
</screen> |
| 7351 |
</para> |
| 7352 |
|
| 7353 |
</listitem> |
| 7354 |
</varlistentry> |
| 7355 |
|
| 7356 |
|
| 7357 |
<varlistentry> |
| 7358 |
<term><anchor id="dirstackref"><varname>$DIRSTACK</varname></term> |
| 7359 |
<indexterm> |
| 7360 |
<primary>$DIRSTACK</primary> |
| 7361 |
</indexterm> |
| 7362 |
<indexterm> |
| 7363 |
<primary>variable</primary> |
| 7364 |
<secondary>$DIRSTACK</secondary> |
| 7365 |
</indexterm> |
| 7366 |
<indexterm> |
| 7367 |
<primary>directory stack</primary> |
| 7368 |
</indexterm> |
| 7369 |
<indexterm> |
| 7370 |
<primary>directory</primary> |
| 7371 |
<secondary>stack</secondary> |
| 7372 |
</indexterm> |
| 7373 |
<listitem><para>The top value in the directory stack |
| 7374 |
|
| 7375 |
<footnote><para><anchor id="stackdefref"> |
| 7376 |
A <firstterm>stack register</firstterm> |
| 7377 |
is a set of consecutive memory locations, such that |
| 7378 |
the values stored (<firstterm>pushed</firstterm>) |
| 7379 |
are retrieved (<firstterm>popped</firstterm>) |
| 7380 |
in <emphasis>reverse</emphasis> order. The last |
| 7381 |
value stored is the first retrieved. This is |
| 7382 |
sometimes called a <replaceable>LIFO</replaceable> |
| 7383 |
(<firstterm>last-in-first-out</firstterm>) or |
| 7384 |
<firstterm>pushdown</firstterm> stack.</para></footnote> |
| 7385 |
|
| 7386 |
(affected by <link linkend="pushdref">pushd</link> and <link |
| 7387 |
linkend="popdref">popd</link>)</para> <para>This builtin |
| 7388 |
variable corresponds to the <link linkend="dirsd">dirs</link> |
| 7389 |
command, however <command>dirs</command> shows the entire |
| 7390 |
contents of the directory stack.</para> |
| 7391 |
</listitem> |
| 7392 |
</varlistentry> |
| 7393 |
|
| 7394 |
<varlistentry> |
| 7395 |
<term><varname>$EDITOR</varname></term> |
| 7396 |
<indexterm> |
| 7397 |
<primary>$EDITOR</primary> |
| 7398 |
</indexterm> |
| 7399 |
<indexterm> |
| 7400 |
<primary>variable</primary> |
| 7401 |
<secondary>$EDITOR</secondary> |
| 7402 |
</indexterm> |
| 7403 |
<indexterm> |
| 7404 |
<primary>editor</primary> |
| 7405 |
</indexterm> |
| 7406 |
<listitem><para>The default editor invoked by a script, usually |
| 7407 |
<command>vi</command> or <command>emacs</command>.</para> |
| 7408 |
|
| 7409 |
</listitem> |
| 7410 |
</varlistentry> |
| 7411 |
|
| 7412 |
<varlistentry> |
| 7413 |
<term><anchor id="euidref"><varname>$EUID</varname></term> |
| 7414 |
<indexterm> |
| 7415 |
<primary>$EUID</primary> |
| 7416 |
</indexterm> |
| 7417 |
<indexterm> |
| 7418 |
<primary>variable</primary> |
| 7419 |
<secondary>$EUID</secondary> |
| 7420 |
</indexterm> |
| 7421 |
<indexterm> |
| 7422 |
<primary>effective user ID</primary> |
| 7423 |
</indexterm> |
| 7424 |
<listitem><para><quote>effective</quote> user ID number</para> |
| 7425 |
<para>Identification number of whatever identity the |
| 7426 |
current user has assumed, perhaps by means of <link |
| 7427 |
linkend="suref">su</link>.</para> |
| 7428 |
<caution><para>The <varname>$EUID</varname> is not necessarily |
| 7429 |
the same as the <link |
| 7430 |
linkend="uidref">$UID</link>.</para></caution> |
| 7431 |
|
| 7432 |
</listitem> |
| 7433 |
</varlistentry> |
| 7434 |
|
| 7435 |
<varlistentry> |
| 7436 |
<term><varname>$FUNCNAME</varname></term> |
| 7437 |
<indexterm> |
| 7438 |
<primary>$FUNCNAME</primary> |
| 7439 |
</indexterm> |
| 7440 |
<indexterm> |
| 7441 |
<primary>variable</primary> |
| 7442 |
<secondary>function</secondary> |
| 7443 |
</indexterm> |
| 7444 |
<indexterm> |
| 7445 |
<primary>name</primary> |
| 7446 |
</indexterm> |
| 7447 |
<listitem> |
| 7448 |
<para>Name of the current function</para> |
| 7449 |
<para><programlisting>xyz23 () |
| 7450 |
{ |
| 7451 |
echo "$FUNCNAME now executing." # xyz23 now executing. |
| 7452 |
} |
| 7453 |
|
| 7454 |
xyz23 |
| 7455 |
|
| 7456 |
echo "FUNCNAME = $FUNCNAME" # FUNCNAME = |
| 7457 |
# Null value outside a function.</programlisting> |
| 7458 |
</para> |
| 7459 |
|
| 7460 |
<para>See also <xref linkend="usegetopt">.</para> |
| 7461 |
|
| 7462 |
</listitem> |
| 7463 |
</varlistentry> |
| 7464 |
|
| 7465 |
<varlistentry> |
| 7466 |
<term><varname>$GLOBIGNORE</varname></term> |
| 7467 |
<indexterm> |
| 7468 |
<primary>$GLOBIGNORE</primary> |
| 7469 |
</indexterm> |
| 7470 |
<indexterm> |
| 7471 |
<primary>variable</primary> |
| 7472 |
<secondary>globbing</secondary> |
| 7473 |
</indexterm> |
| 7474 |
<indexterm> |
| 7475 |
<primary>ignore</primary> |
| 7476 |
</indexterm> |
| 7477 |
<listitem><para>A list of filename patterns to be excluded from |
| 7478 |
matching in <link linkend="globbingref">globbing</link>.</para> |
| 7479 |
</listitem> |
| 7480 |
</varlistentry> |
| 7481 |
|
| 7482 |
<varlistentry> |
| 7483 |
<term><anchor id="groupsref"><varname>$GROUPS</varname></term> |
| 7484 |
<indexterm> |
| 7485 |
<primary>$GROUPS</primary> |
| 7486 |
</indexterm> |
| 7487 |
<indexterm> |
| 7488 |
<primary>variable</primary> |
| 7489 |
<secondary>$GROUPS</secondary> |
| 7490 |
</indexterm> |
| 7491 |
<indexterm> |
| 7492 |
<primary>groups</primary> |
| 7493 |
</indexterm> |
| 7494 |
<listitem><para>Groups current user belongs to</para> |
| 7495 |
<para>This is a listing (array) of the group id numbers for |
| 7496 |
current user, as recorded in |
| 7497 |
<link |
| 7498 |
linkend="datafilesref1"><filename>/etc/passwd</filename></link> |
| 7499 |
and <filename>/etc/group</filename>. |
| 7500 |
</para> |
| 7501 |
|
| 7502 |
<para> |
| 7503 |
<screen> |
| 7504 |
<prompt>root# </prompt><userinput>echo $GROUPS</userinput> |
| 7505 |
<computeroutput>0</computeroutput> |
| 7506 |
|
| 7507 |
|
| 7508 |
<prompt>root# </prompt><userinput>echo ${GROUPS[1]}</userinput> |
| 7509 |
<computeroutput>1</computeroutput> |
| 7510 |
|
| 7511 |
|
| 7512 |
<prompt>root# </prompt><userinput>echo ${GROUPS[5]}</userinput> |
| 7513 |
<computeroutput>6</computeroutput> |
| 7514 |
</screen> |
| 7515 |
</para> |
| 7516 |
|
| 7517 |
</listitem> |
| 7518 |
</varlistentry> |
| 7519 |
|
| 7520 |
<varlistentry> |
| 7521 |
<term><anchor id="homedirref"><varname>$HOME</varname></term> |
| 7522 |
<indexterm> |
| 7523 |
<primary>$HOME</primary> |
| 7524 |
</indexterm> |
| 7525 |
<indexterm> |
| 7526 |
<primary>variable</primary> |
| 7527 |
<secondary>$HOME</secondary> |
| 7528 |
</indexterm> |
| 7529 |
<indexterm> |
| 7530 |
<primary>home directory</primary> |
| 7531 |
</indexterm> |
| 7532 |
<indexterm> |
| 7533 |
<primary>directory</primary> |
| 7534 |
<secondary>home</secondary> |
| 7535 |
</indexterm> |
| 7536 |
<listitem><para>Home directory of the user, usually <filename |
| 7537 |
class="directory">/home/username</filename> (see <xref |
| 7538 |
linkend="ex6">)</para> |
| 7539 |
|
| 7540 |
</listitem> |
| 7541 |
</varlistentry> |
| 7542 |
|
| 7543 |
<varlistentry> |
| 7544 |
<term><anchor id="hostnameref"><varname>$HOSTNAME</varname></term> |
| 7545 |
<indexterm> |
| 7546 |
<primary>$HOSTNAME</primary> |
| 7547 |
</indexterm> |
| 7548 |
<indexterm> |
| 7549 |
<primary>variable</primary> |
| 7550 |
<secondary>$HOSTNAME</secondary> |
| 7551 |
</indexterm> |
| 7552 |
<indexterm> |
| 7553 |
<primary>system name</primary> |
| 7554 |
</indexterm> |
| 7555 |
<indexterm> |
| 7556 |
<primary>variable</primary> |
| 7557 |
<secondary>name</secondary> |
| 7558 |
</indexterm> |
| 7559 |
<listitem> |
| 7560 |
<para>The <link linkend="hnameref">hostname</link> command |
| 7561 |
assigns the system host name at bootup in an init script. |
| 7562 |
However, the <function>gethostname()</function> function |
| 7563 |
sets the Bash internal variable <varname>$HOSTNAME</varname>. |
| 7564 |
See also <xref linkend="ex6">.</para> |
| 7565 |
</listitem> |
| 7566 |
</varlistentry> |
| 7567 |
|
| 7568 |
<varlistentry> |
| 7569 |
<term><varname>$HOSTTYPE</varname></term> |
| 7570 |
<indexterm> |
| 7571 |
<primary>$HOSTTYPE</primary> |
| 7572 |
</indexterm> |
| 7573 |
<indexterm> |
| 7574 |
<primary>variable</primary> |
| 7575 |
<secondary>$HOSTTYPE</secondary> |
| 7576 |
</indexterm> |
| 7577 |
<indexterm> |
| 7578 |
<primary>host type</primary> |
| 7579 |
</indexterm> |
| 7580 |
<listitem><para>host type</para> |
| 7581 |
<para>Like <link linkend="machtyperef">$MACHTYPE</link>, |
| 7582 |
identifies the system hardware.</para> |
| 7583 |
<screen><prompt>bash$ </prompt><userinput>echo $HOSTTYPE</userinput> |
| 7584 |
<computeroutput>i686</computeroutput></screen> |
| 7585 |
</listitem> |
| 7586 |
</varlistentry> |
| 7587 |
|
| 7588 |
<varlistentry> |
| 7589 |
<term><anchor id="ifsref"><varname>$IFS</varname></term> |
| 7590 |
<indexterm> |
| 7591 |
<primary>$IFS</primary> |
| 7592 |
</indexterm> |
| 7593 |
<indexterm> |
| 7594 |
<primary>variable</primary> |
| 7595 |
<secondary>$IFS</secondary> |
| 7596 |
</indexterm> |
| 7597 |
<indexterm> |
| 7598 |
<primary>internal field separator</primary> |
| 7599 |
</indexterm> |
| 7600 |
<listitem><para>internal field separator</para> |
| 7601 |
|
| 7602 |
<para>This variable determines how Bash recognizes <link |
| 7603 |
linkend="fieldref">fields</link>, or word boundaries, |
| 7604 |
when it interprets character strings.</para> |
| 7605 |
|
| 7606 |
<para><anchor id="ifsws"></para> |
| 7607 |
<para>$IFS defaults to <link |
| 7608 |
linkend="whitespaceref">whitespace</link> (space, |
| 7609 |
tab, and newline), but may be changed, for example, |
| 7610 |
to parse a comma-separated data file. Note that |
| 7611 |
<link linkend="appref">$*</link> uses the first |
| 7612 |
character held in <varname>$IFS</varname>. See <xref |
| 7613 |
linkend="weirdvars">.</para> |
| 7614 |
|
| 7615 |
<para> |
| 7616 |
<screen> |
| 7617 |
<prompt>bash$ </prompt><userinput>echo "$IFS"</userinput> |
| 7618 |
<computeroutput> |
| 7619 |
|
| 7620 |
</computeroutput> |
| 7621 |
<computeroutput>(With $IFS set to default, a blank line displays.)</computeroutput> |
| 7622 |
|
| 7623 |
|
| 7624 |
|
| 7625 |
<prompt>bash$ </prompt><userinput>echo "$IFS" | cat -vte</userinput> |
| 7626 |
<computeroutput> ^I$ |
| 7627 |
$</computeroutput> |
| 7628 |
<computeroutput>(Show whitespace: here a single space, ^I [horizontal tab], |
| 7629 |
and newline, and display "$" at end-of-line.)</computeroutput> |
| 7630 |
|
| 7631 |
|
| 7632 |
|
| 7633 |
<prompt>bash$ </prompt><userinput>bash -c 'set w x y z; IFS=":-;"; echo "$*"'</userinput> |
| 7634 |
<computeroutput>w:x:y:z</computeroutput> |
| 7635 |
<computeroutput>(Read commands from string and assign any arguments to pos params.)</computeroutput> |
| 7636 |
</screen> |
| 7637 |
</para> |
| 7638 |
|
| 7639 |
<para>Set <varname>$IFS</varname> to eliminate whitespace |
| 7640 |
in <link linkend="pathnameref">pathnames</link>. |
| 7641 |
<programlisting>IFS="$(printf '\n\t')" # Per David Wheeler.</programlisting> |
| 7642 |
</para> |
| 7643 |
|
| 7644 |
<caution><para><varname>$IFS</varname> does not handle whitespace |
| 7645 |
the same as it does other characters. |
| 7646 |
|
| 7647 |
<example id="ifsh"> |
| 7648 |
<title>$IFS and whitespace</title> |
| 7649 |
<programlisting>&ifsh;</programlisting> |
| 7650 |
</example> |
| 7651 |
|
| 7652 |
</para></caution> |
| 7653 |
|
| 7654 |
<para>(Many thanks, Stéphane Chazelas, for clarification |
| 7655 |
and above examples.)</para> |
| 7656 |
|
| 7657 |
<para>See also <xref linkend="isspammer">, <xref |
| 7658 |
linkend="bingrep">, and <xref linkend="mailboxgrep"> |
| 7659 |
for instructive examples of using |
| 7660 |
<varname>$IFS</varname>.</para> |
| 7661 |
|
| 7662 |
</listitem> |
| 7663 |
</varlistentry> |
| 7664 |
|
| 7665 |
<varlistentry> |
| 7666 |
<term><varname>$IGNOREEOF</varname></term> |
| 7667 |
<indexterm> |
| 7668 |
<primary>$IGNOREEOF</primary> |
| 7669 |
</indexterm> |
| 7670 |
<indexterm> |
| 7671 |
<primary>variable</primary> |
| 7672 |
<secondary>$IGNOREEOF</secondary> |
| 7673 |
</indexterm> |
| 7674 |
<indexterm> |
| 7675 |
<primary>Ignore EOF</primary> |
| 7676 |
</indexterm> |
| 7677 |
<listitem><para>Ignore EOF: how many end-of-files (control-D) |
| 7678 |
the shell will ignore before logging out.</para> |
| 7679 |
</listitem> |
| 7680 |
</varlistentry> |
| 7681 |
|
| 7682 |
<varlistentry> |
| 7683 |
<term><varname>$LC_COLLATE</varname></term> |
| 7684 |
<indexterm> |
| 7685 |
<primary>$LC_COLLATE</primary> |
| 7686 |
</indexterm> |
| 7687 |
<indexterm> |
| 7688 |
<primary>variable</primary> |
| 7689 |
<secondary>$LC_COLLATE</secondary> |
| 7690 |
</indexterm> |
| 7691 |
<indexterm> |
| 7692 |
<primary>lowercase collate</primary> |
| 7693 |
</indexterm> |
| 7694 |
<listitem> |
| 7695 |
|
| 7696 |
<para>Often set in the <link |
| 7697 |
linkend="sample-bashrc"<filename>.bashrc</filename></link> |
| 7698 |
or <filename>/etc/profile</filename> files, this |
| 7699 |
variable controls collation order in filename |
| 7700 |
expansion and pattern matching. If mishandled, |
| 7701 |
<varname>LC_COLLATE</varname> can cause unexpected results in |
| 7702 |
<link linkend="globbingref">filename globbing</link>.</para> |
| 7703 |
|
| 7704 |
<note><para>As of version 2.05 of Bash, |
| 7705 |
filename globbing no longer distinguishes between lowercase |
| 7706 |
and uppercase letters in a character range between |
| 7707 |
brackets. For example, <command>ls [A-M]*</command> |
| 7708 |
would match both <filename>File1.txt</filename> |
| 7709 |
and <filename>file1.txt</filename>. To revert to |
| 7710 |
the customary behavior of bracket matching, set |
| 7711 |
<varname>LC_COLLATE</varname> to <option>C</option> |
| 7712 |
by an <userinput>export LC_COLLATE=C</userinput> |
| 7713 |
in <filename>/etc/profile</filename> and/or |
| 7714 |
<filename>~/.bashrc</filename>.</para></note> |
| 7715 |
|
| 7716 |
</listitem> |
| 7717 |
</varlistentry> |
| 7718 |
|
| 7719 |
<varlistentry> |
| 7720 |
<term><varname>$LC_CTYPE</varname></term> |
| 7721 |
<indexterm> |
| 7722 |
<primary>$LC_CTYPE</primary> |
| 7723 |
</indexterm> |
| 7724 |
<indexterm> |
| 7725 |
<primary>variable</primary> |
| 7726 |
<secondary>$LC_CTYPE</secondary> |
| 7727 |
</indexterm> |
| 7728 |
<indexterm> |
| 7729 |
<primary>lowercase character type</primary> |
| 7730 |
</indexterm> |
| 7731 |
<listitem> |
| 7732 |
|
| 7733 |
<para>This internal variable controls character interpretation |
| 7734 |
in <link linkend="globbingref">globbing</link> and pattern |
| 7735 |
matching.</para> |
| 7736 |
|
| 7737 |
</listitem> |
| 7738 |
</varlistentry> |
| 7739 |
|
| 7740 |
<varlistentry> |
| 7741 |
<term><anchor id="linenoref"><varname>$LINENO</varname></term> |
| 7742 |
<indexterm> |
| 7743 |
<primary>$LINENO</primary> |
| 7744 |
</indexterm> |
| 7745 |
<indexterm> |
| 7746 |
<primary>variable</primary> |
| 7747 |
<secondary>$LINENO</secondary> |
| 7748 |
</indexterm> |
| 7749 |
<indexterm> |
| 7750 |
<primary>line number</primary> |
| 7751 |
</indexterm> |
| 7752 |
<listitem><para>This variable is the line number of the shell |
| 7753 |
script in which this variable appears. It has significance only |
| 7754 |
within the script in which it appears, and is chiefly useful for |
| 7755 |
debugging purposes.</para> |
| 7756 |
<para><programlisting># *** BEGIN DEBUG BLOCK *** |
| 7757 |
last_cmd_arg=$_ # Save it. |
| 7758 |
|
| 7759 |
echo "At line number $LINENO, variable \"v1\" = $v1" |
| 7760 |
echo "Last command argument processed = $last_cmd_arg" |
| 7761 |
# *** END DEBUG BLOCK ***</programlisting></para> |
| 7762 |
|
| 7763 |
</listitem> |
| 7764 |
</varlistentry> |
| 7765 |
|
| 7766 |
<varlistentry> |
| 7767 |
<term><anchor id="machtyperef"><varname>$MACHTYPE</varname></term> |
| 7768 |
<indexterm> |
| 7769 |
<primary>$MACHTYPE</primary> |
| 7770 |
</indexterm> |
| 7771 |
<indexterm> |
| 7772 |
<primary>variable</primary> |
| 7773 |
<secondary>$MACHTYPE</secondary> |
| 7774 |
</indexterm> |
| 7775 |
<indexterm> |
| 7776 |
<primary>machine type</primary> |
| 7777 |
</indexterm> |
| 7778 |
<listitem><para>machine type</para> |
| 7779 |
<para>Identifies the system hardware.</para> |
| 7780 |
<screen><prompt>bash$ </prompt><userinput>echo $MACHTYPE</userinput> |
| 7781 |
<computeroutput>i686</computeroutput></screen> |
| 7782 |
|
| 7783 |
</listitem> |
| 7784 |
</varlistentry> |
| 7785 |
|
| 7786 |
<varlistentry> |
| 7787 |
<term><anchor id="oldpwd"><varname>$OLDPWD</varname></term> |
| 7788 |
<indexterm> |
| 7789 |
<primary>$OLDPWD</primary> |
| 7790 |
</indexterm> |
| 7791 |
<indexterm> |
| 7792 |
<primary>variable</primary> |
| 7793 |
<secondary>$OLDPWD</secondary> |
| 7794 |
</indexterm> |
| 7795 |
<indexterm> |
| 7796 |
<primary>previous working directory</primary> |
| 7797 |
</indexterm> |
| 7798 |
<indexterm> |
| 7799 |
<primary>directory</primary> |
| 7800 |
<secondary>working</secondary> |
| 7801 |
</indexterm> |
| 7802 |
<listitem><para>Old working directory |
| 7803 |
(<quote>OLD-Print-Working-Directory</quote>, |
| 7804 |
previous directory you were in).</para> |
| 7805 |
</listitem> |
| 7806 |
</varlistentry> |
| 7807 |
|
| 7808 |
<varlistentry> |
| 7809 |
<term><varname>$OSTYPE</varname></term> |
| 7810 |
<indexterm> |
| 7811 |
<primary>$OSTYPE</primary> |
| 7812 |
</indexterm> |
| 7813 |
<indexterm> |
| 7814 |
<primary>variable</primary> |
| 7815 |
<secondary>$OSTYPE</secondary> |
| 7816 |
</indexterm> |
| 7817 |
<indexterm> |
| 7818 |
<primary>os type</primary> |
| 7819 |
</indexterm> |
| 7820 |
<listitem><para>operating system type</para> |
| 7821 |
<screen><prompt>bash$ </prompt><userinput>echo $OSTYPE</userinput> |
| 7822 |
<computeroutput>linux</computeroutput></screen> |
| 7823 |
|
| 7824 |
</listitem> |
| 7825 |
</varlistentry> |
| 7826 |
|
| 7827 |
<varlistentry> |
| 7828 |
<term><anchor id="pathref"><varname>$PATH</varname></term> |
| 7829 |
<indexterm> |
| 7830 |
<primary>$PATH</primary> |
| 7831 |
</indexterm> |
| 7832 |
<indexterm> |
| 7833 |
<primary>variable</primary> |
| 7834 |
<secondary>$PATH</secondary> |
| 7835 |
</indexterm> |
| 7836 |
<indexterm> |
| 7837 |
<primary>path to binaries</primary> |
| 7838 |
</indexterm> |
| 7839 |
<listitem><para>Path to binaries, usually |
| 7840 |
<filename class="directory">/usr/bin/</filename>, |
| 7841 |
<filename class="directory">/usr/X11R6/bin/</filename>, |
| 7842 |
<filename class="directory">/usr/local/bin</filename>, etc.</para> |
| 7843 |
|
| 7844 |
<para>When given a command, the shell automatically does |
| 7845 |
a hash table search on the directories listed in the |
| 7846 |
<firstterm>path</firstterm> for the executable. The path |
| 7847 |
is stored in the <link linkend="envref">environmental |
| 7848 |
variable</link>, <varname>$PATH</varname>, a list |
| 7849 |
of directories, separated by colons. Normally, |
| 7850 |
the system stores the <varname>$PATH</varname> |
| 7851 |
definition in <filename>/etc/profile</filename> |
| 7852 |
and/or <link |
| 7853 |
linkend="sample-bashrc"><filename>~/.bashrc</filename></link> |
| 7854 |
(see <xref linkend="files">).</para> |
| 7855 |
|
| 7856 |
<para><screen><prompt>bash$ </prompt><command>echo $PATH</command> |
| 7857 |
<computeroutput>/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin:/sbin:/usr/sbin</computeroutput></screen> |
| 7858 |
</para> |
| 7859 |
|
| 7860 |
<para><userinput>PATH=${PATH}:/opt/bin</userinput> appends |
| 7861 |
the <filename class="directory">/opt/bin</filename> |
| 7862 |
directory to the current path. In a script, it may be |
| 7863 |
expedient to temporarily add a directory to the path |
| 7864 |
in this way. When the script exits, this restores the |
| 7865 |
original <varname>$PATH</varname> (a child process, such |
| 7866 |
as a script, may not change the environment of the parent |
| 7867 |
process, the shell).</para> |
| 7868 |
|
| 7869 |
<para><anchor id="currentwdref"></para> |
| 7870 |
<note><para>The current <quote>working directory</quote>, |
| 7871 |
<filename class="directory">./</filename>, is usually |
| 7872 |
omitted from the <varname>$PATH</varname> as a security |
| 7873 |
measure.</para></note> |
| 7874 |
|
| 7875 |
</listitem> |
| 7876 |
</varlistentry> |
| 7877 |
|
| 7878 |
<varlistentry> |
| 7879 |
<term><anchor |
| 7880 |
id="pipestatusref"><varname>$PIPESTATUS</varname></term> |
| 7881 |
<indexterm> |
| 7882 |
<primary>$PIPESTATUS</primary> |
| 7883 |
</indexterm> |
| 7884 |
<indexterm> |
| 7885 |
<primary>variable</primary> |
| 7886 |
<secondary>pipe</secondary> |
| 7887 |
</indexterm> |
| 7888 |
<listitem> |
| 7889 |
<para><link linkend="arrayref">Array</link> variable holding |
| 7890 |
<link linkend="exitstatusref">exit status</link>(es) of |
| 7891 |
last executed <firstterm>foreground</firstterm> <link |
| 7892 |
linkend="piperef">pipe</link>.</para> |
| 7893 |
|
| 7894 |
<para> |
| 7895 |
<screen> |
| 7896 |
<prompt>bash$ </prompt><userinput>echo $PIPESTATUS</userinput> |
| 7897 |
<computeroutput>0</computeroutput> |
| 7898 |
|
| 7899 |
<prompt>bash$ </prompt><userinput>ls -al | bogus_command</userinput> |
| 7900 |
<computeroutput>bash: bogus_command: command not found</computeroutput> |
| 7901 |
<prompt>bash$ </prompt><userinput>echo ${PIPESTATUS[1]}</userinput> |
| 7902 |
<computeroutput>127</computeroutput> |
| 7903 |
|
| 7904 |
<prompt>bash$ </prompt><userinput>ls -al | bogus_command</userinput> |
| 7905 |
<computeroutput>bash: bogus_command: command not found</computeroutput> |
| 7906 |
<prompt>bash$ </prompt><userinput>echo $?</userinput> |
| 7907 |
<computeroutput>127</computeroutput> |
| 7908 |
</screen> |
| 7909 |
</para> |
| 7910 |
|
| 7911 |
|
| 7912 |
<para>The members of the <varname>$PIPESTATUS</varname> |
| 7913 |
array hold the exit status of each respective command |
| 7914 |
executed in a pipe. <varname>$PIPESTATUS[0]</varname> |
| 7915 |
holds the exit status of the first command in the pipe, |
| 7916 |
<varname>$PIPESTATUS[1]</varname> the exit status of |
| 7917 |
the second command, and so on.</para> |
| 7918 |
|
| 7919 |
|
| 7920 |
<caution> |
| 7921 |
<para> |
| 7922 |
The <varname>$PIPESTATUS</varname> variable |
| 7923 |
may contain an erroneous <errorcode>0</errorcode> value |
| 7924 |
in a login shell (in releases prior to 3.0 of Bash). |
| 7925 |
</para> |
| 7926 |
|
| 7927 |
<para> |
| 7928 |
<screen> |
| 7929 |
<prompt>tcsh% </prompt><userinput>bash</userinput> |
| 7930 |
|
| 7931 |
<prompt>bash$ </prompt><userinput>who | grep nobody | sort</userinput> |
| 7932 |
<prompt>bash$ </prompt><userinput>echo ${PIPESTATUS[*]}</userinput> |
| 7933 |
<computeroutput>0</computeroutput> |
| 7934 |
</screen> |
| 7935 |
</para> |
| 7936 |
|
| 7937 |
|
| 7938 |
<para> |
| 7939 |
The above lines contained in a script would produce the expected |
| 7940 |
<computeroutput>0 1 0</computeroutput> output. |
| 7941 |
</para> |
| 7942 |
|
| 7943 |
<para> |
| 7944 |
Thank you, Wayne Pollock for pointing this out and supplying the |
| 7945 |
above example. |
| 7946 |
</para> |
| 7947 |
|
| 7948 |
</caution> |
| 7949 |
|
| 7950 |
|
| 7951 |
<note> |
| 7952 |
|
| 7953 |
<para>The <varname>$PIPESTATUS</varname> variable gives |
| 7954 |
unexpected results in some contexts.</para> |
| 7955 |
|
| 7956 |
<para> |
| 7957 |
<screen> |
| 7958 |
<prompt>bash$ </prompt><userinput>echo $BASH_VERSION</userinput> |
| 7959 |
<computeroutput>3.00.14(1)-release</computeroutput> |
| 7960 |
|
| 7961 |
<prompt>bash$ </prompt><userinput>$ ls | bogus_command | wc</userinput> |
| 7962 |
<computeroutput>bash: bogus_command: command not found |
| 7963 |
0 0 0</computeroutput> |
| 7964 |
|
| 7965 |
<prompt>bash$ </prompt><userinput>echo ${PIPESTATUS[@]}</userinput> |
| 7966 |
<computeroutput>141 127 0</computeroutput> |
| 7967 |
</screen> |
| 7968 |
</para> |
| 7969 |
|
| 7970 |
<para>Chet Ramey attributes the above output to the behavior of |
| 7971 |
<link linkend="lsref">ls</link>. If <firstterm>ls</firstterm> |
| 7972 |
writes to a <firstterm>pipe</firstterm> whose output is not |
| 7973 |
read, then <replaceable>SIGPIPE</replaceable> kills it, |
| 7974 |
and its <link linkend="exitstatusref">exit status</link> |
| 7975 |
is <returnvalue>141</returnvalue>. Otherwise |
| 7976 |
its exit status is <returnvalue>0</returnvalue>, |
| 7977 |
as expected. This likewise is the case for <link |
| 7978 |
linkend="trref">tr</link>.</para> |
| 7979 |
|
| 7980 |
</note> |
| 7981 |
|
| 7982 |
<note> |
| 7983 |
<para><varname>$PIPESTATUS</varname> is a |
| 7984 |
<quote>volatile</quote> variable. It needs to be |
| 7985 |
captured immediately after the pipe in question, before |
| 7986 |
any other command intervenes.</para> |
| 7987 |
|
| 7988 |
<para> |
| 7989 |
<screen> |
| 7990 |
<prompt>bash$ </prompt><userinput>$ ls | bogus_command | wc</userinput> |
| 7991 |
<computeroutput>bash: bogus_command: command not found |
| 7992 |
0 0 0</computeroutput> |
| 7993 |
|
| 7994 |
<prompt>bash$ </prompt><userinput>echo ${PIPESTATUS[@]}</userinput> |
| 7995 |
<computeroutput>0 127 0</computeroutput> |
| 7996 |
|
| 7997 |
<prompt>bash$ </prompt><userinput>echo ${PIPESTATUS[@]}</userinput> |
| 7998 |
<computeroutput>0</computeroutput> |
| 7999 |
</screen> |
| 8000 |
</para> |
| 8001 |
</note> |
| 8002 |
|
| 8003 |
<note> |
| 8004 |
<para>The <link linkend="pipefailref">pipefail option</link> |
| 8005 |
may be useful in cases where |
| 8006 |
<varname>$PIPESTATUS</varname> does not give the desired |
| 8007 |
information.</para> |
| 8008 |
</note> |
| 8009 |
|
| 8010 |
|
| 8011 |
</listitem> |
| 8012 |
</varlistentry> |
| 8013 |
|
| 8014 |
<varlistentry> |
| 8015 |
<term><anchor id="ppidref"><varname>$PPID</varname></term> |
| 8016 |
<indexterm> |
| 8017 |
<primary>$PPID</primary> |
| 8018 |
</indexterm> |
| 8019 |
<indexterm> |
| 8020 |
<primary>variable</primary> |
| 8021 |
<secondary>$PPID</secondary> |
| 8022 |
</indexterm> |
| 8023 |
<indexterm> |
| 8024 |
<primary>process ID</primary> |
| 8025 |
</indexterm> |
| 8026 |
<listitem><para></para> |
| 8027 |
<para>The <varname>$PPID</varname> of a process is |
| 8028 |
the process ID (<varname>pid</varname>) of its parent process. |
| 8029 |
|
| 8030 |
<footnote> |
| 8031 |
<para>The PID of the currently running script is |
| 8032 |
<varname>$$</varname>, of course.</para> |
| 8033 |
</footnote> |
| 8034 |
</para> |
| 8035 |
|
| 8036 |
<para>Compare this with the <link |
| 8037 |
linkend="pidofref">pidof</link> command.</para> |
| 8038 |
|
| 8039 |
</listitem> |
| 8040 |
</varlistentry> |
| 8041 |
|
| 8042 |
<varlistentry> |
| 8043 |
<term><varname>$PROMPT_COMMAND</varname></term> |
| 8044 |
<indexterm> |
| 8045 |
<primary>$PROMPT_COMMAND</primary> |
| 8046 |
</indexterm> |
| 8047 |
<indexterm> |
| 8048 |
<primary>variable</primary> |
| 8049 |
<secondary>prompt</secondary> |
| 8050 |
</indexterm> |
| 8051 |
<listitem> |
| 8052 |
<para>A variable holding a command to be executed |
| 8053 |
just before the primary prompt, <varname>$PS1</varname> |
| 8054 |
is to be displayed.</para> |
| 8055 |
</listitem> |
| 8056 |
</varlistentry> |
| 8057 |
|
| 8058 |
<varlistentry> |
| 8059 |
<term><anchor id="ps1ref"><varname>$PS1</varname></term> |
| 8060 |
<indexterm> |
| 8061 |
<primary>$PS1</primary> |
| 8062 |
</indexterm> |
| 8063 |
<indexterm> |
| 8064 |
<primary>variable</primary> |
| 8065 |
<secondary>$PS1</secondary> |
| 8066 |
</indexterm> |
| 8067 |
<indexterm> |
| 8068 |
<primary>prompt</primary> |
| 8069 |
</indexterm> |
| 8070 |
<listitem><para>This is the main prompt, seen at the command-line.</para> |
| 8071 |
</listitem> |
| 8072 |
</varlistentry> |
| 8073 |
|
| 8074 |
<varlistentry> |
| 8075 |
<term><anchor id="secpromptref"><varname>$PS2</varname></term> |
| 8076 |
<indexterm> |
| 8077 |
<primary>$PS2</primary> |
| 8078 |
</indexterm> |
| 8079 |
<indexterm> |
| 8080 |
<primary>variable</primary> |
| 8081 |
<secondary>$PS2</secondary> |
| 8082 |
</indexterm> |
| 8083 |
<indexterm> |
| 8084 |
<primary>prompt</primary> |
| 8085 |
<secondary>secondary</secondary> |
| 8086 |
</indexterm> |
| 8087 |
<listitem> |
| 8088 |
<para>The secondary prompt, seen when additional input is |
| 8089 |
expected. It displays as <quote>></quote>.</para> |
| 8090 |
</listitem> |
| 8091 |
</varlistentry> |
| 8092 |
|
| 8093 |
<varlistentry> |
| 8094 |
<term><varname>$PS3</varname></term> |
| 8095 |
<indexterm> |
| 8096 |
<primary>$PS3</primary> |
| 8097 |
</indexterm> |
| 8098 |
<indexterm> |
| 8099 |
<primary>variable</primary> |
| 8100 |
<secondary>$PS3</secondary> |
| 8101 |
</indexterm> |
| 8102 |
<indexterm> |
| 8103 |
<primary>prompt</primary> |
| 8104 |
<secondary>tertiary</secondary> |
| 8105 |
</indexterm> |
| 8106 |
<listitem><para>The tertiary prompt, displayed in a |
| 8107 |
<link linkend="selectref">select</link> loop (see <xref |
| 8108 |
linkend="ex31">).</para> |
| 8109 |
</listitem> |
| 8110 |
</varlistentry> |
| 8111 |
|
| 8112 |
<varlistentry> |
| 8113 |
<term><varname>$PS4</varname></term> |
| 8114 |
<indexterm> |
| 8115 |
<primary>$PS4</primary> |
| 8116 |
</indexterm> |
| 8117 |
<indexterm> |
| 8118 |
<primary>variable</primary> |
| 8119 |
<secondary>$PS4</secondary> |
| 8120 |
</indexterm> |
| 8121 |
<indexterm> |
| 8122 |
<primary>prompt</primary> |
| 8123 |
<secondary>quartenary</secondary> |
| 8124 |
</indexterm> |
| 8125 |
<listitem> |
| 8126 |
<para>The quartenary prompt, shown at the beginning of |
| 8127 |
each line of output when invoking a script with the |
| 8128 |
<token>-x</token> <emphasis>[verbose trace]</emphasis> |
| 8129 |
<link linkend="optionsref">option</link>. It displays as |
| 8130 |
<quote>+</quote>.</para> |
| 8131 |
|
| 8132 |
<para>As a debugging aid, it may be useful to embed diagnostic |
| 8133 |
information in <varname>$PS4</varname>. |
| 8134 |
<programlisting>P4='$(read time junk < /proc/$$/schedstat; echo "@@@ $time @@@ " )' |
| 8135 |
# Per suggestion by Erik Brandsberg. |
| 8136 |
set -x |
| 8137 |
# Various commands follow ...</programlisting></para> |
| 8138 |
|
| 8139 |
</listitem> |
| 8140 |
</varlistentry> |
| 8141 |
|
| 8142 |
|
| 8143 |
<varlistentry> |
| 8144 |
<term><anchor id="pwdref"><varname>$PWD</varname></term> |
| 8145 |
<indexterm> |
| 8146 |
<primary>$PWD</primary> |
| 8147 |
</indexterm> |
| 8148 |
<indexterm> |
| 8149 |
<primary>variable</primary> |
| 8150 |
<secondary>$PWD</secondary> |
| 8151 |
</indexterm> |
| 8152 |
<indexterm> |
| 8153 |
<primary>working directory</primary> |
| 8154 |
</indexterm> |
| 8155 |
<indexterm> |
| 8156 |
<primary>directory</primary> |
| 8157 |
<secondary>working</secondary> |
| 8158 |
</indexterm> |
| 8159 |
<listitem> |
| 8160 |
<para>Working directory (directory you are in at the time)</para> |
| 8161 |
<para>This is the analog to the <link linkend="pwd2ref">pwd</link> |
| 8162 |
builtin command.</para> |
| 8163 |
<para><programlisting>&wipedir;</programlisting></para> |
| 8164 |
</listitem> |
| 8165 |
</varlistentry> |
| 8166 |
|
| 8167 |
<varlistentry> |
| 8168 |
<term><anchor id="replyref"><varname>$REPLY</varname></term> |
| 8169 |
<indexterm> |
| 8170 |
<primary>$REPLY</primary> |
| 8171 |
</indexterm> |
| 8172 |
<indexterm> |
| 8173 |
<primary>variable</primary> |
| 8174 |
<secondary>$REPLY</secondary> |
| 8175 |
</indexterm> |
| 8176 |
<indexterm> |
| 8177 |
<primary>default value of read</primary> |
| 8178 |
</indexterm> |
| 8179 |
<indexterm> |
| 8180 |
<primary>reply</primary> |
| 8181 |
<secondary>read</secondary> |
| 8182 |
</indexterm> |
| 8183 |
<listitem><para>The default value when a variable is not |
| 8184 |
supplied to <link linkend="readref">read</link>. Also |
| 8185 |
applicable to <link linkend="selectref">select</link> menus, |
| 8186 |
but only supplies the item number of the variable chosen, |
| 8187 |
not the value of the variable itself.</para> |
| 8188 |
<para><programlisting>&reply;</programlisting></para> |
| 8189 |
</listitem> |
| 8190 |
</varlistentry> |
| 8191 |
|
| 8192 |
<varlistentry> |
| 8193 |
<term><varname>$SECONDS</varname></term> |
| 8194 |
<indexterm> |
| 8195 |
<primary>$SECONDS</primary> |
| 8196 |
</indexterm> |
| 8197 |
<indexterm> |
| 8198 |
<primary>variable</primary> |
| 8199 |
<secondary>$SECONDS</secondary> |
| 8200 |
</indexterm> |
| 8201 |
<indexterm> |
| 8202 |
<primary>seconds execution time</primary> |
| 8203 |
</indexterm> |
| 8204 |
<indexterm> |
| 8205 |
<primary>runtime</primary> |
| 8206 |
<secondary>seconds</secondary> |
| 8207 |
</indexterm> |
| 8208 |
<listitem><para>The number of seconds the script has been running.</para> |
| 8209 |
<para><programlisting>&seconds;</programlisting></para> |
| 8210 |
</listitem> |
| 8211 |
</varlistentry> |
| 8212 |
|
| 8213 |
<varlistentry> |
| 8214 |
<term><varname>$SHELLOPTS</varname></term> |
| 8215 |
<indexterm> |
| 8216 |
<primary>$SHELLOPTS</primary> |
| 8217 |
</indexterm> |
| 8218 |
<indexterm> |
| 8219 |
<primary>variable</primary> |
| 8220 |
<secondary>$SHELLOPTS</secondary> |
| 8221 |
</indexterm> |
| 8222 |
<indexterm> |
| 8223 |
<primary>shell options</primary> |
| 8224 |
</indexterm> |
| 8225 |
<listitem> |
| 8226 |
<para>The list of enabled shell <link |
| 8227 |
linkend="optionsref">options</link>, a readonly variable. |
| 8228 |
<screen><prompt>bash$ </prompt><userinput>echo $SHELLOPTS</userinput> |
| 8229 |
<computeroutput>braceexpand:hashall:histexpand:monitor:history:interactive-comments:emacs</computeroutput> |
| 8230 |
</screen> |
| 8231 |
</para> |
| 8232 |
</listitem> |
| 8233 |
</varlistentry> |
| 8234 |
|
| 8235 |
<varlistentry> |
| 8236 |
<term><anchor id="shlvlref"><varname>$SHLVL</varname></term> |
| 8237 |
<indexterm> |
| 8238 |
<primary>$SHLVL</primary> |
| 8239 |
</indexterm> |
| 8240 |
<indexterm> |
| 8241 |
<primary>variable</primary> |
| 8242 |
<secondary>$SHLVL</secondary> |
| 8243 |
</indexterm> |
| 8244 |
<indexterm> |
| 8245 |
<primary>shell level</primary> |
| 8246 |
</indexterm> |
| 8247 |
<listitem> |
| 8248 |
|
| 8249 |
<para>Shell level, how deeply Bash is nested. |
| 8250 |
<footnote><para> |
| 8251 |
Somewhat analogous to <link |
| 8252 |
linkend="recursionref">recursion</link>, in this context |
| 8253 |
<firstterm>nesting</firstterm> refers to a pattern |
| 8254 |
embedded within a larger pattern. One of the definitions |
| 8255 |
of <firstterm>nest</firstterm>, according to the 1913 |
| 8256 |
edition of <emphasis>Webster's Dictionary</emphasis>, |
| 8257 |
illustrates this beautifully: <quote><emphasis>A collection of |
| 8258 |
boxes, cases, or the like, of graduated size, each put |
| 8259 |
within the one next larger.</emphasis></quote> |
| 8260 |
</para></footnote> |
| 8261 |
If, at the command-line, $SHLVL is 1, then in a script it |
| 8262 |
will increment to 2.</para> |
| 8263 |
|
| 8264 |
<note><para>This variable is <link linkend = "subshnlevref"> |
| 8265 |
<emphasis>not</emphasis> affected by |
| 8266 |
subshells</link>. Use <link |
| 8267 |
linkend="bashsubshellref">$BASH_SUBSHELL</link> when you |
| 8268 |
need an indication of subshell nesting.</para></note> |
| 8269 |
|
| 8270 |
|
| 8271 |
</listitem> |
| 8272 |
</varlistentry> |
| 8273 |
|
| 8274 |
<varlistentry> |
| 8275 |
<term><anchor id="tmoutref"><varname>$TMOUT</varname></term> |
| 8276 |
<indexterm> |
| 8277 |
<primary>$TMOUT</primary> |
| 8278 |
</indexterm> |
| 8279 |
<indexterm> |
| 8280 |
<primary>variable</primary> |
| 8281 |
<secondary>$TMOUT</secondary> |
| 8282 |
</indexterm> |
| 8283 |
<indexterm> |
| 8284 |
<primary>timeout interval</primary> |
| 8285 |
</indexterm> |
| 8286 |
<listitem><para>If the <replaceable>$TMOUT</replaceable> |
| 8287 |
environmental variable is set to a non-zero value |
| 8288 |
<varname>time</varname>, then the shell prompt will time out |
| 8289 |
after <varname>$time</varname> seconds. This will cause a |
| 8290 |
logout.</para> |
| 8291 |
|
| 8292 |
|
| 8293 |
<para>As of version 2.05b of Bash, it is now possible to use |
| 8294 |
<replaceable>$TMOUT</replaceable> in a script in combination |
| 8295 |
with <link linkend="readref">read</link>.</para> |
| 8296 |
|
| 8297 |
<para> |
| 8298 |
<programlisting># Works in scripts for Bash, versions 2.05b and later. |
| 8299 |
|
| 8300 |
TMOUT=3 # Prompt times out at three seconds. |
| 8301 |
|
| 8302 |
echo "What is your favorite song?" |
| 8303 |
echo "Quickly now, you only have $TMOUT seconds to answer!" |
| 8304 |
read song |
| 8305 |
|
| 8306 |
if [ -z "$song" ] |
| 8307 |
then |
| 8308 |
song="(no answer)" |
| 8309 |
# Default response. |
| 8310 |
fi |
| 8311 |
|
| 8312 |
echo "Your favorite song is $song."</programlisting> |
| 8313 |
</para> |
| 8314 |
|
| 8315 |
|
| 8316 |
<para><anchor id="timingloop"></para> |
| 8317 |
<para>There are other, more complex, ways of implementing |
| 8318 |
timed input in a script. One alternative is to set up |
| 8319 |
a timing loop to signal the script when it times out. |
| 8320 |
This also requires a signal handling routine to <link |
| 8321 |
linkend="trapref1">trap</link> (see <xref linkend="ex76">) |
| 8322 |
the interrupt generated by the timing loop (whew!).</para> |
| 8323 |
|
| 8324 |
<example id="tmdin"> |
| 8325 |
<title>Timed Input</title> |
| 8326 |
<programlisting>&tmdin;</programlisting> |
| 8327 |
</example> |
| 8328 |
|
| 8329 |
<para><anchor id="sttyto"></para> |
| 8330 |
<para>An alternative is using <link |
| 8331 |
linkend="sttyref">stty</link>.</para> |
| 8332 |
|
| 8333 |
<example id="timeout"> |
| 8334 |
<title>Once more, timed input</title> |
| 8335 |
<programlisting>&timeout;</programlisting> |
| 8336 |
</example> |
| 8337 |
|
| 8338 |
<para>Perhaps the simplest method is using the |
| 8339 |
<option>-t</option> option to <link |
| 8340 |
linkend="readref">read</link>.</para> |
| 8341 |
|
| 8342 |
<example id="tout"> |
| 8343 |
<title>Timed <firstterm>read</firstterm></title> |
| 8344 |
<programlisting>&tout;</programlisting> |
| 8345 |
</example> |
| 8346 |
|
| 8347 |
|
| 8348 |
</listitem> |
| 8349 |
</varlistentry> |
| 8350 |
|
| 8351 |
|
| 8352 |
<varlistentry> |
| 8353 |
<term><anchor id="uidref"><varname>$UID</varname></term> |
| 8354 |
<indexterm> |
| 8355 |
<primary>$UID</primary> |
| 8356 |
</indexterm> |
| 8357 |
<indexterm> |
| 8358 |
<primary>variable</primary> |
| 8359 |
<secondary>$UID</secondary> |
| 8360 |
</indexterm> |
| 8361 |
<indexterm> |
| 8362 |
<primary>user ID</primary> |
| 8363 |
</indexterm> |
| 8364 |
<listitem><para>User ID number</para> |
| 8365 |
|
| 8366 |
<para>Current user's user identification number, as |
| 8367 |
recorded in <link |
| 8368 |
linkend="datafilesref1"><filename>/etc/passwd</filename></link></para> |
| 8369 |
|
| 8370 |
<para>This is the current user's real id, even if she has |
| 8371 |
temporarily assumed another identity through <link |
| 8372 |
linkend="suref">su</link>. <varname>$UID</varname> is a |
| 8373 |
readonly variable, not subject to change from the command |
| 8374 |
line or within a script, and is the counterpart to the |
| 8375 |
<link linkend="idref">id</link> builtin.</para> |
| 8376 |
|
| 8377 |
<example id="amiroot"> |
| 8378 |
<title>Am I root?</title> |
| 8379 |
<programlisting>&amiroot;</programlisting> |
| 8380 |
</example> |
| 8381 |
|
| 8382 |
<para>See also <xref linkend="ex2">.</para> |
| 8383 |
|
| 8384 |
|
| 8385 |
<!-- Nest note within last entry --> |
| 8386 |
<note> |
| 8387 |
<para>The variables <varname>$ENV</varname>, |
| 8388 |
<varname>$LOGNAME</varname>, <varname>$MAIL</varname>, |
| 8389 |
<varname>$TERM</varname>, <varname>$USER</varname>, and |
| 8390 |
<varname>$USERNAME</varname> are <emphasis>not</emphasis> |
| 8391 |
Bash <link linkend="builtinref">builtins</link>. These are, |
| 8392 |
however, often set as <link |
| 8393 |
linkend="envref">environmental variables</link> in |
| 8394 |
one of the <link linkend="filesref1">Bash</link> or |
| 8395 |
<firstterm>login</firstterm> startup files. <anchor |
| 8396 |
id="shellvarref"><varname>$SHELL</varname>, |
| 8397 |
the name of the user's login shell, may be set from |
| 8398 |
<filename>/etc/passwd</filename> or in an <quote>init</quote> |
| 8399 |
script, and it is likewise not a Bash builtin.</para> |
| 8400 |
<para> |
| 8401 |
<screen> |
| 8402 |
<prompt>tcsh% </prompt><userinput>echo $LOGNAME</userinput> |
| 8403 |
<computeroutput>bozo</computeroutput> |
| 8404 |
<prompt>tcsh% </prompt><userinput>echo $SHELL</userinput> |
| 8405 |
<computeroutput>/bin/tcsh</computeroutput> |
| 8406 |
<prompt>tcsh% </prompt><userinput>echo $TERM</userinput> |
| 8407 |
<computeroutput>rxvt</computeroutput> |
| 8408 |
|
| 8409 |
<prompt>bash$ </prompt><userinput>echo $LOGNAME</userinput> |
| 8410 |
<computeroutput>bozo</computeroutput> |
| 8411 |
<prompt>bash$ </prompt><userinput>echo $SHELL</userinput> |
| 8412 |
<computeroutput>/bin/tcsh</computeroutput> |
| 8413 |
<prompt>bash$ </prompt><userinput>echo $TERM</userinput> |
| 8414 |
<computeroutput>rxvt</computeroutput> |
| 8415 |
</screen> |
| 8416 |
</para> |
| 8417 |
</note> |
| 8418 |
|
| 8419 |
<!-- Nest note after $USER --> |
| 8420 |
|
| 8421 |
</listitem> |
| 8422 |
</varlistentry> |
| 8423 |
|
| 8424 |
|
| 8425 |
</variablelist> |
| 8426 |
|
| 8427 |
<!-- Last entry of intrinsic BASH variables --> |
| 8428 |
|
| 8429 |
<variablelist id="posparmslist"> |
| 8430 |
<title>Positional Parameters</title> |
| 8431 |
|
| 8432 |
<varlistentry> |
| 8433 |
<term><anchor id="posparamref"><varname>$0</varname>, <varname>$1</varname>, |
| 8434 |
<varname>$2</varname>, etc.</term> |
| 8435 |
<indexterm> |
| 8436 |
<primary>$0</primary> |
| 8437 |
</indexterm> |
| 8438 |
<indexterm> |
| 8439 |
<primary>variable</primary> |
| 8440 |
<secondary>$0</secondary> |
| 8441 |
</indexterm> |
| 8442 |
<indexterm> |
| 8443 |
<primary>positional parameter</primary> |
| 8444 |
</indexterm> |
| 8445 |
<indexterm> |
| 8446 |
<primary>parameter</primary> |
| 8447 |
<secondary>positional</secondary> |
| 8448 |
</indexterm> |
| 8449 |
<listitem> |
| 8450 |
<para>Positional parameters, passed from command |
| 8451 |
line to script, passed to a function, or <link |
| 8452 |
linkend="setref">set</link> to a variable (see <xref |
| 8453 |
linkend="ex17"> and <xref linkend="ex34">)</para> |
| 8454 |
</listitem> |
| 8455 |
</varlistentry> |
| 8456 |
|
| 8457 |
<varlistentry> |
| 8458 |
<term><anchor id="clacountref"><varname>$#</varname></term> |
| 8459 |
<indexterm> |
| 8460 |
<primary>$#</primary> |
| 8461 |
</indexterm> |
| 8462 |
<indexterm> |
| 8463 |
<primary>variable</primary> |
| 8464 |
<secondary>$#</secondary> |
| 8465 |
</indexterm> |
| 8466 |
<indexterm> |
| 8467 |
<primary>positional parameter</primary> |
| 8468 |
<secondary>number of</secondary> |
| 8469 |
</indexterm> |
| 8470 |
<indexterm> |
| 8471 |
<primary>parameter</primary> |
| 8472 |
<secondary>positional</secondary> |
| 8473 |
<tertiary>number of</tertiary> |
| 8474 |
</indexterm> |
| 8475 |
<listitem><para>Number of command-line arguments |
| 8476 |
<footnote><para>The words <quote>argument</quote> |
| 8477 |
and <quote>parameter</quote> are often used |
| 8478 |
interchangeably. In the context of this document, they |
| 8479 |
have the same precise meaning: <emphasis>a variable passed |
| 8480 |
to a script or function.</emphasis></para></footnote> |
| 8481 |
or positional parameters (see <xref linkend="ex4">)</para> |
| 8482 |
</listitem> |
| 8483 |
</varlistentry> |
| 8484 |
|
| 8485 |
<varlistentry> |
| 8486 |
<term><anchor id="appref"><varname>$*</varname></term> |
| 8487 |
<indexterm> |
| 8488 |
<primary>$*</primary> |
| 8489 |
</indexterm> |
| 8490 |
<indexterm> |
| 8491 |
<primary>variable</primary> |
| 8492 |
<secondary>$*</secondary> |
| 8493 |
</indexterm> |
| 8494 |
<indexterm> |
| 8495 |
<primary>positional parameter</primary> |
| 8496 |
<secondary>all</secondary> |
| 8497 |
</indexterm> |
| 8498 |
<indexterm> |
| 8499 |
<primary>parameter</primary> |
| 8500 |
<secondary>positional</secondary> |
| 8501 |
<tertiary>all</tertiary> |
| 8502 |
</indexterm> |
| 8503 |
<listitem> |
| 8504 |
<para>All of the positional parameters, seen as a single word</para> |
| 8505 |
<note><para><quote><varname>$*</varname></quote> must be |
| 8506 |
quoted.</para></note> |
| 8507 |
</listitem> |
| 8508 |
</varlistentry> |
| 8509 |
|
| 8510 |
<varlistentry> |
| 8511 |
<term><anchor id="appref2"><varname>$@</varname></term> |
| 8512 |
<indexterm> |
| 8513 |
<primary>$@</primary> |
| 8514 |
</indexterm> |
| 8515 |
<indexterm> |
| 8516 |
<primary>variable</primary> |
| 8517 |
<secondary>$*</secondary> |
| 8518 |
</indexterm> |
| 8519 |
<indexterm> |
| 8520 |
<primary>positional parameter</primary> |
| 8521 |
<secondary>all</secondary> |
| 8522 |
</indexterm> |
| 8523 |
|
| 8524 |
<listitem> |
| 8525 |
|
| 8526 |
<para>Same as <token>$*</token>, but each parameter is a |
| 8527 |
quoted string, that is, the parameters are passed on |
| 8528 |
intact, without interpretation or expansion. This means, |
| 8529 |
among other things, that each parameter in the argument |
| 8530 |
list is seen as a separate word.</para> |
| 8531 |
|
| 8532 |
<note><para>Of course, <quote><varname>$@</varname></quote> |
| 8533 |
should be quoted.</para></note> |
| 8534 |
|
| 8535 |
<example id="arglist"> |
| 8536 |
<title><firstterm>arglist</firstterm>: Listing arguments |
| 8537 |
with $* and $@</title> |
| 8538 |
<programlisting>&arglist;</programlisting> |
| 8539 |
</example> |
| 8540 |
|
| 8541 |
<para>Following a <command>shift</command>, the |
| 8542 |
<varname>$@</varname> holds the remaining command-line |
| 8543 |
parameters, lacking the previous <varname>$1</varname>, |
| 8544 |
which was lost. |
| 8545 |
<programlisting>#!/bin/bash |
| 8546 |
# Invoke with ./scriptname 1 2 3 4 5 |
| 8547 |
|
| 8548 |
echo "$@" # 1 2 3 4 5 |
| 8549 |
shift |
| 8550 |
echo "$@" # 2 3 4 5 |
| 8551 |
shift |
| 8552 |
echo "$@" # 3 4 5 |
| 8553 |
|
| 8554 |
# Each "shift" loses parameter $1. |
| 8555 |
# "$@" then contains the remaining parameters.</programlisting> |
| 8556 |
</para> |
| 8557 |
|
| 8558 |
|
| 8559 |
<para>The <varname>$@</varname> special parameter finds |
| 8560 |
use as a tool for filtering input into shell scripts. The |
| 8561 |
<command>cat "$@"</command> construction accepts input |
| 8562 |
to a script either from <filename>stdin</filename> or |
| 8563 |
from files given as parameters to the script. See <xref |
| 8564 |
linkend="rot13"> and <xref linkend="cryptoquote">.</para> |
| 8565 |
|
| 8566 |
|
| 8567 |
<caution><para>The <varname>$*</varname> and <varname>$@</varname> |
| 8568 |
parameters sometimes display inconsistent and |
| 8569 |
puzzling behavior, depending on the setting of <link |
| 8570 |
linkend="ifsref">$IFS</link>.</para></caution> |
| 8571 |
|
| 8572 |
<example id="incompat"> |
| 8573 |
<title>Inconsistent <varname>$*</varname> and <varname>$@</varname> behavior</title> |
| 8574 |
<programlisting>&incompat;</programlisting> |
| 8575 |
</example> |
| 8576 |
|
| 8577 |
<note><para>The <command>$@</command> and <command>$*</command> |
| 8578 |
parameters differ only when between double quotes.</para></note> |
| 8579 |
|
| 8580 |
<example id="ifsempty"> |
| 8581 |
<title><varname>$*</varname> and <varname>$@</varname> when |
| 8582 |
<varname>$IFS</varname> is empty</title> |
| 8583 |
<programlisting>&ifsempty;</programlisting> |
| 8584 |
</example> |
| 8585 |
|
| 8586 |
</listitem> |
| 8587 |
</varlistentry> |
| 8588 |
|
| 8589 |
</variablelist> |
| 8590 |
|
| 8591 |
<variablelist id="otherspecparams"> |
| 8592 |
<title>Other Special Parameters</title> |
| 8593 |
|
| 8594 |
<varlistentry> |
| 8595 |
<term><anchor id="flpref"><varname>$-</varname></term> |
| 8596 |
<indexterm> |
| 8597 |
<primary>$-</primary> |
| 8598 |
</indexterm> |
| 8599 |
<indexterm> |
| 8600 |
<primary>variable</primary> |
| 8601 |
<secondary>$-</secondary> |
| 8602 |
</indexterm> |
| 8603 |
<indexterm> |
| 8604 |
<primary>flags</primary> |
| 8605 |
</indexterm> |
| 8606 |
<listitem> |
| 8607 |
<para>Flags passed to script (using <link |
| 8608 |
linkend="setref">set</link>). See <xref linkend="ex34">.</para> |
| 8609 |
<caution><para>This was originally a <firstterm>ksh</firstterm> |
| 8610 |
construct adopted into Bash, and unfortunately it does not |
| 8611 |
seem to work reliably in Bash scripts. One possible use |
| 8612 |
for it is to have a script <link linkend="iitest">self-test |
| 8613 |
whether it is interactive</link>.</para></caution> |
| 8614 |
</listitem> |
| 8615 |
</varlistentry> |
| 8616 |
|
| 8617 |
<varlistentry> |
| 8618 |
<term><anchor id="pidvarref"><varname>$!</varname></term> |
| 8619 |
<indexterm> |
| 8620 |
<primary>$!</primary> |
| 8621 |
</indexterm> |
| 8622 |
<indexterm> |
| 8623 |
<primary>variable</primary> |
| 8624 |
<secondary>$!</secondary> |
| 8625 |
</indexterm> |
| 8626 |
<indexterm> |
| 8627 |
<primary>PID</primary> |
| 8628 |
<secondary>last job background</secondary> |
| 8629 |
</indexterm> |
| 8630 |
<listitem> |
| 8631 |
|
| 8632 |
<para><link linkend="processiddef">PID</link> (process ID) of last |
| 8633 |
job run in background</para> |
| 8634 |
|
| 8635 |
<para> |
| 8636 |
<programlisting>LOG=$0.log |
| 8637 |
|
| 8638 |
COMMAND1="sleep 100" |
| 8639 |
|
| 8640 |
echo "Logging PIDs background commands for script: $0" >> "$LOG" |
| 8641 |
# So they can be monitored, and killed as necessary. |
| 8642 |
echo >> "$LOG" |
| 8643 |
|
| 8644 |
# Logging commands. |
| 8645 |
|
| 8646 |
echo -n "PID of \"$COMMAND1\": " >> "$LOG" |
| 8647 |
${COMMAND1} & |
| 8648 |
echo $! >> "$LOG" |
| 8649 |
# PID of "sleep 100": 1506 |
| 8650 |
|
| 8651 |
# Thank you, Jacques Lederer, for suggesting this.</programlisting> |
| 8652 |
</para> |
| 8653 |
|
| 8654 |
<para>Using <varname>$!</varname> for job control:</para> |
| 8655 |
|
| 8656 |
<para> |
| 8657 |
<programlisting>possibly_hanging_job & { sleep ${TIMEOUT}; eval 'kill -9 $!' &> /dev/null; } |
| 8658 |
# Forces completion of an ill-behaved program. |
| 8659 |
# Useful, for example, in init scripts. |
| 8660 |
|
| 8661 |
# Thank you, Sylvain Fourmanoit, for this creative use of the "!" variable.</programlisting> |
| 8662 |
|
| 8663 |
</para> |
| 8664 |
|
| 8665 |
<para>Or, alternately:</para> |
| 8666 |
|
| 8667 |
<para> |
| 8668 |
<programlisting># This example by Matthew Sage. |
| 8669 |
# Used with permission. |
| 8670 |
|
| 8671 |
TIMEOUT=30 # Timeout value in seconds |
| 8672 |
count=0 |
| 8673 |
|
| 8674 |
possibly_hanging_job & { |
| 8675 |
while ((count < TIMEOUT )); do |
| 8676 |
eval '[ ! -d "/proc/$!" ] && ((count = TIMEOUT))' |
| 8677 |
# /proc is where information about running processes is found. |
| 8678 |
# "-d" tests whether it exists (whether directory exists). |
| 8679 |
# So, we're waiting for the job in question to show up. |
| 8680 |
((count++)) |
| 8681 |
sleep 1 |
| 8682 |
done |
| 8683 |
eval '[ -d "/proc/$!" ] && kill -15 $!' |
| 8684 |
# If the hanging job is running, kill it. |
| 8685 |
} |
| 8686 |
|
| 8687 |
# -------------------------------------------------------------- # |
| 8688 |
|
| 8689 |
# However, this may not not work as specified if another process |
| 8690 |
#+ begins to run after the "hanging_job" . . . |
| 8691 |
# In such a case, the wrong job may be killed. |
| 8692 |
# Ariel Meragelman suggests the following fix. |
| 8693 |
|
| 8694 |
TIMEOUT=30 |
| 8695 |
count=0 |
| 8696 |
# Timeout value in seconds |
| 8697 |
possibly_hanging_job & { |
| 8698 |
|
| 8699 |
while ((count < TIMEOUT )); do |
| 8700 |
eval '[ ! -d "/proc/$lastjob" ] && ((count = TIMEOUT))' |
| 8701 |
lastjob=$! |
| 8702 |
((count++)) |
| 8703 |
sleep 1 |
| 8704 |
done |
| 8705 |
eval '[ -d "/proc/$lastjob" ] && kill -15 $lastjob' |
| 8706 |
|
| 8707 |
} |
| 8708 |
|
| 8709 |
exit</programlisting> |
| 8710 |
</para> |
| 8711 |
|
| 8712 |
|
| 8713 |
</listitem> |
| 8714 |
</varlistentry> |
| 8715 |
|
| 8716 |
|
| 8717 |
<varlistentry> |
| 8718 |
<term><anchor id="underscoreref"><varname>$_</varname></term> |
| 8719 |
<indexterm> |
| 8720 |
<primary>$_</primary> |
| 8721 |
</indexterm> |
| 8722 |
<indexterm> |
| 8723 |
<primary>variable</primary> |
| 8724 |
<secondary>$_</secondary> |
| 8725 |
</indexterm> |
| 8726 |
<indexterm> |
| 8727 |
<primary>underscore</primary> |
| 8728 |
<secondary>last argument</secondary> |
| 8729 |
</indexterm> |
| 8730 |
<listitem> |
| 8731 |
<para>Special variable set to final argument of previous command |
| 8732 |
executed.</para> |
| 8733 |
|
| 8734 |
|
| 8735 |
<example id="uscref"> |
| 8736 |
<title>Underscore variable</title> |
| 8737 |
<programlisting>#!/bin/bash |
| 8738 |
|
| 8739 |
echo $_ # /bin/bash |
| 8740 |
# Just called /bin/bash to run the script. |
| 8741 |
# Note that this will vary according to |
| 8742 |
#+ how the script is invoked. |
| 8743 |
|
| 8744 |
du >/dev/null # So no output from command. |
| 8745 |
echo $_ # du |
| 8746 |
|
| 8747 |
ls -al >/dev/null # So no output from command. |
| 8748 |
echo $_ # -al (last argument) |
| 8749 |
|
| 8750 |
: |
| 8751 |
echo $_ # :</programlisting></example> |
| 8752 |
</listitem> |
| 8753 |
</varlistentry> |
| 8754 |
|
| 8755 |
<varlistentry> |
| 8756 |
<term><anchor id="xstatvarref"><varname>$?</varname></term> |
| 8757 |
<indexterm> |
| 8758 |
<primary>$?</primary> |
| 8759 |
</indexterm> |
| 8760 |
<indexterm> |
| 8761 |
<primary>variable</primary> |
| 8762 |
<secondary>$?</secondary> |
| 8763 |
</indexterm> |
| 8764 |
<indexterm> |
| 8765 |
<primary>exit status</primary> |
| 8766 |
</indexterm> |
| 8767 |
<listitem><para><link linkend="exitstatusref">Exit status</link> |
| 8768 |
of a command, <link linkend="functionref">function</link>, |
| 8769 |
or the script itself (see <xref linkend="max">)</para> |
| 8770 |
</listitem> |
| 8771 |
</varlistentry> |
| 8772 |
|
| 8773 |
<varlistentry> |
| 8774 |
<term><anchor id="proccid"><varname>$$</varname></term> |
| 8775 |
<indexterm> |
| 8776 |
<primary>$$</primary> |
| 8777 |
</indexterm> |
| 8778 |
<indexterm> |
| 8779 |
<primary>variable</primary> |
| 8780 |
<secondary>$$</secondary> |
| 8781 |
</indexterm> |
| 8782 |
<indexterm> |
| 8783 |
<primary>PID</primary> |
| 8784 |
<secondary>of script</secondary> |
| 8785 |
</indexterm> |
| 8786 |
<listitem><para>Process ID (<firstterm>PID</firstterm>) of |
| 8787 |
the script itself. |
| 8788 |
|
| 8789 |
<footnote><para>Within a script, inside a subshell, |
| 8790 |
<varname>$$</varname> <link linkend="bashpidref">returns |
| 8791 |
the PID of the script</link>, not the |
| 8792 |
subshell.</para></footnote> |
| 8793 |
|
| 8794 |
The <varname>$$</varname> variable often |
| 8795 |
finds use in scripts to construct <quote>unique</quote> |
| 8796 |
temp file names (see <xref linkend="online">, <xref |
| 8797 |
linkend="derpm">, and <xref linkend="selfdestruct">). |
| 8798 |
This is usually simpler than invoking <link |
| 8799 |
linkend="mktempref">mktemp</link>.</para> |
| 8800 |
|
| 8801 |
</listitem> |
| 8802 |
</varlistentry> |
| 8803 |
|
| 8804 |
</variablelist> |
| 8805 |
|
| 8806 |
|
| 8807 |
</sect1> <!-- Internal Variables --> |
| 8808 |
|
| 8809 |
|
| 8810 |
|
| 8811 |
<sect1 id="declareref"> |
| 8812 |
|
| 8813 |
<indexterm> |
| 8814 |
<primary>declare</primary> |
| 8815 |
</indexterm> |
| 8816 |
<indexterm> |
| 8817 |
<primary>typeset</primary> |
| 8818 |
</indexterm> |
| 8819 |
<indexterm> |
| 8820 |
<primary>command</primary> |
| 8821 |
<secondary>declare</secondary> |
| 8822 |
</indexterm> |
| 8823 |
<indexterm> |
| 8824 |
<primary>command</primary> |
| 8825 |
<secondary>typeset</secondary> |
| 8826 |
</indexterm> |
| 8827 |
<title>Typing variables: <command>declare</command> or |
| 8828 |
<command>typeset</command></title> |
| 8829 |
|
| 8830 |
<para><anchor id="declare1ref"></para> |
| 8831 |
|
| 8832 |
<para>The <firstterm>declare</firstterm> or |
| 8833 |
<firstterm>typeset</firstterm> <link |
| 8834 |
linkend="builtinref">builtins</link>, which are exact synonyms, |
| 8835 |
permit modifying the properties of variables. This is |
| 8836 |
a very weak form of the <firstterm>typing</firstterm> |
| 8837 |
|
| 8838 |
<footnote> |
| 8839 |
|
| 8840 |
<para><anchor id="typingref">In this context, |
| 8841 |
<firstterm>typing</firstterm> |
| 8842 |
a variable means to classify it and restrict its properties. |
| 8843 |
For example, a variable <firstterm>declared</firstterm> |
| 8844 |
or <firstterm>typed</firstterm> as an integer |
| 8845 |
is no longer available for <link linkend="stringopstab">string |
| 8846 |
operations</link>.</para> |
| 8847 |
|
| 8848 |
<para><programlisting>declare -i intvar |
| 8849 |
|
| 8850 |
intvar=23 |
| 8851 |
echo "$intvar" # 23 |
| 8852 |
intvar=stringval |
| 8853 |
echo "$intvar" # 0</programlisting></para> |
| 8854 |
|
| 8855 |
</footnote> |
| 8856 |
|
| 8857 |
available in certain programming languages. The |
| 8858 |
<firstterm>declare</firstterm> command is specific to version |
| 8859 |
2 or later of Bash. The <firstterm>typeset</firstterm> command |
| 8860 |
also works in ksh scripts.</para> |
| 8861 |
|
| 8862 |
<variablelist id="declareopsref"> |
| 8863 |
<title><anchor id="declareopsref1">declare/typeset options</title> |
| 8864 |
|
| 8865 |
<varlistentry> |
| 8866 |
<term><token>-r</token> |
| 8867 |
<replaceable>readonly</replaceable></term> |
| 8868 |
<listitem> |
| 8869 |
<para>(<userinput>declare -r var1</userinput> works the same as |
| 8870 |
<userinput>readonly var1</userinput>)</para> |
| 8871 |
<para>This is the rough equivalent of the <command>C</command> |
| 8872 |
<firstterm>const</firstterm> type qualifier. An attempt |
| 8873 |
to change the value of a <firstterm>readonly</firstterm> |
| 8874 |
variable fails with an error message.</para> |
| 8875 |
<para><programlisting>declare -r var1=1 |
| 8876 |
echo "var1 = $var1" # var1 = 1 |
| 8877 |
|
| 8878 |
(( var1++ )) # x.sh: line 4: var1: readonly variable</programlisting> |
| 8879 |
</para> |
| 8880 |
</listitem> |
| 8881 |
</varlistentry> |
| 8882 |
|
| 8883 |
<varlistentry> |
| 8884 |
<term><token>-i</token> <replaceable>integer</replaceable></term> |
| 8885 |
|
| 8886 |
<listitem> |
| 8887 |
|
| 8888 |
<para><programlisting>declare -i number |
| 8889 |
# The script will treat subsequent occurrences of "number" as an integer. |
| 8890 |
|
| 8891 |
number=3 |
| 8892 |
echo "Number = $number" # Number = 3 |
| 8893 |
|
| 8894 |
number=three |
| 8895 |
echo "Number = $number" # Number = 0 |
| 8896 |
# Tries to evaluate the string "three" as an integer.</programlisting></para> |
| 8897 |
|
| 8898 |
<para>Certain arithmetic operations are permitted |
| 8899 |
for declared integer variables without the need |
| 8900 |
for <link linkend="exprref">expr</link> or <link |
| 8901 |
linkend="letref">let</link>.</para> |
| 8902 |
|
| 8903 |
<para><programlisting>n=6/3 |
| 8904 |
echo "n = $n" # n = 6/3 |
| 8905 |
|
| 8906 |
declare -i n |
| 8907 |
n=6/3 |
| 8908 |
echo "n = $n" # n = 2</programlisting></para> |
| 8909 |
|
| 8910 |
</listitem> |
| 8911 |
</varlistentry> |
| 8912 |
|
| 8913 |
<varlistentry> |
| 8914 |
<term><anchor id="arraydeclare"><token>-a</token> |
| 8915 |
<replaceable>array</replaceable></term> |
| 8916 |
<listitem><para><programlisting>declare -a indices</programlisting></para> |
| 8917 |
<para>The variable <parameter>indices</parameter> will be treated as |
| 8918 |
an <link linkend="arrayref">array</link>.</para></listitem> |
| 8919 |
</varlistentry> |
| 8920 |
|
| 8921 |
<varlistentry> |
| 8922 |
<term><token>-f</token> <replaceable>function(s)</replaceable></term> |
| 8923 |
<listitem> |
| 8924 |
|
| 8925 |
<para><programlisting>declare -f</programlisting></para> |
| 8926 |
<para>A <userinput>declare -f</userinput> line with no |
| 8927 |
arguments in a script causes a listing of all the |
| 8928 |
<link linkend="functionref">functions</link> previously |
| 8929 |
defined in that script.</para> |
| 8930 |
|
| 8931 |
<para><programlisting>declare -f function_name</programlisting></para> |
| 8932 |
<para>A <userinput>declare -f function_name</userinput> |
| 8933 |
in a script lists just the function named.</para> |
| 8934 |
|
| 8935 |
</listitem> |
| 8936 |
</varlistentry> |
| 8937 |
|
| 8938 |
<varlistentry> |
| 8939 |
<term><token>-x</token> <link linkend="exportref">export</link></term> |
| 8940 |
<listitem><para><programlisting>declare -x var3</programlisting></para> |
| 8941 |
<para>This declares a variable as available for exporting outside the |
| 8942 |
environment of the script itself.</para></listitem> |
| 8943 |
</varlistentry> |
| 8944 |
|
| 8945 |
<varlistentry> |
| 8946 |
<term>-x var=$value</term> |
| 8947 |
<listitem> |
| 8948 |
<para><programlisting>declare -x var3=373</programlisting></para> |
| 8949 |
<para>The <command>declare</command> command permits |
| 8950 |
assigning a value to a variable in the same statement |
| 8951 |
as setting its properties.</para> |
| 8952 |
</listitem> |
| 8953 |
</varlistentry> |
| 8954 |
|
| 8955 |
|
| 8956 |
</variablelist> |
| 8957 |
|
| 8958 |
<example id="ex20"> |
| 8959 |
<title>Using <firstterm>declare</firstterm> to type variables</title> |
| 8960 |
<programlisting>&ex20;</programlisting> |
| 8961 |
</example> |
| 8962 |
|
| 8963 |
|
| 8964 |
<caution> |
| 8965 |
|
| 8966 |
<para>Using the <firstterm>declare</firstterm> builtin |
| 8967 |
restricts the <link linkend="scoperef">scope</link> |
| 8968 |
of a variable. |
| 8969 |
|
| 8970 |
|
| 8971 |
<programlisting>foo () |
| 8972 |
{ |
| 8973 |
FOO="bar" |
| 8974 |
} |
| 8975 |
|
| 8976 |
bar () |
| 8977 |
{ |
| 8978 |
foo |
| 8979 |
echo $FOO |
| 8980 |
} |
| 8981 |
|
| 8982 |
bar # Prints bar.</programlisting></para> |
| 8983 |
|
| 8984 |
<para>However . . . |
| 8985 |
|
| 8986 |
<programlisting>foo (){ |
| 8987 |
declare FOO="bar" |
| 8988 |
} |
| 8989 |
|
| 8990 |
bar () |
| 8991 |
{ |
| 8992 |
foo |
| 8993 |
echo $FOO |
| 8994 |
} |
| 8995 |
|
| 8996 |
bar # Prints nothing. |
| 8997 |
|
| 8998 |
|
| 8999 |
# Thank you, Michael Iatrou, for pointing this out.</programlisting></para> |
| 9000 |
|
| 9001 |
</caution> |
| 9002 |
|
| 9003 |
|
| 9004 |
<sect2 id="declare2x"> |
| 9005 |
<title>Another use for <firstterm>declare</firstterm></title> |
| 9006 |
|
| 9007 |
<para>The <firstterm>declare</firstterm> command can be |
| 9008 |
helpful in identifying variables, <link |
| 9009 |
linkend="envref">environmental</link> or otherwise. |
| 9010 |
This can be especially useful with <link |
| 9011 |
linkend="arrayref">arrays</link>.</para> |
| 9012 |
|
| 9013 |
<para> |
| 9014 |
<screen><prompt>bash$ </prompt><userinput>declare | grep HOME</userinput> |
| 9015 |
<computeroutput>HOME=/home/bozo</computeroutput> |
| 9016 |
|
| 9017 |
|
| 9018 |
<prompt>bash$ </prompt><userinput>zzy=68</userinput> |
| 9019 |
<prompt>bash$ </prompt><userinput>declare | grep zzy</userinput> |
| 9020 |
<computeroutput>zzy=68</computeroutput> |
| 9021 |
|
| 9022 |
|
| 9023 |
<prompt>bash$ </prompt><userinput>Colors=([0]="purple" [1]="reddish-orange" [2]="light green")</userinput> |
| 9024 |
<prompt>bash$ </prompt><userinput>echo ${Colors[@]}</userinput> |
| 9025 |
<computeroutput>purple reddish-orange light green</computeroutput> |
| 9026 |
<prompt>bash$ </prompt><userinput>declare | grep Colors</userinput> |
| 9027 |
<computeroutput>Colors=([0]="purple" [1]="reddish-orange" [2]="light green")</computeroutput> |
| 9028 |
</screen> |
| 9029 |
</para> |
| 9030 |
|
| 9031 |
|
| 9032 |
</sect2> <!-- Another use for declare --> |
| 9033 |
|
| 9034 |
</sect1> <!-- Typing variables: declare or typeset --> |
| 9035 |
|
| 9036 |
|
| 9037 |
|
| 9038 |
|
| 9039 |
|
| 9040 |
<sect1 id="randomvar"> |
| 9041 |
|
| 9042 |
<indexterm> |
| 9043 |
<primary>$RANDOM</primary> |
| 9044 |
</indexterm> |
| 9045 |
<indexterm> |
| 9046 |
<primary>variable</primary> |
| 9047 |
<secondary>$RANDOM</secondary> |
| 9048 |
</indexterm> |
| 9049 |
<title>$RANDOM: generate random integer</title> |
| 9050 |
|
| 9051 |
<epigraph> |
| 9052 |
<para>Anyone who attempts to generate random numbers by |
| 9053 |
deterministic means is, of course, living in a state of |
| 9054 |
sin.</para> |
| 9055 |
<para>--John von Neumann</para> |
| 9056 |
</epigraph> |
| 9057 |
|
| 9058 |
<para><anchor id="randomvar01"></para> |
| 9059 |
<para><varname>$RANDOM</varname> is an internal Bash <link |
| 9060 |
linkend="functionref">function</link> (not a constant) that |
| 9061 |
returns a <firstterm>pseudorandom</firstterm> |
| 9062 |
|
| 9063 |
<footnote><para>True <quote>randomness,</quote> insofar as |
| 9064 |
it exists at all, can only be found in certain incompletely |
| 9065 |
understood natural phenomena, such as radioactive |
| 9066 |
decay. Computers only <firstterm>simulate</firstterm> |
| 9067 |
randomness, and computer-generated sequences of |
| 9068 |
<quote>random</quote> numbers are therefore referred to as |
| 9069 |
<firstterm>pseudorandom</firstterm>.</para></footnote> |
| 9070 |
|
| 9071 |
integer in the range 0 - 32767. It should |
| 9072 |
<replaceable>not</replaceable> be used to generate an encryption |
| 9073 |
key.</para> |
| 9074 |
|
| 9075 |
<example id="ex21"> |
| 9076 |
<title>Generating random numbers</title> |
| 9077 |
<programlisting>&ex21;</programlisting> |
| 9078 |
</example> |
| 9079 |
|
| 9080 |
<example id="pickcard"> |
| 9081 |
<title>Picking a random card from a deck</title> |
| 9082 |
<programlisting>&pickcard;</programlisting> |
| 9083 |
</example> |
| 9084 |
|
| 9085 |
<para><anchor id="brownianref"></para> |
| 9086 |
<example id="brownian"> |
| 9087 |
<title>Brownian Motion Simulation</title> |
| 9088 |
<programlisting>&brownian;</programlisting> |
| 9089 |
</example> |
| 9090 |
|
| 9091 |
|
| 9092 |
|
| 9093 |
<para> |
| 9094 |
<emphasis>Jipe</emphasis> points out a set of techniques for |
| 9095 |
generating random numbers within a range. |
| 9096 |
|
| 9097 |
<programlisting># Generate random number between 6 and 30. |
| 9098 |
rnumber=$((RANDOM%25+6)) |
| 9099 |
|
| 9100 |
# Generate random number in the same 6 - 30 range, |
| 9101 |
#+ but the number must be evenly divisible by 3. |
| 9102 |
rnumber=$(((RANDOM%30/3+1)*3)) |
| 9103 |
|
| 9104 |
# Note that this will not work all the time. |
| 9105 |
# It fails if $RANDOM%30 returns 0. |
| 9106 |
|
| 9107 |
# Frank Wang suggests the following alternative: |
| 9108 |
rnumber=$(( RANDOM%27/3*3+6 ))</programlisting> |
| 9109 |
</para> |
| 9110 |
|
| 9111 |
|
| 9112 |
<para> |
| 9113 |
<emphasis>Bill Gradwohl</emphasis> came up with an improved |
| 9114 |
formula that works for positive numbers. |
| 9115 |
<programlisting>rnumber=$(((RANDOM%(max-min+divisibleBy))/divisibleBy*divisibleBy+min))</programlisting> |
| 9116 |
</para> |
| 9117 |
|
| 9118 |
<para>Here Bill presents a versatile function that returns |
| 9119 |
a random number between two specified values.</para> |
| 9120 |
|
| 9121 |
<example id="randombetween"> |
| 9122 |
<title>Random between values</title> |
| 9123 |
<programlisting>&randombetween;</programlisting> |
| 9124 |
</example> |
| 9125 |
|
| 9126 |
|
| 9127 |
|
| 9128 |
<para>Just how random is <varname>$RANDOM</varname>? The best |
| 9129 |
way to test this is to write a script that tracks |
| 9130 |
the distribution of <quote>random</quote> numbers |
| 9131 |
generated by <varname>$RANDOM</varname>. Let's roll a |
| 9132 |
<varname>$RANDOM</varname> die a few times . . .</para> |
| 9133 |
|
| 9134 |
<example id="randomtest"> |
| 9135 |
<title>Rolling a single die with RANDOM</title> |
| 9136 |
<programlisting>&randomtest;</programlisting> |
| 9137 |
</example> |
| 9138 |
|
| 9139 |
<para>As we have seen in the last example, it is best to |
| 9140 |
<firstterm>reseed</firstterm> the <parameter>RANDOM</parameter> |
| 9141 |
generator each time it is invoked. Using the same seed |
| 9142 |
for <parameter>RANDOM</parameter> repeats the same series |
| 9143 |
of numbers. |
| 9144 |
<footnote> |
| 9145 |
<para>The <firstterm>seed</firstterm> of a |
| 9146 |
computer-generated pseudorandom number series |
| 9147 |
can be considered an identification label. For |
| 9148 |
example, think of the pseudorandom series with a |
| 9149 |
seed of <emphasis>23</emphasis> as <replaceable>Series |
| 9150 |
#23</replaceable>.</para> |
| 9151 |
<para>A property of a pseurandom number series is the length of |
| 9152 |
the cycle before it starts repeating itself. A good pseurandom |
| 9153 |
generator will produce series with very long cycles.</para> |
| 9154 |
</footnote> |
| 9155 |
(This mirrors the behavior of the |
| 9156 |
<replaceable>random()</replaceable> function in |
| 9157 |
<firstterm>C</firstterm>.)</para> |
| 9158 |
|
| 9159 |
<example id="seedingrandom"> |
| 9160 |
<title>Reseeding RANDOM</title> |
| 9161 |
<programlisting>&seedingrandom;</programlisting> |
| 9162 |
</example> |
| 9163 |
|
| 9164 |
<para><anchor id="urandomref"></para> |
| 9165 |
<note> |
| 9166 |
<para>The <filename>/dev/urandom</filename> pseudo-device file |
| 9167 |
provides a method of generating much more <quote>random</quote> |
| 9168 |
pseudorandom numbers than the <varname>$RANDOM</varname> |
| 9169 |
variable. <userinput>dd if=/dev/urandom of=targetfile |
| 9170 |
bs=1 count=XX</userinput> creates a file of well-scattered |
| 9171 |
pseudorandom numbers. However, assigning these numbers |
| 9172 |
to a variable in a script requires a workaround, such |
| 9173 |
as filtering through <link linkend="odref">od</link> |
| 9174 |
(as in above example, <xref linkend="rnd">, and |
| 9175 |
<xref linkend="insertionsort">), or even piping to |
| 9176 |
<link linkend="md5sumref">md5sum</link> (see <xref |
| 9177 |
linkend="horserace">).</para> |
| 9178 |
|
| 9179 |
<para><anchor id="awkrandomref"></para> |
| 9180 |
|
| 9181 |
<para>There are also other ways to generate pseudorandom |
| 9182 |
numbers in a script. <command>Awk</command> provides a |
| 9183 |
convenient means of doing this.</para> |
| 9184 |
|
| 9185 |
<example id="random2"> |
| 9186 |
<title>Pseudorandom numbers, using <link |
| 9187 |
linkend="awkref">awk</link></title> |
| 9188 |
<programlisting>&random2;</programlisting> |
| 9189 |
</example> |
| 9190 |
|
| 9191 |
<para>The <link linkend="dateref">date</link> command also lends |
| 9192 |
itself to <link linkend="daterandref">generating pseudorandom |
| 9193 |
integer sequences</link>.</para> |
| 9194 |
|
| 9195 |
</note> |
| 9196 |
|
| 9197 |
|
| 9198 |
|
| 9199 |
</sect1> <!-- RANDOM: generate random integer --> |
| 9200 |
|
| 9201 |
|
| 9202 |
|
| 9203 |
|
| 9204 |
</chapter> <!-- Variables Revisited --> |
| 9205 |
|
| 9206 |
|
| 9207 |
|
| 9208 |
<chapter id="manipulatingvars"> |
| 9209 |
<title>Manipulating Variables</title> |
| 9210 |
|
| 9211 |
<sect1 id="String-Manipulation"> |
| 9212 |
<title>Manipulating Strings</title> |
| 9213 |
|
| 9214 |
<para><anchor id="stringmanip"></para> |
| 9215 |
|
| 9216 |
<para>Bash supports a surprising number of string manipulation |
| 9217 |
operations. Unfortunately, these tools lack |
| 9218 |
a unified focus. Some are a subset of <link |
| 9219 |
linkend="paramsubref">parameter substitution</link>, and |
| 9220 |
others fall under the functionality of the UNIX <link |
| 9221 |
linkend="exprref">expr</link> command. This results in |
| 9222 |
inconsistent command syntax and overlap of functionality, |
| 9223 |
not to mention confusion.</para> |
| 9224 |
|
| 9225 |
<variablelist id="stringlength"> |
| 9226 |
<title>String Length</title> |
| 9227 |
|
| 9228 |
<varlistentry> |
| 9229 |
<term>${#string}</term> |
| 9230 |
<indexterm> |
| 9231 |
<primary>string length</primary> |
| 9232 |
<secondary>parameter substitution</secondary> |
| 9233 |
</indexterm> |
| 9234 |
|
| 9235 |
<listitem><para></para> |
| 9236 |
</listitem> |
| 9237 |
</varlistentry> |
| 9238 |
|
| 9239 |
<varlistentry> |
| 9240 |
<term>expr length $string</term> |
| 9241 |
<indexterm> |
| 9242 |
<primary>string length</primary> |
| 9243 |
<secondary>expr</secondary> |
| 9244 |
</indexterm> |
| 9245 |
|
| 9246 |
<listitem><para><anchor id="strlen">These are the equivalent of |
| 9247 |
<firstterm>strlen()</firstterm> in |
| 9248 |
<firstterm>C</firstterm>.</para></listitem> |
| 9249 |
</varlistentry> |
| 9250 |
|
| 9251 |
<varlistentry> |
| 9252 |
<term>expr "$string" : '.*'</term> |
| 9253 |
<indexterm> |
| 9254 |
<primary>string length</primary> |
| 9255 |
<secondary>expr</secondary> |
| 9256 |
</indexterm> |
| 9257 |
|
| 9258 |
<listitem> |
| 9259 |
<para> |
| 9260 |
<programlisting>stringZ=abcABC123ABCabc |
| 9261 |
|
| 9262 |
echo ${#stringZ} # 15 |
| 9263 |
echo `expr length $stringZ` # 15 |
| 9264 |
echo `expr "$stringZ" : '.*'` # 15</programlisting> |
| 9265 |
</para> |
| 9266 |
</listitem> |
| 9267 |
|
| 9268 |
</varlistentry> |
| 9269 |
|
| 9270 |
</variablelist> |
| 9271 |
|
| 9272 |
<example id="paragraphspace"> |
| 9273 |
<title>Inserting a blank line between paragraphs in a text file</title> |
| 9274 |
<programlisting>¶graphspace;</programlisting> |
| 9275 |
</example> |
| 9276 |
|
| 9277 |
<variablelist id="lengthsubstring"> |
| 9278 |
<title>Length of Matching Substring at Beginning of String</title> |
| 9279 |
|
| 9280 |
<varlistentry> |
| 9281 |
<term><anchor id="exprmatch">expr match "$string" |
| 9282 |
'$substring'</term> |
| 9283 |
<indexterm> |
| 9284 |
<primary>substring length</primary> |
| 9285 |
<secondary>expr</secondary> |
| 9286 |
</indexterm> |
| 9287 |
|
| 9288 |
<listitem> |
| 9289 |
<para><replaceable>$substring</replaceable> is a <link |
| 9290 |
linkend="regexref">regular expression</link>.</para> |
| 9291 |
</listitem> |
| 9292 |
</varlistentry> |
| 9293 |
|
| 9294 |
<varlistentry> |
| 9295 |
<term>expr "$string" : '$substring'</term> |
| 9296 |
<indexterm> |
| 9297 |
<primary>substring length</primary> |
| 9298 |
<secondary>expr</secondary> |
| 9299 |
</indexterm> |
| 9300 |
|
| 9301 |
<listitem> |
| 9302 |
<para><replaceable>$substring</replaceable> is a regular |
| 9303 |
expression.</para> |
| 9304 |
<para> |
| 9305 |
|
| 9306 |
<programlisting>stringZ=abcABC123ABCabc |
| 9307 |
# |------| |
| 9308 |
# 12345678 |
| 9309 |
|
| 9310 |
echo `expr match "$stringZ" 'abc[A-Z]*.2'` # 8 |
| 9311 |
echo `expr "$stringZ" : 'abc[A-Z]*.2'` # 8</programlisting> |
| 9312 |
</para> |
| 9313 |
</listitem> |
| 9314 |
</varlistentry> |
| 9315 |
|
| 9316 |
</variablelist> |
| 9317 |
|
| 9318 |
<variablelist id="substringindex"> |
| 9319 |
<title>Index</title> |
| 9320 |
|
| 9321 |
<varlistentry> |
| 9322 |
<term><anchor id="substringindex2">expr index $string |
| 9323 |
$substring</term> |
| 9324 |
<indexterm> |
| 9325 |
<primary>substring index</primary> |
| 9326 |
<secondary>expr</secondary> |
| 9327 |
</indexterm> |
| 9328 |
|
| 9329 |
<listitem> |
| 9330 |
|
| 9331 |
<para>Numerical position in $string of first character in |
| 9332 |
$substring that matches.</para> |
| 9333 |
|
| 9334 |
<para><programlisting>stringZ=abcABC123ABCabc |
| 9335 |
# 123456 ... |
| 9336 |
echo `expr index "$stringZ" C12` # 6 |
| 9337 |
# C position. |
| 9338 |
|
| 9339 |
echo `expr index "$stringZ" 1c` # 3 |
| 9340 |
# 'c' (in #3 position) matches before '1'.</programlisting></para> |
| 9341 |
|
| 9342 |
<para>This is the near equivalent of |
| 9343 |
<firstterm>strchr()</firstterm> in |
| 9344 |
<firstterm>C</firstterm>.</para> |
| 9345 |
</listitem> |
| 9346 |
</varlistentry> |
| 9347 |
|
| 9348 |
</variablelist> |
| 9349 |
|
| 9350 |
<variablelist id="substringextraction"> |
| 9351 |
<title>Substring Extraction</title> |
| 9352 |
|
| 9353 |
<varlistentry> |
| 9354 |
<term><anchor id="substrextr01">${string:position}</term> |
| 9355 |
<indexterm> |
| 9356 |
<primary>substring</primary> |
| 9357 |
<secondary>extraction</secondary> |
| 9358 |
</indexterm> |
| 9359 |
|
| 9360 |
<listitem> |
| 9361 |
<para>Extracts substring from <replaceable>$string</replaceable> at |
| 9362 |
<replaceable>$position</replaceable>.</para> |
| 9363 |
<para>If the <varname>$string</varname> parameter is |
| 9364 |
<quote><token>*</token></quote> |
| 9365 |
or <quote><token>@</token></quote>, then this extracts the |
| 9366 |
<link linkend="posparamref">positional parameters</link>, |
| 9367 |
<footnote><para>This applies to either command-line |
| 9368 |
arguments or parameters passed to a <link |
| 9369 |
linkend="functionref">function</link>.</para></footnote> |
| 9370 |
starting at <varname>$position</varname>.</para> |
| 9371 |
</listitem> |
| 9372 |
</varlistentry> |
| 9373 |
|
| 9374 |
<varlistentry> |
| 9375 |
<term><anchor id="substrextr02">${string:position:length}</term> |
| 9376 |
<indexterm> |
| 9377 |
<primary>substring</primary> |
| 9378 |
<secondary>extraction</secondary> |
| 9379 |
</indexterm> |
| 9380 |
|
| 9381 |
<listitem> |
| 9382 |
<para>Extracts <replaceable>$length</replaceable> characters |
| 9383 |
of substring from <replaceable>$string</replaceable> at |
| 9384 |
<replaceable>$position</replaceable>.</para> |
| 9385 |
<para> |
| 9386 |
<programlisting>stringZ=abcABC123ABCabc |
| 9387 |
# 0123456789..... |
| 9388 |
# 0-based indexing. |
| 9389 |
|
| 9390 |
echo ${stringZ:0} # abcABC123ABCabc |
| 9391 |
echo ${stringZ:1} # bcABC123ABCabc |
| 9392 |
echo ${stringZ:7} # 23ABCabc |
| 9393 |
|
| 9394 |
echo ${stringZ:7:3} # 23A |
| 9395 |
# Three characters of substring. |
| 9396 |
|
| 9397 |
|
| 9398 |
|
| 9399 |
# Is it possible to index from the right end of the string? |
| 9400 |
|
| 9401 |
echo ${stringZ:-4} # abcABC123ABCabc |
| 9402 |
# Defaults to full string, as in ${parameter:-default}. |
| 9403 |
# However . . . |
| 9404 |
|
| 9405 |
echo ${stringZ:(-4)} # Cabc |
| 9406 |
echo ${stringZ: -4} # Cabc |
| 9407 |
# Now, it works. |
| 9408 |
# Parentheses or added space "escape" the position parameter. |
| 9409 |
|
| 9410 |
# Thank you, Dan Jacobson, for pointing this out.</programlisting> |
| 9411 |
</para> |
| 9412 |
|
| 9413 |
<para>The <firstterm>position</firstterm> and |
| 9414 |
<firstterm>length</firstterm> arguments can be |
| 9415 |
<quote>parameterized,</quote> that is, represented as a |
| 9416 |
variable, rather than as a numerical constant.</para> |
| 9417 |
|
| 9418 |
<para><anchor id="randstring0"></para> |
| 9419 |
<example id="randstring"> |
| 9420 |
<title>Generating an 8-character <quote>random</quote> |
| 9421 |
string</title> |
| 9422 |
<programlisting>&randstring;</programlisting> |
| 9423 |
</example> |
| 9424 |
|
| 9425 |
<para><anchor id="substrextrp"></para> |
| 9426 |
<para>If the <varname>$string</varname> parameter is |
| 9427 |
<quote><token>*</token></quote> or |
| 9428 |
<quote><token>@</token></quote>, then this extracts a maximum |
| 9429 |
of <varname>$length</varname> positional parameters, starting |
| 9430 |
at <varname>$position</varname>.</para> |
| 9431 |
|
| 9432 |
<para> |
| 9433 |
<programlisting>echo ${*:2} # Echoes second and following positional parameters. |
| 9434 |
echo ${@:2} # Same as above. |
| 9435 |
|
| 9436 |
echo ${*:2:3} # Echoes three positional parameters, starting at second.</programlisting> |
| 9437 |
</para> |
| 9438 |
|
| 9439 |
</listitem> |
| 9440 |
</varlistentry> |
| 9441 |
|
| 9442 |
<varlistentry> |
| 9443 |
<term>expr substr $string $position $length</term> |
| 9444 |
<indexterm> |
| 9445 |
<primary>substring</primary> |
| 9446 |
<secondary>extraction expr</secondary> |
| 9447 |
</indexterm> |
| 9448 |
|
| 9449 |
<listitem> |
| 9450 |
|
| 9451 |
<para>Extracts <replaceable>$length</replaceable> characters |
| 9452 |
from <replaceable>$string</replaceable> starting at |
| 9453 |
<replaceable>$position</replaceable>.</para> |
| 9454 |
|
| 9455 |
<para> |
| 9456 |
<programlisting>stringZ=abcABC123ABCabc |
| 9457 |
# 123456789...... |
| 9458 |
# 1-based indexing. |
| 9459 |
|
| 9460 |
echo `expr substr $stringZ 1 2` # ab |
| 9461 |
echo `expr substr $stringZ 4 3` # ABC</programlisting> |
| 9462 |
</para> |
| 9463 |
|
| 9464 |
<para><anchor id="exprparen"></para> |
| 9465 |
|
| 9466 |
</listitem> |
| 9467 |
</varlistentry> |
| 9468 |
|
| 9469 |
<varlistentry> |
| 9470 |
<term>expr match "$string" '\($substring\)'</term> |
| 9471 |
<indexterm> |
| 9472 |
<primary>substring extraction</primary> |
| 9473 |
<secondary>expr</secondary> |
| 9474 |
</indexterm> |
| 9475 |
|
| 9476 |
<listitem> |
| 9477 |
<para>Extracts <replaceable>$substring</replaceable> |
| 9478 |
at beginning of <replaceable>$string</replaceable>, |
| 9479 |
where <replaceable>$substring</replaceable> is a <link |
| 9480 |
linkend="regexref">regular expression</link>.</para> |
| 9481 |
</listitem> |
| 9482 |
</varlistentry> |
| 9483 |
|
| 9484 |
<varlistentry> |
| 9485 |
<term>expr "$string" : '\($substring\)'</term> |
| 9486 |
<indexterm> |
| 9487 |
<primary>substring extraction</primary> |
| 9488 |
<secondary>expr</secondary> |
| 9489 |
</indexterm> |
| 9490 |
|
| 9491 |
<listitem> |
| 9492 |
|
| 9493 |
<para>Extracts <replaceable>$substring</replaceable> |
| 9494 |
at beginning of <replaceable>$string</replaceable>, |
| 9495 |
where <replaceable>$substring</replaceable> is a regular |
| 9496 |
expression.</para> |
| 9497 |
|
| 9498 |
<para> |
| 9499 |
<programlisting>stringZ=abcABC123ABCabc |
| 9500 |
# ======= |
| 9501 |
|
| 9502 |
echo `expr match "$stringZ" '\(.[b-c]*[A-Z]..[0-9]\)'` # abcABC1 |
| 9503 |
echo `expr "$stringZ" : '\(.[b-c]*[A-Z]..[0-9]\)'` # abcABC1 |
| 9504 |
echo `expr "$stringZ" : '\(.......\)'` # abcABC1 |
| 9505 |
# All of the above forms give an identical result.</programlisting> |
| 9506 |
</para> |
| 9507 |
</listitem> |
| 9508 |
</varlistentry> |
| 9509 |
|
| 9510 |
<varlistentry> |
| 9511 |
<term>expr match "$string" '.*\($substring\)'</term> |
| 9512 |
<indexterm> |
| 9513 |
<primary>substring extraction</primary> |
| 9514 |
<secondary>expr</secondary> |
| 9515 |
</indexterm> |
| 9516 |
|
| 9517 |
<listitem> |
| 9518 |
<para>Extracts <replaceable>$substring</replaceable> |
| 9519 |
at <emphasis>end</emphasis> of |
| 9520 |
<replaceable>$string</replaceable>, where |
| 9521 |
<replaceable>$substring</replaceable> is a regular |
| 9522 |
expression.</para> |
| 9523 |
</listitem> |
| 9524 |
</varlistentry> |
| 9525 |
|
| 9526 |
<varlistentry> |
| 9527 |
<term>expr "$string" : '.*\($substring\)'</term> |
| 9528 |
<indexterm> |
| 9529 |
<primary>substring extraction</primary> |
| 9530 |
<secondary>expr</secondary> |
| 9531 |
</indexterm> |
| 9532 |
|
| 9533 |
<listitem> |
| 9534 |
|
| 9535 |
<para>Extracts <replaceable>$substring</replaceable> |
| 9536 |
at <emphasis>end</emphasis> of <replaceable>$string</replaceable>, |
| 9537 |
where <replaceable>$substring</replaceable> is a regular |
| 9538 |
expression.</para> |
| 9539 |
|
| 9540 |
<para> |
| 9541 |
<programlisting>stringZ=abcABC123ABCabc |
| 9542 |
# ====== |
| 9543 |
|
| 9544 |
echo `expr match "$stringZ" '.*\([A-C][A-C][A-C][a-c]*\)'` # ABCabc |
| 9545 |
echo `expr "$stringZ" : '.*\(......\)'` # ABCabc</programlisting> |
| 9546 |
|
| 9547 |
</para> |
| 9548 |
</listitem> |
| 9549 |
</varlistentry> |
| 9550 |
|
| 9551 |
</variablelist> |
| 9552 |
|
| 9553 |
<variablelist id="substringremoval"> |
| 9554 |
<title>Substring Removal</title> |
| 9555 |
|
| 9556 |
<varlistentry> |
| 9557 |
<term>${string#substring}</term> |
| 9558 |
<indexterm> |
| 9559 |
<primary>substring</primary> |
| 9560 |
<secondary>removal</secondary> |
| 9561 |
</indexterm> |
| 9562 |
|
| 9563 |
<listitem> |
| 9564 |
<para>Deletes shortest match of |
| 9565 |
<replaceable>$substring</replaceable> from |
| 9566 |
<emphasis>front</emphasis> of |
| 9567 |
<replaceable>$string</replaceable>.</para> |
| 9568 |
</listitem> |
| 9569 |
</varlistentry> |
| 9570 |
|
| 9571 |
<varlistentry> |
| 9572 |
<term>${string##substring}</term> |
| 9573 |
<indexterm> |
| 9574 |
<primary>substring</primary> |
| 9575 |
<secondary>removal</secondary> |
| 9576 |
</indexterm> |
| 9577 |
|
| 9578 |
<listitem> |
| 9579 |
|
| 9580 |
<para>Deletes longest match of |
| 9581 |
<replaceable>$substring</replaceable> from |
| 9582 |
<emphasis>front</emphasis> of |
| 9583 |
<replaceable>$string</replaceable>.</para> |
| 9584 |
|
| 9585 |
<para> |
| 9586 |
<programlisting>stringZ=abcABC123ABCabc |
| 9587 |
# |----| shortest |
| 9588 |
# |----------| longest |
| 9589 |
|
| 9590 |
echo ${stringZ#a*C} # 123ABCabc |
| 9591 |
# Strip out shortest match between 'a' and 'C'. |
| 9592 |
|
| 9593 |
echo ${stringZ##a*C} # abc |
| 9594 |
# Strip out longest match between 'a' and 'C'. |
| 9595 |
|
| 9596 |
|
| 9597 |
|
| 9598 |
# You can parameterize the substrings. |
| 9599 |
|
| 9600 |
X='a*C' |
| 9601 |
|
| 9602 |
echo ${stringZ#$X} # 123ABCabc |
| 9603 |
echo ${stringZ##$X} # abc |
| 9604 |
# As above.</programlisting> |
| 9605 |
</para> |
| 9606 |
</listitem> |
| 9607 |
</varlistentry> |
| 9608 |
|
| 9609 |
<varlistentry> |
| 9610 |
<term>${string%substring}</term> |
| 9611 |
<indexterm> |
| 9612 |
<primary>substring</primary> |
| 9613 |
<secondary>removal</secondary> |
| 9614 |
</indexterm> |
| 9615 |
|
| 9616 |
<listitem> |
| 9617 |
|
| 9618 |
<para>Deletes shortest match of |
| 9619 |
<replaceable>$substring</replaceable> from |
| 9620 |
<emphasis>back</emphasis> of |
| 9621 |
<replaceable>$string</replaceable>.</para> |
| 9622 |
|
| 9623 |
<para>For example: |
| 9624 |
<programlisting># Rename all filenames in $PWD with "TXT" suffix to a "txt" suffix. |
| 9625 |
# For example, "file1.TXT" becomes "file1.txt" . . . |
| 9626 |
|
| 9627 |
SUFF=TXT |
| 9628 |
suff=txt |
| 9629 |
|
| 9630 |
for i in $(ls *.$SUFF) |
| 9631 |
do |
| 9632 |
mv -f $i ${i%.$SUFF}.$suff |
| 9633 |
# Leave unchanged everything *except* the shortest pattern match |
| 9634 |
#+ starting from the right-hand-side of the variable $i . . . |
| 9635 |
done ### This could be condensed into a "one-liner" if desired. |
| 9636 |
|
| 9637 |
# Thank you, Rory Winston.</programlisting> |
| 9638 |
</para> |
| 9639 |
|
| 9640 |
</listitem> |
| 9641 |
</varlistentry> |
| 9642 |
|
| 9643 |
<varlistentry> |
| 9644 |
<term>${string%%substring}</term> |
| 9645 |
<indexterm> |
| 9646 |
<primary>substring</primary> |
| 9647 |
<secondary>removal</secondary> |
| 9648 |
</indexterm> |
| 9649 |
|
| 9650 |
<listitem> |
| 9651 |
|
| 9652 |
<para>Deletes longest match of |
| 9653 |
<replaceable>$substring</replaceable> from |
| 9654 |
<emphasis>back</emphasis> of |
| 9655 |
<replaceable>$string</replaceable>.</para> |
| 9656 |
|
| 9657 |
<para> |
| 9658 |
<programlisting>stringZ=abcABC123ABCabc |
| 9659 |
# || shortest |
| 9660 |
# |------------| longest |
| 9661 |
|
| 9662 |
echo ${stringZ%b*c} # abcABC123ABCa |
| 9663 |
# Strip out shortest match between 'b' and 'c', from back of $stringZ. |
| 9664 |
|
| 9665 |
echo ${stringZ%%b*c} # a |
| 9666 |
# Strip out longest match between 'b' and 'c', from back of $stringZ.</programlisting> |
| 9667 |
</para> |
| 9668 |
|
| 9669 |
<para>This operator is useful for generating filenames.</para> |
| 9670 |
|
| 9671 |
<example id="cvt"> |
| 9672 |
<title>Converting graphic file formats, with filename change</title> |
| 9673 |
<programlisting>&cvt;</programlisting> |
| 9674 |
</example> |
| 9675 |
|
| 9676 |
<example id="ra2ogg"> |
| 9677 |
<title>Converting streaming audio files to |
| 9678 |
<firstterm>ogg</firstterm></title> |
| 9679 |
<programlisting>&ra2ogg;</programlisting> |
| 9680 |
</example> |
| 9681 |
|
| 9682 |
<para><anchor id="getoptsimple1"></para> |
| 9683 |
<para>A simple emulation of <link linkend="getopty">getopt</link> |
| 9684 |
using substring-extraction constructs.</para> |
| 9685 |
|
| 9686 |
<example id="getoptsimple"> |
| 9687 |
<title>Emulating <firstterm>getopt</firstterm></title> |
| 9688 |
<programlisting>&getoptsimple;</programlisting> |
| 9689 |
</example> |
| 9690 |
|
| 9691 |
|
| 9692 |
|
| 9693 |
</listitem> |
| 9694 |
</varlistentry> |
| 9695 |
|
| 9696 |
</variablelist> |
| 9697 |
|
| 9698 |
<variablelist id="substringreplacement"> |
| 9699 |
<title>Substring Replacement</title> |
| 9700 |
|
| 9701 |
<varlistentry> |
| 9702 |
<term><anchor |
| 9703 |
id="substrrepl00">${string/substring/replacement}</term> |
| 9704 |
<indexterm> |
| 9705 |
<primary>substring</primary> |
| 9706 |
<secondary>replacement</secondary> |
| 9707 |
</indexterm> |
| 9708 |
|
| 9709 |
<listitem> |
| 9710 |
<para> |
| 9711 |
Replace first <firstterm>match</firstterm> of |
| 9712 |
<replaceable>$substring</replaceable> with |
| 9713 |
<replaceable>$replacement</replaceable>. |
| 9714 |
<footnote><para>Note that |
| 9715 |
<replaceable>$substring</replaceable> and |
| 9716 |
<replaceable>$replacement</replaceable> may refer to |
| 9717 |
either <firstterm>literal strings</firstterm> or |
| 9718 |
<firstterm>variables</firstterm>, depending on |
| 9719 |
context. See the first usage example.</para></footnote> |
| 9720 |
</para> |
| 9721 |
</listitem> |
| 9722 |
</varlistentry> |
| 9723 |
|
| 9724 |
<varlistentry> |
| 9725 |
<term><anchor |
| 9726 |
id="substrrepl01">${string//substring/replacement}</term> |
| 9727 |
<indexterm> |
| 9728 |
<primary>substring</primary> |
| 9729 |
<secondary>replacement</secondary> |
| 9730 |
</indexterm> |
| 9731 |
|
| 9732 |
<listitem> |
| 9733 |
|
| 9734 |
<para>Replace all matches of |
| 9735 |
<replaceable>$substring</replaceable> with |
| 9736 |
<replaceable>$replacement</replaceable>.</para> |
| 9737 |
|
| 9738 |
<para> |
| 9739 |
<programlisting>stringZ=abcABC123ABCabc |
| 9740 |
|
| 9741 |
echo ${stringZ/abc/xyz} # xyzABC123ABCabc |
| 9742 |
# Replaces first match of 'abc' with 'xyz'. |
| 9743 |
|
| 9744 |
echo ${stringZ//abc/xyz} # xyzABC123ABCxyz |
| 9745 |
# Replaces all matches of 'abc' with # 'xyz'. |
| 9746 |
|
| 9747 |
echo --------------- |
| 9748 |
echo "$stringZ" # abcABC123ABCabc |
| 9749 |
echo --------------- |
| 9750 |
# The string itself is not altered! |
| 9751 |
|
| 9752 |
# Can the match and replacement strings be parameterized? |
| 9753 |
match=abc |
| 9754 |
repl=000 |
| 9755 |
echo ${stringZ/$match/$repl} # 000ABC123ABCabc |
| 9756 |
# ^ ^ ^^^ |
| 9757 |
echo ${stringZ//$match/$repl} # 000ABC123ABC000 |
| 9758 |
# Yes! ^ ^ ^^^ ^^^ |
| 9759 |
|
| 9760 |
echo |
| 9761 |
|
| 9762 |
# What happens if no $replacement string is supplied? |
| 9763 |
echo ${stringZ/abc} # ABC123ABCabc |
| 9764 |
echo ${stringZ//abc} # ABC123ABC |
| 9765 |
# A simple deletion takes place.</programlisting> |
| 9766 |
</para> |
| 9767 |
|
| 9768 |
</listitem> |
| 9769 |
</varlistentry> |
| 9770 |
|
| 9771 |
<varlistentry> |
| 9772 |
<term><anchor |
| 9773 |
id="substrrepl02">${string/#substring/replacement}</term> |
| 9774 |
<indexterm> |
| 9775 |
<primary>substring</primary> |
| 9776 |
<secondary>replacement</secondary> |
| 9777 |
</indexterm> |
| 9778 |
|
| 9779 |
<listitem> |
| 9780 |
<para>If <replaceable>$substring</replaceable> matches |
| 9781 |
<emphasis>front</emphasis> end of |
| 9782 |
<replaceable>$string</replaceable>, substitute |
| 9783 |
<replaceable>$replacement</replaceable> for |
| 9784 |
<replaceable>$substring</replaceable>.</para> |
| 9785 |
</listitem> |
| 9786 |
</varlistentry> |
| 9787 |
|
| 9788 |
<varlistentry> |
| 9789 |
<term><anchor |
| 9790 |
id="substrrepl03">${string/%substring/replacement}</term> |
| 9791 |
<indexterm> |
| 9792 |
<primary>substring</primary> |
| 9793 |
<secondary>replacement</secondary> |
| 9794 |
</indexterm> |
| 9795 |
|
| 9796 |
<listitem> |
| 9797 |
|
| 9798 |
<para>If <replaceable>$substring</replaceable> matches |
| 9799 |
<emphasis>back</emphasis> end of |
| 9800 |
<replaceable>$string</replaceable>, substitute |
| 9801 |
<replaceable>$replacement</replaceable> for |
| 9802 |
<replaceable>$substring</replaceable>.</para> |
| 9803 |
|
| 9804 |
<para> |
| 9805 |
<programlisting>stringZ=abcABC123ABCabc |
| 9806 |
|
| 9807 |
echo ${stringZ/#abc/XYZ} # XYZABC123ABCabc |
| 9808 |
# Replaces front-end match of 'abc' with 'XYZ'. |
| 9809 |
|
| 9810 |
echo ${stringZ/%abc/XYZ} # abcABC123ABCXYZ |
| 9811 |
# Replaces back-end match of 'abc' with 'XYZ'.</programlisting> |
| 9812 |
</para> |
| 9813 |
|
| 9814 |
</listitem> |
| 9815 |
</varlistentry> |
| 9816 |
|
| 9817 |
</variablelist> |
| 9818 |
|
| 9819 |
<sect2 id="awkstringmanip"> |
| 9820 |
<title>Manipulating strings using awk</title> |
| 9821 |
|
| 9822 |
<para><anchor id="awkstringmanip2"</para> |
| 9823 |
<para>A Bash script may invoke the string manipulation facilities of |
| 9824 |
<link linkend="awkref">awk</link> as an alternative to using its |
| 9825 |
built-in operations.</para> |
| 9826 |
|
| 9827 |
<example id="substringex"> |
| 9828 |
<title>Alternate ways of extracting and locating substrings</title> |
| 9829 |
<programlisting>&substringex;</programlisting> |
| 9830 |
</example> |
| 9831 |
|
| 9832 |
</sect2> <!-- Manipulating strings using awk --> |
| 9833 |
|
| 9834 |
|
| 9835 |
|
| 9836 |
<sect2 id="strfdisc"> |
| 9837 |
<title>Further Reference</title> |
| 9838 |
|
| 9839 |
<para>For more on string manipulation in scripts, refer to <xref |
| 9840 |
linkend="Parameter-Substitution"> and the |
| 9841 |
<link linkend="expextrsub">relevant section</link> of the <link |
| 9842 |
linkend="exprref">expr</link> command listing.</para> |
| 9843 |
|
| 9844 |
<para>Script examples: |
| 9845 |
<orderedlist> |
| 9846 |
<listitem><para><xref linkend="ex45"></para></listitem> |
| 9847 |
<listitem><para><xref linkend="length"></para></listitem> |
| 9848 |
<listitem><para><xref linkend="pattmatching"></para></listitem> |
| 9849 |
<listitem><para><xref linkend="rfe"></para></listitem> |
| 9850 |
<listitem><para><xref linkend="varmatch"></para></listitem> |
| 9851 |
<listitem><para><xref linkend="insertionsort"></para></listitem> |
| 9852 |
<listitem><para><xref linkend="qky"></para></listitem> |
| 9853 |
</orderedlist> |
| 9854 |
</para> |
| 9855 |
|
| 9856 |
</sect2> <!-- Further Reference--> |
| 9857 |
|
| 9858 |
|
| 9859 |
</sect1> <!-- Manipulating Strings --> |
| 9860 |
|
| 9861 |
|
| 9862 |
|
| 9863 |
<sect1 id="Parameter-Substitution"> |
| 9864 |
<title>Parameter Substitution</title> |
| 9865 |
|
| 9866 |
<para><anchor id="paramsubref"></para> |
| 9867 |
|
| 9868 |
<variablelist id="pssub"> |
| 9869 |
<title><anchor id="pssub1">Manipulating and/or expanding variables</title> |
| 9870 |
<varlistentry> |
| 9871 |
<term> |
| 9872 |
<userinput>${parameter}</userinput></term> |
| 9873 |
<listitem> |
| 9874 |
|
| 9875 |
<para>Same as <replaceable>$parameter</replaceable>, i.e., |
| 9876 |
value of the variable |
| 9877 |
<replaceable>parameter</replaceable>. |
| 9878 |
In certain contexts, only the less ambiguous |
| 9879 |
<replaceable>${parameter}</replaceable> form |
| 9880 |
works.</para> |
| 9881 |
|
| 9882 |
<para>May be used for concatenating variables with strings.</para> |
| 9883 |
|
| 9884 |
<para><programlisting> |
| 9885 |
your_id=${USER}-on-${HOSTNAME} |
| 9886 |
echo "$your_id" |
| 9887 |
# |
| 9888 |
echo "Old \$PATH = $PATH" |
| 9889 |
PATH=${PATH}:/opt/bin # Add /opt/bin to $PATH for duration of script. |
| 9890 |
echo "New \$PATH = $PATH" |
| 9891 |
</programlisting></para> |
| 9892 |
</listitem> |
| 9893 |
</varlistentry> |
| 9894 |
|
| 9895 |
<varlistentry> |
| 9896 |
<term><anchor |
| 9897 |
id="defparam1"><userinput>${parameter-default}</userinput></term> |
| 9898 |
<term><userinput>${parameter:-default}</userinput></term> |
| 9899 |
<listitem> |
| 9900 |
<para>If parameter not set, use default.</para> |
| 9901 |
<para><programlisting>var1=1 |
| 9902 |
var2=2 |
| 9903 |
# var3 is unset. |
| 9904 |
|
| 9905 |
echo ${var1-$var2} # 1 |
| 9906 |
echo ${var3-$var2} # 2 |
| 9907 |
# ^ Note the $ prefix. |
| 9908 |
|
| 9909 |
|
| 9910 |
|
| 9911 |
echo ${username-`whoami`} |
| 9912 |
# Echoes the result of `whoami`, if variable $username is still unset.</programlisting></para> |
| 9913 |
|
| 9914 |
<note><para><anchor |
| 9915 |
id="unddr"><replaceable>${parameter-default}</replaceable> |
| 9916 |
and <replaceable>${parameter:-default}</replaceable> |
| 9917 |
are almost equivalent. The extra <token>:</token> makes |
| 9918 |
a difference only when <parameter>parameter</parameter> |
| 9919 |
has been declared, but is null. </para></note> |
| 9920 |
|
| 9921 |
<para><programlisting>¶msub;</programlisting></para> |
| 9922 |
|
| 9923 |
<para>The <firstterm>default parameter</firstterm> construct |
| 9924 |
finds use in providing <quote>missing</quote> command-line |
| 9925 |
arguments in scripts.</para> |
| 9926 |
|
| 9927 |
<para> |
| 9928 |
<programlisting>DEFAULT_FILENAME=generic.data |
| 9929 |
filename=${1:-$DEFAULT_FILENAME} |
| 9930 |
# If not otherwise specified, the following command block operates |
| 9931 |
#+ on the file "generic.data". |
| 9932 |
# Begin-Command-Block |
| 9933 |
# ... |
| 9934 |
# ... |
| 9935 |
# ... |
| 9936 |
# End-Command-Block |
| 9937 |
|
| 9938 |
|
| 9939 |
|
| 9940 |
# From "hanoi2.bash" example: |
| 9941 |
DISKS=${1:-E_NOPARAM} # Must specify how many disks. |
| 9942 |
# Set $DISKS to $1 command-line-parameter, |
| 9943 |
#+ or to $E_NOPARAM if that is unset.</programlisting> |
| 9944 |
</para> |
| 9945 |
|
| 9946 |
<para>See also <xref linkend="ex58">, <xref |
| 9947 |
linkend="ex73">, and <xref linkend="collatz">.</para> |
| 9948 |
|
| 9949 |
<para>Compare this method with <link |
| 9950 |
linkend="anddefault">using an <firstterm>and |
| 9951 |
list</firstterm> to supply a default command-line |
| 9952 |
argument</link>.</para> |
| 9953 |
|
| 9954 |
</listitem> |
| 9955 |
</varlistentry> |
| 9956 |
|
| 9957 |
<varlistentry> |
| 9958 |
<term><userinput>${parameter=default}</userinput></term> |
| 9959 |
<term><userinput>${parameter:=default}</userinput></term> |
| 9960 |
<listitem> |
| 9961 |
<para><anchor id="defparam"></para> |
| 9962 |
<para>If parameter not set, set it to |
| 9963 |
<firstterm>default</firstterm>.</para> |
| 9964 |
|
| 9965 |
<para>Both forms nearly equivalent. The <token>:</token> |
| 9966 |
makes a difference only when <varname>$parameter</varname> |
| 9967 |
has been declared and is null, |
| 9968 |
<footnote> |
| 9969 |
<para>If $parameter is null in a |
| 9970 |
non-interactive script, it will terminate with a <link |
| 9971 |
linkend="exitcodesref"><returnvalue>127</returnvalue> |
| 9972 |
exit status</link> (the Bash error code for |
| 9973 |
<quote>command not found</quote>).</para> |
| 9974 |
</footnote> |
| 9975 |
as above. |
| 9976 |
</para> |
| 9977 |
|
| 9978 |
<para><programlisting>echo ${var=abc} # abc |
| 9979 |
echo ${var=xyz} # abc |
| 9980 |
# $var had already been set to abc, so it did not change.</programlisting></para> |
| 9981 |
</listitem> |
| 9982 |
</varlistentry> |
| 9983 |
|
| 9984 |
<varlistentry> |
| 9985 |
<term><anchor |
| 9986 |
id="paramaltv"><userinput>${parameter+alt_value}</userinput></term> |
| 9987 |
<term><userinput>${parameter:+alt_value}</userinput></term> |
| 9988 |
<listitem> |
| 9989 |
<para>If parameter set, use |
| 9990 |
<userinput>alt_value</userinput>, else use null |
| 9991 |
string.</para> |
| 9992 |
|
| 9993 |
<para>Both forms nearly equivalent. The <token>:</token> |
| 9994 |
makes a difference only when |
| 9995 |
<parameter>parameter</parameter> |
| 9996 |
has been declared and is null, see below.</para> |
| 9997 |
|
| 9998 |
<para><programlisting>echo "###### \${parameter+alt_value} ########" |
| 9999 |
echo |
| 10000 |
|
| 10001 |
a=${param1+xyz} |
| 10002 |
echo "a = $a" # a = |
| 10003 |
|
| 10004 |
param2= |
| 10005 |
a=${param2+xyz} |
| 10006 |
echo "a = $a" # a = xyz |
| 10007 |
|
| 10008 |
param3=123 |
| 10009 |
a=${param3+xyz} |
| 10010 |
echo "a = $a" # a = xyz |
| 10011 |
|
| 10012 |
echo |
| 10013 |
echo "###### \${parameter:+alt_value} ########" |
| 10014 |
echo |
| 10015 |
|
| 10016 |
a=${param4:+xyz} |
| 10017 |
echo "a = $a" # a = |
| 10018 |
|
| 10019 |
param5= |
| 10020 |
a=${param5:+xyz} |
| 10021 |
echo "a = $a" # a = |
| 10022 |
# Different result from a=${param5+xyz} |
| 10023 |
|
| 10024 |
param6=123 |
| 10025 |
a=${param6:+xyz} |
| 10026 |
echo "a = $a" # a = xyz</programlisting></para> |
| 10027 |
|
| 10028 |
</listitem> |
| 10029 |
</varlistentry> |
| 10030 |
<varlistentry> |
| 10031 |
<term><anchor id="qerrmsg"><userinput>${parameter?err_msg}</userinput></term> |
| 10032 |
<term><userinput>${parameter:?err_msg}</userinput></term> |
| 10033 |
<listitem><para>If parameter set, use it, else print |
| 10034 |
<firstterm>err_msg</firstterm> and <emphasis>abort |
| 10035 |
the script</emphasis> with an <link |
| 10036 |
linkend="exitstatusref">exit status</link> of |
| 10037 |
<errorcode>1</errorcode>.</para> |
| 10038 |
|
| 10039 |
<para>Both forms nearly equivalent. The <token>:</token> |
| 10040 |
makes a difference only when <parameter>parameter</parameter> |
| 10041 |
has been declared and is null, as above.</para> |
| 10042 |
</listitem> |
| 10043 |
</varlistentry> |
| 10044 |
</variablelist> |
| 10045 |
|
| 10046 |
<example id="ex6"> |
| 10047 |
<title>Using parameter substitution and error messages</title> |
| 10048 |
<programlisting>&ex6;</programlisting> |
| 10049 |
</example> |
| 10050 |
|
| 10051 |
<example id="usagemessage"> |
| 10052 |
<title>Parameter substitution and <quote>usage</quote> messages</title> |
| 10053 |
<programlisting>&usagemessage;</programlisting> |
| 10054 |
</example> |
| 10055 |
|
| 10056 |
|
| 10057 |
|
| 10058 |
<formalpara><title>Parameter substitution and/or expansion</title> |
| 10059 |
|
| 10060 |
<para><anchor id="psub2">The following expressions are |
| 10061 |
the complement to the <command>match</command> |
| 10062 |
<replaceable>in</replaceable> <command>expr</command> |
| 10063 |
string operations (see <xref linkend="ex45">). |
| 10064 |
These particular ones are used mostly in parsing file |
| 10065 |
path names.</para></formalpara> |
| 10066 |
|
| 10067 |
<variablelist id="psorex"> |
| 10068 |
<title><anchor id="psorex1">Variable length / Substring removal</title> |
| 10069 |
|
| 10070 |
<varlistentry> |
| 10071 |
|
| 10072 |
<term><userinput>${#var}</userinput></term> |
| 10073 |
<listitem> |
| 10074 |
<para><userinput>String length</userinput> (number |
| 10075 |
of characters in <varname>$var</varname>). For |
| 10076 |
an <link linkend="arrayref">array</link>, |
| 10077 |
<command>${#array}</command> is the length of the |
| 10078 |
first element in the array.</para> |
| 10079 |
|
| 10080 |
<note><para> |
| 10081 |
Exceptions: |
| 10082 |
|
| 10083 |
<itemizedlist> |
| 10084 |
<listitem> |
| 10085 |
<para><anchor id="numposparam"></para> |
| 10086 |
<para> |
| 10087 |
<command>${#*}</command> and |
| 10088 |
<command>${#@}</command> give the <emphasis>number |
| 10089 |
of positional parameters</emphasis>. |
| 10090 |
</para></listitem> |
| 10091 |
|
| 10092 |
<listitem><para> |
| 10093 |
For an array, <command>${#array[*]}</command> and |
| 10094 |
<command>${#array[@]}</command> give the number |
| 10095 |
of elements in the array. |
| 10096 |
</para></listitem> |
| 10097 |
</itemizedlist> |
| 10098 |
</para></note> |
| 10099 |
|
| 10100 |
<example id="length"> |
| 10101 |
<title>Length of a variable</title> |
| 10102 |
<programlisting>&length;</programlisting> |
| 10103 |
</example> |
| 10104 |
|
| 10105 |
</listitem> |
| 10106 |
</varlistentry> |
| 10107 |
|
| 10108 |
<varlistentry> |
| 10109 |
<term><anchor |
| 10110 |
id="psorex2"><userinput>${var#Pattern}</userinput></term> |
| 10111 |
<term><userinput>${var##Pattern}</userinput></term> |
| 10112 |
|
| 10113 |
<listitem> |
| 10114 |
|
| 10115 |
<para><anchor id="psorexsh"></para> |
| 10116 |
<para><command>${var#Pattern} </command> |
| 10117 |
Remove from <varname>$var</varname> |
| 10118 |
the <emphasis>shortest</emphasis> part of |
| 10119 |
<varname>$Pattern</varname> that matches |
| 10120 |
the <replaceable>front end</replaceable> of |
| 10121 |
<varname>$var</varname>. |
| 10122 |
</para> |
| 10123 |
|
| 10124 |
<para><anchor id="psorexlo"></para> |
| 10125 |
<para><command>${var##Pattern} </command> |
| 10126 |
Remove from <varname>$var</varname> |
| 10127 |
the <emphasis>longest</emphasis> part of |
| 10128 |
<varname>$Pattern</varname> that matches |
| 10129 |
the <replaceable>front end</replaceable> of |
| 10130 |
<varname>$var</varname>. |
| 10131 |
</para> |
| 10132 |
|
| 10133 |
<para>A usage illustration from <xref |
| 10134 |
linkend="daysbetween">: |
| 10135 |
<programlisting># Function from "days-between.sh" example. |
| 10136 |
# Strips leading zero(s) from argument passed. |
| 10137 |
|
| 10138 |
strip_leading_zero () # Strip possible leading zero(s) |
| 10139 |
{ #+ from argument passed. |
| 10140 |
return=${1#0} # The "1" refers to "$1" -- passed arg. |
| 10141 |
} # The "0" is what to remove from "$1" -- strips zeros.</programlisting> |
| 10142 |
</para> |
| 10143 |
|
| 10144 |
<para>Manfred Schwarb's more elaborate variation of the |
| 10145 |
above:</para> |
| 10146 |
|
| 10147 |
<para> |
| 10148 |
<programlisting>strip_leading_zero2 () # Strip possible leading zero(s), since otherwise |
| 10149 |
{ # Bash will interpret such numbers as octal values. |
| 10150 |
shopt -s extglob # Turn on extended globbing. |
| 10151 |
local val=${1##+(0)} # Use local variable, longest matching series of 0's. |
| 10152 |
shopt -u extglob # Turn off extended globbing. |
| 10153 |
_strip_leading_zero2=${val:-0} |
| 10154 |
# If input was 0, return 0 instead of "". |
| 10155 |
}</programlisting> |
| 10156 |
</para> |
| 10157 |
|
| 10158 |
|
| 10159 |
<para>Another usage illustration:</para> |
| 10160 |
<para> |
| 10161 |
<programlisting>echo `basename $PWD` # Basename of current working directory. |
| 10162 |
echo "${PWD##*/}" # Basename of current working directory. |
| 10163 |
echo |
| 10164 |
echo `basename $0` # Name of script. |
| 10165 |
echo $0 # Name of script. |
| 10166 |
echo "${0##*/}" # Name of script. |
| 10167 |
echo |
| 10168 |
filename=test.data |
| 10169 |
echo "${filename##*.}" # data |
| 10170 |
# Extension of filename.</programlisting> |
| 10171 |
</para> |
| 10172 |
|
| 10173 |
</listitem> |
| 10174 |
|
| 10175 |
</varlistentry> |
| 10176 |
|
| 10177 |
<varlistentry> |
| 10178 |
<term><anchor id="pctpatref"><userinput>${var%Pattern}</userinput></term> |
| 10179 |
<term><userinput>${var%%Pattern}</userinput></term> |
| 10180 |
|
| 10181 |
|
| 10182 |
<listitem> |
| 10183 |
|
| 10184 |
<para><anchor id="pctrep1"></para> |
| 10185 |
<para><command>${var%Pattern}</command> |
| 10186 |
Remove from <varname>$var</varname> |
| 10187 |
the <emphasis>shortest</emphasis> part of |
| 10188 |
<varname>$Pattern</varname> that matches |
| 10189 |
the <replaceable>back end</replaceable> of |
| 10190 |
<varname>$var</varname>. </para> |
| 10191 |
|
| 10192 |
<para><anchor id="pctrep2"></para> |
| 10193 |
<para><command>${var%%Pattern}</command> |
| 10194 |
Remove from <varname>$var</varname> |
| 10195 |
the <emphasis>longest</emphasis> part of |
| 10196 |
<varname>$Pattern</varname> that matches |
| 10197 |
the <replaceable>back end</replaceable> of |
| 10198 |
<varname>$var</varname>. </para> |
| 10199 |
|
| 10200 |
</listitem> |
| 10201 |
|
| 10202 |
|
| 10203 |
</varlistentry> |
| 10204 |
</variablelist> |
| 10205 |
|
| 10206 |
<para><link linkend="bash2ref">Version 2</link> of Bash added |
| 10207 |
additional options.</para> |
| 10208 |
|
| 10209 |
<example id="pattmatching"> |
| 10210 |
<title>Pattern matching in parameter substitution</title> |
| 10211 |
<programlisting>&pattmatching;</programlisting> |
| 10212 |
</example> |
| 10213 |
|
| 10214 |
|
| 10215 |
<example id="rfe"> |
| 10216 |
<title>Renaming file extensions<token>:</token></title> |
| 10217 |
<programlisting>&rfe;</programlisting> |
| 10218 |
</example> |
| 10219 |
|
| 10220 |
|
| 10221 |
<variablelist id="exprepl"> |
| 10222 |
<title><anchor id="exprepl1">Variable expansion / Substring |
| 10223 |
replacement</title> |
| 10224 |
|
| 10225 |
<varlistentry> |
| 10226 |
<term></term> |
| 10227 |
<listitem> |
| 10228 |
<para>These constructs have been adopted from |
| 10229 |
<firstterm>ksh</firstterm>.</para> |
| 10230 |
</listitem> |
| 10231 |
</varlistentry> |
| 10232 |
|
| 10233 |
<varlistentry> |
| 10234 |
<term><userinput>${var:pos}</userinput></term> |
| 10235 |
<listitem> |
| 10236 |
<para>Variable <replaceable>var</replaceable> expanded, |
| 10237 |
starting from offset <replaceable>pos</replaceable>. |
| 10238 |
</para> |
| 10239 |
</listitem> |
| 10240 |
</varlistentry> |
| 10241 |
|
| 10242 |
<varlistentry> |
| 10243 |
<term><userinput>${var:pos:len}</userinput></term> |
| 10244 |
<listitem> |
| 10245 |
<para>Expansion to a max of <replaceable>len</replaceable> |
| 10246 |
characters of variable <replaceable>var</replaceable>, from offset |
| 10247 |
<replaceable>pos</replaceable>. See <xref linkend="pw"> |
| 10248 |
for an example of the creative use of this operator. |
| 10249 |
</para> |
| 10250 |
</listitem> |
| 10251 |
</varlistentry> |
| 10252 |
|
| 10253 |
<varlistentry> |
| 10254 |
<term><userinput>${var/Pattern/Replacement}</userinput></term> |
| 10255 |
<listitem> |
| 10256 |
<para>First match of <replaceable>Pattern</replaceable>, |
| 10257 |
within <replaceable>var</replaceable> replaced with |
| 10258 |
<replaceable>Replacement</replaceable>.</para> |
| 10259 |
<para>If <replaceable>Replacement</replaceable> is |
| 10260 |
omitted, then the first match of |
| 10261 |
<replaceable>Pattern</replaceable> is replaced by |
| 10262 |
<emphasis>nothing</emphasis>, that is, deleted.</para> |
| 10263 |
</listitem> |
| 10264 |
</varlistentry> |
| 10265 |
|
| 10266 |
<varlistentry> |
| 10267 |
<term><userinput>${var//Pattern/Replacement}</userinput></term> |
| 10268 |
<listitem> |
| 10269 |
|
| 10270 |
<formalpara><title>Global replacement</title> |
| 10271 |
<para><anchor id="psglob"> |
| 10272 |
All matches of <replaceable>Pattern</replaceable>, |
| 10273 |
within <replaceable>var</replaceable> replaced with |
| 10274 |
<replaceable>Replacement</replaceable>.</para> |
| 10275 |
</formalpara> |
| 10276 |
|
| 10277 |
<para>As above, if <replaceable>Replacement</replaceable> |
| 10278 |
is omitted, then all occurrences of |
| 10279 |
<replaceable>Pattern</replaceable> are replaced by |
| 10280 |
<emphasis>nothing</emphasis>, that is, deleted.</para> |
| 10281 |
|
| 10282 |
<example id="ex7"> |
| 10283 |
<title>Using pattern matching to parse arbitrary strings</title> |
| 10284 |
<programlisting>&ex7;</programlisting> |
| 10285 |
</example> |
| 10286 |
|
| 10287 |
</listitem> |
| 10288 |
</varlistentry> |
| 10289 |
|
| 10290 |
<varlistentry> |
| 10291 |
<term><userinput>${var/#Pattern/Replacement}</userinput></term> |
| 10292 |
<listitem> |
| 10293 |
<para>If <firstterm>prefix</firstterm> of |
| 10294 |
<replaceable>var</replaceable> matches |
| 10295 |
<replaceable>Pattern</replaceable>, then substitute |
| 10296 |
<replaceable>Replacement</replaceable> for |
| 10297 |
<replaceable>Pattern</replaceable>.</para> |
| 10298 |
</listitem> |
| 10299 |
</varlistentry> |
| 10300 |
|
| 10301 |
<varlistentry> |
| 10302 |
<term><userinput>${var/%Pattern/Replacement}</userinput></term> |
| 10303 |
<listitem> |
| 10304 |
<para>If <firstterm>suffix</firstterm> of |
| 10305 |
<replaceable>var</replaceable> matches |
| 10306 |
<replaceable>Pattern</replaceable>, then substitute |
| 10307 |
<replaceable>Replacement</replaceable> for |
| 10308 |
<replaceable>Pattern</replaceable>.</para> |
| 10309 |
|
| 10310 |
<example id="varmatch"> |
| 10311 |
<title>Matching patterns at prefix or suffix of string</title> |
| 10312 |
<programlisting>&varmatch;</programlisting> |
| 10313 |
</example> |
| 10314 |
|
| 10315 |
</listitem> |
| 10316 |
</varlistentry> |
| 10317 |
|
| 10318 |
<varlistentry> |
| 10319 |
<term><anchor |
| 10320 |
id="varprefixm"><userinput>${!varprefix*}</userinput></term> |
| 10321 |
<term><userinput>${!varprefix@}</userinput></term> |
| 10322 |
<listitem> |
| 10323 |
<para>Matches <emphasis>names</emphasis> of all |
| 10324 |
previously declared variables beginning |
| 10325 |
with <parameter>varprefix</parameter>. |
| 10326 |
<programlisting># This is a variation on indirect reference, but with a * or @. |
| 10327 |
# Bash, version 2.04, adds this feature. |
| 10328 |
|
| 10329 |
xyz23=whatever |
| 10330 |
xyz24= |
| 10331 |
|
| 10332 |
a=${!xyz*} # Expands to *names* of declared variables |
| 10333 |
# ^ ^ ^ + beginning with "xyz". |
| 10334 |
echo "a = $a" # a = xyz23 xyz24 |
| 10335 |
a=${!xyz@} # Same as above. |
| 10336 |
echo "a = $a" # a = xyz23 xyz24 |
| 10337 |
|
| 10338 |
echo "---" |
| 10339 |
|
| 10340 |
abc23=something_else |
| 10341 |
b=${!abc*} |
| 10342 |
echo "b = $b" # b = abc23 |
| 10343 |
c=${!b} # Now, the more familiar type of indirect reference. |
| 10344 |
echo $c # something_else</programlisting> |
| 10345 |
</para> |
| 10346 |
|
| 10347 |
</listitem> |
| 10348 |
</varlistentry> |
| 10349 |
|
| 10350 |
</variablelist> |
| 10351 |
|
| 10352 |
</sect1> <!-- Parameter Substitution --> |
| 10353 |
|
| 10354 |
</chapter> <!-- Manipulating Variables --> |
| 10355 |
|
| 10356 |
|
| 10357 |
|
| 10358 |
<chapter id="loops"> |
| 10359 |
<title>Loops and Branches</title> |
| 10360 |
|
| 10361 |
<epigraph> |
| 10362 |
<para>What needs this iteration, woman?</para> |
| 10363 |
<para>--Shakespeare, <replaceable>Othello</replaceable></para> |
| 10364 |
</epigraph> |
| 10365 |
|
| 10366 |
|
| 10367 |
<para><anchor id="loopref00"></para> |
| 10368 |
<para>Operations on code blocks are the key to structured and organized |
| 10369 |
shell scripts. Looping and branching constructs provide the tools for |
| 10370 |
accomplishing this.</para> |
| 10371 |
|
| 10372 |
<sect1 id="loops1"> |
| 10373 |
<title>Loops</title> |
| 10374 |
|
| 10375 |
<para>A <firstterm>loop</firstterm> is a block of code that |
| 10376 |
<firstterm>iterates</firstterm> |
| 10377 |
|
| 10378 |
<footnote><para><anchor |
| 10379 |
id="iterationref"><firstterm>Iteration</firstterm>: |
| 10380 |
Repeated execution of a command or group of commands, usually -- |
| 10381 |
but not always, <firstterm>while</firstterm> a given condition |
| 10382 |
holds, or <firstterm>until</firstterm> a given condition is |
| 10383 |
met.</para></footnote> |
| 10384 |
|
| 10385 |
a list of commands |
| 10386 |
as long as the <firstterm>loop control condition</firstterm> |
| 10387 |
is true.</para> |
| 10388 |
|
| 10389 |
|
| 10390 |
<variablelist id="forloopref"> |
| 10391 |
<title><anchor id="forloopref1">for loops</title> |
| 10392 |
|
| 10393 |
<varlistentry> |
| 10394 |
<term><command>for <parameter>arg</parameter> in |
| 10395 |
<replaceable>[list]</replaceable></command></term> |
| 10396 |
<indexterm> |
| 10397 |
<primary>for</primary> |
| 10398 |
</indexterm> |
| 10399 |
<indexterm> |
| 10400 |
<primary>in</primary> |
| 10401 |
</indexterm> |
| 10402 |
<indexterm> |
| 10403 |
<primary>do</primary> |
| 10404 |
</indexterm> |
| 10405 |
<indexterm> |
| 10406 |
<primary>done</primary> |
| 10407 |
</indexterm> |
| 10408 |
<indexterm> |
| 10409 |
<primary>loop</primary> |
| 10410 |
<secondary>for</secondary> |
| 10411 |
</indexterm> |
| 10412 |
<listitem> |
| 10413 |
<para>This is the basic looping construct. It differs significantly |
| 10414 |
from its <firstterm>C</firstterm> counterpart.</para> |
| 10415 |
|
| 10416 |
<para><anchor id="doinref"></para> |
| 10417 |
|
| 10418 |
<para><cmdsynopsis> |
| 10419 |
<command>for</command> |
| 10420 |
<arg choice="plain"><replaceable>arg</replaceable></arg> |
| 10421 |
<arg choice="plain">in</arg> |
| 10422 |
<arg choice="opt"><replaceable>list</replaceable></arg><sbr> |
| 10423 |
<arg choice="plain">do</arg><sbr> |
| 10424 |
<arg rep=repeat choice=plain><replaceable> command(s)</replaceable></arg><sbr> |
| 10425 |
<arg choice="plain">done</arg> |
| 10426 |
</cmdsynopsis></para> |
| 10427 |
|
| 10428 |
<note><para>During each pass through the loop, |
| 10429 |
<replaceable>arg</replaceable> takes on the |
| 10430 |
value of each successive variable in the |
| 10431 |
<replaceable>list</replaceable>.</para></note> |
| 10432 |
|
| 10433 |
<para><programlisting>for arg in "$var1" "$var2" "$var3" ... "$varN" |
| 10434 |
# In pass 1 of the loop, arg = $var1 |
| 10435 |
# In pass 2 of the loop, arg = $var2 |
| 10436 |
# In pass 3 of the loop, arg = $var3 |
| 10437 |
# ... |
| 10438 |
# In pass N of the loop, arg = $varN |
| 10439 |
|
| 10440 |
# Arguments in [list] quoted to prevent possible word splitting.</programlisting></para> |
| 10441 |
|
| 10442 |
|
| 10443 |
<para>The argument <replaceable>list</replaceable> may |
| 10444 |
contain <link linkend="asteriskref">wild cards</link>.</para> |
| 10445 |
|
| 10446 |
<para><anchor id="needsemicolon"></para> |
| 10447 |
<para>If <firstterm>do</firstterm> is on same line as |
| 10448 |
<firstterm>for</firstterm>, there needs to be a semicolon |
| 10449 |
after list.</para> |
| 10450 |
|
| 10451 |
<para><cmdsynopsis> |
| 10452 |
<command>for</command> |
| 10453 |
<arg choice="plain"><replaceable>arg</replaceable></arg> |
| 10454 |
<arg choice="plain">in</arg> |
| 10455 |
<arg choice="opt"><replaceable>list</replaceable></arg> |
| 10456 |
<arg choice="plain">;</arg> |
| 10457 |
<arg choice="plain">do</arg><sbr> |
| 10458 |
</cmdsynopsis></para> |
| 10459 |
|
| 10460 |
<example id="ex22"> |
| 10461 |
<title>Simple <firstterm>for</firstterm> loops</title> |
| 10462 |
<programlisting>&ex22;</programlisting> |
| 10463 |
</example> |
| 10464 |
|
| 10465 |
|
| 10466 |
<para><anchor id="multparaml"></para> |
| 10467 |
<para>Each <userinput>[list]</userinput> element |
| 10468 |
may contain multiple parameters. This is useful when |
| 10469 |
processing parameters in groups. In such cases, |
| 10470 |
use the <link linkend="setref">set</link> command |
| 10471 |
(see <xref linkend="ex34">) to force parsing of each |
| 10472 |
<userinput>[list]</userinput> element and assignment of |
| 10473 |
each component to the positional parameters.</para> |
| 10474 |
|
| 10475 |
<example id="ex22a"> |
| 10476 |
<title><firstterm>for</firstterm> loop with two parameters in each |
| 10477 |
[list] element</title> |
| 10478 |
<programlisting>&ex22a;</programlisting> |
| 10479 |
</example> |
| 10480 |
|
| 10481 |
|
| 10482 |
<para><anchor id="paramli"></para> |
| 10483 |
<para>A variable may supply the <userinput>[list]</userinput> in a |
| 10484 |
<firstterm>for loop</firstterm>.</para> |
| 10485 |
|
| 10486 |
<example id="fileinfo"> |
| 10487 |
<title><emphasis>Fileinfo:</emphasis> operating on a file list |
| 10488 |
contained in a variable</title> |
| 10489 |
<programlisting>&fileinfo;</programlisting> |
| 10490 |
</example> |
| 10491 |
|
| 10492 |
<para><anchor id="paramli2"></para> |
| 10493 |
<para>The <userinput>[list]</userinput> in a |
| 10494 |
<firstterm>for loop</firstterm> may be parameterized.</para> |
| 10495 |
|
| 10496 |
<example id="fileinfo01"> |
| 10497 |
<title>Operating on a parameterized file list</title> |
| 10498 |
<programlisting>&fileinfo01;</programlisting> |
| 10499 |
</example> |
| 10500 |
|
| 10501 |
|
| 10502 |
<para><anchor id="liglob"></para> |
| 10503 |
<para>If the <userinput>[list]</userinput> in a |
| 10504 |
<firstterm>for loop</firstterm> contains wild cards |
| 10505 |
(<token>*</token> and <token>?</token>) used in filename |
| 10506 |
expansion, then <link linkend="globbingref">globbing</link> |
| 10507 |
takes place.</para> |
| 10508 |
|
| 10509 |
<example id="listglob"> |
| 10510 |
<title>Operating on files with a <firstterm>for</firstterm> loop</title> |
| 10511 |
<programlisting>&listglob;</programlisting> |
| 10512 |
</example> |
| 10513 |
|
| 10514 |
|
| 10515 |
<para><anchor id="omitlist"></para> |
| 10516 |
<para>Omitting the <userinput>in [list]</userinput> part of a |
| 10517 |
<firstterm>for loop</firstterm> causes the loop to operate |
| 10518 |
on <token>$@</token> -- the <link linkend="posparamref"> |
| 10519 |
positional parameters</link>. A particularly clever |
| 10520 |
illustration of this is <xref linkend="primes">. See also <xref |
| 10521 |
linkend="revposparams">.</para> |
| 10522 |
|
| 10523 |
<example id="ex23"> |
| 10524 |
<title>Missing <userinput>in [list]</userinput> in a |
| 10525 |
<firstterm>for</firstterm> loop</title> |
| 10526 |
<programlisting>&ex23;</programlisting> |
| 10527 |
</example> |
| 10528 |
|
| 10529 |
|
| 10530 |
<para><anchor id="loopcs"></para> |
| 10531 |
<para>It is possible to use <link |
| 10532 |
linkend="commandsubref">command substitution</link> |
| 10533 |
to generate the <userinput>[list]</userinput> in a |
| 10534 |
<firstterm>for loop</firstterm>. See also <xref linkend="ex53">, |
| 10535 |
<xref linkend="symlinks"> and <xref linkend="base">.</para> |
| 10536 |
|
| 10537 |
<example id="forloopcmd"> |
| 10538 |
<title>Generating the <userinput>[list]</userinput> in |
| 10539 |
a <firstterm>for</firstterm> loop with command substitution</title> |
| 10540 |
<programlisting>&forloopcmd;</programlisting> |
| 10541 |
</example> |
| 10542 |
|
| 10543 |
|
| 10544 |
<para>Here is a somewhat more complex example of using command |
| 10545 |
substitution to create the <userinput>[list]</userinput>.</para> |
| 10546 |
|
| 10547 |
<example id="bingrep"> |
| 10548 |
<title>A <firstterm>grep</firstterm> replacement |
| 10549 |
for binary files</title> |
| 10550 |
<programlisting>&bingrep;</programlisting> |
| 10551 |
</example> |
| 10552 |
|
| 10553 |
<para>More of the same.</para> |
| 10554 |
|
| 10555 |
<example id="userlist"> |
| 10556 |
<title>Listing all users on the system</title> |
| 10557 |
<programlisting>&userlist;</programlisting> |
| 10558 |
</example> |
| 10559 |
|
| 10560 |
<para>Yet another example of the <userinput>[list]</userinput> |
| 10561 |
resulting from command substitution.</para> |
| 10562 |
|
| 10563 |
<example id="findstring"> |
| 10564 |
<title>Checking all the binaries in a directory for |
| 10565 |
authorship</title> |
| 10566 |
<programlisting>&findstring;</programlisting> |
| 10567 |
</example> |
| 10568 |
|
| 10569 |
<para>A final example of <userinput>[list]</userinput> |
| 10570 |
/ command substitution, but this time |
| 10571 |
the <quote>command</quote> is a <link |
| 10572 |
linkend="functionref">function</link>.</para> |
| 10573 |
|
| 10574 |
<para><programlisting>generate_list () |
| 10575 |
{ |
| 10576 |
echo "one two three" |
| 10577 |
} |
| 10578 |
|
| 10579 |
for word in $(generate_list) # Let "word" grab output of function. |
| 10580 |
do |
| 10581 |
echo "$word" |
| 10582 |
done |
| 10583 |
|
| 10584 |
# one |
| 10585 |
# two |
| 10586 |
# three</programlisting></para> |
| 10587 |
|
| 10588 |
|
| 10589 |
<para><anchor id="loopredir"></para> |
| 10590 |
<para>The output of a <firstterm>for loop</firstterm> may |
| 10591 |
be piped to a command or commands.</para> |
| 10592 |
|
| 10593 |
<example id="symlinks"> |
| 10594 |
<title>Listing the <firstterm>symbolic |
| 10595 |
links</firstterm> in a directory</title> |
| 10596 |
<programlisting>&symlinks;</programlisting> |
| 10597 |
</example> |
| 10598 |
|
| 10599 |
<para>The <filename>stdout</filename> of a loop may be <link |
| 10600 |
linkend="ioredirref">redirected</link> to a file, as this slight |
| 10601 |
modification to the previous example shows.</para> |
| 10602 |
|
| 10603 |
<example id="symlinks2"> |
| 10604 |
<title>Symbolic links in a directory, saved to a file</title> |
| 10605 |
<programlisting>&symlinks2;</programlisting> |
| 10606 |
</example> |
| 10607 |
|
| 10608 |
<para><anchor id="loopcstyle"></para> |
| 10609 |
<para>There is an alternative syntax to a <firstterm>for |
| 10610 |
loop</firstterm> that will look very familiar to C |
| 10611 |
programmers. This requires <link |
| 10612 |
linkend="dblparensref">double parentheses</link>.</para> |
| 10613 |
|
| 10614 |
<example id="forloopc"> |
| 10615 |
<title>A C-style <firstterm>for</firstterm> loop</title> |
| 10616 |
<programlisting>&forloopc;</programlisting> |
| 10617 |
</example> |
| 10618 |
|
| 10619 |
<para>See also <xref linkend="qfunction">, <xref |
| 10620 |
linkend="twodim">, and <xref linkend="collatz">.</para> |
| 10621 |
|
| 10622 |
<para>---</para> |
| 10623 |
|
| 10624 |
<para>Now, a <firstterm>for loop</firstterm> used in a |
| 10625 |
<quote>real-life</quote> context.</para> |
| 10626 |
|
| 10627 |
<example id="ex24"> |
| 10628 |
<title>Using <firstterm>efax</firstterm> in batch mode</title> |
| 10629 |
<programlisting>&ex24;</programlisting> |
| 10630 |
</example> |
| 10631 |
|
| 10632 |
<note><para><anchor id="nododone">The |
| 10633 |
<link linkend="keywordref">keywords</link> |
| 10634 |
<command>do</command> and <command>done</command> delineate |
| 10635 |
the <firstterm>for-loop</firstterm> command block. However, |
| 10636 |
these may, in certain contexts, be omitted by framing the |
| 10637 |
command block within <link linkend="codeblockref">curly |
| 10638 |
brackets</link> |
| 10639 |
|
| 10640 |
<programlisting>for((n=1; n<=10; n++)) |
| 10641 |
# No do! |
| 10642 |
{ |
| 10643 |
echo -n "* $n *" |
| 10644 |
} |
| 10645 |
# No done! |
| 10646 |
|
| 10647 |
|
| 10648 |
# Outputs: |
| 10649 |
# * 1 ** 2 ** 3 ** 4 ** 5 ** 6 ** 7 ** 8 ** 9 ** 10 * |
| 10650 |
# And, echo $? returns 0, so Bash does not register an error. |
| 10651 |
|
| 10652 |
|
| 10653 |
echo |
| 10654 |
|
| 10655 |
|
| 10656 |
# But, note that in a classic for-loop: for n in [list] ... |
| 10657 |
#+ a terminal semicolon is required. |
| 10658 |
|
| 10659 |
for n in 1 2 3 |
| 10660 |
{ echo -n "$n "; } |
| 10661 |
# ^ |
| 10662 |
|
| 10663 |
|
| 10664 |
# Thank you, YongYe, for pointing this out.</programlisting> |
| 10665 |
</para></note> |
| 10666 |
|
| 10667 |
</listitem> |
| 10668 |
</varlistentry> |
| 10669 |
|
| 10670 |
|
| 10671 |
<varlistentry> |
| 10672 |
<term><anchor id="whileloopref"><command>while</command></term> |
| 10673 |
<indexterm> |
| 10674 |
<primary>while</primary> |
| 10675 |
</indexterm> |
| 10676 |
<indexterm> |
| 10677 |
<primary>do</primary> |
| 10678 |
</indexterm> |
| 10679 |
<indexterm> |
| 10680 |
<primary>done</primary> |
| 10681 |
</indexterm> |
| 10682 |
<indexterm> |
| 10683 |
<primary>loop</primary> |
| 10684 |
<secondary>while</secondary> |
| 10685 |
</indexterm> |
| 10686 |
<listitem> |
| 10687 |
<para>This construct tests for a condition at the top of a |
| 10688 |
loop, and keeps looping as long as that condition |
| 10689 |
is true (returns a <returnvalue>0</returnvalue> <link |
| 10690 |
linkend="exitstatusref">exit status</link>). In contrast |
| 10691 |
to a <link linkend="forloopref1">for loop</link>, a |
| 10692 |
<firstterm>while loop</firstterm> finds use in situations |
| 10693 |
where the number of loop repetitions is not known |
| 10694 |
beforehand.</para> |
| 10695 |
|
| 10696 |
<para><cmdsynopsis> |
| 10697 |
<command>while</command> |
| 10698 |
<arg choice="opt"><replaceable> condition </replaceable></arg><sbr> |
| 10699 |
<arg choice="plain">do</arg><sbr> |
| 10700 |
<arg choice="plain" rep="repeat"><replaceable> command(s)</replaceable></arg><sbr> |
| 10701 |
<arg choice="plain">done</arg> |
| 10702 |
</cmdsynopsis></para> |
| 10703 |
|
| 10704 |
|
| 10705 |
<para>The bracket construct in a <firstterm>while |
| 10706 |
loop</firstterm> is nothing more than our old friend, |
| 10707 |
the <link linkend="testconstructs1">test brackets</link> |
| 10708 |
used in an <firstterm>if/then</firstterm> test. In fact, |
| 10709 |
a <firstterm>while loop</firstterm> can legally use the |
| 10710 |
more versatile <link linkend="dblbrackets">double-brackets |
| 10711 |
construct</link> (while [[ condition ]]).</para> |
| 10712 |
|
| 10713 |
<para><anchor id="whileneedsemi"></para> |
| 10714 |
<para><link linkend="needsemicolon">As is the case with |
| 10715 |
<firstterm>for loops</firstterm></link>, placing the |
| 10716 |
<firstterm>do</firstterm> on the same line as the condition |
| 10717 |
test requires a semicolon.</para> |
| 10718 |
|
| 10719 |
<para><cmdsynopsis> |
| 10720 |
<command>while</command> |
| 10721 |
<arg choice="opt"><replaceable> condition </replaceable></arg> |
| 10722 |
<arg choice="plain">;</arg> |
| 10723 |
<arg choice="plain">do</arg> |
| 10724 |
</cmdsynopsis></para> |
| 10725 |
|
| 10726 |
<para>Note that the <firstterm>test brackets</firstterm> |
| 10727 |
<link linkend="whilenobrackets">are <emphasis>not</emphasis> |
| 10728 |
mandatory</link> in a <firstterm>while</firstterm> loop. |
| 10729 |
See, for example, the <link linkend="getoptsx">getopts |
| 10730 |
construct</link>.</para> |
| 10731 |
|
| 10732 |
<example id="ex25"> |
| 10733 |
<title>Simple <firstterm>while</firstterm> loop</title> |
| 10734 |
<programlisting>&ex25;</programlisting> |
| 10735 |
</example> |
| 10736 |
|
| 10737 |
<example id="ex26"> |
| 10738 |
<title>Another <firstterm>while</firstterm> loop</title> |
| 10739 |
<programlisting>&ex26;</programlisting> |
| 10740 |
</example> |
| 10741 |
|
| 10742 |
|
| 10743 |
<para><anchor id="whmultcond"></para> |
| 10744 |
<para>A <firstterm>while loop</firstterm> may have multiple |
| 10745 |
conditions. Only the final condition determines when the loop |
| 10746 |
terminates. This necessitates a slightly different loop syntax, |
| 10747 |
however.</para> |
| 10748 |
|
| 10749 |
<example id="ex26a"> |
| 10750 |
<title><firstterm>while</firstterm> loop with multiple conditions</title> |
| 10751 |
<programlisting>&ex26a;</programlisting> |
| 10752 |
</example> |
| 10753 |
|
| 10754 |
<para><anchor id="wloopcstyle"></para> |
| 10755 |
<para>As with a <firstterm>for loop</firstterm>, a |
| 10756 |
<firstterm>while loop</firstterm> may employ C-style syntax |
| 10757 |
by using the double-parentheses construct (see also <xref |
| 10758 |
linkend="cvars">).</para> |
| 10759 |
|
| 10760 |
<example id="whloopc"> |
| 10761 |
<title>C-style syntax in a <firstterm>while</firstterm> loop</title> |
| 10762 |
<programlisting>&whloopc;</programlisting> |
| 10763 |
</example> |
| 10764 |
|
| 10765 |
<para><anchor id="whilefunc"></para> |
| 10766 |
<para> |
| 10767 |
Inside its test brackets, a <firstterm>while loop</firstterm> |
| 10768 |
can call a <link linkend="functionref">function</link>. |
| 10769 |
|
| 10770 |
<programlisting>t=0 |
| 10771 |
|
| 10772 |
condition () |
| 10773 |
{ |
| 10774 |
((t++)) |
| 10775 |
|
| 10776 |
if [ $t -lt 5 ] |
| 10777 |
then |
| 10778 |
return 0 # true |
| 10779 |
else |
| 10780 |
return 1 # false |
| 10781 |
fi |
| 10782 |
} |
| 10783 |
|
| 10784 |
while condition |
| 10785 |
# ^^^^^^^^^ |
| 10786 |
# Function call -- four loop iterations. |
| 10787 |
do |
| 10788 |
echo "Still going: t = $t" |
| 10789 |
done |
| 10790 |
|
| 10791 |
# Still going: t = 1 |
| 10792 |
# Still going: t = 2 |
| 10793 |
# Still going: t = 3 |
| 10794 |
# Still going: t = 4</programlisting> |
| 10795 |
|
| 10796 |
|
| 10797 |
|
| 10798 |
</para> |
| 10799 |
|
| 10800 |
|
| 10801 |
<sidebar><para><anchor id="whilenobrackets"></para> |
| 10802 |
<para>Similar to the <link linkend="ifgrepref">if-test</link> |
| 10803 |
construct, a <firstterm>while</firstterm> loop can omit the test |
| 10804 |
brackets. |
| 10805 |
<programlisting>while condition |
| 10806 |
do |
| 10807 |
command(s) ... |
| 10808 |
done</programlisting></para></sidebar> |
| 10809 |
|
| 10810 |
<para><anchor id="whilereadref2"></para> |
| 10811 |
<para>By coupling the power of the <link |
| 10812 |
linkend="readref">read</link> command with a |
| 10813 |
<firstterm>while loop</firstterm>, we get the handy <link |
| 10814 |
linkend="whilereadref">while read</link> construct, useful |
| 10815 |
for reading and parsing files.</para> |
| 10816 |
|
| 10817 |
<para><programlisting>cat $filename | # Supply input from a file. |
| 10818 |
while read line # As long as there is another line to read ... |
| 10819 |
do |
| 10820 |
... |
| 10821 |
done |
| 10822 |
|
| 10823 |
# =========== Snippet from "sd.sh" example script ========== # |
| 10824 |
|
| 10825 |
while read value # Read one data point at a time. |
| 10826 |
do |
| 10827 |
rt=$(echo "scale=$SC; $rt + $value" | bc) |
| 10828 |
(( ct++ )) |
| 10829 |
done |
| 10830 |
|
| 10831 |
am=$(echo "scale=$SC; $rt / $ct" | bc) |
| 10832 |
|
| 10833 |
echo $am; return $ct # This function "returns" TWO values! |
| 10834 |
# Caution: This little trick will not work if $ct > 255! |
| 10835 |
# To handle a larger number of data points, |
| 10836 |
#+ simply comment out the "return $ct" above. |
| 10837 |
} <"$datafile" # Feed in data file.</programlisting></para> |
| 10838 |
|
| 10839 |
<para><anchor id="whredir"></para> |
| 10840 |
<note> |
| 10841 |
<para>A <firstterm>while loop</firstterm> may have its |
| 10842 |
<filename>stdin</filename> <link |
| 10843 |
linkend="redirref">redirected to a file</link> by a |
| 10844 |
<token><</token> at its end.</para> |
| 10845 |
|
| 10846 |
<para>A <firstterm>while loop</firstterm> may have its |
| 10847 |
<filename>stdin</filename> <link linkend="readpiperef"> |
| 10848 |
supplied by a pipe</link>.</para> |
| 10849 |
</note> |
| 10850 |
|
| 10851 |
</listitem> |
| 10852 |
</varlistentry> |
| 10853 |
|
| 10854 |
|
| 10855 |
|
| 10856 |
<varlistentry> |
| 10857 |
<term><anchor id="untilloopref"><command>until</command></term> |
| 10858 |
<indexterm> |
| 10859 |
<primary>until</primary> |
| 10860 |
</indexterm> |
| 10861 |
<indexterm> |
| 10862 |
<primary>do</primary> |
| 10863 |
</indexterm> |
| 10864 |
<indexterm> |
| 10865 |
<primary>done</primary> |
| 10866 |
</indexterm> |
| 10867 |
<indexterm> |
| 10868 |
<primary>loop</primary> |
| 10869 |
<secondary>until</secondary> |
| 10870 |
</indexterm> |
| 10871 |
|
| 10872 |
<listitem> |
| 10873 |
<para>This construct tests for a condition at the top of a loop, and keeps |
| 10874 |
looping as long as that condition is |
| 10875 |
<emphasis>false</emphasis> (opposite of <firstterm>while |
| 10876 |
loop</firstterm>).</para> |
| 10877 |
|
| 10878 |
<para><cmdsynopsis> |
| 10879 |
<command>until</command> |
| 10880 |
<arg choice="opt"><replaceable> condition-is-true </replaceable></arg><sbr> |
| 10881 |
<arg choice="plain">do</arg><sbr> |
| 10882 |
<arg choice="plain" rep="repeat"><replaceable> command(s)</replaceable></arg><sbr> |
| 10883 |
<arg choice="plain">done</arg> |
| 10884 |
</cmdsynopsis></para> |
| 10885 |
|
| 10886 |
<para>Note that an <firstterm>until loop</firstterm> tests for the |
| 10887 |
terminating condition at the <emphasis>top</emphasis> |
| 10888 |
of the loop, differing from a similar construct in some |
| 10889 |
programming languages.</para> |
| 10890 |
|
| 10891 |
<para>As is the case with <firstterm>for loops</firstterm>, |
| 10892 |
placing the <firstterm>do</firstterm> on the same line as |
| 10893 |
the condition test requires a semicolon.</para> |
| 10894 |
|
| 10895 |
<para><cmdsynopsis> |
| 10896 |
<command>until</command> |
| 10897 |
<arg choice="opt"><replaceable> condition-is-true </replaceable></arg> |
| 10898 |
<arg choice="plain">;</arg> |
| 10899 |
<arg choice="plain">do</arg> |
| 10900 |
</cmdsynopsis></para> |
| 10901 |
|
| 10902 |
<example id="ex27"> |
| 10903 |
<title><firstterm>until</firstterm> loop</title> |
| 10904 |
<programlisting>&ex27;</programlisting> |
| 10905 |
</example> |
| 10906 |
</listitem> |
| 10907 |
</varlistentry> |
| 10908 |
|
| 10909 |
</variablelist> |
| 10910 |
|
| 10911 |
<para><anchor id="chooseloop"></para> |
| 10912 |
<para>How to choose between a <firstterm>for</firstterm> loop or a |
| 10913 |
<firstterm>while</firstterm> loop or |
| 10914 |
<firstterm>until</firstterm> loop? In <command>C</command>, |
| 10915 |
you would typically use a <firstterm>for</firstterm> loop |
| 10916 |
when the number of loop iterations is known beforehand. With |
| 10917 |
<firstterm>Bash</firstterm>, however, the situation is |
| 10918 |
fuzzier. The Bash <firstterm>for</firstterm> loop is more |
| 10919 |
loosely structured and more flexible than its equivalent in |
| 10920 |
other languages. Therefore, feel free to use whatever type |
| 10921 |
of loop gets the job done in the simplest way.</para> |
| 10922 |
|
| 10923 |
</sect1> <!-- Loops --> |
| 10924 |
|
| 10925 |
|
| 10926 |
|
| 10927 |
<sect1 id="nestedloops"> |
| 10928 |
<title>Nested Loops</title> |
| 10929 |
|
| 10930 |
<para>A <firstterm>nested loop</firstterm> is a loop within a |
| 10931 |
loop, an inner loop within the body of an outer one. How |
| 10932 |
this works is that the first pass of the outer loop triggers |
| 10933 |
the inner loop, which executes to completion. Then the |
| 10934 |
second pass of the outer loop triggers the inner loop |
| 10935 |
again. This repeats until the outer loop finishes. Of course, |
| 10936 |
a <firstterm>break</firstterm> within either the inner or outer |
| 10937 |
loop would interrupt this process.</para> |
| 10938 |
|
| 10939 |
<example id="nestedloop"> |
| 10940 |
<title>Nested Loop</title> |
| 10941 |
<programlisting>&nestedloop;</programlisting> |
| 10942 |
</example> |
| 10943 |
|
| 10944 |
<para>See <xref linkend="bubble"> for an illustration of nested |
| 10945 |
<link linkend="whileloopref">while loops</link>, and <xref |
| 10946 |
linkend="ex68"> to see a while loop nested inside an <link |
| 10947 |
linkend="untilloopref">until loop</link>.</para> |
| 10948 |
|
| 10949 |
</sect1> <!-- Nested Loops --> |
| 10950 |
|
| 10951 |
|
| 10952 |
<sect1 id="loopcontrol"> |
| 10953 |
<title>Loop Control</title> |
| 10954 |
|
| 10955 |
<epigraph> |
| 10956 |
<para>Tournez cent tours, tournez mille tours,</para> |
| 10957 |
<para>Tournez souvent et tournez toujours . . .</para> |
| 10958 |
<para>--Verlaine, <quote>Chevaux de bois</quote></para> |
| 10959 |
</epigraph> |
| 10960 |
|
| 10961 |
<variablelist id="brkcont"> |
| 10962 |
<title><anchor id="brkcont1">Commands affecting loop behavior</title> |
| 10963 |
|
| 10964 |
<varlistentry> |
| 10965 |
<indexterm> |
| 10966 |
<primary>break</primary> |
| 10967 |
</indexterm> |
| 10968 |
<indexterm> |
| 10969 |
<primary>continue</primary> |
| 10970 |
</indexterm> |
| 10971 |
<indexterm> |
| 10972 |
<primary>loop</primary> |
| 10973 |
<secondary>break</secondary> |
| 10974 |
</indexterm> |
| 10975 |
<indexterm> |
| 10976 |
<primary>loop</primary> |
| 10977 |
<secondary>continue</secondary> |
| 10978 |
</indexterm> |
| 10979 |
<term><command>break</command></term> |
| 10980 |
<term><command>continue</command></term> |
| 10981 |
<listitem> |
| 10982 |
<para>The <command>break</command> and <command>continue</command> |
| 10983 |
loop control commands |
| 10984 |
<footnote><para>These are shell <link |
| 10985 |
linkend="builtinref">builtins</link>, |
| 10986 |
whereas other loop commands, such as <link |
| 10987 |
linkend="whileloopref">while</link> and <link |
| 10988 |
linkend="caseesac1">case</link>, are <link |
| 10989 |
linkend="keywordref">keywords</link>.</para></footnote> |
| 10990 |
correspond exactly to their counterparts in other |
| 10991 |
programming languages. The <command>break</command> |
| 10992 |
command terminates the loop (<emphasis>breaks</emphasis> |
| 10993 |
out of it), while <command>continue</command> causes a jump |
| 10994 |
to the next <link linkend="iterationref">iteration</link> |
| 10995 |
of the loop, skipping all the remaining commands in that |
| 10996 |
particular loop cycle.</para> |
| 10997 |
|
| 10998 |
<example id="ex28"> |
| 10999 |
<title>Effects of <firstterm>break</firstterm> and |
| 11000 |
<command>continue</command> in a loop</title> |
| 11001 |
<programlisting>&ex28;</programlisting> |
| 11002 |
</example> |
| 11003 |
|
| 11004 |
<para><anchor id="breakparam"></para> |
| 11005 |
<para>The <command>break</command> command may optionally take a |
| 11006 |
parameter. A plain <command>break</command> terminates |
| 11007 |
only the innermost loop in which it is embedded, |
| 11008 |
but a <command>break N</command> breaks out of |
| 11009 |
<parameter>N</parameter> levels of loop.</para> |
| 11010 |
|
| 11011 |
<example id="breaklevels"> |
| 11012 |
<title>Breaking out of multiple loop levels</title> |
| 11013 |
<programlisting>&breaklevels;</programlisting> |
| 11014 |
</example> |
| 11015 |
|
| 11016 |
<para>The <command>continue</command> command, similar to |
| 11017 |
<command>break</command>, optionally takes a parameter. A |
| 11018 |
plain <command>continue</command> cuts short the |
| 11019 |
current iteration within its loop and begins the next. |
| 11020 |
A <command>continue N</command> terminates all remaining |
| 11021 |
iterations at its loop level and continues with the |
| 11022 |
next iteration at the loop, <option>N</option> levels |
| 11023 |
above.</para> |
| 11024 |
|
| 11025 |
<example id="continuelevels"> |
| 11026 |
<title>Continuing at a higher loop level</title> |
| 11027 |
<programlisting>&continuelevels;</programlisting> |
| 11028 |
</example> |
| 11029 |
|
| 11030 |
<example id="continuenex"> |
| 11031 |
<title>Using <firstterm>continue N</firstterm> in an actual task</title> |
| 11032 |
<programlisting>&continuenex;</programlisting> |
| 11033 |
</example> |
| 11034 |
|
| 11035 |
<caution><para>The <command>continue N</command> construct is |
| 11036 |
difficult to understand and tricky to use in any meaningful |
| 11037 |
context. It is probably best avoided.</para></caution> |
| 11038 |
|
| 11039 |
</listitem> |
| 11040 |
</varlistentry> |
| 11041 |
|
| 11042 |
</variablelist> |
| 11043 |
|
| 11044 |
</sect1> <!-- Loop Control Commands --> |
| 11045 |
|
| 11046 |
|
| 11047 |
|
| 11048 |
<sect1 id="testbranch"> |
| 11049 |
<title>Testing and Branching</title> |
| 11050 |
|
| 11051 |
<para>The <command>case</command> and <command>select</command> |
| 11052 |
constructs are technically not loops, since they do not iterate the |
| 11053 |
execution of a code block. Like loops, however, they direct |
| 11054 |
program flow according to conditions at the top or bottom of |
| 11055 |
the block.</para> |
| 11056 |
|
| 11057 |
<variablelist id="caseesac"> |
| 11058 |
<title><anchor id="caseesac1">Controlling program flow in a code |
| 11059 |
block</title> |
| 11060 |
|
| 11061 |
<varlistentry> |
| 11062 |
<term><command>case (in) / esac</command></term> |
| 11063 |
<indexterm> |
| 11064 |
<primary>case</primary> |
| 11065 |
</indexterm> |
| 11066 |
<indexterm> |
| 11067 |
<primary>in</primary> |
| 11068 |
</indexterm> |
| 11069 |
<indexterm> |
| 11070 |
<primary>esac</primary> |
| 11071 |
</indexterm> |
| 11072 |
<indexterm> |
| 11073 |
<primary>switch</primary> |
| 11074 |
</indexterm> |
| 11075 |
<indexterm> |
| 11076 |
<primary>;;</primary> |
| 11077 |
</indexterm> |
| 11078 |
<indexterm> |
| 11079 |
<primary>menus</primary> |
| 11080 |
</indexterm> |
| 11081 |
|
| 11082 |
<listitem> |
| 11083 |
<para>The <command>case</command> construct is the shell |
| 11084 |
scripting analog to <replaceable>switch</replaceable> |
| 11085 |
in <command>C/C++</command>. |
| 11086 |
It permits branching to one of a number of code blocks, |
| 11087 |
depending on condition tests. It serves as a kind of |
| 11088 |
shorthand for multiple <token>if/then/else</token> |
| 11089 |
statements and is an appropriate tool for creating |
| 11090 |
menus.</para> |
| 11091 |
|
| 11092 |
<para><cmdsynopsis> |
| 11093 |
<command>case</command> |
| 11094 |
<arg choice="plain">"$<replaceable>variable</replaceable>"</arg> |
| 11095 |
<arg choice="plain">in</arg><sbr><sbr> |
| 11096 |
<arg choice="plain"> "$<replaceable>condition1</replaceable>" )</arg><sbr> |
| 11097 |
<arg choice="plain" rep="repeat"> <replaceable>command</replaceable></arg><sbr> |
| 11098 |
<arg choice="plain"> ;;</arg><sbr><sbr> |
| 11099 |
<arg choice="plain"> "$<replaceable>condition2</replaceable>" )</arg><sbr> |
| 11100 |
<arg choice="plain" rep="repeat"> <replaceable>command</replaceable></arg><sbr> |
| 11101 |
<arg choice="plain"> ;;</arg><sbr><sbr> |
| 11102 |
<command>esac</command> |
| 11103 |
</cmdsynopsis></para> |
| 11104 |
|
| 11105 |
<note><para> |
| 11106 |
<itemizedlist> |
| 11107 |
<listitem><para>Quoting the variables is not mandatory, |
| 11108 |
since word splitting does not take place.</para> |
| 11109 |
</listitem> |
| 11110 |
<listitem> |
| 11111 |
|
| 11112 |
<para><anchor id="caseparen">Each test line |
| 11113 |
ends with a right paren <command>)</command>. |
| 11114 |
|
| 11115 |
<footnote><para>Pattern-match lines may also <emphasis>start</emphasis> |
| 11116 |
with a <command>(</command> left paren to give the layout |
| 11117 |
a more structured appearance.</para> |
| 11118 |
<para><programlisting>case $( arch ) in # $( arch ) returns machine architecture. |
| 11119 |
( i386 ) echo "80386-based machine";; |
| 11120 |
# ^ ^ |
| 11121 |
( i486 ) echo "80486-based machine";; |
| 11122 |
( i586 ) echo "Pentium-based machine";; |
| 11123 |
( i686 ) echo "Pentium2+-based machine";; |
| 11124 |
( * ) echo "Other type of machine";; |
| 11125 |
esac</programlisting></para></footnote> |
| 11126 |
</para> |
| 11127 |
|
| 11128 |
</listitem> |
| 11129 |
|
| 11130 |
<listitem><para>Each condition block ends |
| 11131 |
with a <emphasis>double</emphasis> semicolon |
| 11132 |
<token>;;</token>.</para> |
| 11133 |
</listitem> |
| 11134 |
<listitem><para>If a condition tests |
| 11135 |
<firstterm>true</firstterm>, then the associated |
| 11136 |
commands execute and the <command>case</command> |
| 11137 |
block terminates.</para></listitem> |
| 11138 |
<listitem><para>The entire <command>case</command> |
| 11139 |
block ends with an <command>esac</command> |
| 11140 |
(<wordasword>case</wordasword> spelled backwards).</para> |
| 11141 |
</listitem> |
| 11142 |
</itemizedlist> |
| 11143 |
</para></note> |
| 11144 |
|
| 11145 |
<example id="ex29"> |
| 11146 |
<title>Using <firstterm>case</firstterm></title> |
| 11147 |
<programlisting>&ex29;</programlisting> |
| 11148 |
</example> |
| 11149 |
|
| 11150 |
<example id="ex30"> |
| 11151 |
<title>Creating menus using <firstterm>case</firstterm></title> |
| 11152 |
<programlisting>&ex30;</programlisting> |
| 11153 |
</example> |
| 11154 |
|
| 11155 |
<para><anchor id="casecl"></para> |
| 11156 |
<para>An exceptionally clever use of <command>case</command> |
| 11157 |
involves testing for command-line parameters. |
| 11158 |
<programlisting>#! /bin/bash |
| 11159 |
|
| 11160 |
case "$1" in |
| 11161 |
"") echo "Usage: ${0##*/} <filename>"; exit $E_PARAM;; |
| 11162 |
# No command-line parameters, |
| 11163 |
# or first parameter empty. |
| 11164 |
# Note that ${0##*/} is ${var##pattern} param substitution. |
| 11165 |
# Net result is $0. |
| 11166 |
|
| 11167 |
-*) FILENAME=./$1;; # If filename passed as argument ($1) |
| 11168 |
#+ starts with a dash, |
| 11169 |
#+ replace it with ./$1 |
| 11170 |
#+ so further commands don't interpret it |
| 11171 |
#+ as an option. |
| 11172 |
|
| 11173 |
* ) FILENAME=$1;; # Otherwise, $1. |
| 11174 |
esac</programlisting></para> |
| 11175 |
|
| 11176 |
<para>Here is a more straightforward example of |
| 11177 |
command-line parameter handling: |
| 11178 |
<programlisting>#! /bin/bash |
| 11179 |
|
| 11180 |
|
| 11181 |
while [ $# -gt 0 ]; do # Until you run out of parameters . . . |
| 11182 |
case "$1" in |
| 11183 |
-d|--debug) |
| 11184 |
# "-d" or "--debug" parameter? |
| 11185 |
DEBUG=1 |
| 11186 |
;; |
| 11187 |
-c|--conf) |
| 11188 |
CONFFILE="$2" |
| 11189 |
shift |
| 11190 |
if [ ! -f $CONFFILE ]; then |
| 11191 |
echo "Error: Supplied file doesn't exist!" |
| 11192 |
exit $E_CONFFILE # File not found error. |
| 11193 |
fi |
| 11194 |
;; |
| 11195 |
esac |
| 11196 |
shift # Check next set of parameters. |
| 11197 |
done |
| 11198 |
|
| 11199 |
# From Stefano Falsetto's "Log2Rot" script, |
| 11200 |
#+ part of his "rottlog" package. |
| 11201 |
# Used with permission.</programlisting></para> |
| 11202 |
|
| 11203 |
|
| 11204 |
<example id="casecmd"> |
| 11205 |
<title>Using <firstterm>command substitution</firstterm> |
| 11206 |
to generate the <firstterm>case</firstterm> variable</title> |
| 11207 |
<programlisting>&casecmd;</programlisting> |
| 11208 |
</example> |
| 11209 |
|
| 11210 |
<para><anchor id="csglob"></para> |
| 11211 |
<para>A <command>case</command> construct can filter strings for |
| 11212 |
<link linkend="globbingref">globbing</link> patterns.</para> |
| 11213 |
|
| 11214 |
<example id="matchstring"> |
| 11215 |
<title>Simple string matching</title> |
| 11216 |
<programlisting>&matchstring;</programlisting> |
| 11217 |
</example> |
| 11218 |
|
| 11219 |
<example id="isalpha"> |
| 11220 |
<title>Checking for alphabetic input</title> |
| 11221 |
<programlisting>&isalpha;</programlisting> |
| 11222 |
</example> |
| 11223 |
|
| 11224 |
</listitem> |
| 11225 |
</varlistentry> |
| 11226 |
|
| 11227 |
|
| 11228 |
|
| 11229 |
<varlistentry> |
| 11230 |
<term><anchor id="selectref"><command>select</command></term> |
| 11231 |
<indexterm> |
| 11232 |
<primary>select</primary> |
| 11233 |
</indexterm> |
| 11234 |
<indexterm> |
| 11235 |
<primary>menus</primary> |
| 11236 |
</indexterm> |
| 11237 |
<listitem> |
| 11238 |
<para>The <command>select</command> construct, adopted from the Korn |
| 11239 |
Shell, is yet another tool for building menus.</para> |
| 11240 |
|
| 11241 |
<para><cmdsynopsis> |
| 11242 |
<command>select</command> |
| 11243 |
<arg choice="plain"><replaceable>variable</replaceable></arg> |
| 11244 |
<arg choice="opt">in <replaceable>list</replaceable></arg><sbr> |
| 11245 |
<arg choice="plain">do</arg><sbr> |
| 11246 |
<arg choice="plain" rep="repeat"> <replaceable>command</replaceable></arg><sbr> |
| 11247 |
<arg choice="plain"> break</arg><sbr> |
| 11248 |
<arg choice="plain">done</arg> |
| 11249 |
</cmdsynopsis></para> |
| 11250 |
|
| 11251 |
<para>This prompts the user to enter one of the choices presented in the |
| 11252 |
variable list. Note that <command>select</command> uses the |
| 11253 |
<varname>$PS3</varname> prompt (<prompt>#? </prompt>) by default, |
| 11254 |
but this may be changed.</para> |
| 11255 |
|
| 11256 |
<example id="ex31"> |
| 11257 |
<title>Creating menus using <firstterm>select</firstterm></title> |
| 11258 |
<programlisting>&ex31;</programlisting> |
| 11259 |
</example> |
| 11260 |
|
| 11261 |
<para><anchor id="inlistomit"></para> |
| 11262 |
<para>If <userinput>in <replaceable>list</replaceable></userinput> is |
| 11263 |
omitted, then <command>select</command> uses the list of command |
| 11264 |
line arguments (<varname>$@</varname>) passed to the script or |
| 11265 |
the function containing the <command>select</command> |
| 11266 |
construct.</para> |
| 11267 |
|
| 11268 |
<para>Compare this to the behavior of a |
| 11269 |
<cmdsynopsis> |
| 11270 |
<command>for</command> |
| 11271 |
<arg choice="plain"><replaceable>variable</replaceable></arg> |
| 11272 |
<arg choice="opt">in <replaceable>list</replaceable></arg> |
| 11273 |
</cmdsynopsis> |
| 11274 |
construct with the |
| 11275 |
<userinput>in <replaceable>list</replaceable></userinput> |
| 11276 |
omitted.</para> |
| 11277 |
|
| 11278 |
<example id="ex32"> |
| 11279 |
<title>Creating menus using <firstterm>select</firstterm> |
| 11280 |
in a function</title> |
| 11281 |
<programlisting>&ex32;</programlisting> |
| 11282 |
</example> |
| 11283 |
|
| 11284 |
<para>See also <xref linkend="resistor">.</para> |
| 11285 |
|
| 11286 |
</listitem> |
| 11287 |
</varlistentry> |
| 11288 |
|
| 11289 |
</variablelist> |
| 11290 |
|
| 11291 |
</sect1> <!-- Testing and Branching --> |
| 11292 |
|
| 11293 |
|
| 11294 |
</chapter> <!-- Loops --> |
| 11295 |
|
| 11296 |
|
| 11297 |
|
| 11298 |
<chapter id="commandsub"> |
| 11299 |
<title>Command Substitution</title> |
| 11300 |
|
| 11301 |
<indexterm> |
| 11302 |
<primary>$</primary> |
| 11303 |
</indexterm> |
| 11304 |
<indexterm> |
| 11305 |
<primary>special character</primary> |
| 11306 |
<secondary>`</secondary> |
| 11307 |
</indexterm> |
| 11308 |
|
| 11309 |
<para> |
| 11310 |
<anchor id="commandsubref"><command>Command |
| 11311 |
substitution</command> reassigns the output of a command |
| 11312 |
<footnote><para>For purposes of <firstterm>command |
| 11313 |
substitution</firstterm>, a <command>command</command> |
| 11314 |
may be an external system command, an internal scripting |
| 11315 |
<link linkend="builtinref">builtin</link>, or even <link |
| 11316 |
linkend="rvt">a script function</link>.</para></footnote> |
| 11317 |
or even multiple commands; it literally plugs the command |
| 11318 |
output into another context. |
| 11319 |
|
| 11320 |
<footnote><para>In a more technically correct sense, |
| 11321 |
<firstterm>command substitution</firstterm> extracts the |
| 11322 |
<filename>stdout</filename> of a command, then assigns |
| 11323 |
it to a variable using the <token>=</token> |
| 11324 |
operator.</para></footnote> |
| 11325 |
</para> |
| 11326 |
|
| 11327 |
|
| 11328 |
<para><anchor id="backquotesref">The classic form of command |
| 11329 |
substitution uses <firstterm>backquotes</firstterm> |
| 11330 |
(`...`). Commands within backquotes (backticks) generate |
| 11331 |
command-line text. |
| 11332 |
|
| 11333 |
<programlisting>script_name=`basename $0` |
| 11334 |
echo "The name of this script is $script_name."</programlisting></para> |
| 11335 |
|
| 11336 |
|
| 11337 |
<formalpara> |
| 11338 |
<title>The output of commands can be used as arguments to |
| 11339 |
another command, to set a variable, and even for generating |
| 11340 |
the argument list in a <link linkend="forloopref1">for</link> |
| 11341 |
loop.</title> |
| 11342 |
<para></para> |
| 11343 |
</formalpara> |
| 11344 |
|
| 11345 |
<para> |
| 11346 |
<programlisting>rm `cat filename` # <quote>filename</quote> contains a list of files to delete. |
| 11347 |
# |
| 11348 |
# S. C. points out that "arg list too long" error might result. |
| 11349 |
# Better is xargs rm -- < filename |
| 11350 |
# ( -- covers those cases where <quote>filename</quote> begins with a <quote>-</quote> ) |
| 11351 |
|
| 11352 |
textfile_listing=`ls *.txt` |
| 11353 |
# Variable contains names of all *.txt files in current working directory. |
| 11354 |
echo $textfile_listing |
| 11355 |
|
| 11356 |
textfile_listing2=$(ls *.txt) # The alternative form of command substitution. |
| 11357 |
echo $textfile_listing2 |
| 11358 |
# Same result. |
| 11359 |
|
| 11360 |
# A possible problem with putting a list of files into a single string |
| 11361 |
# is that a newline may creep in. |
| 11362 |
# |
| 11363 |
# A safer way to assign a list of files to a parameter is with an array. |
| 11364 |
# shopt -s nullglob # If no match, filename expands to nothing. |
| 11365 |
# textfile_listing=( *.txt ) |
| 11366 |
# |
| 11367 |
# Thanks, S.C.</programlisting> |
| 11368 |
</para> |
| 11369 |
|
| 11370 |
<note><para><anchor id="cssubsh">Command substitution |
| 11371 |
invokes a <link |
| 11372 |
linkend="subshellsref">subshell</link>.</para></note> |
| 11373 |
|
| 11374 |
|
| 11375 |
|
| 11376 |
<caution><para><anchor id="csws">Command substitution may |
| 11377 |
result in <link linkend="wsplitref">word splitting</link>. |
| 11378 |
<programlisting>COMMAND `echo a b` # 2 args: a and b |
| 11379 |
|
| 11380 |
COMMAND "`echo a b`" # 1 arg: "a b" |
| 11381 |
|
| 11382 |
COMMAND `echo` # no arg |
| 11383 |
|
| 11384 |
COMMAND "`echo`" # one empty arg |
| 11385 |
|
| 11386 |
|
| 11387 |
# Thanks, S.C.</programlisting></para> |
| 11388 |
|
| 11389 |
|
| 11390 |
<para><anchor id="cstrnl"></para> |
| 11391 |
<para>Even when there is no word splitting, command |
| 11392 |
substitution can remove trailing newlines. |
| 11393 |
|
| 11394 |
<programlisting># cd "`pwd`" # This should always work. |
| 11395 |
# However... |
| 11396 |
|
| 11397 |
mkdir 'dir with trailing newline |
| 11398 |
' |
| 11399 |
|
| 11400 |
cd 'dir with trailing newline |
| 11401 |
' |
| 11402 |
|
| 11403 |
cd "`pwd`" # Error message: |
| 11404 |
# bash: cd: /tmp/file with trailing newline: No such file or directory |
| 11405 |
|
| 11406 |
cd "$PWD" # Works fine. |
| 11407 |
|
| 11408 |
|
| 11409 |
|
| 11410 |
|
| 11411 |
|
| 11412 |
old_tty_setting=$(stty -g) # Save old terminal setting. |
| 11413 |
echo "Hit a key " |
| 11414 |
stty -icanon -echo # Disable "canonical" mode for terminal. |
| 11415 |
# Also, disable *local* echo. |
| 11416 |
key=$(dd bs=1 count=1 2> /dev/null) # Using 'dd' to get a keypress. |
| 11417 |
stty "$old_tty_setting" # Restore old setting. |
| 11418 |
echo "You hit ${#key} key." # ${#variable} = number of characters in $variable |
| 11419 |
# |
| 11420 |
# Hit any key except RETURN, and the output is "You hit 1 key." |
| 11421 |
# Hit RETURN, and it's "You hit 0 key." |
| 11422 |
# The newline gets eaten in the command substitution. |
| 11423 |
|
| 11424 |
#Code snippet by Stéphane Chazelas.</programlisting> |
| 11425 |
</para> |
| 11426 |
</caution> |
| 11427 |
|
| 11428 |
|
| 11429 |
<caution> |
| 11430 |
<para>Using <command>echo</command> to output an |
| 11431 |
<firstterm>unquoted</firstterm> variable set with command |
| 11432 |
substitution removes trailing newlines characters from |
| 11433 |
the output of the reassigned command(s). This can cause |
| 11434 |
unpleasant surprises. |
| 11435 |
|
| 11436 |
<programlisting>dir_listing=`ls -l` |
| 11437 |
echo $dir_listing # unquoted |
| 11438 |
|
| 11439 |
# Expecting a nicely ordered directory listing. |
| 11440 |
|
| 11441 |
# However, what you get is: |
| 11442 |
# total 3 -rw-rw-r-- 1 bozo bozo 30 May 13 17:15 1.txt -rw-rw-r-- 1 bozo |
| 11443 |
# bozo 51 May 15 20:57 t2.sh -rwxr-xr-x 1 bozo bozo 217 Mar 5 21:13 wi.sh |
| 11444 |
|
| 11445 |
# The newlines disappeared. |
| 11446 |
|
| 11447 |
|
| 11448 |
echo "$dir_listing" # quoted |
| 11449 |
# -rw-rw-r-- 1 bozo 30 May 13 17:15 1.txt |
| 11450 |
# -rw-rw-r-- 1 bozo 51 May 15 20:57 t2.sh |
| 11451 |
# -rwxr-xr-x 1 bozo 217 Mar 5 21:13 wi.sh</programlisting> |
| 11452 |
</para> |
| 11453 |
</caution> |
| 11454 |
|
| 11455 |
|
| 11456 |
|
| 11457 |
<para>Command substitution even permits setting a variable to the |
| 11458 |
contents of a file, using either <link |
| 11459 |
linkend="ioredirref">redirection</link> or the <link |
| 11460 |
linkend="catref">cat</link> command.</para> |
| 11461 |
|
| 11462 |
<para> |
| 11463 |
<programlisting>variable1=`<file1` # Set "variable1" to contents of "file1". |
| 11464 |
variable2=`cat file2` # Set "variable2" to contents of "file2". |
| 11465 |
# This, however, forks a new process, |
| 11466 |
#+ so the line of code executes slower than the above version. |
| 11467 |
|
| 11468 |
# Note that the variables may contain embedded whitespace, |
| 11469 |
#+ or even (horrors), control characters. |
| 11470 |
|
| 11471 |
# It is not necessary to explicitly assign a variable. |
| 11472 |
echo "` <$0`" # Echoes the script itself to stdout.</programlisting> |
| 11473 |
</para> |
| 11474 |
|
| 11475 |
<para> |
| 11476 |
<programlisting># Excerpts from system file, /etc/rc.d/rc.sysinit |
| 11477 |
#+ (on a Red Hat Linux installation) |
| 11478 |
|
| 11479 |
|
| 11480 |
if [ -f /fsckoptions ]; then |
| 11481 |
fsckoptions=`cat /fsckoptions` |
| 11482 |
... |
| 11483 |
fi |
| 11484 |
# |
| 11485 |
# |
| 11486 |
if [ -e "/proc/ide/${disk[$device]}/media" ] ; then |
| 11487 |
hdmedia=`cat /proc/ide/${disk[$device]}/media` |
| 11488 |
... |
| 11489 |
fi |
| 11490 |
# |
| 11491 |
# |
| 11492 |
if [ ! -n "`uname -r | grep -- "-"`" ]; then |
| 11493 |
ktag="`cat /proc/version`" |
| 11494 |
... |
| 11495 |
fi |
| 11496 |
# |
| 11497 |
# |
| 11498 |
if [ $usb = "1" ]; then |
| 11499 |
sleep 5 |
| 11500 |
mouseoutput=`cat /proc/bus/usb/devices 2>/dev/null|grep -E "^I.*Cls=03.*Prot=02"` |
| 11501 |
kbdoutput=`cat /proc/bus/usb/devices 2>/dev/null|grep -E "^I.*Cls=03.*Prot=01"` |
| 11502 |
... |
| 11503 |
fi</programlisting> |
| 11504 |
</para> |
| 11505 |
|
| 11506 |
<caution> |
| 11507 |
<para>Do not set a variable to the contents of a |
| 11508 |
<emphasis>long</emphasis> text file unless you have a very good |
| 11509 |
reason for doing so. Do not set a variable to the contents of a |
| 11510 |
<firstterm>binary</firstterm> file, even as a joke.</para> |
| 11511 |
|
| 11512 |
<example id="stupscr"> |
| 11513 |
<title>Stupid script tricks</title> |
| 11514 |
<programlisting>&stupscr;</programlisting> |
| 11515 |
</example> |
| 11516 |
|
| 11517 |
<para>Notice that a <firstterm>buffer overrun</firstterm> |
| 11518 |
does not occur. This is one instance where an interpreted |
| 11519 |
language, such as Bash, provides more protection from |
| 11520 |
programmer mistakes than a compiled language.</para> |
| 11521 |
|
| 11522 |
</caution> |
| 11523 |
|
| 11524 |
|
| 11525 |
<para><anchor id="csvl"></para> |
| 11526 |
<para>Command substitution permits setting a variable to the |
| 11527 |
output of a <link linkend="forloopref1">loop</link>. The |
| 11528 |
key to this is grabbing the output of an <link |
| 11529 |
linkend="echoref">echo</link> command within the |
| 11530 |
loop.</para> |
| 11531 |
|
| 11532 |
<example id="csubloop"> |
| 11533 |
<title>Generating a variable from a loop</title> |
| 11534 |
<programlisting>&csubloop;</programlisting> |
| 11535 |
</example> |
| 11536 |
|
| 11537 |
|
| 11538 |
|
| 11539 |
<para><anchor id="cstoolset"></para> |
| 11540 |
<sidebar> |
| 11541 |
<para>Command substitution makes it possible to extend the |
| 11542 |
toolset available to Bash. It is simply a matter |
| 11543 |
of writing a program or script that outputs to |
| 11544 |
<filename>stdout</filename> (like a well-behaved UNIX |
| 11545 |
tool should) and assigning that output to a variable.</para> |
| 11546 |
|
| 11547 |
<para> |
| 11548 |
<programlisting>#include <stdio.h> |
| 11549 |
|
| 11550 |
/* "Hello, world." C program */ |
| 11551 |
|
| 11552 |
int main() |
| 11553 |
{ |
| 11554 |
printf( "Hello, world.\n" ); |
| 11555 |
return (0); |
| 11556 |
}</programlisting> |
| 11557 |
<screen><prompt>bash$ </prompt><userinput>gcc -o hello hello.c</userinput> |
| 11558 |
</screen> |
| 11559 |
</para> |
| 11560 |
|
| 11561 |
<para> |
| 11562 |
<programlisting>#!/bin/bash |
| 11563 |
# hello.sh |
| 11564 |
|
| 11565 |
greeting=`./hello` |
| 11566 |
echo $greeting</programlisting> |
| 11567 |
<screen><prompt>bash$ </prompt><userinput>sh hello.sh</userinput> |
| 11568 |
<computeroutput>Hello, world.</computeroutput> |
| 11569 |
</screen> |
| 11570 |
</para> |
| 11571 |
</sidebar> |
| 11572 |
|
| 11573 |
<note> |
| 11574 |
|
| 11575 |
<para><anchor id="csparens">The <command>$(...)</command> |
| 11576 |
form has superseded backticks for command |
| 11577 |
substitution.</para> |
| 11578 |
|
| 11579 |
<para><programlisting>output=$(sed -n /"$1"/p $file) # From "grp.sh" example. |
| 11580 |
|
| 11581 |
# Setting a variable to the contents of a text file. |
| 11582 |
File_contents1=$(cat $file1) |
| 11583 |
File_contents2=$(<$file2) # Bash permits this also.</programlisting></para> |
| 11584 |
|
| 11585 |
<para>The <command>$(...)</command> form of command substitution |
| 11586 |
treats a double backslash in a different way than |
| 11587 |
<command>`...`</command>.</para> |
| 11588 |
|
| 11589 |
<para> |
| 11590 |
<screen><prompt>bash$ </prompt><userinput>echo `echo \\`</userinput> |
| 11591 |
<computeroutput></computeroutput> |
| 11592 |
|
| 11593 |
<prompt>bash$ </prompt><userinput>echo $(echo \\)</userinput> |
| 11594 |
<computeroutput>\</computeroutput> |
| 11595 |
</screen> |
| 11596 |
</para> |
| 11597 |
|
| 11598 |
<para><anchor id="csnest"></para> |
| 11599 |
<para>The <command>$(...)</command> form of command |
| 11600 |
substitution permits nesting. |
| 11601 |
<footnote> |
| 11602 |
<para> |
| 11603 |
In fact, nesting with backticks is also possible, |
| 11604 |
but only by escaping the inner backticks, as John |
| 11605 |
Default points out. |
| 11606 |
<programlisting>word_count=` wc -w \`echo * | awk '{print $8}'\` `</programlisting> |
| 11607 |
</para> |
| 11608 |
</footnote> |
| 11609 |
|
| 11610 |
</para> |
| 11611 |
|
| 11612 |
<para><programlisting>word_count=$( wc -w $(echo * | awk '{print $8}') )</programlisting> |
| 11613 |
</para> |
| 11614 |
|
| 11615 |
<para>Or, for something a bit more elaborate . . .</para> |
| 11616 |
|
| 11617 |
<example id="agram2"> |
| 11618 |
<title>Finding anagrams</title> |
| 11619 |
<programlisting>&agram2;</programlisting> |
| 11620 |
</example> |
| 11621 |
|
| 11622 |
|
| 11623 |
</note> |
| 11624 |
|
| 11625 |
|
| 11626 |
<para>Examples of command substitution in shell scripts: |
| 11627 |
<orderedlist> |
| 11628 |
<listitem><para><xref linkend="bingrep"></para></listitem> |
| 11629 |
<listitem><para><xref linkend="casecmd"></para></listitem> |
| 11630 |
<listitem><para><xref linkend="seedingrandom"></para></listitem> |
| 11631 |
<listitem><para><xref linkend="ex57"></para></listitem> |
| 11632 |
<listitem><para><xref linkend="lowercase"></para></listitem> |
| 11633 |
<listitem><para><xref linkend="grp"></para></listitem> |
| 11634 |
<listitem><para><xref linkend="ex53"></para></listitem> |
| 11635 |
<listitem><para><xref linkend="ex24"></para></listitem> |
| 11636 |
<listitem><para><xref linkend="symlinks"></para></listitem> |
| 11637 |
<listitem><para><xref linkend="stripc"></para></listitem> |
| 11638 |
<listitem><para><xref linkend="redir4"></para></listitem> |
| 11639 |
<listitem><para><xref linkend="tree"></para></listitem> |
| 11640 |
<listitem><para><xref linkend="pidid"></para></listitem> |
| 11641 |
<listitem><para><xref linkend="monthlypmt"></para></listitem> |
| 11642 |
<listitem><para><xref linkend="base"></para></listitem> |
| 11643 |
<listitem><para><xref linkend="altbc"></para></listitem> |
| 11644 |
</orderedlist> |
| 11645 |
</para> |
| 11646 |
|
| 11647 |
</chapter> <!-- Command Substitution --> |
| 11648 |
|
| 11649 |
|
| 11650 |
|
| 11651 |
<chapter id="arithexp"> |
| 11652 |
<title>Arithmetic Expansion</title> |
| 11653 |
|
| 11654 |
|
| 11655 |
<para><anchor id="arithexpref">Arithmetic expansion provides a |
| 11656 |
powerful tool for performing (integer) arithmetic |
| 11657 |
operations in scripts. Translating a string into a |
| 11658 |
numerical expression is relatively straightforward using |
| 11659 |
<firstterm>backticks</firstterm>, <firstterm>double |
| 11660 |
parentheses</firstterm>, or <firstterm>let</firstterm>.</para> |
| 11661 |
|
| 11662 |
<variablelist id="arithexpvar"> |
| 11663 |
<title><anchor id="arithexpvar1">Variations</title> |
| 11664 |
|
| 11665 |
<varlistentry> |
| 11666 |
<term>Arithmetic expansion with <link linkend="backquotesref">backticks</link> (often used in |
| 11667 |
conjunction with <link linkend="exprref">expr</link>)</term> <indexterm> |
| 11668 |
<primary>arithmetic</primary> <secondary>expansion</secondary> |
| 11669 |
</indexterm> <indexterm> |
| 11670 |
<primary>arithmetic</primary> <secondary>expansion</secondary> |
| 11671 |
</indexterm> <listitem> |
| 11672 |
<para><programlisting>z=`expr $z + 3` # The 'expr' command performs the expansion.</programlisting></para> |
| 11673 |
|
| 11674 |
</listitem> |
| 11675 |
</varlistentry> |
| 11676 |
|
| 11677 |
|
| 11678 |
<varlistentry> |
| 11679 |
<term>Arithmetic expansion with <link linkend="dblparens">double |
| 11680 |
parentheses</link></term> |
| 11681 |
<indexterm><primary>double</primary> |
| 11682 |
<secondary>parentheses</secondary></indexterm> |
| 11683 |
<term>and using <link linkend="letref">let</link></term> |
| 11684 |
<indexterm><primary>let</primary> |
| 11685 |
<secondary>let</secondary></indexterm> |
| 11686 |
|
| 11687 |
|
| 11688 |
<listitem> |
| 11689 |
|
| 11690 |
<para>The use of <firstterm>backticks</firstterm> |
| 11691 |
(<firstterm>backquotes</firstterm>) in arithmetic |
| 11692 |
expansion has been superseded by <firstterm>double |
| 11693 |
parentheses</firstterm> -- |
| 11694 |
<userinput>((...))</userinput> and |
| 11695 |
<userinput>$((...))</userinput> -- and also by the very |
| 11696 |
convenient <link linkend="letref">let</link> construction.</para> |
| 11697 |
|
| 11698 |
<para> |
| 11699 |
<programlisting>z=$(($z+3)) |
| 11700 |
z=$((z+3)) # Also correct. |
| 11701 |
# Within double parentheses, |
| 11702 |
#+ parameter dereferencing |
| 11703 |
#+ is optional. |
| 11704 |
|
| 11705 |
# $((EXPRESSION)) is arithmetic expansion. # Not to be confused with |
| 11706 |
#+ command substitution. |
| 11707 |
|
| 11708 |
|
| 11709 |
|
| 11710 |
# You may also use operations within double parentheses without assignment. |
| 11711 |
|
| 11712 |
n=0 |
| 11713 |
echo "n = $n" # n = 0 |
| 11714 |
|
| 11715 |
(( n += 1 )) # Increment. |
| 11716 |
# (( $n += 1 )) is incorrect! |
| 11717 |
echo "n = $n" # n = 1 |
| 11718 |
|
| 11719 |
|
| 11720 |
let z=z+3 |
| 11721 |
let "z += 3" # Quotes permit the use of spaces in variable assignment. |
| 11722 |
# The 'let' operator actually performs arithmetic evaluation, |
| 11723 |
#+ rather than expansion.</programlisting> |
| 11724 |
</para> |
| 11725 |
|
| 11726 |
<para>Examples of arithmetic expansion in scripts: |
| 11727 |
<orderedlist> |
| 11728 |
<listitem><para><xref linkend="ex45"></para></listitem> |
| 11729 |
<listitem><para><xref linkend="ex25"></para></listitem> |
| 11730 |
<listitem><para><xref linkend="ex66"></para></listitem> |
| 11731 |
<listitem><para><xref linkend="bubble"></para></listitem> |
| 11732 |
<listitem><para><xref linkend="tree"></para></listitem> |
| 11733 |
</orderedlist> |
| 11734 |
</para> |
| 11735 |
|
| 11736 |
</listitem> |
| 11737 |
</varlistentry> |
| 11738 |
|
| 11739 |
</variablelist> |
| 11740 |
|
| 11741 |
</chapter> <!-- Arithmetic Expansion --> |
| 11742 |
|
| 11743 |
|
| 11744 |
|
| 11745 |
|
| 11746 |
<chapter id="Recess-Time"> |
| 11747 |
<title>Recess Time</title> |
| 11748 |
|
| 11749 |
<para><emphasis> |
| 11750 |
This bizarre little intermission gives the reader a chance to |
| 11751 |
relax and maybe laugh a bit. |
| 11752 |
</emphasis></para> |
| 11753 |
|
| 11754 |
<blockquote> |
| 11755 |
<literallayout> |
| 11756 |
|
| 11757 |
Fellow Linux user, greetings! You are reading something which |
| 11758 |
will bring you luck and good fortune. Just e-mail a copy of |
| 11759 |
this document to 10 of your friends. Before making the copies, |
| 11760 |
send a 100-line Bash script to the first person on the list |
| 11761 |
at the bottom of this letter. Then delete their name and add |
| 11762 |
yours to the bottom of the list. |
| 11763 |
|
| 11764 |
Don't break the chain! Make the copies within 48 hours. |
| 11765 |
Wilfred P. of Brooklyn failed to send out his ten copies and |
| 11766 |
woke the next morning to find his job description changed |
| 11767 |
to "COBOL programmer." Howard L. of Newport News sent |
| 11768 |
out his ten copies and within a month had enough hardware |
| 11769 |
to build a 100-node Beowulf cluster dedicated to playing |
| 11770 |
<emphasis>Tuxracer</emphasis>. Amelia V. of Chicago laughed at this letter |
| 11771 |
and broke the chain. Shortly thereafter, a fire broke out |
| 11772 |
in her terminal and she now spends her days writing |
| 11773 |
documentation for MS Windows. |
| 11774 |
|
| 11775 |
Don't break the chain! Send out your ten copies today! |
| 11776 |
|
| 11777 |
</literallayout> |
| 11778 |
</blockquote> |
| 11779 |
|
| 11780 |
|
| 11781 |
<para><emphasis>Courtesy 'NIX "fortune cookies", with some |
| 11782 |
alterations and many apologies</emphasis></para> |
| 11783 |
|
| 11784 |
</chapter> <!-- Recess Time --> |
| 11785 |
|
| 11786 |
|
| 11787 |
</part> <!-- Part 3 (Beyond the Basics) --> |
| 11788 |
|
| 11789 |
|
| 11790 |
<part label="Part 4" id="part4"> |
| 11791 |
<title>Commands</title> |
| 11792 |
|
| 11793 |
|
| 11794 |
<partintro> |
| 11795 |
|
| 11796 |
<para><anchor id="part4A"></para> |
| 11797 |
<para>Mastering the commands on your Linux machine is an indispensable |
| 11798 |
prelude to writing effective shell scripts.</para> |
| 11799 |
|
| 11800 |
<para>This section covers the following commands:</para> |
| 11801 |
|
| 11802 |
|
| 11803 |
<itemizedlist id="Commandlist"> |
| 11804 |
|
| 11805 |
<listitem><para><link linkend="dotref">.</link> |
| 11806 |
(See also <link linkend="sourceref">source</link>)</para></listitem> |
| 11807 |
<listitem><para><link linkend="acref">ac</link></para></listitem> |
| 11808 |
<listitem><para><link linkend="useraddref">adduser</link></para></listitem> |
| 11809 |
<listitem><para><link linkend="agettyref">agetty</link></para></listitem> |
| 11810 |
<listitem><para><link linkend="agrepref">agrep</link></para></listitem> |
| 11811 |
<listitem><para><link linkend="arref">ar</link></para></listitem> |
| 11812 |
<listitem><para><link linkend="archref">arch</link></para></listitem> |
| 11813 |
<listitem><para><link linkend="atref">at</link></para></listitem> |
| 11814 |
<listitem><para><link linkend="autoloadref">autoload</link></para></listitem> |
| 11815 |
<listitem><para><link linkend="awkref">awk</link> |
| 11816 |
(See also <link linkend="awkmath">Using |
| 11817 |
<command>awk</command> for |
| 11818 |
math operations</link>)</para></listitem> |
| 11819 |
<listitem><para><link linkend="badblocksref">badblocks</link></para></listitem> |
| 11820 |
<listitem><para><link linkend="bannerref">banner</link></para></listitem> |
| 11821 |
<listitem><para><link linkend="basenameref">basename</link></para></listitem> |
| 11822 |
<listitem><para><link linkend="batchref">batch</link></para></listitem> |
| 11823 |
<listitem><para><link linkend="bcref">bc</link></para></listitem> |
| 11824 |
<listitem><para><link linkend="bgref">bg</link></para></listitem> |
| 11825 |
<listitem><para><link linkend="bindref">bind</link></para></listitem> |
| 11826 |
<listitem><para><link linkend="bisonref">bison</link></para></listitem> |
| 11827 |
<listitem><para><link linkend="bltref">builtin</link></para></listitem> |
| 11828 |
<listitem><para><link linkend="bzgrepref">bzgrep</link></para></listitem> |
| 11829 |
<listitem><para><link linkend="bzipref">bzip2</link></para></listitem> |
| 11830 |
<listitem><para><link linkend="calref">cal</link></para></listitem> |
| 11831 |
<listitem><para><link linkend="callerref">caller</link></para></listitem> |
| 11832 |
<listitem><para><link linkend="catref">cat</link></para></listitem> |
| 11833 |
<listitem><para><link linkend="cdref">cd</link></para></listitem> |
| 11834 |
<listitem><para><link linkend="chattrref">chattr</link></para></listitem> |
| 11835 |
<listitem><para><link linkend="chfnref">chfn</link></para></listitem> |
| 11836 |
<listitem><para><link linkend="chgrpref">chgrp</link></para></listitem> |
| 11837 |
<listitem><para><link linkend="chkconfigref">chkconfig</link></para></listitem> |
| 11838 |
<listitem><para><link linkend="chmodref">chmod</link></para></listitem> |
| 11839 |
<listitem><para><link linkend="chownref">chown</link></para></listitem> |
| 11840 |
<listitem><para><link linkend="chrootref">chroot</link></para></listitem> |
| 11841 |
<listitem><para><link linkend="cksumref">cksum</link></para></listitem> |
| 11842 |
<listitem><para><link linkend="clearref">clear</link></para></listitem> |
| 11843 |
<listitem><para><link linkend="clockref">clock</link></para></listitem> |
| 11844 |
<listitem><para><link linkend="cmpref">cmp</link></para></listitem> |
| 11845 |
<listitem><para><link linkend="colref">col</link></para></listitem> |
| 11846 |
<listitem><para><link linkend="colrmref">colrm</link></para></listitem> |
| 11847 |
<listitem><para><link linkend="columnref">column</link></para></listitem> |
| 11848 |
<listitem><para><link linkend="commref">comm</link></para></listitem> |
| 11849 |
<listitem><para><link linkend="commandref">command</link></para></listitem> |
| 11850 |
<listitem><para><link linkend="compgenref">compgen</link></para></listitem> |
| 11851 |
<listitem><para><link linkend="completeref">complete</link></para></listitem> |
| 11852 |
<listitem><para><link linkend="compressref">compress</link></para></listitem> |
| 11853 |
<listitem><para><link linkend="coprocref">coproc</link></para></listitem> |
| 11854 |
<listitem><para><link linkend="cpref">cp</link></para></listitem> |
| 11855 |
<listitem><para><link linkend="cpioref">cpio</link></para></listitem> |
| 11856 |
<listitem><para><link linkend="cronref">cron</link></para></listitem> |
| 11857 |
<listitem><para><link linkend="cryptref">crypt</link></para></listitem> |
| 11858 |
<listitem><para><link linkend="csplitref">csplit</link></para></listitem> |
| 11859 |
<listitem><para><link linkend="curef">cu</link></para></listitem> |
| 11860 |
<listitem><para><link linkend="cutref">cut</link></para></listitem> |
| 11861 |
<listitem><para><link linkend="dateref">date</link></para></listitem> |
| 11862 |
<listitem><para><link linkend="dcref">dc</link></para></listitem> |
| 11863 |
<listitem><para><link linkend="ddref">dd</link></para></listitem> |
| 11864 |
<listitem><para><link linkend="debugfsref">debugfs</link></para></listitem> |
| 11865 |
<listitem><para><link linkend="declareref">declare</link></para></listitem> |
| 11866 |
<listitem><para><link linkend="depmodref">depmod</link></para></listitem> |
| 11867 |
<listitem><para><link linkend="dfref">df</link></para></listitem> |
| 11868 |
<listitem><para><link linkend="dialogref">dialog</link></para></listitem> |
| 11869 |
<listitem><para><link linkend="diffref">diff</link></para></listitem> |
| 11870 |
<listitem><para><link linkend="diff3ref">diff3</link></para></listitem> |
| 11871 |
<listitem><para><link linkend="diffstatref">diffstat</link></para></listitem> |
| 11872 |
<listitem><para><link linkend="digref">dig</link></para></listitem> |
| 11873 |
<listitem><para><link linkend="dirnameref">dirname</link></para></listitem> |
| 11874 |
<listitem><para><link linkend="dirsd">dirs</link></para></listitem> |
| 11875 |
<listitem><para><link linkend="disownref">disown</link></para></listitem> |
| 11876 |
<listitem><para><link linkend="dmesgref">dmesg</link></para></listitem> |
| 11877 |
<listitem><para><link linkend="doexecref">doexec</link></para></listitem> |
| 11878 |
<listitem><para><link linkend="dos2unixref">dos2unix</link></para></listitem> |
| 11879 |
<listitem><para><link linkend="duref">du</link></para></listitem> |
| 11880 |
<listitem><para><link linkend="dumpref">dump</link></para></listitem> |
| 11881 |
<listitem><para><link linkend="dumpe2fsref">dumpe2fs</link></para></listitem> |
| 11882 |
<listitem><para><link linkend="e2fsckref">e2fsck</link></para></listitem> |
| 11883 |
<listitem><para><link linkend="echoref">echo</link></para></listitem> |
| 11884 |
<listitem><para><link linkend="egrepref">egrep</link></para></listitem> |
| 11885 |
<listitem><para><link linkend="enableref">enable</link></para></listitem> |
| 11886 |
<listitem><para><link linkend="enscriptref">enscript</link></para></listitem> |
| 11887 |
<listitem><para><link linkend="envvref">env</link></para></listitem> |
| 11888 |
<listitem><para><link linkend="eqnref">eqn</link></para></listitem> |
| 11889 |
<listitem><para><link linkend="evalref">eval</link></para></listitem> |
| 11890 |
<listitem><para><link linkend="execref">exec</link></para></listitem> |
| 11891 |
<listitem><para><link linkend="exitref">exit</link> |
| 11892 |
(Related topic: <link linkend="exitstatusref">exit |
| 11893 |
status</link>)</para></listitem> |
| 11894 |
<listitem><para><link linkend="expandref">expand</link></para></listitem> |
| 11895 |
<listitem><para><link linkend="exportref">export</link></para></listitem> |
| 11896 |
<listitem><para><link linkend="exprref">expr</link></para></listitem> |
| 11897 |
<listitem><para><link linkend="factorref">factor</link></para></listitem> |
| 11898 |
<listitem><para><link linkend="falseref">false</link></para></listitem> |
| 11899 |
<listitem><para><link linkend="fdformatref">fdformat</link></para></listitem> |
| 11900 |
<listitem><para><link linkend="fdiskref">fdisk</link></para></listitem> |
| 11901 |
<listitem><para><link linkend="fgref">fg</link></para></listitem> |
| 11902 |
<listitem><para><link linkend="fgrepref">fgrep</link></para></listitem> |
| 11903 |
<listitem><para><link linkend="fileref">file</link></para></listitem> |
| 11904 |
<listitem><para><link linkend="findref">find</link></para></listitem> |
| 11905 |
<listitem><para><link linkend="fingerref">finger</link></para></listitem> |
| 11906 |
<listitem><para><link linkend="flexref">flex</link></para></listitem> |
| 11907 |
<listitem><para><link linkend="flockref">flock</link></para></listitem> |
| 11908 |
<listitem><para><link linkend="fmtref">fmt</link></para></listitem> |
| 11909 |
<listitem><para><link linkend="foldref">fold</link></para></listitem> |
| 11910 |
<listitem><para><link linkend="freeref">free</link></para></listitem> |
| 11911 |
<listitem><para><link linkend="fsckref">fsck</link></para></listitem> |
| 11912 |
<listitem><para><link linkend="ftpref">ftp</link></para></listitem> |
| 11913 |
<listitem><para><link linkend="fuserref">fuser</link></para></listitem> |
| 11914 |
<listitem><para><link linkend="getfaclref">getfacl</link></para></listitem> |
| 11915 |
<listitem><para><link linkend="getopty">getopt</link></para></listitem> |
| 11916 |
<listitem><para><link linkend="getoptsx">getopts</link></para></listitem> |
| 11917 |
<listitem><para><link linkend="gettextref">gettext</link></para></listitem> |
| 11918 |
<listitem><para><link linkend="gettyref">getty</link></para></listitem> |
| 11919 |
<listitem><para><link linkend="gnomemountref">gnome-mount</link></para></listitem> |
| 11920 |
<listitem><para><link linkend="grepref">grep</link></para></listitem> |
| 11921 |
<listitem><para><link linkend="groffref">groff</link></para></listitem> |
| 11922 |
<listitem><para><link linkend="groupmodref">groupmod</link></para></listitem> |
| 11923 |
<listitem><para><link linkend="groupscmdref">groups</link> |
| 11924 |
(Related topic: the <link linkend="groupsref">$GROUPS</link> |
| 11925 |
variable)</para></listitem> |
| 11926 |
<listitem><para><link linkend="gsref">gs</link></para></listitem> |
| 11927 |
<listitem><para><link linkend="gzipref">gzip</link></para></listitem> |
| 11928 |
<listitem><para><link linkend="haltref">halt</link></para></listitem> |
| 11929 |
<listitem><para><link linkend="hashcmdref">hash</link></para></listitem> |
| 11930 |
<listitem><para><link linkend="hdparmref">hdparm</link></para></listitem> |
| 11931 |
<listitem><para><link linkend="headref">head</link></para></listitem> |
| 11932 |
<listitem><para><link linkend="helpref">help</link></para></listitem> |
| 11933 |
<listitem><para><link linkend="hexdumpref">hexdump</link></para></listitem> |
| 11934 |
<listitem><para><link linkend="hostref">host</link></para></listitem> |
| 11935 |
<listitem><para><link linkend="hostidref">hostid</link></para></listitem> |
| 11936 |
<listitem><para><link linkend="hnameref">hostname</link> |
| 11937 |
(Related topic: the <link linkend="hostnameref">$HOSTNAME</link> |
| 11938 |
variable)</para></listitem> |
| 11939 |
<listitem><para><link linkend="hwclockref">hwclock</link></para></listitem> |
| 11940 |
<listitem><para><link linkend="iconvref">iconv</link></para></listitem> |
| 11941 |
<listitem><para><link linkend="idref">id</link> |
| 11942 |
(Related topic: the <link linkend="uidref">$UID</link> |
| 11943 |
variable)</para></listitem> |
| 11944 |
<listitem><para><link linkend="ifconfigref">ifconfig</link></para></listitem> |
| 11945 |
<listitem><para><link linkend="inforef">info</link></para></listitem> |
| 11946 |
<listitem><para><link linkend="infocmpref">infocmp</link></para></listitem> |
| 11947 |
<listitem><para><link linkend="initref">init</link></para></listitem> |
| 11948 |
<listitem><para><link linkend="insmodref">insmod</link></para></listitem> |
| 11949 |
<listitem><para><link linkend="installref">install</link></para></listitem> |
| 11950 |
<listitem><para><link linkend="ipref">ip</link></para></listitem> |
| 11951 |
<listitem><para><link linkend="ipcalcref">ipcalc</link></para></listitem> |
| 11952 |
<listitem><para><link linkend="iptablesref">iptables</link></para></listitem> |
| 11953 |
<listitem><para><link linkend="iwconfigref">iwconfig</link></para></listitem> |
| 11954 |
<listitem><para><link linkend="jobsref">jobs</link></para></listitem> |
| 11955 |
<listitem><para><link linkend="joinref">join</link></para></listitem> |
| 11956 |
<listitem><para><link linkend="jotref">jot</link></para></listitem> |
| 11957 |
<listitem><para><link linkend="killref">kill</link></para></listitem> |
| 11958 |
<listitem><para><link linkend="killallref">killall</link></para></listitem> |
| 11959 |
<listitem><para><link linkend="lastref">last</link></para></listitem> |
| 11960 |
<listitem><para><link linkend="lastcommref">lastcomm</link></para></listitem> |
| 11961 |
<listitem><para><link linkend="lastlogref">lastlog</link></para></listitem> |
| 11962 |
<listitem><para><link linkend="lddref">ldd</link></para></listitem> |
| 11963 |
<listitem><para><link linkend="lessref">less</link></para></listitem> |
| 11964 |
<listitem><para><link linkend="letref">let</link></para></listitem> |
| 11965 |
<listitem><para><link linkend="lexref">lex</link></para></listitem> |
| 11966 |
<listitem><para><link linkend="lidref">lid</link></para></listitem> |
| 11967 |
<listitem><para><link linkend="linkref">ln</link></para></listitem> |
| 11968 |
<listitem><para><link linkend="locateref">locate</link></para></listitem> |
| 11969 |
<listitem><para><link linkend="lockfileref">lockfile</link></para></listitem> |
| 11970 |
<listitem><para><link linkend="loggerref">logger</link></para></listitem> |
| 11971 |
<listitem><para><link linkend="lognameref">logname</link></para></listitem> |
| 11972 |
<listitem><para><link linkend="logoutref">logout</link></para></listitem> |
| 11973 |
<listitem><para><link linkend="logrotateref">logrotate</link></para></listitem> |
| 11974 |
<listitem><para><link linkend="lookref">look</link></para></listitem> |
| 11975 |
<listitem><para><link linkend="losetupref">losetup</link></para></listitem> |
| 11976 |
<listitem><para><link linkend="lpref">lp</link></para></listitem> |
| 11977 |
<listitem><para><link linkend="lsref">ls</link></para></listitem> |
| 11978 |
<listitem><para><link linkend="lsdevref">lsdev</link></para></listitem> |
| 11979 |
<listitem><para><link linkend="lsmodref">lsmod</link></para></listitem> |
| 11980 |
<listitem><para><link linkend="lsofref">lsof</link></para></listitem> |
| 11981 |
<listitem><para><link linkend="lspciref">lspci</link></para></listitem> |
| 11982 |
<listitem><para><link linkend="lsusbref">lsusb</link></para></listitem> |
| 11983 |
<listitem><para><link linkend="ltraceref">ltrace</link></para></listitem> |
| 11984 |
<listitem><para><link linkend="lynxref">lynx</link></para></listitem> |
| 11985 |
<listitem><para><link linkend="lzmaref">lzcat</link></para></listitem> |
| 11986 |
<listitem><para><link linkend="lzmaref">lzma</link></para></listitem> |
| 11987 |
<listitem><para><link linkend="m4ref">m4</link></para></listitem> |
| 11988 |
<listitem><para><link linkend="commmail1">mail</link></para></listitem> |
| 11989 |
<listitem><para><link linkend="mailstatsref">mailstats</link></para></listitem> |
| 11990 |
<listitem><para><link linkend="mailtoref">mailto</link></para></listitem> |
| 11991 |
<listitem><para><link linkend="makeref">make</link></para></listitem> |
| 11992 |
<listitem><para><link linkend="MAKEDEVref">MAKEDEV</link></para></listitem> |
| 11993 |
<listitem><para><link linkend="manref">man</link></para></listitem> |
| 11994 |
<listitem><para><link linkend="mapfileref">mapfile</link></para></listitem> |
| 11995 |
<listitem><para><link linkend="mcookieref">mcookie</link></para></listitem> |
| 11996 |
<listitem><para><link linkend="md5sumref">md5sum</link></para></listitem> |
| 11997 |
<listitem><para><link linkend="mergeref">merge</link></para></listitem> |
| 11998 |
<listitem><para><link linkend="mesgref">mesg</link></para></listitem> |
| 11999 |
<listitem><para><link linkend="mimencoderef">mimencode</link></para></listitem> |
| 12000 |
<listitem><para><link linkend="mkbootdiskref">mkbootdisk</link></para></listitem> |
| 12001 |
<listitem><para><link linkend="mkdirref">mkdir</link></para></listitem> |
| 12002 |
<listitem><para><link linkend="mkdosfsref">mkdosfs</link></para></listitem> |
| 12003 |
<listitem><para><link linkend="mke2fsref">mke2fs</link></para></listitem> |
| 12004 |
<listitem><para><link linkend="mkfiforef">mkfifo</link></para></listitem> |
| 12005 |
<listitem><para><link linkend="mkisofsref">mkisofs</link></para></listitem> |
| 12006 |
<listitem><para><link linkend="mknodref">mknod</link></para></listitem> |
| 12007 |
<listitem><para><link linkend="mkswapref">mkswap</link></para></listitem> |
| 12008 |
<listitem><para><link linkend="mktempref">mktemp</link></para></listitem> |
| 12009 |
<listitem><para><link linkend="mmencoderef">mmencode</link></para></listitem> |
| 12010 |
<listitem><para><link linkend="modinforef">modinfo</link></para></listitem> |
| 12011 |
<listitem><para><link linkend="modproberef">modprobe</link></para></listitem> |
| 12012 |
<listitem><para><link linkend="moreref">more</link></para></listitem> |
| 12013 |
<listitem><para><link linkend="mountref">mount</link></para></listitem> |
| 12014 |
<listitem><para><link linkend="msgfmtref">msgfmt</link></para></listitem> |
| 12015 |
<listitem><para><link linkend="mvref">mv</link></para></listitem> |
| 12016 |
<listitem><para><link linkend="ncref">nc</link></para></listitem> |
| 12017 |
<listitem><para><link linkend="netconfigref">netconfig</link></para></listitem> |
| 12018 |
<listitem><para><link linkend="netstatref">netstat</link></para></listitem> |
| 12019 |
<listitem><para><link linkend="newgrpref">newgrp</link></para></listitem> |
| 12020 |
<listitem><para><link linkend="niceref">nice</link></para></listitem> |
| 12021 |
<listitem><para><link linkend="nlref">nl</link></para></listitem> |
| 12022 |
<listitem><para><link linkend="nmref">nm</link></para></listitem> |
| 12023 |
<listitem><para><link linkend="nmapref">nmap</link></para></listitem> |
| 12024 |
<listitem><para><link linkend="nohupref">nohup</link></para></listitem> |
| 12025 |
<listitem><para><link linkend="nslookupref">nslookup</link></para></listitem> |
| 12026 |
<listitem><para><link linkend="objdumpref">objdump</link></para></listitem> |
| 12027 |
<listitem><para><link linkend="odref">od</link></para></listitem> |
| 12028 |
<listitem><para><link linkend="opensslref">openssl</link></para></listitem> |
| 12029 |
<listitem><para><link linkend="passwdref">passwd</link></para></listitem> |
| 12030 |
<listitem><para><link linkend="pasteref">paste</link></para></listitem> |
| 12031 |
<listitem><para><link linkend="patchref">patch</link> |
| 12032 |
(Related topic: <link linkend="diffref">diff</link>)</para></listitem> |
| 12033 |
<listitem><para><link linkend="pathchkref">pathchk</link></para></listitem> |
| 12034 |
<listitem><para><link linkend="paxref">pax</link></para></listitem> |
| 12035 |
<listitem><para><link linkend="pgrepref">pgrep</link></para></listitem> |
| 12036 |
<listitem><para><link linkend="pidofref">pidof</link></para></listitem> |
| 12037 |
<listitem><para><link linkend="pingref">ping</link></para></listitem> |
| 12038 |
<listitem><para><link linkend="pkillref">pkill</link></para></listitem> |
| 12039 |
<listitem><para><link linkend="dirsd">popd</link></para></listitem> |
| 12040 |
<listitem><para><link linkend="prref">pr</link></para></listitem> |
| 12041 |
<listitem><para><link linkend="printenvref">printenv</link></para></listitem> |
| 12042 |
<listitem><para><link linkend="printfref">printf</link></para></listitem> |
| 12043 |
<listitem><para><link linkend="procinforef">procinfo</link></para></listitem> |
| 12044 |
<listitem><para><link linkend="ppssref">ps</link></para></listitem> |
| 12045 |
<listitem><para><link linkend="pstreeref">pstree</link></para></listitem> |
| 12046 |
<listitem><para><link linkend="ptxref">ptx</link></para></listitem> |
| 12047 |
<listitem><para><link linkend="dirsd">pushd</link></para></listitem> |
| 12048 |
<listitem><para><link linkend="pwd2ref">pwd</link> |
| 12049 |
(Related topic: the <link linkend="pwdref">$PWD</link> |
| 12050 |
variable)</para></listitem> |
| 12051 |
<listitem><para><link linkend="quotaref">quota</link></para></listitem> |
| 12052 |
<listitem><para><link linkend="rcpref">rcp</link></para></listitem> |
| 12053 |
<listitem><para><link linkend="rdevref">rdev</link></para></listitem> |
| 12054 |
<listitem><para><link linkend="rdistref">rdist</link></para></listitem> |
| 12055 |
<listitem><para><link linkend="readref">read</link></para></listitem> |
| 12056 |
<listitem><para><link linkend="readelfref">readelf</link></para></listitem> |
| 12057 |
<listitem><para><link linkend="readlinkref">readlink</link></para></listitem> |
| 12058 |
<listitem><para><link linkend="readonlyref">readonly</link></para></listitem> |
| 12059 |
<listitem><para><link linkend="rebootref">reboot</link></para></listitem> |
| 12060 |
<listitem><para><link linkend="recoderef">recode</link></para></listitem> |
| 12061 |
<listitem><para><link linkend="nice2ref">renice</link></para></listitem> |
| 12062 |
<listitem><para><link linkend="resetref">reset</link></para></listitem> |
| 12063 |
<listitem><para><link linkend="resizeref">resize</link></para></listitem> |
| 12064 |
<listitem><para><link linkend="restoreref">restore</link></para></listitem> |
| 12065 |
<listitem><para><link linkend="revref">rev</link></para></listitem> |
| 12066 |
<listitem><para><link linkend="rloginref">rlogin</link></para></listitem> |
| 12067 |
<listitem><para><link linkend="rmref">rm</link></para></listitem> |
| 12068 |
<listitem><para><link linkend="rmdirref">rmdir</link></para></listitem> |
| 12069 |
<listitem><para><link linkend="rmmodref">rmmod</link></para></listitem> |
| 12070 |
<listitem><para><link linkend="routeref">route</link></para></listitem> |
| 12071 |
<listitem><para><link linkend="rpmref">rpm</link></para></listitem> |
| 12072 |
<listitem><para><link linkend="rpm2cpioref">rpm2cpio</link></para></listitem> |
| 12073 |
<listitem><para><link linkend="rshref">rsh</link></para></listitem> |
| 12074 |
<listitem><para><link linkend="rsyncref">rsync</link></para></listitem> |
| 12075 |
<listitem><para><link linkend="runlevelref">runlevel</link></para></listitem> |
| 12076 |
<listitem><para><link linkend="runpartsref">run-parts</link></para></listitem> |
| 12077 |
<listitem><para><link linkend="rxref">rx</link></para></listitem> |
| 12078 |
<listitem><para><link linkend="rzref">rz</link></para></listitem> |
| 12079 |
<listitem><para><link linkend="sarref">sar</link></para></listitem> |
| 12080 |
<listitem><para><link linkend="scpref">scp</link></para></listitem> |
| 12081 |
<listitem><para><link linkend="scriptref">script</link></para></listitem> |
| 12082 |
<listitem><para><link linkend="sdiffref">sdiff</link></para></listitem> |
| 12083 |
<listitem><para><link linkend="sedref">sed</link></para></listitem> |
| 12084 |
<listitem><para><link linkend="seqref">seq</link></para></listitem> |
| 12085 |
<listitem><para><link linkend="serviceref">service</link></para></listitem> |
| 12086 |
<listitem><para><link linkend="setref">set</link></para></listitem> |
| 12087 |
<listitem><para><link linkend="setfaclref">setfacl</link></para></listitem> |
| 12088 |
<listitem><para><link linkend="setquotaref">setquota</link></para></listitem> |
| 12089 |
<listitem><para><link linkend="setserialref">setserial</link></para></listitem> |
| 12090 |
<listitem><para><link linkend="settermref">setterm</link></para></listitem> |
| 12091 |
<listitem><para><link linkend="sha1sumref">sha1sum</link></para></listitem> |
| 12092 |
<listitem><para><link linkend="sharref">shar</link></para></listitem> |
| 12093 |
<listitem><para><link linkend="shoptref">shopt</link></para></listitem> |
| 12094 |
<listitem><para><link linkend="shredref">shred</link></para></listitem> |
| 12095 |
<listitem><para><link linkend="shutdownref">shutdown</link></para></listitem> |
| 12096 |
<listitem><para><link linkend="sizeref">size</link></para></listitem> |
| 12097 |
<listitem><para><link linkend="nice2ref">skill</link></para></listitem> |
| 12098 |
<listitem><para><link linkend="sleepref">sleep</link></para></listitem> |
| 12099 |
<listitem><para><link linkend="slocateref">slocate</link></para></listitem> |
| 12100 |
<listitem><para><link linkend="nice2ref">snice</link></para></listitem> |
| 12101 |
<listitem><para><link linkend="sortref">sort</link></para></listitem> |
| 12102 |
<listitem><para><link linkend="sourceref">source</link></para></listitem> |
| 12103 |
<listitem><para><link linkend="soxref">sox</link></para></listitem> |
| 12104 |
<listitem><para><link linkend="splitref">split</link></para></listitem> |
| 12105 |
<listitem><para><link linkend="sqref">sq</link></para></listitem> |
| 12106 |
<listitem><para><link linkend="sshref">ssh</link></para></listitem> |
| 12107 |
<listitem><para><link linkend="statref">stat</link></para></listitem> |
| 12108 |
<listitem><para><link linkend="straceref">strace</link></para></listitem> |
| 12109 |
<listitem><para><link linkend="stringsref">strings</link></para></listitem> |
| 12110 |
<listitem><para><link linkend="stripref">strip</link></para></listitem> |
| 12111 |
<listitem><para><link linkend="sttyref">stty</link></para></listitem> |
| 12112 |
<listitem><para><link linkend="suref">su</link></para></listitem> |
| 12113 |
<listitem><para><link linkend="sudoref">sudo</link></para></listitem> |
| 12114 |
<listitem><para><link linkend="sumref">sum</link></para></listitem> |
| 12115 |
<listitem><para><link linkend="suspendref">suspend</link></para></listitem> |
| 12116 |
<listitem><para><link linkend="swaponref">swapoff</link></para></listitem> |
| 12117 |
<listitem><para><link linkend="swaponref">swapon</link></para></listitem> |
| 12118 |
<listitem><para><link linkend="rxref">sx</link></para></listitem> |
| 12119 |
<listitem><para><link linkend="syncref">sync</link></para></listitem> |
| 12120 |
<listitem><para><link linkend="rzref">sz</link></para></listitem> |
| 12121 |
<listitem><para><link linkend="catref">tac</link></para></listitem> |
| 12122 |
<listitem><para><link linkend="tailref">tail</link></para></listitem> |
| 12123 |
<listitem><para><link linkend="tarref">tar</link></para></listitem> |
| 12124 |
<listitem><para><link linkend="tblref">tbl</link></para></listitem> |
| 12125 |
<listitem><para><link linkend="tcpdumpref">tcpdump</link></para></listitem> |
| 12126 |
<listitem><para><link linkend="teeref">tee</link></para></listitem> |
| 12127 |
<listitem><para><link linkend="telinitref">telinit</link></para></listitem> |
| 12128 |
<listitem><para><link linkend="telnetref">telnet</link></para></listitem> |
| 12129 |
<listitem><para><link linkend="texref">Tex</link></para></listitem> |
| 12130 |
<listitem><para><link linkend="texexecref">texexec</link></para></listitem> |
| 12131 |
<listitem><para><link linkend="timref">time</link></para></listitem> |
| 12132 |
<listitem><para><link linkend="timesref">times</link></para></listitem> |
| 12133 |
<listitem><para><link linkend="tmpwatchref">tmpwatch</link></para></listitem> |
| 12134 |
<listitem><para><link linkend="topref">top</link></para></listitem> |
| 12135 |
<listitem><para><link linkend="touchref">touch</link></para></listitem> |
| 12136 |
<listitem><para><link linkend="tputref">tput</link></para></listitem> |
| 12137 |
<listitem><para><link linkend="trref">tr</link></para></listitem> |
| 12138 |
<listitem><para><link linkend="tracerouteref">traceroute</link></para></listitem> |
| 12139 |
<listitem><para><link linkend="trueref">true</link></para></listitem> |
| 12140 |
<listitem><para><link linkend="tsetref">tset</link></para></listitem> |
| 12141 |
<listitem><para><link linkend="tsortref">tsort</link></para></listitem> |
| 12142 |
<listitem><para><link linkend="ttyref">tty</link></para></listitem> |
| 12143 |
<listitem><para><link linkend="tune2fsref">tune2fs</link></para></listitem> |
| 12144 |
<listitem><para><link linkend="typeref">type</link></para></listitem> |
| 12145 |
<listitem><para><link linkend="declareref">typeset</link></para></listitem> |
| 12146 |
<listitem><para><link linkend="ulimitref">ulimit</link></para></listitem> |
| 12147 |
<listitem><para><link linkend="umaskref">umask</link></para></listitem> |
| 12148 |
<listitem><para><link linkend="umountref">umount</link></para></listitem> |
| 12149 |
<listitem><para><link linkend="unameref">uname</link></para></listitem> |
| 12150 |
<listitem><para><link linkend="unarcref">unarc</link></para></listitem> |
| 12151 |
<listitem><para><link linkend="unarcref">unarj</link></para></listitem> |
| 12152 |
<listitem><para><link linkend="uncompressref">uncompress</link></para></listitem> |
| 12153 |
<listitem><para><link linkend="expandref">unexpand</link></para></listitem> |
| 12154 |
<listitem><para><link linkend="uniqref">uniq</link></para></listitem> |
| 12155 |
<listitem><para><link linkend="unitsref">units</link></para></listitem> |
| 12156 |
<listitem><para><link linkend="lzmaref">unlzma</link></para></listitem> |
| 12157 |
<listitem><para><link linkend="unarcref">unrar</link></para></listitem> |
| 12158 |
<listitem><para><link linkend="unsetref">unset</link></para></listitem> |
| 12159 |
<listitem><para><link linkend="sqref">unsq</link></para></listitem> |
| 12160 |
<listitem><para><link linkend="zipref">unzip</link></para></listitem> |
| 12161 |
<listitem><para><link linkend="uptimeref">uptime</link></para></listitem> |
| 12162 |
<listitem><para><link linkend="lsusbref">usbmodules</link></para></listitem> |
| 12163 |
<listitem><para><link linkend="useraddref">useradd</link></para></listitem> |
| 12164 |
<listitem><para><link linkend="useraddref">userdel</link></para></listitem> |
| 12165 |
<listitem><para><link linkend="usermodref">usermod</link></para></listitem> |
| 12166 |
<listitem><para><link linkend="usersref">users</link></para></listitem> |
| 12167 |
<listitem><para><link linkend="usleepref">usleep</link></para></listitem> |
| 12168 |
<listitem><para><link linkend="uucpref">uucp</link></para></listitem> |
| 12169 |
<listitem><para><link linkend="uudecoderef">uudecode</link></para></listitem> |
| 12170 |
<listitem><para><link linkend="uuencoderef">uuencode</link></para></listitem> |
| 12171 |
<listitem><para><link linkend="uuxref">uux</link></para></listitem> |
| 12172 |
<listitem><para><link linkend="vacationref">vacation</link></para></listitem> |
| 12173 |
<listitem><para><link linkend="vdirref">vdir</link></para></listitem> |
| 12174 |
<listitem><para><link linkend="vmstatref">vmstat</link></para></listitem> |
| 12175 |
<listitem><para><link linkend="vrfyref">vrfy</link></para></listitem> |
| 12176 |
<listitem><para><link linkend="wref">w</link></para></listitem> |
| 12177 |
<listitem><para><link linkend="waitref">wait</link></para></listitem> |
| 12178 |
<listitem><para><link linkend="wallref">wall</link></para></listitem> |
| 12179 |
<listitem><para><link linkend="watchref">watch</link></para></listitem> |
| 12180 |
<listitem><para><link linkend="wcref">wc</link></para></listitem> |
| 12181 |
<listitem><para><link linkend="wgetref">wget</link></para></listitem> |
| 12182 |
<listitem><para><link linkend="whatisref">whatis</link></para></listitem> |
| 12183 |
<listitem><para><link linkend="whereisref">whereis</link></para></listitem> |
| 12184 |
<listitem><para><link linkend="whichref">which</link></para></listitem> |
| 12185 |
<listitem><para><link linkend="whoref">who</link></para></listitem> |
| 12186 |
<listitem><para><link linkend="whoamiref">whoami</link></para></listitem> |
| 12187 |
<listitem><para><link linkend="whoisref">whois</link></para></listitem> |
| 12188 |
<listitem><para><link linkend="writeref">write</link></para></listitem> |
| 12189 |
<listitem><para><link linkend="xargsref">xargs</link></para></listitem> |
| 12190 |
<listitem><para><link linkend="xrandrref">xrandr</link></para></listitem> |
| 12191 |
<listitem><para><link linkend="xzref">xz</link></para></listitem> |
| 12192 |
<listitem><para><link linkend="yaccref">yacc</link></para></listitem> |
| 12193 |
<listitem><para><link linkend="yesref">yes</link></para></listitem> |
| 12194 |
<listitem><para><link linkend="zcatref">zcat</link></para></listitem> |
| 12195 |
<listitem><para><link linkend="zdiffref">zdiff</link></para></listitem> |
| 12196 |
<listitem><para><link linkend="zdumpref">zdump</link></para></listitem> |
| 12197 |
<listitem><para><link linkend="zegrepref">zegrep</link></para></listitem> |
| 12198 |
<listitem><para><link linkend="zegrepref">zfgrep</link></para></listitem> |
| 12199 |
<listitem><para><link linkend="zegrepref">zgrep</link></para></listitem> |
| 12200 |
<listitem><para><link linkend="zipref">zip</link></para></listitem> |
| 12201 |
|
| 12202 |
</itemizedlist> |
| 12203 |
|
| 12204 |
|
| 12205 |
</partintro> |
| 12206 |
|
| 12207 |
<chapter id="internal"> |
| 12208 |
<title>Internal Commands and Builtins</title> |
| 12209 |
|
| 12210 |
|
| 12211 |
<indexterm> |
| 12212 |
<primary>builtin</primary> |
| 12213 |
</indexterm> |
| 12214 |
|
| 12215 |
|
| 12216 |
<para><anchor id="builtinref">A <firstterm>builtin</firstterm> |
| 12217 |
is a <command>command</command> contained within the Bash tool |
| 12218 |
set, literally <firstterm>built in</firstterm>. This is either |
| 12219 |
for performance reasons -- builtins execute faster than external |
| 12220 |
commands, which usually require <firstterm>forking off</firstterm> |
| 12221 |
<footnote><para>As Nathan Coulter points out, "while forking a |
| 12222 |
process is a low-cost operation, executing a new program in |
| 12223 |
the newly-forked child process adds more |
| 12224 |
overhead."</para></footnote> |
| 12225 |
a separate process -- or because a particular builtin needs |
| 12226 |
direct access to the shell internals.</para> |
| 12227 |
|
| 12228 |
<para><anchor id="forkref"></para> |
| 12229 |
|
| 12230 |
<sidebar> |
| 12231 |
|
| 12232 |
<para><anchor id="parentref">When a command or |
| 12233 |
the shell itself initiates (or |
| 12234 |
<firstterm>spawns</firstterm>) a new |
| 12235 |
subprocess to carry out a task, this is called |
| 12236 |
<firstterm>forking</firstterm>. This new process |
| 12237 |
is the <firstterm>child</firstterm>, and the process |
| 12238 |
that <firstterm>forked</firstterm> it off is the |
| 12239 |
<firstterm>parent</firstterm>. While the <firstterm>child |
| 12240 |
process</firstterm> is doing its work, the |
| 12241 |
<firstterm>parent process</firstterm> is still |
| 12242 |
executing.</para> |
| 12243 |
|
| 12244 |
<para>Note that while a <firstterm>parent |
| 12245 |
process</firstterm> gets the <firstterm>process |
| 12246 |
ID</firstterm> of the <firstterm>child |
| 12247 |
process</firstterm>, and can thus pass arguments to it, |
| 12248 |
<emphasis>the reverse is not true</emphasis>. <link |
| 12249 |
linkend="parchildprobref">This can create problems |
| 12250 |
that are subtle and hard to track down.</link></para> |
| 12251 |
|
| 12252 |
<example id="spawnscr"> |
| 12253 |
<title>A script that spawns multiple instances of itself</title> |
| 12254 |
<programlisting>&spawnscr;</programlisting> |
| 12255 |
</example> |
| 12256 |
|
| 12257 |
|
| 12258 |
<para><anchor id="bltinfrk"></para> |
| 12259 |
<para>Generally, a Bash <firstterm>builtin</firstterm> |
| 12260 |
does not fork a subprocess when it executes within |
| 12261 |
a script. An external system command or filter in |
| 12262 |
a script usually <emphasis>will</emphasis> fork a |
| 12263 |
subprocess.</para> |
| 12264 |
|
| 12265 |
</sidebar> |
| 12266 |
|
| 12267 |
|
| 12268 |
<para>A builtin may be a synonym to a system command of the same |
| 12269 |
name, but Bash reimplements it internally. For example, |
| 12270 |
the Bash <command>echo</command> command is not the same as |
| 12271 |
<filename>/bin/echo</filename>, although their behavior is |
| 12272 |
almost identical. |
| 12273 |
<programlisting>#!/bin/bash |
| 12274 |
|
| 12275 |
echo "This line uses the \"echo\" builtin." |
| 12276 |
/bin/echo "This line uses the /bin/echo system command."</programlisting> |
| 12277 |
</para> |
| 12278 |
|
| 12279 |
<para><anchor id="keywordref">A <firstterm>keyword</firstterm> |
| 12280 |
is a <firstterm>reserved</firstterm> word, token or |
| 12281 |
operator. Keywords have a special meaning to the shell, |
| 12282 |
and indeed are the building blocks of the shell's |
| 12283 |
syntax. As examples, <firstterm>for</firstterm>, |
| 12284 |
<firstterm>while</firstterm>, <firstterm>do</firstterm>, |
| 12285 |
and <firstterm>!</firstterm> are keywords. Similar to a <link |
| 12286 |
linkend="builtinref">builtin</link>, a keyword is hard-coded into |
| 12287 |
Bash, but unlike a <firstterm>builtin</firstterm>, a keyword is |
| 12288 |
not in itself a command, but <emphasis>a subunit of a command |
| 12289 |
construct</emphasis>. |
| 12290 |
|
| 12291 |
<footnote><para>An exception to this is the <link |
| 12292 |
linkend="timref">time</link> command, listed in the |
| 12293 |
official Bash documentation as a keyword (<quote>reserved |
| 12294 |
word</quote>).</para></footnote> |
| 12295 |
|
| 12296 |
</para> |
| 12297 |
|
| 12298 |
|
| 12299 |
<variablelist id="intio"> |
| 12300 |
<title><anchor id="intio1">I/O</title> |
| 12301 |
|
| 12302 |
<varlistentry> |
| 12303 |
<term><anchor id="echoref"><command>echo</command></term> |
| 12304 |
<indexterm> |
| 12305 |
<primary>echo</primary> |
| 12306 |
</indexterm> |
| 12307 |
<indexterm> |
| 12308 |
<primary>command</primary> |
| 12309 |
<secondary>echo</secondary> |
| 12310 |
</indexterm> |
| 12311 |
<listitem> |
| 12312 |
<para>prints (to <filename>stdout</filename>) an expression |
| 12313 |
or variable (see <xref linkend="ex9">). |
| 12314 |
<programlisting>echo Hello |
| 12315 |
echo $a</programlisting></para> |
| 12316 |
|
| 12317 |
<para>An <command>echo</command> requires the |
| 12318 |
<option>-e</option> option to print escaped characters. See |
| 12319 |
<xref linkend="escaped">.</para> |
| 12320 |
|
| 12321 |
<para>Normally, each <command>echo</command> command prints |
| 12322 |
a terminal newline, but the <option>-n</option> option |
| 12323 |
suppresses this.</para> |
| 12324 |
|
| 12325 |
<para><anchor id="echogrepref"></para> |
| 12326 |
<note> |
| 12327 |
<para>An <command>echo</command> can be used to feed a |
| 12328 |
sequence of commands down a pipe.</para> |
| 12329 |
<para><programlisting>if echo "$VAR" | grep -q txt # if [[ $VAR = *txt* ]] |
| 12330 |
then |
| 12331 |
echo "$VAR contains the substring sequence \"txt\"" |
| 12332 |
fi</programlisting></para> |
| 12333 |
</note> |
| 12334 |
|
| 12335 |
<para><anchor id="echocs"></para> |
| 12336 |
<note><para>An <command>echo</command>, in combination with |
| 12337 |
<link linkend="commandsubref">command substitution</link> |
| 12338 |
can set a variable.</para> <para><userinput>a=`echo |
| 12339 |
"HELLO" | tr A-Z a-z`</userinput></para> |
| 12340 |
<para>See also <xref linkend="lowercase">, <xref |
| 12341 |
linkend="ex57">, <xref linkend="monthlypmt">, and <xref |
| 12342 |
linkend="base">.</para></note> |
| 12343 |
|
| 12344 |
|
| 12345 |
<para>Be aware that <command>echo `command`</command> |
| 12346 |
deletes any linefeeds that the output |
| 12347 |
of <replaceable>command</replaceable> |
| 12348 |
generates.</para> |
| 12349 |
|
| 12350 |
<para>The <link linkend="ifsref">$IFS</link> (internal field |
| 12351 |
separator) variable normally contains |
| 12352 |
<token>\n</token> (linefeed) as one of its set of |
| 12353 |
<link linkend="whitespaceref">whitespace</link> |
| 12354 |
characters. Bash therefore splits the output of |
| 12355 |
<replaceable>command</replaceable> at linefeeds |
| 12356 |
into arguments to <command>echo</command>. Then |
| 12357 |
<command>echo</command> outputs these arguments, |
| 12358 |
separated by spaces.</para> |
| 12359 |
|
| 12360 |
<para> |
| 12361 |
<screen> |
| 12362 |
<prompt>bash$ </prompt><userinput>ls -l /usr/share/apps/kjezz/sounds</userinput> |
| 12363 |
<computeroutput>-rw-r--r-- 1 root root 1407 Nov 7 2000 reflect.au |
| 12364 |
-rw-r--r-- 1 root root 362 Nov 7 2000 seconds.au</computeroutput> |
| 12365 |
|
| 12366 |
|
| 12367 |
|
| 12368 |
|
| 12369 |
<prompt>bash$ </prompt><userinput>echo `ls -l /usr/share/apps/kjezz/sounds`</userinput> |
| 12370 |
<computeroutput>total 40 -rw-r--r-- 1 root root 716 Nov 7 2000 reflect.au -rw-r--r-- 1 root root ...</computeroutput> |
| 12371 |
</screen> |
| 12372 |
</para> |
| 12373 |
|
| 12374 |
<para> |
| 12375 |
So, how can we embed a linefeed within an |
| 12376 |
<link linkend="echoref">echoed</link> character string? |
| 12377 |
|
| 12378 |
<programlisting># Embedding a linefeed? |
| 12379 |
echo "Why doesn't this string \n split on two lines?" |
| 12380 |
# Doesn't split. |
| 12381 |
|
| 12382 |
# Let's try something else. |
| 12383 |
|
| 12384 |
echo |
| 12385 |
|
| 12386 |
echo $"A line of text containing |
| 12387 |
a linefeed." |
| 12388 |
# Prints as two distinct lines (embedded linefeed). |
| 12389 |
# But, is the "$" variable prefix really necessary? |
| 12390 |
|
| 12391 |
echo |
| 12392 |
|
| 12393 |
echo "This string splits |
| 12394 |
on two lines." |
| 12395 |
# No, the "$" is not needed. |
| 12396 |
|
| 12397 |
echo |
| 12398 |
echo "---------------" |
| 12399 |
echo |
| 12400 |
|
| 12401 |
echo -n $"Another line of text containing |
| 12402 |
a linefeed." |
| 12403 |
# Prints as two distinct lines (embedded linefeed). |
| 12404 |
# Even the -n option fails to suppress the linefeed here. |
| 12405 |
|
| 12406 |
echo |
| 12407 |
echo |
| 12408 |
echo "---------------" |
| 12409 |
echo |
| 12410 |
echo |
| 12411 |
|
| 12412 |
# However, the following doesn't work as expected. |
| 12413 |
# Why not? Hint: Assignment to a variable. |
| 12414 |
string1=$"Yet another line of text containing |
| 12415 |
a linefeed (maybe)." |
| 12416 |
|
| 12417 |
echo $string1 |
| 12418 |
# Yet another line of text containing a linefeed (maybe). |
| 12419 |
# ^ |
| 12420 |
# Linefeed becomes a space. |
| 12421 |
|
| 12422 |
# Thanks, Steve Parker, for pointing this out.</programlisting> |
| 12423 |
</para> |
| 12424 |
|
| 12425 |
<para><anchor id="binecho"></para> |
| 12426 |
<note> |
| 12427 |
<para>This command is a shell builtin, and not the same as |
| 12428 |
<filename>/bin/echo</filename>, although its behavior is |
| 12429 |
similar.</para> |
| 12430 |
|
| 12431 |
<para> |
| 12432 |
<screen><prompt>bash$ </prompt><userinput>type -a echo</userinput> |
| 12433 |
<computeroutput>echo is a shell builtin |
| 12434 |
echo is /bin/echo</computeroutput> |
| 12435 |
</screen> |
| 12436 |
</para> |
| 12437 |
</note> |
| 12438 |
|
| 12439 |
</listitem> |
| 12440 |
</varlistentry> |
| 12441 |
|
| 12442 |
<varlistentry> |
| 12443 |
<term><anchor id="printfref"><command>printf</command></term> |
| 12444 |
<indexterm> |
| 12445 |
<primary>printf</primary> |
| 12446 |
</indexterm> |
| 12447 |
<indexterm> |
| 12448 |
<primary>command</primary> |
| 12449 |
<secondary>printf</secondary> |
| 12450 |
</indexterm> |
| 12451 |
<listitem> |
| 12452 |
<para>The <command>printf</command>, formatted print, command is an |
| 12453 |
enhanced <command>echo</command>. It is a limited variant |
| 12454 |
of the <firstterm>C</firstterm> language |
| 12455 |
<function>printf()</function> library function, and its |
| 12456 |
syntax is somewhat different.</para> |
| 12457 |
<cmdsynopsis> |
| 12458 |
<command>printf</command> |
| 12459 |
<arg choice=plain rep=repeat><replaceable>format-string</replaceable></arg> |
| 12460 |
<arg choice=plain rep=repeat><replaceable>parameter</replaceable></arg> |
| 12461 |
</cmdsynopsis> |
| 12462 |
|
| 12463 |
<para>This is the Bash <firstterm>builtin</firstterm> version |
| 12464 |
of the <filename>/bin/printf</filename> or |
| 12465 |
<filename>/usr/bin/printf</filename> command. See the |
| 12466 |
<command>printf</command> <link |
| 12467 |
linkend="manref">manpage</link> (of the system command) |
| 12468 |
for in-depth coverage.</para> |
| 12469 |
|
| 12470 |
<caution><para>Older versions of Bash may not support |
| 12471 |
<command>printf</command>.</para></caution> |
| 12472 |
|
| 12473 |
<example id="ex47"> |
| 12474 |
<title><firstterm>printf</firstterm> in action</title> |
| 12475 |
<programlisting>&ex47;</programlisting> |
| 12476 |
</example> |
| 12477 |
|
| 12478 |
<para>Formatting error messages is a useful application of |
| 12479 |
<command>printf</command></para> |
| 12480 |
|
| 12481 |
<para> |
| 12482 |
<programlisting>E_BADDIR=85 |
| 12483 |
|
| 12484 |
var=nonexistent_directory |
| 12485 |
|
| 12486 |
error() |
| 12487 |
{ |
| 12488 |
printf "$@" >&2 |
| 12489 |
# Formats positional params passed, and sends them to stderr. |
| 12490 |
echo |
| 12491 |
exit $E_BADDIR |
| 12492 |
} |
| 12493 |
|
| 12494 |
cd $var || error $"Can't cd to %s." "$var" |
| 12495 |
|
| 12496 |
# Thanks, S.C.</programlisting> |
| 12497 |
</para> |
| 12498 |
|
| 12499 |
<para>See also <xref linkend="progressbar">.</para> |
| 12500 |
|
| 12501 |
</listitem> |
| 12502 |
</varlistentry> |
| 12503 |
|
| 12504 |
<varlistentry> |
| 12505 |
<term><anchor id="readref"><command>read</command></term> <indexterm> |
| 12506 |
<primary>read</primary> |
| 12507 |
</indexterm> <indexterm> |
| 12508 |
<primary>command</primary> <secondary>read</secondary> |
| 12509 |
</indexterm> <listitem><para><quote>Reads</quote> the value |
| 12510 |
of a variable from <filename>stdin</filename>, that |
| 12511 |
is, interactively fetches input from the keyboard. The |
| 12512 |
<option>-a</option> option lets <command>read</command> |
| 12513 |
get array variables (see <xref linkend="ex67">).</para> |
| 12514 |
|
| 12515 |
<example id="ex36"> |
| 12516 |
<title>Variable assignment, using <firstterm>read</firstterm></title> |
| 12517 |
<programlisting>&ex36;</programlisting> |
| 12518 |
</example> |
| 12519 |
|
| 12520 |
|
| 12521 |
<para>A <command>read</command> without an associated variable |
| 12522 |
assigns its input to the dedicated variable <link |
| 12523 |
linkend="replyref">$REPLY</link>.</para> |
| 12524 |
|
| 12525 |
<example id="readnovar"> |
| 12526 |
<title>What happens when <firstterm>read</firstterm> has no |
| 12527 |
variable</title> |
| 12528 |
<programlisting>&readnovar;</programlisting> |
| 12529 |
</example> |
| 12530 |
|
| 12531 |
|
| 12532 |
<para>Normally, inputting a <userinput>\</userinput> |
| 12533 |
suppresses a newline during input to |
| 12534 |
a <command>read</command>. The <option>-r</option> |
| 12535 |
option causes an inputted <userinput>\</userinput> to be |
| 12536 |
interpreted literally.</para> |
| 12537 |
|
| 12538 |
<example id="readr"> |
| 12539 |
<title>Multi-line input to <firstterm>read</firstterm></title> |
| 12540 |
<programlisting>&readr;</programlisting> |
| 12541 |
</example> |
| 12542 |
|
| 12543 |
|
| 12544 |
<para><anchor id="readoptions"></para> |
| 12545 |
<para>The <command>read</command> command has some interesting |
| 12546 |
options that permit echoing a prompt and even reading keystrokes |
| 12547 |
without hitting <keycap>ENTER</keycap>.</para> |
| 12548 |
|
| 12549 |
<para><programlisting># Read a keypress without hitting ENTER. |
| 12550 |
|
| 12551 |
read -s -n1 -p "Hit a key " keypress |
| 12552 |
echo; echo "Keypress was "\"$keypress\""." |
| 12553 |
|
| 12554 |
# -s option means do not echo input. |
| 12555 |
# -n N option means accept only N characters of input. |
| 12556 |
# -p option means echo the following prompt before reading input. |
| 12557 |
|
| 12558 |
# Using these options is tricky, since they need to be in the correct order.</programlisting></para> |
| 12559 |
|
| 12560 |
|
| 12561 |
<para><anchor id="readarrow"></para> |
| 12562 |
<para>The <option>-n</option> option to <command>read</command> |
| 12563 |
also allows detection of the <keycap>arrow keys</keycap> |
| 12564 |
and certain of the other unusual keys.</para> |
| 12565 |
|
| 12566 |
<example id="arrowdetect"> |
| 12567 |
<title>Detecting the arrow keys</title> |
| 12568 |
<programlisting>&arrowdetect;</programlisting> |
| 12569 |
</example> |
| 12570 |
|
| 12571 |
<note><para>The <option>-n</option> option to <command>read</command> |
| 12572 |
will not detect the <keycap>ENTER</keycap> (newline) |
| 12573 |
key.</para></note> |
| 12574 |
|
| 12575 |
|
| 12576 |
<para><anchor id="readtimed"></para> |
| 12577 |
<para>The <option>-t</option> option to <command>read</command> |
| 12578 |
permits timed input (see <xref linkend="tout"> and <xref |
| 12579 |
linkend="qky">).</para> |
| 12580 |
|
| 12581 |
<para><anchor id="readfd">The <option>-u</option> option |
| 12582 |
takes the <link linkend="fdref">file descriptor</link> |
| 12583 |
of the target file.</para> |
| 12584 |
|
| 12585 |
<para><anchor id="readredir0"></para> |
| 12586 |
<para>The <command>read</command> command may also |
| 12587 |
<quote>read</quote> its variable value from a file |
| 12588 |
<link linkend="ioredirref">redirected</link> to |
| 12589 |
<filename>stdin</filename>. If the file contains |
| 12590 |
more than one line, only the first line is assigned |
| 12591 |
to the variable. If <command>read</command> |
| 12592 |
has more than one parameter, then each of |
| 12593 |
these variables gets assigned a successive <link |
| 12594 |
linkend="whitespaceref">whitespace-delineated</link> |
| 12595 |
string. Caution!</para> |
| 12596 |
|
| 12597 |
<example id="readredir"> |
| 12598 |
<title>Using <firstterm>read</firstterm> with |
| 12599 |
<link linkend="ioredirref">file redirection</link></title> |
| 12600 |
<programlisting>&readredir;</programlisting> |
| 12601 |
</example> |
| 12602 |
|
| 12603 |
<note> |
| 12604 |
|
| 12605 |
<para><anchor id="pipereadref0"></para> |
| 12606 |
<para><link linkend="piperef">Piping</link> output |
| 12607 |
to a <firstterm>read</firstterm>, using <link |
| 12608 |
linkend="echoref">echo</link> to set variables <link |
| 12609 |
linkend="badread0">will fail</link>.</para> |
| 12610 |
|
| 12611 |
<para><anchor id="readpiperef">Yet, piping the output of <link |
| 12612 |
linkend="catref">cat</link> <emphasis>seems</emphasis> to |
| 12613 |
work.</para> |
| 12614 |
|
| 12615 |
<para><anchor id="whilereadref"></para> |
| 12616 |
|
| 12617 |
<para><programlisting>cat file1 file2 | |
| 12618 |
while read line |
| 12619 |
do |
| 12620 |
echo $line |
| 12621 |
done</programlisting></para> |
| 12622 |
|
| 12623 |
|
| 12624 |
<para>However, as Bjön Eriksson shows:</para> |
| 12625 |
|
| 12626 |
<example id="readpipe"> |
| 12627 |
<title>Problems reading from a pipe</title> |
| 12628 |
<programlisting>&readpipe;</programlisting> |
| 12629 |
</example> |
| 12630 |
|
| 12631 |
<para>The <firstterm>gendiff</firstterm> script, usually |
| 12632 |
found in <filename>/usr/bin</filename> on |
| 12633 |
many Linux distros, pipes the output of <link |
| 12634 |
linkend="findref">find</link> to a <firstterm>while |
| 12635 |
read</firstterm> construct. |
| 12636 |
|
| 12637 |
<programlisting>find $1 \( -name "*$2" -o -name ".*$2" \) -print | |
| 12638 |
while read f; do |
| 12639 |
. . .</programlisting> |
| 12640 |
</para> |
| 12641 |
|
| 12642 |
</note> |
| 12643 |
|
| 12644 |
<tip> |
| 12645 |
<para>It is possible to <firstterm>paste</firstterm> text into |
| 12646 |
the input field of a <firstterm>read</firstterm> (but |
| 12647 |
<emphasis>not</emphasis> multiple lines!). See <xref |
| 12648 |
linkend="padsw">.</para> |
| 12649 |
</tip> |
| 12650 |
|
| 12651 |
|
| 12652 |
</listitem> |
| 12653 |
</varlistentry> |
| 12654 |
|
| 12655 |
</variablelist> |
| 12656 |
|
| 12657 |
<variablelist id="intfilesystem"> |
| 12658 |
<title><anchor id="intfilesystem1">Filesystem</title> |
| 12659 |
|
| 12660 |
<varlistentry> |
| 12661 |
<term><anchor id="cdref"><command>cd</command></term> |
| 12662 |
<indexterm> |
| 12663 |
<primary>cd</primary> |
| 12664 |
</indexterm> |
| 12665 |
<indexterm> |
| 12666 |
<primary>command</primary> |
| 12667 |
<secondary>cd</secondary> |
| 12668 |
</indexterm> |
| 12669 |
<listitem> |
| 12670 |
<para>The familiar <command>cd</command> change directory |
| 12671 |
command finds use in scripts where execution of a command |
| 12672 |
requires being in a specified directory.</para> |
| 12673 |
|
| 12674 |
<para> |
| 12675 |
<programlisting>(cd /source/directory && tar cf - . ) | (cd /dest/directory && tar xpvf -)</programlisting> |
| 12676 |
[from the <link linkend="coxex">previously cited</link> |
| 12677 |
example by Alan Cox]</para> |
| 12678 |
|
| 12679 |
<para>The <option>-P</option> (physical) option to |
| 12680 |
<command>cd</command> causes it to ignore symbolic |
| 12681 |
links.</para> |
| 12682 |
|
| 12683 |
<para><command>cd -</command> changes to <link |
| 12684 |
linkend="oldpwd">$OLDPWD</link>, the previous working |
| 12685 |
directory.</para> |
| 12686 |
|
| 12687 |
<para><anchor id="doubleslashref"></para> |
| 12688 |
|
| 12689 |
<caution><para>The <command>cd</command> command does not function |
| 12690 |
as expected when presented with two forward slashes. |
| 12691 |
|
| 12692 |
<screen> |
| 12693 |
<prompt>bash$ </prompt><userinput>cd //</userinput> |
| 12694 |
<prompt>bash$ </prompt><userinput>pwd</userinput> |
| 12695 |
<computeroutput>//</computeroutput> |
| 12696 |
</screen> |
| 12697 |
The output should, of course, be <computeroutput>/</computeroutput>. |
| 12698 |
This is a problem both from the command-line and in a script.</para></caution> |
| 12699 |
|
| 12700 |
</listitem> |
| 12701 |
</varlistentry> |
| 12702 |
|
| 12703 |
<varlistentry> |
| 12704 |
<term><anchor id="pwd2ref"><command>pwd</command></term> |
| 12705 |
<indexterm> |
| 12706 |
<primary>pwd</primary> |
| 12707 |
</indexterm> |
| 12708 |
<indexterm> |
| 12709 |
<primary>command</primary> |
| 12710 |
<secondary>pwd</secondary> |
| 12711 |
</indexterm> |
| 12712 |
<indexterm> |
| 12713 |
<primary>$PWD</primary> |
| 12714 |
</indexterm> |
| 12715 |
<indexterm> |
| 12716 |
<primary>variable</primary> |
| 12717 |
<secondary>$PWD</secondary> |
| 12718 |
</indexterm> |
| 12719 |
<indexterm> |
| 12720 |
<primary>directory</primary> |
| 12721 |
<secondary>working</secondary> |
| 12722 |
</indexterm> |
| 12723 |
<listitem><para>Print Working Directory. This gives the user's |
| 12724 |
(or script's) current directory (see <xref |
| 12725 |
linkend="ex37">). The effect is identical to |
| 12726 |
reading the value of the builtin variable <link |
| 12727 |
linkend="pwdref">$PWD</link>.</para> |
| 12728 |
|
| 12729 |
</listitem> |
| 12730 |
</varlistentry> |
| 12731 |
|
| 12732 |
<varlistentry> |
| 12733 |
<term><anchor id="dirsd"><command>pushd</command></term> |
| 12734 |
<term><command>popd</command></term> |
| 12735 |
<term><command>dirs</command></term> |
| 12736 |
<indexterm> |
| 12737 |
<primary>pushd</primary> |
| 12738 |
</indexterm> |
| 12739 |
<indexterm> |
| 12740 |
<primary>command</primary> |
| 12741 |
<secondary>pushd</secondary> |
| 12742 |
</indexterm> |
| 12743 |
<indexterm> |
| 12744 |
<primary>popd</primary> |
| 12745 |
</indexterm> |
| 12746 |
<indexterm> |
| 12747 |
<primary>command</primary> |
| 12748 |
<secondary>popd</secondary> |
| 12749 |
</indexterm> |
| 12750 |
<indexterm> |
| 12751 |
<primary>dirs</primary> |
| 12752 |
</indexterm> |
| 12753 |
<indexterm> |
| 12754 |
<primary>command</primary> |
| 12755 |
<secondary>dirs</secondary> |
| 12756 |
</indexterm> |
| 12757 |
<indexterm> |
| 12758 |
<primary>directory</primary> |
| 12759 |
<secondary>working</secondary> |
| 12760 |
</indexterm> |
| 12761 |
<indexterm> |
| 12762 |
<primary>bookmark</primary> |
| 12763 |
</indexterm> |
| 12764 |
<listitem><para>This command set is a mechanism for bookmarking |
| 12765 |
working directories, a means of moving back and forth |
| 12766 |
through directories in an orderly manner. A pushdown <link |
| 12767 |
linkend="stackdefref">stack</link> is used to keep track |
| 12768 |
of directory names. Options allow various manipulations |
| 12769 |
of the directory stack.</para> |
| 12770 |
|
| 12771 |
<para><anchor id="pushdref"><userinput>pushd |
| 12772 |
dir-name</userinput> pushes the path |
| 12773 |
<replaceable>dir-name</replaceable> onto the directory |
| 12774 |
stack (to the <firstterm>top</firstterm> of the stack) |
| 12775 |
and simultaneously changes the current working directory |
| 12776 |
to <replaceable>dir-name</replaceable></para> |
| 12777 |
|
| 12778 |
<para><anchor id="popdref"><command>popd</command> removes |
| 12779 |
(pops) the top directory path name off the directory stack |
| 12780 |
and simultaneously changes the current working directory |
| 12781 |
to the directory now at the <firstterm>top</firstterm> of |
| 12782 |
the stack.</para> |
| 12783 |
|
| 12784 |
<para><anchor id="dirsref"><command>dirs</command> lists |
| 12785 |
the contents of the directory stack (compare this |
| 12786 |
with the <link linkend="dirstackref">$DIRSTACK</link> |
| 12787 |
variable). A successful <command>pushd</command> |
| 12788 |
or <command>popd</command> will automatically invoke |
| 12789 |
<command>dirs</command>.</para> |
| 12790 |
|
| 12791 |
|
| 12792 |
<para>Scripts that require various changes to the current |
| 12793 |
working directory without hard-coding the directory name |
| 12794 |
changes can make good use of these commands. Note that |
| 12795 |
the implicit <varname>$DIRSTACK</varname> array variable, |
| 12796 |
accessible from within a script, holds the contents of |
| 12797 |
the directory stack. |
| 12798 |
</para> |
| 12799 |
|
| 12800 |
<example id="ex37"> |
| 12801 |
<title>Changing the current working directory</title> |
| 12802 |
<programlisting>&ex37;</programlisting> |
| 12803 |
</example> |
| 12804 |
|
| 12805 |
</listitem> |
| 12806 |
</varlistentry> |
| 12807 |
|
| 12808 |
</variablelist> |
| 12809 |
|
| 12810 |
|
| 12811 |
<variablelist id="intvar"> |
| 12812 |
<title><anchor id="intvar1">Variables</title> |
| 12813 |
|
| 12814 |
<varlistentry> |
| 12815 |
<term><anchor id="letref"><command>let</command></term> |
| 12816 |
<indexterm> |
| 12817 |
<primary>let</primary> |
| 12818 |
</indexterm> |
| 12819 |
<indexterm> |
| 12820 |
<primary>command</primary> |
| 12821 |
<secondary>let</secondary> |
| 12822 |
</indexterm> |
| 12823 |
<listitem> |
| 12824 |
<para>The <command>let</command> command carries out |
| 12825 |
<firstterm>arithmetic</firstterm> operations on variables. |
| 12826 |
|
| 12827 |
<footnote><para>Note that <firstterm>let</firstterm> |
| 12828 |
<link linkend="letbad">cannot be used |
| 12829 |
for setting <firstterm>string</firstterm> |
| 12830 |
variables.</link></para></footnote> |
| 12831 |
|
| 12832 |
In many cases, it functions as a less complex version |
| 12833 |
of <link linkend="exprref">expr</link>.</para> |
| 12834 |
|
| 12835 |
<example id="ex46"> |
| 12836 |
<title>Letting <firstterm>let</firstterm> do arithmetic.</title> |
| 12837 |
<programlisting>&ex46;</programlisting> |
| 12838 |
</example> |
| 12839 |
|
| 12840 |
<para><anchor id="exitvalanomaly02"></para> |
| 12841 |
|
| 12842 |
<caution> |
| 12843 |
<para>The <firstterm>let</firstterm> command can, |
| 12844 |
in certain contexts, return a surprising <link |
| 12845 |
linkend="exitstatusref">exit status</link>.</para> |
| 12846 |
<para><programlisting># Evgeniy Ivanov points out: |
| 12847 |
|
| 12848 |
var=0 |
| 12849 |
echo $? # 0 |
| 12850 |
# As expected. |
| 12851 |
|
| 12852 |
let var++ |
| 12853 |
echo $? # 1 |
| 12854 |
# The command was successful, so why isn't $?=0 ??? |
| 12855 |
# Anomaly! |
| 12856 |
|
| 12857 |
let var++ |
| 12858 |
echo $? # 0 |
| 12859 |
# As expected. |
| 12860 |
|
| 12861 |
|
| 12862 |
# Likewise . . . |
| 12863 |
|
| 12864 |
let var=0 |
| 12865 |
echo $? # 1 |
| 12866 |
# The command was successful, so why isn't $?=0 ??? |
| 12867 |
|
| 12868 |
# However, as Jeff Gorak points out, |
| 12869 |
#+ this is part of the design spec for 'let' . . . |
| 12870 |
# "If the last ARG evaluates to 0, let returns 1; |
| 12871 |
# let returns 0 otherwise." ['help let']</programlisting></para></caution> |
| 12872 |
|
| 12873 |
</listitem> |
| 12874 |
</varlistentry> |
| 12875 |
|
| 12876 |
<varlistentry> |
| 12877 |
<term><anchor id="evalref"><command>eval</command></term> |
| 12878 |
<indexterm> |
| 12879 |
<primary>eval</primary> |
| 12880 |
</indexterm> |
| 12881 |
<indexterm> |
| 12882 |
<primary>command</primary> |
| 12883 |
<secondary>eval</secondary> |
| 12884 |
</indexterm> |
| 12885 |
<listitem> |
| 12886 |
|
| 12887 |
<para><userinput>eval arg1 [arg2] ... [argN]</userinput></para> |
| 12888 |
|
| 12889 |
<para>Combines the arguments in an expression or list of |
| 12890 |
expressions and <replaceable>evaluates</replaceable> them. |
| 12891 |
Any variables within the expression are expanded. The |
| 12892 |
net result is to <command>convert a string into a |
| 12893 |
command</command>.</para> |
| 12894 |
|
| 12895 |
<tip><para>The <command>eval</command> command can be used for |
| 12896 |
code generation from the command-line or within a script. |
| 12897 |
</para></tip> |
| 12898 |
|
| 12899 |
<para> |
| 12900 |
<screen> |
| 12901 |
<prompt>bash$ </prompt><userinput>command_string="ps ax"</userinput> |
| 12902 |
<prompt>bash$ </prompt><userinput>process="ps ax"</userinput> |
| 12903 |
<prompt>bash$ </prompt><userinput>eval "$command_string" | grep "$process"</userinput> |
| 12904 |
<computeroutput>26973 pts/3 R+ 0:00 grep --color ps ax |
| 12905 |
26974 pts/3 R+ 0:00 ps ax</computeroutput> |
| 12906 |
</screen> |
| 12907 |
</para> |
| 12908 |
|
| 12909 |
<para><anchor id="evalforced"></para> |
| 12910 |
<para>Each invocation of <firstterm>eval</firstterm> forces |
| 12911 |
a re-<emphasis>evaluation</emphasis> of its arguments. |
| 12912 |
<programlisting>a='$b' |
| 12913 |
b='$c' |
| 12914 |
c=d |
| 12915 |
|
| 12916 |
echo $a # $b |
| 12917 |
# First level. |
| 12918 |
eval echo $a # $c |
| 12919 |
# Second level. |
| 12920 |
eval eval echo $a # d |
| 12921 |
# Third level. |
| 12922 |
|
| 12923 |
# Thank you, E. Choroba.</programlisting></para> |
| 12924 |
|
| 12925 |
|
| 12926 |
<para><anchor id="evaleff"></para> |
| 12927 |
<example id="ex43"> |
| 12928 |
<title>Showing the effect of <firstterm>eval</firstterm></title> |
| 12929 |
<programlisting>&ex43;</programlisting> |
| 12930 |
</example> |
| 12931 |
|
| 12932 |
<para><anchor id="arrchoice0"></para> |
| 12933 |
<example id="arrchoice"> |
| 12934 |
<title>Using <firstterm>eval</firstterm> to select |
| 12935 |
among variables</title> |
| 12936 |
<programlisting>&arrchoice;</programlisting> |
| 12937 |
</example> |
| 12938 |
|
| 12939 |
<example id="echoparams"> |
| 12940 |
<title><firstterm>Echoing</firstterm> the |
| 12941 |
<firstterm>command-line parameters</firstterm></title> |
| 12942 |
<programlisting>&echoparams;</programlisting> |
| 12943 |
</example> |
| 12944 |
|
| 12945 |
<example id="ex44"> |
| 12946 |
<title>Forcing a log-off</title> |
| 12947 |
<programlisting>&ex44;</programlisting> |
| 12948 |
</example> |
| 12949 |
|
| 12950 |
<example id="rot14"> |
| 12951 |
<title>A version of <firstterm>rot13</firstterm></title> |
| 12952 |
<programlisting>&rot14;</programlisting> |
| 12953 |
</example> |
| 12954 |
|
| 12955 |
<para>Here is another example of using |
| 12956 |
<firstterm>eval</firstterm> to |
| 12957 |
<emphasis>evaluate</emphasis> a complex expression, |
| 12958 |
this one from an earlier version of YongYe's <ulink |
| 12959 |
url="https://github.com/yongye/shell/blob/master/Tetris_Game.sh">Tetris |
| 12960 |
game script</ulink>.</para |
| 12961 |
|
| 12962 |
<para> |
| 12963 |
<programlisting>eval ${1}+=\"${x} ${y} \"</programlisting> |
| 12964 |
</para> |
| 12965 |
|
| 12966 |
<para><xref linkend="samorse"> uses |
| 12967 |
<firstterm>eval</firstterm> to convert <link |
| 12968 |
linkend="arrayref">array</link> elements into a command |
| 12969 |
list.</para> |
| 12970 |
|
| 12971 |
<para>The <firstterm>eval</firstterm> command occurs |
| 12972 |
in the older version of <link linkend="ivrref">indirect |
| 12973 |
referencing</link>. |
| 12974 |
<programlisting>eval var=\$$var</programlisting> |
| 12975 |
</para> |
| 12976 |
|
| 12977 |
<tip><para>The <firstterm>eval</firstterm> command can |
| 12978 |
be used to <link linkend="braceexpref3">parameterize |
| 12979 |
<firstterm>brace expansion</firstterm></link>.</para></tip> |
| 12980 |
|
| 12981 |
|
| 12982 |
<para><anchor id="evalrisk"></para> |
| 12983 |
<caution><para>The <command>eval</command> command can be |
| 12984 |
risky, and normally should be avoided when there |
| 12985 |
exists a reasonable alternative. An <userinput>eval |
| 12986 |
$COMMANDS</userinput> executes the contents of |
| 12987 |
<replaceable>COMMANDS</replaceable>, which may |
| 12988 |
contain such unpleasant surprises as <command>rm -rf |
| 12989 |
*</command>. Running an <command>eval</command> on |
| 12990 |
unfamiliar code written by persons unknown is living |
| 12991 |
dangerously.</para></caution> |
| 12992 |
|
| 12993 |
|
| 12994 |
</listitem> |
| 12995 |
</varlistentry> |
| 12996 |
|
| 12997 |
<varlistentry> |
| 12998 |
<term><anchor id="setref"><command>set</command></term> |
| 12999 |
<indexterm> |
| 13000 |
<primary>set</primary> |
| 13001 |
</indexterm> |
| 13002 |
<indexterm> |
| 13003 |
<primary>command</primary> |
| 13004 |
<secondary>set</secondary> |
| 13005 |
</indexterm> |
| 13006 |
<listitem><para>The <command>set</command> command changes |
| 13007 |
the value of internal script variables/options. One use for |
| 13008 |
this is to toggle <link linkend="optionsref">option |
| 13009 |
flags</link> which help determine the behavior of the |
| 13010 |
script. Another application for it is to reset the <link |
| 13011 |
linkend="posparamref">positional parameters</link> that |
| 13012 |
a script sees as the result of a command (<userinput>set |
| 13013 |
`command`</userinput>). The script can then parse the |
| 13014 |
<link linkend="fieldref">fields</link> of the command |
| 13015 |
output.</para> |
| 13016 |
|
| 13017 |
<example id="ex34"> |
| 13018 |
<title>Using <firstterm>set</firstterm> with positional |
| 13019 |
parameters</title> |
| 13020 |
<programlisting>&ex34;</programlisting> |
| 13021 |
</example> |
| 13022 |
|
| 13023 |
<para>More fun with positional parameters.</para> |
| 13024 |
|
| 13025 |
<example id="revposparams"> |
| 13026 |
<title>Reversing the positional parameters</title> |
| 13027 |
<programlisting>&revposparams;</programlisting> |
| 13028 |
</example> |
| 13029 |
|
| 13030 |
<para>Invoking <command>set</command> without any options or |
| 13031 |
arguments simply lists all the <link |
| 13032 |
linkend="envref">environmental</link> and other variables |
| 13033 |
that have been initialized.</para> |
| 13034 |
|
| 13035 |
<para> |
| 13036 |
<screen> |
| 13037 |
<prompt>bash$ </prompt><userinput>set</userinput> |
| 13038 |
<computeroutput>AUTHORCOPY=/home/bozo/posts |
| 13039 |
BASH=/bin/bash |
| 13040 |
BASH_VERSION=$'2.05.8(1)-release' |
| 13041 |
... |
| 13042 |
XAUTHORITY=/home/bozo/.Xauthority |
| 13043 |
_=/etc/bashrc |
| 13044 |
variable22=abc |
| 13045 |
variable23=xzy</computeroutput> |
| 13046 |
</screen> |
| 13047 |
</para> |
| 13048 |
|
| 13049 |
|
| 13050 |
<para>Using <command>set</command> with the <option>--</option> |
| 13051 |
option explicitly assigns the contents of a variable to |
| 13052 |
the positional parameters. If no variable follows the |
| 13053 |
<option>--</option> it <firstterm>unsets</firstterm> |
| 13054 |
the positional parameters.</para> |
| 13055 |
|
| 13056 |
<example id="setpos"> |
| 13057 |
<title>Reassigning the positional parameters</title> |
| 13058 |
<programlisting>&setpos;</programlisting> |
| 13059 |
</example> |
| 13060 |
|
| 13061 |
<para>See also <xref linkend="ex22a"> and <xref linkend="ex33a">.</para> |
| 13062 |
|
| 13063 |
</listitem> |
| 13064 |
</varlistentry> |
| 13065 |
|
| 13066 |
<varlistentry> |
| 13067 |
<term><anchor id="unsetref"><command>unset</command></term> |
| 13068 |
<indexterm> |
| 13069 |
<primary>unset</primary> |
| 13070 |
</indexterm> |
| 13071 |
<indexterm> |
| 13072 |
<primary>command</primary> |
| 13073 |
<secondary>unset</secondary> |
| 13074 |
</indexterm> |
| 13075 |
<listitem><para>The <command>unset</command> command deletes a |
| 13076 |
shell variable, effectively setting it to |
| 13077 |
<firstterm>null</firstterm>. Note that this command does |
| 13078 |
not affect positional parameters.</para> |
| 13079 |
|
| 13080 |
<para> |
| 13081 |
<screen> |
| 13082 |
<prompt>bash$ </prompt><userinput>unset PATH</userinput> |
| 13083 |
|
| 13084 |
<prompt>bash$ </prompt><userinput>echo $PATH</userinput> |
| 13085 |
<computeroutput> |
| 13086 |
|
| 13087 |
|
| 13088 |
</computeroutput> |
| 13089 |
<prompt>bash$ </prompt></screen> |
| 13090 |
</para> |
| 13091 |
|
| 13092 |
<example id="uns"> |
| 13093 |
<title><quote>Unsetting</quote> a variable</title> |
| 13094 |
<programlisting>&uns;</programlisting> |
| 13095 |
</example> |
| 13096 |
|
| 13097 |
<note><para>In most contexts, an <firstterm>undeclared</firstterm> |
| 13098 |
variable and one that has been <firstterm>unset</firstterm> |
| 13099 |
are equivalent. However, the <link linkend="unddr"> |
| 13100 |
${parameter:-default}</link> parameter substitution |
| 13101 |
construct can distinguish between the two.</para></note> |
| 13102 |
|
| 13103 |
</listitem> |
| 13104 |
</varlistentry> |
| 13105 |
|
| 13106 |
<varlistentry> |
| 13107 |
<term><anchor id="exportref"><command>export</command></term> |
| 13108 |
<indexterm> |
| 13109 |
<primary>export</primary> |
| 13110 |
</indexterm> |
| 13111 |
<indexterm> |
| 13112 |
<primary>command</primary> |
| 13113 |
<secondary>export</secondary> |
| 13114 |
</indexterm> |
| 13115 |
<listitem> |
| 13116 |
|
| 13117 |
<para><anchor id="exportref2"></para> |
| 13118 |
<para>The <command>export</command> |
| 13119 |
|
| 13120 |
<footnote><para>To <firstterm>Export</firstterm> |
| 13121 |
information is to make it available in a more general context. |
| 13122 |
See also <link |
| 13123 |
linkend="scoperef">scope</link>.</para></footnote> |
| 13124 |
|
| 13125 |
command makes available variables to all child processes |
| 13126 |
of the running script or shell. One important use |
| 13127 |
of the <command>export</command> command is in <link |
| 13128 |
linkend="filesref1">startup files</link>, to initialize |
| 13129 |
and make accessible <link linkend="envref">environmental |
| 13130 |
variables</link> to subsequent user processes.</para> |
| 13131 |
|
| 13132 |
<caution><para>Unfortunately, <link linkend="parchildprobref"> |
| 13133 |
there is no way to export variables back to the parent |
| 13134 |
process</link>, to the process that called or invoked the |
| 13135 |
script or shell.</para></caution> |
| 13136 |
|
| 13137 |
|
| 13138 |
<para><anchor id="exportawk"></para> |
| 13139 |
<example id="coltotaler3"> |
| 13140 |
<title>Using <firstterm>export</firstterm> to pass a variable to an |
| 13141 |
embedded <firstterm>awk</firstterm> script</title> |
| 13142 |
<programlisting>&coltotaler3;</programlisting> |
| 13143 |
</example> |
| 13144 |
|
| 13145 |
|
| 13146 |
|
| 13147 |
<tip> |
| 13148 |
|
| 13149 |
<para>It is possible to initialize and export |
| 13150 |
variables in the same operation, as in <command>export |
| 13151 |
var1=xxx</command>.</para> |
| 13152 |
|
| 13153 |
<para>However, as Greg Keraunen points out, in certain |
| 13154 |
situations this may have a different effect than |
| 13155 |
setting a variable, then exporting it.</para> |
| 13156 |
|
| 13157 |
<para> |
| 13158 |
<screen> |
| 13159 |
<prompt>bash$ </prompt><userinput>export var=(a b); echo ${var[0]}</userinput> |
| 13160 |
<computeroutput>(a b)</computeroutput> |
| 13161 |
|
| 13162 |
|
| 13163 |
|
| 13164 |
<prompt>bash$ </prompt><userinput>var=(a b); export var; echo ${var[0]}</userinput> |
| 13165 |
<computeroutput>a</computeroutput> |
| 13166 |
</screen> |
| 13167 |
</para> |
| 13168 |
|
| 13169 |
|
| 13170 |
</tip> |
| 13171 |
|
| 13172 |
<note> |
| 13173 |
<para>A variable to be exported may require special |
| 13174 |
treatment. See <xref linkend="bashprof">.</para> |
| 13175 |
</note> |
| 13176 |
|
| 13177 |
</listitem> |
| 13178 |
</varlistentry> |
| 13179 |
|
| 13180 |
<varlistentry> |
| 13181 |
<term><anchor id="declare2ref"><command>declare</command></term> |
| 13182 |
<term><command>typeset</command></term> |
| 13183 |
<indexterm> |
| 13184 |
<primary>declare</primary> |
| 13185 |
</indexterm> |
| 13186 |
<indexterm> |
| 13187 |
<primary>command</primary> |
| 13188 |
<secondary>declare</secondary> |
| 13189 |
</indexterm> |
| 13190 |
<indexterm> |
| 13191 |
<primary>typeset</primary> |
| 13192 |
</indexterm> |
| 13193 |
<indexterm> |
| 13194 |
<primary>command</primary> |
| 13195 |
<secondary>typeset</secondary> |
| 13196 |
</indexterm> |
| 13197 |
<listitem> |
| 13198 |
<para>The <link linkend="declareref">declare</link> and |
| 13199 |
<link linkend="declareref">typeset</link> commands specify |
| 13200 |
and/or restrict properties of variables.</para> |
| 13201 |
</listitem> |
| 13202 |
</varlistentry> |
| 13203 |
|
| 13204 |
<varlistentry> |
| 13205 |
<term><anchor id="readonlyref"><command>readonly</command></term> |
| 13206 |
<indexterm> |
| 13207 |
<primary>readonly</primary> |
| 13208 |
</indexterm> |
| 13209 |
<indexterm> |
| 13210 |
<primary>command</primary> |
| 13211 |
<secondary>readonly</secondary> |
| 13212 |
</indexterm> |
| 13213 |
<listitem><para>Same as <link linkend="declareref">declare -r</link>, |
| 13214 |
sets a variable as read-only, or, in effect, as a |
| 13215 |
constant. Attempts to change the variable fail with |
| 13216 |
an error message. This is the shell analog of the |
| 13217 |
<firstterm>C</firstterm> language <command>const</command> |
| 13218 |
type qualifier.</para> |
| 13219 |
|
| 13220 |
</listitem> |
| 13221 |
</varlistentry> |
| 13222 |
|
| 13223 |
<varlistentry> |
| 13224 |
<term><anchor id="getoptsx"><command>getopts</command></term> |
| 13225 |
<indexterm> |
| 13226 |
<primary>getopts</primary> |
| 13227 |
</indexterm> |
| 13228 |
<indexterm> |
| 13229 |
<primary>command</primary> |
| 13230 |
<secondary>getopts</secondary> |
| 13231 |
</indexterm> |
| 13232 |
<indexterm> |
| 13233 |
<primary>$OPTIND</primary> |
| 13234 |
</indexterm> |
| 13235 |
<indexterm> |
| 13236 |
<primary>variable</primary> |
| 13237 |
<secondary>$OPTIND</secondary> |
| 13238 |
</indexterm> |
| 13239 |
<indexterm> |
| 13240 |
<primary>$OPTARG</primary> |
| 13241 |
</indexterm> |
| 13242 |
<indexterm> |
| 13243 |
<primary>variable</primary> |
| 13244 |
<secondary>$OPTARG</secondary> |
| 13245 |
</indexterm> |
| 13246 |
<listitem> |
| 13247 |
<para>This powerful tool parses command-line arguments passed |
| 13248 |
to the script. This is the Bash analog of the <link |
| 13249 |
linkend="getopty">getopt</link> external command and the |
| 13250 |
<firstterm>getopt</firstterm> library function familiar to |
| 13251 |
<firstterm>C</firstterm> programmers. It permits passing |
| 13252 |
and concatenating multiple options |
| 13253 |
|
| 13254 |
<footnote><para>An <firstterm>option</firstterm> is an |
| 13255 |
argument that acts as a flag, switching script behaviors |
| 13256 |
on or off. The argument associated with a particular |
| 13257 |
option indicates the behavior that the option (flag) |
| 13258 |
switches on or off.</para></footnote> |
| 13259 |
|
| 13260 |
and associated arguments to a script (for |
| 13261 |
example <userinput>scriptname -abc -e |
| 13262 |
/usr/local</userinput>).</para> |
| 13263 |
|
| 13264 |
<para><anchor id="getoptsopt"></para> |
| 13265 |
<para>The <command>getopts</command> construct uses two implicit |
| 13266 |
variables. <varname>$OPTIND</varname> is the argument |
| 13267 |
pointer (<wordasword>OPTion INDex</wordasword>) |
| 13268 |
and <varname>$OPTARG</varname> (<wordasword>OPTion |
| 13269 |
ARGument</wordasword>) the (optional) argument attached |
| 13270 |
to an option. A colon following the option name in the |
| 13271 |
declaration tags that option as having an associated |
| 13272 |
argument.</para> |
| 13273 |
|
| 13274 |
<para>A <command>getopts</command> construct usually comes |
| 13275 |
packaged in a <link linkend="whileloopref">while |
| 13276 |
loop</link>, which processes the options and |
| 13277 |
arguments one at a time, then increments the implicit |
| 13278 |
<varname>$OPTIND</varname> variable to point to the |
| 13279 |
next.</para> |
| 13280 |
|
| 13281 |
<note> |
| 13282 |
<para> |
| 13283 |
<orderedlist> |
| 13284 |
<listitem> |
| 13285 |
|
| 13286 |
<para>The arguments passed from the command-line to |
| 13287 |
the script must be preceded by a |
| 13288 |
dash (<option>-</option>). It is the |
| 13289 |
prefixed <option>-</option> that lets |
| 13290 |
<command>getopts</command> recognize command-line |
| 13291 |
arguments as <firstterm>options</firstterm>. |
| 13292 |
In fact, <command>getopts</command> will not process |
| 13293 |
arguments without the prefixed <option>-</option>, |
| 13294 |
and will terminate option processing at the first |
| 13295 |
argument encountered lacking them.</para> |
| 13296 |
|
| 13297 |
</listitem> |
| 13298 |
<listitem><para>The <command>getopts</command> template |
| 13299 |
differs slightly from the standard <link |
| 13300 |
linkend="whileloopref">while loop</link>, in that |
| 13301 |
it lacks condition brackets.</para> |
| 13302 |
</listitem> |
| 13303 |
<listitem> |
| 13304 |
|
| 13305 |
<para>The <command>getopts</command> construct is a highly |
| 13306 |
functional replacement for the traditional |
| 13307 |
<link linkend="getopty">getopt</link> external |
| 13308 |
command.</para> |
| 13309 |
|
| 13310 |
</listitem> |
| 13311 |
</orderedlist> |
| 13312 |
</para> |
| 13313 |
</note> |
| 13314 |
|
| 13315 |
<para><programlisting> |
| 13316 |
while getopts ":abcde:fg" Option |
| 13317 |
# Initial declaration. |
| 13318 |
# a, b, c, d, e, f, and g are the options (flags) expected. |
| 13319 |
# The : after option 'e' shows it will have an argument passed with it. |
| 13320 |
do |
| 13321 |
case $Option in |
| 13322 |
a ) # Do something with variable 'a'. |
| 13323 |
b ) # Do something with variable 'b'. |
| 13324 |
... |
| 13325 |
e) # Do something with 'e', and also with $OPTARG, |
| 13326 |
# which is the associated argument passed with option 'e'. |
| 13327 |
... |
| 13328 |
g ) # Do something with variable 'g'. |
| 13329 |
esac |
| 13330 |
done |
| 13331 |
shift $(($OPTIND - 1)) |
| 13332 |
# Move argument pointer to next. |
| 13333 |
|
| 13334 |
# All this is not nearly as complicated as it looks <grin>.</programlisting></para> |
| 13335 |
|
| 13336 |
<example id="ex33"> |
| 13337 |
<title>Using <firstterm>getopts</firstterm> to read the |
| 13338 |
options/arguments passed to a script</title> |
| 13339 |
<programlisting>&ex33;</programlisting> |
| 13340 |
</example> |
| 13341 |
|
| 13342 |
</listitem> |
| 13343 |
</varlistentry> |
| 13344 |
|
| 13345 |
</variablelist> |
| 13346 |
|
| 13347 |
|
| 13348 |
<variablelist id="intscrbeh"> |
| 13349 |
<title><anchor id="intscrbeh1">Script Behavior</title> |
| 13350 |
|
| 13351 |
<varlistentry> |
| 13352 |
<term><anchor id="sourceref"><command>source</command></term> |
| 13353 |
<term><token>.</token> (<link linkend="dotref">dot</link> command)</term> |
| 13354 |
<indexterm> |
| 13355 |
<primary>source</primary> |
| 13356 |
</indexterm> |
| 13357 |
<indexterm> |
| 13358 |
<primary>command</primary> |
| 13359 |
<secondary>source</secondary> |
| 13360 |
</indexterm> |
| 13361 |
<indexterm> |
| 13362 |
<primary>.</primary> |
| 13363 |
</indexterm> |
| 13364 |
<indexterm> |
| 13365 |
<primary>command</primary> |
| 13366 |
<secondary>.</secondary> |
| 13367 |
</indexterm> |
| 13368 |
|
| 13369 |
<listitem> |
| 13370 |
|
| 13371 |
<para>This command, when invoked from the command-line, |
| 13372 |
executes a script. Within a script, a |
| 13373 |
<userinput>source file-name</userinput> |
| 13374 |
loads the file <filename>file-name</filename>. |
| 13375 |
<firstterm>Sourcing</firstterm> a file (dot-command) |
| 13376 |
<firstterm>imports</firstterm> |
| 13377 |
code into the script, appending to the script (same effect |
| 13378 |
as the <userinput>#include</userinput> directive in a |
| 13379 |
<firstterm>C</firstterm> program). The net result is the |
| 13380 |
same as if the <quote>sourced</quote> lines of code were |
| 13381 |
physically present in the body of the script. This is useful |
| 13382 |
in situations when multiple scripts use a common data file |
| 13383 |
or function library.</para> |
| 13384 |
|
| 13385 |
<example id="ex38"> |
| 13386 |
<title><quote>Including</quote> a data file</title> |
| 13387 |
<programlisting>&ex38;</programlisting> |
| 13388 |
|
| 13389 |
<para>File <filename>data-file</filename> for <xref |
| 13390 |
linkend="ex38">, above. Must be present in same |
| 13391 |
directory.</para> |
| 13392 |
|
| 13393 |
<programlisting>&ex38bis;</programlisting> |
| 13394 |
</example> |
| 13395 |
|
| 13396 |
<para>If the <firstterm>sourced</firstterm> file is itself |
| 13397 |
an executable script, then it will run, then return |
| 13398 |
control to the script that called it. A |
| 13399 |
<firstterm>sourced</firstterm> executable script may use a |
| 13400 |
<link linkend="returnref">return</link> for this |
| 13401 |
purpose.</para> |
| 13402 |
|
| 13403 |
<para><anchor id="sourceparams"></para> |
| 13404 |
<para> |
| 13405 |
Arguments may be (optionally) passed to the |
| 13406 |
<firstterm>sourced</firstterm> file as <link |
| 13407 |
linkend="posparamref1">positional parameters</link>. |
| 13408 |
<programlisting>source $filename $arg1 arg2</programlisting> |
| 13409 |
</para> |
| 13410 |
|
| 13411 |
|
| 13412 |
|
| 13413 |
<para>It is even possible for a script to |
| 13414 |
<firstterm>source</firstterm> itself, though this does not |
| 13415 |
seem to have any practical applications.</para> |
| 13416 |
|
| 13417 |
<example id="selfsource"> |
| 13418 |
<title>A (useless) script that sources itself</title> |
| 13419 |
<programlisting>&selfsource;</programlisting> |
| 13420 |
</example> |
| 13421 |
|
| 13422 |
|
| 13423 |
</listitem> |
| 13424 |
</varlistentry> |
| 13425 |
|
| 13426 |
|
| 13427 |
<varlistentry> |
| 13428 |
<term><anchor id="exitref"><command>exit</command></term> |
| 13429 |
<indexterm> |
| 13430 |
<primary>exit</primary> |
| 13431 |
</indexterm> |
| 13432 |
<indexterm> |
| 13433 |
<primary>command</primary> |
| 13434 |
<secondary>exit</secondary> |
| 13435 |
</indexterm> |
| 13436 |
<listitem> |
| 13437 |
<para>Unconditionally terminates a script. |
| 13438 |
<footnote><para>Technically, an |
| 13439 |
<command>exit</command> only terminates the |
| 13440 |
process (or shell) in which it is running, |
| 13441 |
<emphasis>not</emphasis> the <firstterm>parent |
| 13442 |
process</firstterm>.</para></footnote> |
| 13443 |
The <command>exit</command> command may optionally take an |
| 13444 |
integer argument, which is returned to the shell as |
| 13445 |
the <link linkend="exitstatusref">exit status</link> |
| 13446 |
of the script. It is good practice to end all but the |
| 13447 |
simplest scripts with an <userinput>exit 0</userinput>, |
| 13448 |
indicating a successful run.</para> |
| 13449 |
|
| 13450 |
<note><para>If a script terminates with an <command>exit</command> |
| 13451 |
lacking an argument, the exit status of the script is the exit |
| 13452 |
status of the last command executed in the script, not counting |
| 13453 |
the <command>exit</command>. This is equivalent to an |
| 13454 |
<command>exit $?</command>.</para></note> |
| 13455 |
|
| 13456 |
<note><para>An <command>exit</command> command may also be used to |
| 13457 |
terminate a <link |
| 13458 |
linkend="subshellsref">subshell</link>.</para></note> |
| 13459 |
|
| 13460 |
</listitem> |
| 13461 |
</varlistentry> |
| 13462 |
|
| 13463 |
<varlistentry> |
| 13464 |
<term><anchor id="execref"><command>exec</command></term> |
| 13465 |
<indexterm> |
| 13466 |
<primary>exec</primary> |
| 13467 |
</indexterm> |
| 13468 |
<indexterm> |
| 13469 |
<primary>command</primary> |
| 13470 |
<secondary>exec</secondary> |
| 13471 |
</indexterm> |
| 13472 |
<listitem> |
| 13473 |
|
| 13474 |
<para> |
| 13475 |
This shell builtin replaces the current process with |
| 13476 |
a specified command. Normally, when the shell encounters |
| 13477 |
a command, it <link linkend="forkref">forks off</link> a |
| 13478 |
child process to actually execute the command. Using the |
| 13479 |
<command>exec</command> builtin, the shell does not fork, |
| 13480 |
and the command <firstterm>exec</firstterm>'ed replaces |
| 13481 |
the shell. When used in a script, therefore, it forces an |
| 13482 |
exit from the script when the <command>exec</command>'ed |
| 13483 |
command terminates. |
| 13484 |
<footnote><para>Unless the <command>exec</command> is used |
| 13485 |
to <link linkend="usingexecref">reassign file |
| 13486 |
descriptors</link>.</para></footnote> |
| 13487 |
</para> |
| 13488 |
|
| 13489 |
<example id="ex54"> |
| 13490 |
<title>Effects of <firstterm>exec</firstterm></title> |
| 13491 |
<programlisting>&ex54;</programlisting> |
| 13492 |
</example> |
| 13493 |
|
| 13494 |
<example id="selfexec"> |
| 13495 |
<title>A script that <firstterm>exec's</firstterm> itself</title> |
| 13496 |
<programlisting>&selfexec;</programlisting> |
| 13497 |
</example> |
| 13498 |
|
| 13499 |
<para>An <command>exec</command> also serves to <link |
| 13500 |
linkend="usingexecref">reassign |
| 13501 |
file descriptors</link>. For example, <userinput>exec |
| 13502 |
<zzz-file</userinput> replaces <filename>stdin</filename> |
| 13503 |
with the file <filename>zzz-file</filename>.</para> |
| 13504 |
|
| 13505 |
<note><para>The <option>-exec</option> option to |
| 13506 |
<link linkend="findref">find</link> is |
| 13507 |
<replaceable>not</replaceable> the same as the |
| 13508 |
<command>exec</command> shell builtin.</para></note> |
| 13509 |
|
| 13510 |
</listitem> |
| 13511 |
</varlistentry> |
| 13512 |
|
| 13513 |
|
| 13514 |
<varlistentry> |
| 13515 |
<term><anchor id="shoptref"><command>shopt</command></term> |
| 13516 |
<indexterm> |
| 13517 |
<primary>shopt</primary> |
| 13518 |
</indexterm> |
| 13519 |
<indexterm> |
| 13520 |
<primary>command</primary> |
| 13521 |
<secondary>shopt</secondary> |
| 13522 |
</indexterm> |
| 13523 |
<listitem> |
| 13524 |
<para>This command permits changing <firstterm>shell |
| 13525 |
options</firstterm> on the fly (see <xref linkend="al"> |
| 13526 |
and <xref linkend="unal">). It often appears in the Bash |
| 13527 |
<link linkend="filesref1">startup files</link>, but also has |
| 13528 |
its uses in scripts. Needs <link linkend="bash2ref">version |
| 13529 |
2</link> or later of Bash.</para> |
| 13530 |
|
| 13531 |
<para><programlisting>shopt -s cdspell |
| 13532 |
# Allows minor misspelling of directory names with 'cd' |
| 13533 |
# Option -s sets, -u unsets. |
| 13534 |
|
| 13535 |
cd /hpme # Oops! Mistyped '/home'. |
| 13536 |
pwd # /home |
| 13537 |
# The shell corrected the misspelling.</programlisting></para> |
| 13538 |
|
| 13539 |
</listitem> |
| 13540 |
</varlistentry> |
| 13541 |
|
| 13542 |
|
| 13543 |
<varlistentry> |
| 13544 |
<term><command>caller</command><anchor id="callerref"></term> |
| 13545 |
<indexterm> |
| 13546 |
<primary>caller</primary> |
| 13547 |
</indexterm> |
| 13548 |
<indexterm> |
| 13549 |
<primary>command</primary> |
| 13550 |
<secondary>caller</secondary> |
| 13551 |
</indexterm> |
| 13552 |
<listitem> |
| 13553 |
<para>Putting a <command>caller</command> command |
| 13554 |
inside a <link linkend="functionref">function</link> |
| 13555 |
echoes to <filename>stdout</filename> information about |
| 13556 |
the <firstterm>caller</firstterm> of that function.</para> |
| 13557 |
|
| 13558 |
|
| 13559 |
<para><programlisting>#!/bin/bash |
| 13560 |
|
| 13561 |
function1 () |
| 13562 |
{ |
| 13563 |
# Inside function1 (). |
| 13564 |
caller 0 # Tell me about it. |
| 13565 |
} |
| 13566 |
|
| 13567 |
function1 # Line 9 of script. |
| 13568 |
|
| 13569 |
# 9 main test.sh |
| 13570 |
# ^ Line number that the function was called from. |
| 13571 |
# ^^^^ Invoked from "main" part of script. |
| 13572 |
# ^^^^^^^ Name of calling script. |
| 13573 |
|
| 13574 |
caller 0 # Has no effect because it's not inside a function.</programlisting></para> |
| 13575 |
|
| 13576 |
<para>A <command>caller</command> command can also return |
| 13577 |
<firstterm>caller</firstterm> information from a script <link |
| 13578 |
linkend="sourceref">sourced</link> within another |
| 13579 |
script. Analogous to a function, this is a <quote>subroutine |
| 13580 |
call.</quote></para> |
| 13581 |
|
| 13582 |
<para>You may find this command useful in debugging.</para> |
| 13583 |
|
| 13584 |
</listitem> |
| 13585 |
</varlistentry> |
| 13586 |
|
| 13587 |
|
| 13588 |
|
| 13589 |
</variablelist> |
| 13590 |
|
| 13591 |
|
| 13592 |
<variablelist id="intcommand"> |
| 13593 |
<title><anchor id="intcommand1">Commands</title> |
| 13594 |
|
| 13595 |
<varlistentry> |
| 13596 |
<term><anchor id="trueref"><command>true</command></term> |
| 13597 |
<indexterm> |
| 13598 |
<primary>true</primary> |
| 13599 |
</indexterm> |
| 13600 |
<indexterm> |
| 13601 |
<primary>command</primary> |
| 13602 |
<secondary>true</secondary> |
| 13603 |
</indexterm> |
| 13604 |
<listitem><para>A command that returns a successful |
| 13605 |
(<returnvalue>zero</returnvalue>) <link |
| 13606 |
linkend="exitstatusref">exit status</link>, but does |
| 13607 |
nothing else. |
| 13608 |
</para> |
| 13609 |
|
| 13610 |
<para> |
| 13611 |
<screen> |
| 13612 |
<prompt>bash$ </prompt><userinput>true</userinput> |
| 13613 |
<prompt>bash$ </prompt><userinput>echo $?</userinput> |
| 13614 |
<computeroutput>0</computeroutput> |
| 13615 |
</screen> |
| 13616 |
</para> |
| 13617 |
|
| 13618 |
<para><programlisting># Endless loop |
| 13619 |
while true # alias for ":" |
| 13620 |
do |
| 13621 |
operation-1 |
| 13622 |
operation-2 |
| 13623 |
... |
| 13624 |
operation-n |
| 13625 |
# Need a way to break out of loop or script will hang. |
| 13626 |
done</programlisting></para> |
| 13627 |
|
| 13628 |
</listitem> |
| 13629 |
</varlistentry> |
| 13630 |
|
| 13631 |
<varlistentry> |
| 13632 |
<term><anchor id="falseref"><command>false</command></term> |
| 13633 |
<indexterm> |
| 13634 |
<primary>false</primary> |
| 13635 |
</indexterm> |
| 13636 |
<indexterm> |
| 13637 |
<primary>command</primary> |
| 13638 |
<secondary>false</secondary> |
| 13639 |
</indexterm> |
| 13640 |
<listitem><para>A command that returns an unsuccessful <link |
| 13641 |
linkend="exitstatusref">exit status</link>, |
| 13642 |
but does nothing else.</para> |
| 13643 |
|
| 13644 |
<para> |
| 13645 |
<screen> |
| 13646 |
<prompt>bash$ </prompt><userinput>false</userinput> |
| 13647 |
<prompt>bash$ </prompt><userinput>echo $?</userinput> |
| 13648 |
<computeroutput>1</computeroutput> |
| 13649 |
</screen> |
| 13650 |
</para> |
| 13651 |
|
| 13652 |
<para><programlisting># Testing "false" |
| 13653 |
if false |
| 13654 |
then |
| 13655 |
echo "false evaluates \"true\"" |
| 13656 |
else |
| 13657 |
echo "false evaluates \"false\"" |
| 13658 |
fi |
| 13659 |
# false evaluates "false" |
| 13660 |
|
| 13661 |
|
| 13662 |
# Looping while "false" (null loop) |
| 13663 |
while false |
| 13664 |
do |
| 13665 |
# The following code will not execute. |
| 13666 |
operation-1 |
| 13667 |
operation-2 |
| 13668 |
... |
| 13669 |
operation-n |
| 13670 |
# Nothing happens! |
| 13671 |
done |
| 13672 |
</programlisting></para> |
| 13673 |
|
| 13674 |
</listitem> |
| 13675 |
</varlistentry> |
| 13676 |
|
| 13677 |
<varlistentry> |
| 13678 |
<term><anchor id="typeref"><command>type [cmd]</command></term> |
| 13679 |
<indexterm> |
| 13680 |
<primary>type</primary> |
| 13681 |
</indexterm> |
| 13682 |
<indexterm> |
| 13683 |
<primary>command</primary> |
| 13684 |
<secondary>type</secondary> |
| 13685 |
</indexterm> |
| 13686 |
<indexterm> |
| 13687 |
<primary>variable</primary> |
| 13688 |
<secondary>which</secondary> |
| 13689 |
</indexterm> |
| 13690 |
<listitem><para>Similar to the <link |
| 13691 |
linkend="whichref">which</link> external command, |
| 13692 |
<command>type cmd</command> identifies |
| 13693 |
<quote>cmd.</quote> Unlike <command>which</command>, |
| 13694 |
<command>type</command> is a Bash builtin. The useful |
| 13695 |
<option>-a</option> option to <command>type</command> |
| 13696 |
identifies <replaceable>keywords</replaceable> |
| 13697 |
and <replaceable>builtins</replaceable>, and also locates |
| 13698 |
system commands with identical names.</para> |
| 13699 |
|
| 13700 |
<para> |
| 13701 |
<screen> |
| 13702 |
<prompt>bash$ </prompt><userinput>type '['</userinput> |
| 13703 |
<computeroutput>[ is a shell builtin</computeroutput> |
| 13704 |
<prompt>bash$ </prompt><userinput>type -a '['</userinput> |
| 13705 |
<computeroutput>[ is a shell builtin |
| 13706 |
[ is /usr/bin/[</computeroutput> |
| 13707 |
|
| 13708 |
|
| 13709 |
<prompt>bash$ </prompt><userinput>type type</userinput> |
| 13710 |
<computeroutput>type is a shell builtin</computeroutput> |
| 13711 |
</screen> |
| 13712 |
</para> |
| 13713 |
|
| 13714 |
<para>The <command>type</command> command can be useful |
| 13715 |
for <link linkend="devnullredirect">testing whether a |
| 13716 |
certain command exists</link>.</para> |
| 13717 |
|
| 13718 |
</listitem> |
| 13719 |
</varlistentry> |
| 13720 |
|
| 13721 |
<varlistentry> |
| 13722 |
<term><anchor id="hashcmdref"><command>hash [cmds]</command></term> |
| 13723 |
<indexterm> |
| 13724 |
<primary>hash</primary> |
| 13725 |
</indexterm> |
| 13726 |
<indexterm> |
| 13727 |
<primary>command</primary> |
| 13728 |
<secondary>hash</secondary> |
| 13729 |
</indexterm> |
| 13730 |
<indexterm> |
| 13731 |
<primary>$PATH</primary> |
| 13732 |
</indexterm> |
| 13733 |
<indexterm> |
| 13734 |
<primary>variable</primary> |
| 13735 |
<secondary>$PATH</secondary> |
| 13736 |
</indexterm> |
| 13737 |
<listitem><para>Records the <firstterm>path</firstterm> |
| 13738 |
name of specified commands -- in the shell <firstterm>hash |
| 13739 |
table</firstterm> |
| 13740 |
|
| 13741 |
<footnote> |
| 13742 |
<para><anchor id="hashref"></para> |
| 13743 |
<para><firstterm>Hashing</firstterm> is a method of |
| 13744 |
creating lookup keys for data stored in a table. The |
| 13745 |
<emphasis>data items themselves</emphasis> are |
| 13746 |
<quote>scrambled</quote> to create keys, using one of |
| 13747 |
a number of simple mathematical |
| 13748 |
<firstterm>algorithms</firstterm> (methods, or |
| 13749 |
recipes).</para> |
| 13750 |
|
| 13751 |
<para>An advantage of <firstterm>hashing</firstterm> is that |
| 13752 |
it is fast. A disadvantage is that |
| 13753 |
<firstterm>collisions</firstterm> -- where a single key |
| 13754 |
maps to more than one data item -- are possible.</para> |
| 13755 |
|
| 13756 |
<para>For examples of hashing see <xref linkend="hashlib"> and |
| 13757 |
<xref linkend="hashexample">.</para> |
| 13758 |
</footnote> |
| 13759 |
|
| 13760 |
-- so the shell or script will not need to search the |
| 13761 |
<link linkend="pathref">$PATH</link> on subsequent calls to those |
| 13762 |
commands. When <command>hash</command> is called with no |
| 13763 |
arguments, it simply lists the commands that have been hashed. |
| 13764 |
The <option>-r</option> option resets the hash table.</para> |
| 13765 |
</listitem> |
| 13766 |
</varlistentry> |
| 13767 |
|
| 13768 |
|
| 13769 |
<varlistentry> |
| 13770 |
<term><anchor id="bindref"><command>bind</command></term> |
| 13771 |
<indexterm> |
| 13772 |
<primary>bind</primary> |
| 13773 |
</indexterm> |
| 13774 |
<indexterm> |
| 13775 |
<primary>bind</primary> |
| 13776 |
<secondary>key bindings </secondary> |
| 13777 |
</indexterm> |
| 13778 |
<listitem> |
| 13779 |
|
| 13780 |
<para>The <command>bind</command> builtin displays or modifies |
| 13781 |
<firstterm>readline</firstterm> |
| 13782 |
<footnote><para><anchor id="readlineref">The |
| 13783 |
<firstterm>readline</firstterm> library is what |
| 13784 |
Bash uses for reading input in an |
| 13785 |
interactive shell.</para></footnote> |
| 13786 |
key bindings.</para> |
| 13787 |
|
| 13788 |
</listitem> |
| 13789 |
</varlistentry> |
| 13790 |
|
| 13791 |
<varlistentry> |
| 13792 |
<term><anchor id="helpref"><command>help</command></term> |
| 13793 |
<indexterm> |
| 13794 |
<primary>help</primary> |
| 13795 |
</indexterm> |
| 13796 |
<indexterm> |
| 13797 |
<primary>command</primary> |
| 13798 |
<secondary></secondary> |
| 13799 |
</indexterm> |
| 13800 |
<listitem> |
| 13801 |
|
| 13802 |
<para>Gets a short usage summary of a shell builtin. This is |
| 13803 |
the counterpart to <link linkend="whatisref">whatis</link>, |
| 13804 |
but for builtins. The display of <firstterm>help</firstterm> |
| 13805 |
information got a much-needed update in the <link |
| 13806 |
linkend="bash4ref">version 4 release</link> of Bash.</para> |
| 13807 |
|
| 13808 |
<para> |
| 13809 |
<screen><prompt>bash$ </prompt><userinput>help exit</userinput> |
| 13810 |
<computeroutput>exit: exit [n] |
| 13811 |
Exit the shell with a status of N. If N is omitted, the exit status |
| 13812 |
is that of the last command executed.</computeroutput> |
| 13813 |
</screen> |
| 13814 |
</para> |
| 13815 |
</listitem> |
| 13816 |
</varlistentry> |
| 13817 |
|
| 13818 |
</variablelist> |
| 13819 |
|
| 13820 |
|
| 13821 |
<sect1> |
| 13822 |
<title>Job Control Commands</title> |
| 13823 |
|
| 13824 |
|
| 13825 |
<para>Certain of the following job control commands take a |
| 13826 |
<firstterm>job identifier</firstterm> as an argument. See |
| 13827 |
the <link linkend="jobidtable">table</link> at end of the |
| 13828 |
chapter.</para> |
| 13829 |
|
| 13830 |
<variablelist id="jccommandlist"> |
| 13831 |
|
| 13832 |
<varlistentry> |
| 13833 |
<term><anchor id="jobsref"><command>jobs</command></term> |
| 13834 |
<indexterm> |
| 13835 |
<primary>jobs</primary> |
| 13836 |
</indexterm> |
| 13837 |
<indexterm> |
| 13838 |
<primary>command</primary> |
| 13839 |
<secondary>jobs</secondary> |
| 13840 |
</indexterm> |
| 13841 |
<indexterm> |
| 13842 |
<primary>ps</primary> |
| 13843 |
</indexterm> |
| 13844 |
<indexterm> |
| 13845 |
<primary>command</primary> |
| 13846 |
<secondary>ps</secondary> |
| 13847 |
</indexterm> |
| 13848 |
<listitem> |
| 13849 |
|
| 13850 |
<para>Lists the jobs running in the background, giving |
| 13851 |
the <firstterm>job number</firstterm>. |
| 13852 |
Not as useful as <link linkend="ppssref">ps</link>.</para> |
| 13853 |
|
| 13854 |
<note> |
| 13855 |
<para>It is all too easy to confuse |
| 13856 |
<firstterm>jobs</firstterm> and |
| 13857 |
<firstterm>processes</firstterm>. Certain <link |
| 13858 |
linkend="builtinref">builtins</link>, such as |
| 13859 |
<command>kill</command>, <command>disown</command>, and |
| 13860 |
<command>wait</command> accept either a job number or a |
| 13861 |
process number as an argument. The <link linkend="fgref">fg</link>, |
| 13862 |
<link linkend="bgref">bg</link> and <command>jobs</command> |
| 13863 |
commands accept only a job number.</para> |
| 13864 |
|
| 13865 |
<para> |
| 13866 |
<screen><prompt>bash$ </prompt><userinput>sleep 100 &</userinput> |
| 13867 |
<computeroutput>[1] 1384</computeroutput> |
| 13868 |
|
| 13869 |
<prompt>bash $ </prompt><userinput>jobs</userinput> |
| 13870 |
<computeroutput>[1]+ Running sleep 100 &</computeroutput></screen> |
| 13871 |
</para> |
| 13872 |
|
| 13873 |
<para><quote>1</quote> is the job number (jobs are |
| 13874 |
maintained by the current shell). <quote>1384</quote> |
| 13875 |
is the <link linkend="ppidref">PID</link> or <firstterm>process ID |
| 13876 |
number</firstterm> (processes are maintained by the system). To kill |
| 13877 |
this job/process, either a <command>kill %1</command> |
| 13878 |
or a <command>kill 1384</command> works.</para> |
| 13879 |
|
| 13880 |
<para><emphasis>Thanks, S.C.</emphasis></para> |
| 13881 |
</note> |
| 13882 |
|
| 13883 |
</listitem> |
| 13884 |
</varlistentry> |
| 13885 |
|
| 13886 |
<varlistentry> |
| 13887 |
<term><anchor id="disownref"><command>disown</command></term> |
| 13888 |
<indexterm> |
| 13889 |
<primary>disown</primary> |
| 13890 |
</indexterm> |
| 13891 |
<indexterm> |
| 13892 |
<primary>command</primary> |
| 13893 |
<secondary>disown</secondary> |
| 13894 |
</indexterm> |
| 13895 |
<listitem> |
| 13896 |
<para>Remove job(s) from the shell's table of active jobs.</para> |
| 13897 |
</listitem> |
| 13898 |
</varlistentry> |
| 13899 |
|
| 13900 |
|
| 13901 |
<varlistentry> |
| 13902 |
<term><anchor id="fgref"><command>fg</command></term> |
| 13903 |
<term><anchor id="bgref"><command>bg</command></term> |
| 13904 |
<indexterm> |
| 13905 |
<primary>fg</primary> |
| 13906 |
</indexterm> |
| 13907 |
<indexterm> |
| 13908 |
<primary>command</primary> |
| 13909 |
<secondary>foreground</secondary> |
| 13910 |
</indexterm> |
| 13911 |
<indexterm> |
| 13912 |
<primary>background</primary> |
| 13913 |
</indexterm> |
| 13914 |
<indexterm> |
| 13915 |
<primary>command</primary> |
| 13916 |
<secondary>bg</secondary> |
| 13917 |
</indexterm> |
| 13918 |
<listitem> |
| 13919 |
<para>The <command>fg</command> command switches a job |
| 13920 |
running in the background into the foreground. The |
| 13921 |
<command>bg</command> command restarts a suspended job, and |
| 13922 |
runs it in the background. If no job number is specified, |
| 13923 |
then the <command>fg</command> or <command>bg</command> |
| 13924 |
command acts upon the currently running job.</para> |
| 13925 |
</listitem> |
| 13926 |
</varlistentry> |
| 13927 |
|
| 13928 |
<varlistentry> |
| 13929 |
<term><anchor id="waitref"><command>wait</command></term> |
| 13930 |
<indexterm> |
| 13931 |
<primary>wait</primary> |
| 13932 |
</indexterm> |
| 13933 |
<indexterm> |
| 13934 |
<primary>command</primary> |
| 13935 |
<secondary>wait</secondary> |
| 13936 |
</indexterm> |
| 13937 |
<listitem> |
| 13938 |
|
| 13939 |
<para>Suspend script execution until all jobs running in |
| 13940 |
background have terminated, or until the job number or |
| 13941 |
process ID specified as an option terminates. Returns the <link |
| 13942 |
linkend="exitstatusref">exit status</link> of waited-for |
| 13943 |
command.</para> |
| 13944 |
|
| 13945 |
<para>You may use the <command>wait</command> command |
| 13946 |
to prevent a script from exiting before a background |
| 13947 |
job finishes executing (this would create a dreaded |
| 13948 |
<link linkend="zombieref">orphan process</link>).</para> |
| 13949 |
|
| 13950 |
<example id="ex39"> |
| 13951 |
<title>Waiting for a process to finish before proceeding</title> |
| 13952 |
<programlisting>&ex39;</programlisting> |
| 13953 |
</example> |
| 13954 |
|
| 13955 |
<para>Optionally, <command>wait</command> can take a <firstterm>job |
| 13956 |
identifier</firstterm> as an argument, for example, |
| 13957 |
<replaceable>wait%1</replaceable> or <replaceable>wait |
| 13958 |
$PPID</replaceable>. |
| 13959 |
|
| 13960 |
<footnote><para>This only applies to <firstterm>child |
| 13961 |
processes</firstterm>, of course.</para></footnote> |
| 13962 |
|
| 13963 |
See the <link linkend="jobidtable">job id table</link>.</para> |
| 13964 |
|
| 13965 |
<para><anchor id="waithang"></para> |
| 13966 |
<tip> |
| 13967 |
<para>Within a script, running a command in the background |
| 13968 |
with an ampersand (&) may cause the script |
| 13969 |
to hang until <keycap>ENTER</keycap> is hit. This |
| 13970 |
seems to occur with commands that write to |
| 13971 |
<filename>stdout</filename>. It can be a major annoyance. |
| 13972 |
<programlisting>#!/bin/bash |
| 13973 |
# test.sh |
| 13974 |
|
| 13975 |
ls -l & |
| 13976 |
echo "Done."</programlisting> |
| 13977 |
<screen><prompt>bash$ </prompt><userinput>./test.sh</userinput> |
| 13978 |
<computeroutput>Done. |
| 13979 |
[bozo@localhost test-scripts]$ total 1 |
| 13980 |
-rwxr-xr-x 1 bozo bozo 34 Oct 11 15:09 test.sh |
| 13981 |
_ |
| 13982 |
</computeroutput> |
| 13983 |
</screen> |
| 13984 |
</para> |
| 13985 |
|
| 13986 |
<blockquote> |
| 13987 |
<literallayout> |
| 13988 |
As Walter Brameld IV explains it: |
| 13989 |
|
| 13990 |
As far as I can tell, such scripts don't actually hang. It just |
| 13991 |
seems that they do because the background command writes text to |
| 13992 |
the console after the prompt. The user gets the impression that |
| 13993 |
the prompt was never displayed. Here's the sequence of events: |
| 13994 |
|
| 13995 |
1. Script launches background command. |
| 13996 |
2. Script exits. |
| 13997 |
3. Shell displays the prompt. |
| 13998 |
4. Background command continues running and writing text to the |
| 13999 |
console. |
| 14000 |
5. Background command finishes. |
| 14001 |
6. User doesn't see a prompt at the bottom of the output, thinks script |
| 14002 |
is hanging. |
| 14003 |
</literallayout> |
| 14004 |
</blockquote> |
| 14005 |
|
| 14006 |
<para>Placing a <command>wait</command> after the background |
| 14007 |
command seems to remedy this. |
| 14008 |
<programlisting>#!/bin/bash |
| 14009 |
# test.sh |
| 14010 |
|
| 14011 |
ls -l & |
| 14012 |
echo "Done." |
| 14013 |
wait</programlisting> |
| 14014 |
<screen><prompt>bash$ </prompt><userinput>./test.sh</userinput> |
| 14015 |
<computeroutput>Done. |
| 14016 |
[bozo@localhost test-scripts]$ total 1 |
| 14017 |
-rwxr-xr-x 1 bozo bozo 34 Oct 11 15:09 test.sh</computeroutput> |
| 14018 |
</screen> |
| 14019 |
<link linkend="ioredirref">Redirecting</link> the |
| 14020 |
output of the command to a file or even to |
| 14021 |
<filename>/dev/null</filename> also takes care of this |
| 14022 |
problem. |
| 14023 |
</para> |
| 14024 |
</tip> |
| 14025 |
|
| 14026 |
|
| 14027 |
</listitem> |
| 14028 |
</varlistentry> |
| 14029 |
|
| 14030 |
|
| 14031 |
<varlistentry> |
| 14032 |
<term><anchor id="suspendref"><command>suspend</command></term> |
| 14033 |
<indexterm> |
| 14034 |
<primary>suspend</primary> |
| 14035 |
</indexterm> |
| 14036 |
<indexterm> |
| 14037 |
<primary>command</primary> |
| 14038 |
<secondary>suspend</secondary> |
| 14039 |
</indexterm> |
| 14040 |
<listitem> |
| 14041 |
<para>This has a similar effect to |
| 14042 |
<keycombo><keycap>Control</keycap><keycap>Z</keycap></keycombo>, |
| 14043 |
but it suspends the shell (the shell's parent process should |
| 14044 |
resume it at an appropriate time).</para> |
| 14045 |
</listitem> |
| 14046 |
</varlistentry> |
| 14047 |
|
| 14048 |
<varlistentry> |
| 14049 |
<term><anchor id="logoutref"><command>logout</command></term> |
| 14050 |
<indexterm> |
| 14051 |
<primary>logout</primary> |
| 14052 |
</indexterm> |
| 14053 |
<indexterm> |
| 14054 |
<primary>command</primary> |
| 14055 |
<secondary>log out</secondary> |
| 14056 |
</indexterm> |
| 14057 |
<listitem> |
| 14058 |
<para>Exit a login shell, optionally specifying an <link |
| 14059 |
linkend="exitstatusref">exit status</link>.</para> |
| 14060 |
</listitem> |
| 14061 |
</varlistentry> |
| 14062 |
|
| 14063 |
<varlistentry> |
| 14064 |
<term><anchor id="timesref"><command>times</command></term> |
| 14065 |
<indexterm> |
| 14066 |
<primary>times</primary> |
| 14067 |
</indexterm> |
| 14068 |
<indexterm> |
| 14069 |
<primary>command</primary> |
| 14070 |
<secondary>times</secondary> |
| 14071 |
</indexterm> |
| 14072 |
<listitem> |
| 14073 |
<para>Gives statistics on the system time elapsed when |
| 14074 |
executing commands, in the following form: |
| 14075 |
<screen><computeroutput>0m0.020s 0m0.020s</computeroutput></screen></para> |
| 14076 |
|
| 14077 |
<para>This capability is of relatively limited value, since it is not common to |
| 14078 |
profile and benchmark shell scripts.</para> |
| 14079 |
|
| 14080 |
</listitem> |
| 14081 |
</varlistentry> |
| 14082 |
|
| 14083 |
<varlistentry> |
| 14084 |
<term><anchor id="killref"><command>kill</command></term> |
| 14085 |
<indexterm> |
| 14086 |
<primary>kill</primary> |
| 14087 |
</indexterm> |
| 14088 |
<indexterm> |
| 14089 |
<primary>command</primary> |
| 14090 |
<secondary>kill</secondary> |
| 14091 |
</indexterm> |
| 14092 |
<listitem> |
| 14093 |
<para>Forcibly terminate a process by sending it an |
| 14094 |
appropriate <firstterm>terminate</firstterm> signal |
| 14095 |
(see <xref linkend="killprocess">).</para> |
| 14096 |
|
| 14097 |
<example id="selfdestruct"> |
| 14098 |
<title>A script that kills itself</title> |
| 14099 |
<programlisting>&selfdestruct;</programlisting> |
| 14100 |
</example> |
| 14101 |
|
| 14102 |
<para><anchor id="zombieref"></para> |
| 14103 |
<note><para><userinput>kill -l</userinput> lists all the |
| 14104 |
<link linkend="signald">signals</link> (as does the |
| 14105 |
file <filename>/usr/include/asm/signal.h</filename>). |
| 14106 |
A <userinput>kill -9</userinput> is a <firstterm>sure |
| 14107 |
kill</firstterm>, which will usually terminate a |
| 14108 |
process that stubbornly refuses to die with a plain |
| 14109 |
<command>kill</command>. Sometimes, a <userinput>kill |
| 14110 |
-15</userinput> works. A <firstterm>zombie</firstterm> process, |
| 14111 |
that is, a child process that has terminated, but that |
| 14112 |
the <link linkend="forkref">parent process</link> |
| 14113 |
has not (yet) killed, cannot be killed by a logged-on |
| 14114 |
user -- you can't kill something that is already dead -- |
| 14115 |
but <command>init</command> will generally clean it up |
| 14116 |
sooner or later.</para></note> |
| 14117 |
|
| 14118 |
</listitem> |
| 14119 |
</varlistentry> |
| 14120 |
|
| 14121 |
|
| 14122 |
<varlistentry> |
| 14123 |
<term><anchor id="killallref"><command>killall</command></term> |
| 14124 |
<indexterm> |
| 14125 |
<primary>killall</primary> |
| 14126 |
</indexterm> |
| 14127 |
<indexterm> |
| 14128 |
<primary>command</primary> |
| 14129 |
<secondary>kill</secondary> |
| 14130 |
</indexterm> |
| 14131 |
<listitem><para>The <command>killall</command> command |
| 14132 |
kills a running process by <firstterm>name</firstterm>, |
| 14133 |
rather than by <link linkend="processidref">process ID</link>. |
| 14134 |
If there are multiple instances of a particular command running, |
| 14135 |
then doing a <firstterm>killall</firstterm> on that command will |
| 14136 |
terminate them <emphasis>all</emphasis>.</para> |
| 14137 |
|
| 14138 |
<note><para>This refers to the <command>killall</command> |
| 14139 |
command in <filename class="directory">/usr/bin</filename>, |
| 14140 |
<emphasis>not</emphasis> the <link |
| 14141 |
linkend="killall2ref">killall script</link> in <filename |
| 14142 |
class="directory">/etc/rc.d/init.d</filename>.</para></note> |
| 14143 |
</listitem> |
| 14144 |
</varlistentry> |
| 14145 |
|
| 14146 |
<varlistentry> |
| 14147 |
<term><anchor id="commandref"><command>command</command></term> |
| 14148 |
<indexterm> |
| 14149 |
<primary>command</primary> |
| 14150 |
</indexterm> |
| 14151 |
<indexterm> |
| 14152 |
<primary>command</primary> |
| 14153 |
<secondary>command</secondary> |
| 14154 |
</indexterm> |
| 14155 |
<listitem><para>The <command>command</command> directive |
| 14156 |
disables aliases and functions for the command immediately |
| 14157 |
following it.</para> |
| 14158 |
<para> |
| 14159 |
<screen> |
| 14160 |
<prompt>bash$ </prompt><userinput>command ls</userinput> |
| 14161 |
</screen> |
| 14162 |
</para> |
| 14163 |
|
| 14164 |
<note><para>This is one of three shell directives that |
| 14165 |
effect script command processing. The others are |
| 14166 |
<link linkend="bltref">builtin</link> and <link |
| 14167 |
linkend="enableref">enable</link>.</para></note> |
| 14168 |
</listitem> |
| 14169 |
</varlistentry> |
| 14170 |
|
| 14171 |
<varlistentry> |
| 14172 |
<term><anchor id="bltref"><command>builtin</command></term> |
| 14173 |
<indexterm> |
| 14174 |
<primary>builtin</primary> |
| 14175 |
</indexterm> |
| 14176 |
<indexterm> |
| 14177 |
<primary>command</primary> |
| 14178 |
<secondary>builtin</secondary> |
| 14179 |
</indexterm> |
| 14180 |
<listitem><para>Invoking <command>builtin |
| 14181 |
BUILTIN_COMMAND</command> runs the command |
| 14182 |
<replaceable>BUILTIN_COMMAND</replaceable> as a shell <link |
| 14183 |
linkend="builtinref">builtin</link>, temporarily disabling |
| 14184 |
both functions and external system commands with the |
| 14185 |
same name.</para> |
| 14186 |
</listitem> |
| 14187 |
</varlistentry> |
| 14188 |
|
| 14189 |
<varlistentry> |
| 14190 |
<term><anchor id="enableref"><command>enable</command></term> |
| 14191 |
<indexterm> |
| 14192 |
<primary>enable</primary> |
| 14193 |
</indexterm> |
| 14194 |
<indexterm> |
| 14195 |
<primary>command</primary> |
| 14196 |
<secondary>enable</secondary> |
| 14197 |
</indexterm> |
| 14198 |
<listitem> |
| 14199 |
|
| 14200 |
<para>This either enables or disables a shell |
| 14201 |
builtin command. As an example, <replaceable>enable -n |
| 14202 |
kill</replaceable> disables the shell builtin <link |
| 14203 |
linkend="killref">kill</link>, so that when Bash |
| 14204 |
subsequently encounters <firstterm>kill</firstterm>, it invokes |
| 14205 |
the external command <filename>/bin/kill</filename>.</para> |
| 14206 |
|
| 14207 |
<para><anchor id="enableref1">The <option>-a</option> |
| 14208 |
option to <firstterm>enable</firstterm> lists all the |
| 14209 |
shell builtins, indicating whether or not they |
| 14210 |
are enabled. The <option>-f filename</option> |
| 14211 |
option lets <firstterm>enable</firstterm> load a <link |
| 14212 |
linkend="builtinref">builtin</link> as a shared library |
| 14213 |
(DLL) module from a properly compiled object file. |
| 14214 |
<footnote> |
| 14215 |
<para>The C source for a number of loadable builtins is |
| 14216 |
typically found in the <filename |
| 14217 |
class="directory">/usr/share/doc/bash-?.??/functions</filename> |
| 14218 |
directory.</para> |
| 14219 |
<para>Note that the <option>-f</option> option to |
| 14220 |
<command>enable</command> is not <link |
| 14221 |
linkend="portabilityissues">portable</link> to all |
| 14222 |
systems.</para> |
| 14223 |
</footnote>. |
| 14224 |
</para> |
| 14225 |
|
| 14226 |
</listitem> |
| 14227 |
</varlistentry> |
| 14228 |
|
| 14229 |
<varlistentry> |
| 14230 |
<term><anchor id="autoloadref"><command>autoload</command></term> |
| 14231 |
<indexterm> |
| 14232 |
<primary>autoload</primary> |
| 14233 |
</indexterm> |
| 14234 |
<indexterm> |
| 14235 |
<primary>command</primary> |
| 14236 |
<secondary>autoloader</secondary> |
| 14237 |
</indexterm> |
| 14238 |
<listitem> |
| 14239 |
|
| 14240 |
<para>This is a port to Bash of the |
| 14241 |
<firstterm>ksh</firstterm> autoloader. With |
| 14242 |
<command>autoload</command> in place, a function with |
| 14243 |
an <firstterm>autoload</firstterm> declaration will load from an |
| 14244 |
external file at its first invocation. |
| 14245 |
<footnote><para>The same effect as |
| 14246 |
<command>autoload</command> can be achieved with <link |
| 14247 |
linkend="declareref">typeset -fu</link>.</para></footnote> |
| 14248 |
This saves system resources.</para> |
| 14249 |
|
| 14250 |
<para>Note that <firstterm>autoload</firstterm> is not a part of the |
| 14251 |
core Bash installation. It needs to be loaded in with |
| 14252 |
<replaceable>enable -f</replaceable> (see above).</para> |
| 14253 |
|
| 14254 |
</listitem> |
| 14255 |
</varlistentry> |
| 14256 |
|
| 14257 |
</variablelist> |
| 14258 |
|
| 14259 |
<para><anchor id="jobidtable0"></para> |
| 14260 |
<table id="jobidtable"> |
| 14261 |
<title>Job identifiers</title> |
| 14262 |
<tgroup cols="2"> |
| 14263 |
<thead> |
| 14264 |
<row> |
| 14265 |
<entry>Notation</entry> |
| 14266 |
<entry>Meaning</entry> |
| 14267 |
</row> |
| 14268 |
</thead> |
| 14269 |
<tbody> |
| 14270 |
<row> |
| 14271 |
<entry><option>%N</option></entry> |
| 14272 |
<entry>Job number [N]</entry> |
| 14273 |
</row> |
| 14274 |
<row> |
| 14275 |
<entry><option>%S</option></entry> |
| 14276 |
<entry>Invocation (command-line) of job begins with string <emphasis>S</emphasis></entry> |
| 14277 |
</row> |
| 14278 |
<row> |
| 14279 |
<entry><option>%?S</option></entry> |
| 14280 |
<entry>Invocation (command-line) of job contains within it string <emphasis>S</emphasis></entry> |
| 14281 |
</row> |
| 14282 |
<row> |
| 14283 |
<entry><option>%%</option></entry> |
| 14284 |
<entry><quote>current</quote> job (last job stopped in |
| 14285 |
foreground or started in background)</entry> |
| 14286 |
</row> |
| 14287 |
<row> |
| 14288 |
<entry><option>%+</option></entry> |
| 14289 |
<entry><quote>current</quote> job (last job stopped in |
| 14290 |
foreground or started in background)</entry> |
| 14291 |
</row> |
| 14292 |
<row> |
| 14293 |
<entry><option>%-</option></entry> |
| 14294 |
<entry>Last job</entry> |
| 14295 |
</row> |
| 14296 |
<row> |
| 14297 |
<entry><option>$!</option></entry> |
| 14298 |
<entry>Last background process</entry> |
| 14299 |
</row> |
| 14300 |
</tbody> |
| 14301 |
</tgroup> |
| 14302 |
</table> |
| 14303 |
|
| 14304 |
</sect1> <!-- Job Control Commands --> |
| 14305 |
|
| 14306 |
</chapter> <!-- Internal Commands and Builtins --> |
| 14307 |
|
| 14308 |
|
| 14309 |
|
| 14310 |
<chapter id="external"> |
| 14311 |
<title>External Filters, Programs and Commands</title> |
| 14312 |
|
| 14313 |
<para><anchor id="externalref"></para> |
| 14314 |
|
| 14315 |
<para>Standard UNIX commands make shell scripts more versatile. The |
| 14316 |
power of scripts comes from coupling system commands and shell |
| 14317 |
directives with simple programming constructs.</para> |
| 14318 |
|
| 14319 |
<sect1 id="basic"> |
| 14320 |
<title>Basic Commands</title> |
| 14321 |
|
| 14322 |
<variablelist id="basiccommands"> |
| 14323 |
<title><anchor id="basiccommands1">The first commands a novice learns</title> |
| 14324 |
|
| 14325 |
<varlistentry> |
| 14326 |
<term><anchor id="lsref"><command>ls</command></term> |
| 14327 |
<indexterm> |
| 14328 |
<primary>ls</primary> |
| 14329 |
</indexterm> |
| 14330 |
<indexterm> |
| 14331 |
<primary>command</primary> |
| 14332 |
<secondary>ls</secondary> |
| 14333 |
</indexterm> |
| 14334 |
<listitem> |
| 14335 |
<para>The basic file <quote>list</quote> command. It is all too easy |
| 14336 |
to underestimate the power of this humble command. For |
| 14337 |
example, using the <option>-R</option>, recursive option, |
| 14338 |
<command>ls</command> provides a tree-like listing of |
| 14339 |
a directory structure. Other useful options are |
| 14340 |
<option>-S</option>, sort listing by file size, |
| 14341 |
<option>-t</option>, sort by file modification time, |
| 14342 |
<option>-v</option>, sort by (numerical) version numbers |
| 14343 |
embedded in the filenames, |
| 14344 |
<footnote><para>The <option>-v</option> option also orders the |
| 14345 |
sort by <emphasis>upper- and lowercase prefixed</emphasis> |
| 14346 |
filenames.</para></footnote> |
| 14347 |
<option>-b</option>, show escape characters, and |
| 14348 |
<option>-i</option>, show file inodes (see <xref |
| 14349 |
linkend="idelete">).</para> |
| 14350 |
|
| 14351 |
<para> |
| 14352 |
<screen><prompt>bash$ </prompt><userinput>ls -l</userinput> |
| 14353 |
<computeroutput>-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter10.txt |
| 14354 |
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter11.txt |
| 14355 |
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter12.txt |
| 14356 |
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter1.txt |
| 14357 |
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter2.txt |
| 14358 |
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter3.txt |
| 14359 |
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:49 Chapter_headings.txt |
| 14360 |
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:49 Preface.txt</computeroutput> |
| 14361 |
|
| 14362 |
|
| 14363 |
<prompt>bash$ </prompt><userinput>ls -lv</userinput> |
| 14364 |
<computeroutput> total 0 |
| 14365 |
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:49 Chapter_headings.txt |
| 14366 |
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:49 Preface.txt |
| 14367 |
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter1.txt |
| 14368 |
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter2.txt |
| 14369 |
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter3.txt |
| 14370 |
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter10.txt |
| 14371 |
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter11.txt |
| 14372 |
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter12.txt</computeroutput></screen> |
| 14373 |
</para> |
| 14374 |
|
| 14375 |
|
| 14376 |
<tip><para> |
| 14377 |
The <firstterm>ls</firstterm> command returns a |
| 14378 |
non-zero <link linkend="exitstatusref">exit status</link> when |
| 14379 |
attempting to list a non-existent file. |
| 14380 |
<screen><prompt>bash$ </prompt><userinput>ls abc</userinput> |
| 14381 |
<computeroutput>ls: abc: No such file or directory</computeroutput> |
| 14382 |
|
| 14383 |
|
| 14384 |
<prompt>bash$ </prompt><userinput>echo $?</userinput> |
| 14385 |
<computeroutput>2</computeroutput></screen> |
| 14386 |
</para></tip> |
| 14387 |
|
| 14388 |
<example id="ex40"> |
| 14389 |
<title>Using <firstterm>ls</firstterm> to create a table of contents |
| 14390 |
for burning a <abbrev>CDR</abbrev> disk</title> |
| 14391 |
<programlisting>&ex40;</programlisting> |
| 14392 |
</example> |
| 14393 |
|
| 14394 |
</listitem> |
| 14395 |
</varlistentry> |
| 14396 |
|
| 14397 |
<varlistentry> |
| 14398 |
<term><anchor id="catref"><command>cat</command></term> |
| 14399 |
<term><command>tac</command></term> |
| 14400 |
<indexterm> |
| 14401 |
<primary>cat</primary> |
| 14402 |
</indexterm> |
| 14403 |
<indexterm> |
| 14404 |
<primary>command</primary> |
| 14405 |
<secondary>cat</secondary> |
| 14406 |
</indexterm> |
| 14407 |
<indexterm> |
| 14408 |
<primary>tac</primary> |
| 14409 |
</indexterm> |
| 14410 |
<indexterm> |
| 14411 |
<primary>command</primary> |
| 14412 |
<secondary>tac</secondary> |
| 14413 |
</indexterm> |
| 14414 |
<listitem> |
| 14415 |
<para><command>cat</command>, an acronym for |
| 14416 |
<wordasword>concatenate</wordasword>, |
| 14417 |
lists a file to <filename>stdout</filename>. When |
| 14418 |
combined with redirection (<token>></token> or |
| 14419 |
<token>>></token>), it is commonly used to concatenate |
| 14420 |
files. |
| 14421 |
|
| 14422 |
<anchor id="catuses"> |
| 14423 |
<programlisting># Uses of 'cat' |
| 14424 |
cat filename # Lists the file. |
| 14425 |
|
| 14426 |
cat file.1 file.2 file.3 > file.123 # Combines three files into one.</programlisting> |
| 14427 |
|
| 14428 |
The <option>-n</option> option to <command>cat</command> |
| 14429 |
inserts consecutive numbers before all lines of the |
| 14430 |
target file(s). The <option>-b</option> option numbers |
| 14431 |
only the non-blank lines. The <option>-v</option> option |
| 14432 |
echoes nonprintable characters, using <token>^</token> |
| 14433 |
notation. The <option>-s</option> option squeezes multiple |
| 14434 |
consecutive blank lines into a single blank line.</para> |
| 14435 |
|
| 14436 |
<para>See also <xref linkend="lnum"> and <xref linkend="rot13">.</para> |
| 14437 |
|
| 14438 |
<note> |
| 14439 |
<para><anchor id="catlesseff"> |
| 14440 |
In a <link linkend="piperef">pipe</link>, it may be |
| 14441 |
more efficient to <link linkend="ioredirref">redirect</link> |
| 14442 |
the <filename>stdin</filename> to a file, rather than to |
| 14443 |
<command>cat</command> the file. |
| 14444 |
</para> |
| 14445 |
|
| 14446 |
<para> |
| 14447 |
<programlisting>cat filename | tr a-z A-Z |
| 14448 |
|
| 14449 |
tr a-z A-Z < filename # Same effect, but starts one less process, |
| 14450 |
#+ and also dispenses with the pipe.</programlisting> |
| 14451 |
</para> |
| 14452 |
</note> |
| 14453 |
|
| 14454 |
|
| 14455 |
<para><command>tac</command>, is the inverse of |
| 14456 |
<wordasword>cat</wordasword>, listing a file backwards from its end.</para> |
| 14457 |
|
| 14458 |
</listitem> |
| 14459 |
</varlistentry> |
| 14460 |
|
| 14461 |
|
| 14462 |
<varlistentry> |
| 14463 |
<term><anchor id="revref"><command>rev</command></term> |
| 14464 |
<indexterm> |
| 14465 |
<primary>rev</primary> |
| 14466 |
</indexterm> |
| 14467 |
<indexterm> |
| 14468 |
<primary>command</primary> |
| 14469 |
<secondary>rev</secondary> |
| 14470 |
</indexterm> |
| 14471 |
<listitem> |
| 14472 |
<para>reverses each line of a file, and outputs to |
| 14473 |
<filename>stdout</filename>. This does not have the same effect |
| 14474 |
as <command>tac</command>, as it preserves the order of |
| 14475 |
the lines, but flips each one around (mirror image).</para> |
| 14476 |
<para> |
| 14477 |
<screen><prompt>bash$ </prompt><userinput>cat file1.txt</userinput> |
| 14478 |
<computeroutput>This is line 1. |
| 14479 |
This is line 2.</computeroutput> |
| 14480 |
|
| 14481 |
|
| 14482 |
<prompt>bash$ </prompt><userinput>tac file1.txt</userinput> |
| 14483 |
<computeroutput>This is line 2. |
| 14484 |
This is line 1.</computeroutput> |
| 14485 |
|
| 14486 |
|
| 14487 |
<prompt>bash$ </prompt><userinput>rev file1.txt</userinput> |
| 14488 |
<computeroutput>.1 enil si sihT |
| 14489 |
.2 enil si sihT</computeroutput> |
| 14490 |
</screen> |
| 14491 |
</para> |
| 14492 |
|
| 14493 |
</listitem> |
| 14494 |
</varlistentry> |
| 14495 |
|
| 14496 |
<varlistentry> |
| 14497 |
<term><anchor id="cpref"><command>cp</command></term> |
| 14498 |
<indexterm> |
| 14499 |
<primary>cp</primary> |
| 14500 |
</indexterm> |
| 14501 |
<indexterm> |
| 14502 |
<primary>command</primary> |
| 14503 |
<secondary>cp</secondary> |
| 14504 |
</indexterm> |
| 14505 |
<listitem> |
| 14506 |
|
| 14507 |
<para>This is the file copy command. <userinput>cp file1 |
| 14508 |
file2</userinput> copies <filename>file1</filename> |
| 14509 |
to <filename>file2</filename>, overwriting |
| 14510 |
<filename>file2</filename> if it already exists (see <xref |
| 14511 |
linkend="ex42">).</para> |
| 14512 |
|
| 14513 |
<tip> |
| 14514 |
<para>Particularly useful are the <option>-a</option> |
| 14515 |
archive flag (for copying an entire directory tree), |
| 14516 |
the <option>-u</option> update flag (which prevents |
| 14517 |
overwriting identically-named newer files), and the |
| 14518 |
<option>-r</option> and <option>-R</option> recursive |
| 14519 |
flags.</para> |
| 14520 |
<para><programlisting>cp -u source_dir/* dest_dir |
| 14521 |
# "Synchronize" dest_dir to source_dir |
| 14522 |
#+ by copying over all newer and not previously existing files.</programlisting></para> |
| 14523 |
</tip> |
| 14524 |
|
| 14525 |
</listitem> |
| 14526 |
</varlistentry> |
| 14527 |
|
| 14528 |
<varlistentry> |
| 14529 |
<term><anchor id="mvref"><command>mv</command></term> |
| 14530 |
<listitem> |
| 14531 |
<para>This is the file <firstterm>move</firstterm> command. |
| 14532 |
It is equivalent to a combination of <command>cp</command> |
| 14533 |
and <command>rm</command>. It may be used to move multiple |
| 14534 |
files to a directory, or even to rename a directory. For |
| 14535 |
some examples of using <command>mv</command> in a script, |
| 14536 |
see <xref linkend="rfe"> and <xref linkend="rn">.</para> |
| 14537 |
|
| 14538 |
<note> |
| 14539 |
<para>When used in a non-interactive script, |
| 14540 |
<command>mv</command> takes the <option>-f</option> |
| 14541 |
(<firstterm>force</firstterm>) option to bypass user |
| 14542 |
input.</para> |
| 14543 |
|
| 14544 |
<para>When a directory is moved to a preexisting directory, |
| 14545 |
it becomes a subdirectory of the destination directory.</para> |
| 14546 |
<para> |
| 14547 |
<screen> |
| 14548 |
<prompt>bash$ </prompt><userinput>mv source_directory target_directory</userinput> |
| 14549 |
|
| 14550 |
<prompt>bash$ </prompt><userinput>ls -lF target_directory</userinput> |
| 14551 |
<computeroutput>total 1 |
| 14552 |
drwxrwxr-x 2 bozo bozo 1024 May 28 19:20 source_directory/</computeroutput> |
| 14553 |
</screen> |
| 14554 |
</para> |
| 14555 |
</note> |
| 14556 |
|
| 14557 |
</listitem> |
| 14558 |
</varlistentry> |
| 14559 |
|
| 14560 |
<varlistentry> |
| 14561 |
<term><anchor id="rmref"><command>rm</command></term> |
| 14562 |
<indexterm> |
| 14563 |
<primary>rm</primary> |
| 14564 |
</indexterm> |
| 14565 |
<indexterm> |
| 14566 |
<primary>command</primary> |
| 14567 |
<secondary>rm</secondary> |
| 14568 |
</indexterm> |
| 14569 |
<listitem> |
| 14570 |
<para>Delete (remove) a file or files. The <option>-f</option> |
| 14571 |
option forces removal of even readonly files, and is useful |
| 14572 |
for bypassing user input in a script.</para> |
| 14573 |
|
| 14574 |
<note> |
| 14575 |
<para><anchor id="dashrem"></para> |
| 14576 |
<para>The <firstterm>rm</firstterm> command will, by |
| 14577 |
itself, fail to remove filenames beginning with |
| 14578 |
a dash. Why? Because <firstterm>rm</firstterm> |
| 14579 |
sees a dash-prefixed filename as an |
| 14580 |
<firstterm>option</firstterm>.</para> |
| 14581 |
|
| 14582 |
<para> |
| 14583 |
<screen><prompt>bash$ </prompt><userinput>rm -badname</userinput> |
| 14584 |
<computeroutput>rm: invalid option -- b |
| 14585 |
Try `rm --help' for more information.</computeroutput></screen> |
| 14586 |
</para> |
| 14587 |
|
| 14588 |
|
| 14589 |
<para> |
| 14590 |
One clever workaround is to precede |
| 14591 |
the filename with a <quote> -- </quote> (the |
| 14592 |
<firstterm>end-of-options</firstterm> flag). |
| 14593 |
|
| 14594 |
<screen><prompt>bash$ </prompt><userinput>rm -- -badname</userinput></screen> |
| 14595 |
</para> |
| 14596 |
|
| 14597 |
<para> |
| 14598 |
Another method to is to preface the filename to be removed |
| 14599 |
with a <filename>dot-slash</filename> . |
| 14600 |
|
| 14601 |
<screen><prompt>bash$ </prompt><userinput>rm ./-badname</userinput></screen> |
| 14602 |
</para> |
| 14603 |
</note> |
| 14604 |
|
| 14605 |
|
| 14606 |
<warning><para><anchor id="rmrecurs">When used with the |
| 14607 |
recursive flag <option>-r</option>, this command removes |
| 14608 |
files all the way down the directory tree from the current |
| 14609 |
directory. A careless <command>rm -rf *</command> can wipe |
| 14610 |
out a big chunk of a directory structure.</para></warning> |
| 14611 |
</listitem> |
| 14612 |
</varlistentry> |
| 14613 |
|
| 14614 |
<varlistentry> |
| 14615 |
<term><anchor id="rmdirref"><command>rmdir</command></term> |
| 14616 |
<indexterm> |
| 14617 |
<primary>rmdir</primary> |
| 14618 |
</indexterm> |
| 14619 |
<indexterm> |
| 14620 |
<primary>command</primary> |
| 14621 |
<secondary>rmdir</secondary> |
| 14622 |
</indexterm> |
| 14623 |
<listitem> |
| 14624 |
<para>Remove directory. The directory must be empty of |
| 14625 |
all files -- including <quote>invisible</quote> |
| 14626 |
<firstterm>dotfiles</firstterm> |
| 14627 |
|
| 14628 |
<footnote> |
| 14629 |
<para><anchor id="dotfilesref"></para> |
| 14630 |
<para><firstterm>Dotfiles</firstterm> are files whose |
| 14631 |
names begin with a <firstterm>dot</firstterm>, such as |
| 14632 |
<filename>~/.Xdefaults</filename>. Such filenames do |
| 14633 |
not appear in a normal <command>ls</command> listing |
| 14634 |
(although an <command>ls -a</command> will show |
| 14635 |
them), and they cannot be deleted by an accidental |
| 14636 |
<command>rm -rf *</command>. Dotfiles are generally |
| 14637 |
used as setup and configuration files in a user's |
| 14638 |
home directory.</para> |
| 14639 |
</footnote> |
| 14640 |
|
| 14641 |
-- for this command to succeed.</para> |
| 14642 |
</listitem> |
| 14643 |
</varlistentry> |
| 14644 |
|
| 14645 |
<varlistentry> |
| 14646 |
<term><anchor id="mkdirref"><command>mkdir</command></term> |
| 14647 |
<indexterm> |
| 14648 |
<primary>mkdir</primary> |
| 14649 |
</indexterm> |
| 14650 |
<indexterm> |
| 14651 |
<primary>command</primary> |
| 14652 |
<secondary>mkdir</secondary> |
| 14653 |
</indexterm> |
| 14654 |
<listitem> |
| 14655 |
<para>Make directory, creates a new directory. For example, |
| 14656 |
<userinput>mkdir -p project/programs/December</userinput> |
| 14657 |
creates the named directory. The |
| 14658 |
<replaceable>-p</replaceable> option automatically creates |
| 14659 |
any necessary parent directories.</para> |
| 14660 |
|
| 14661 |
</listitem> |
| 14662 |
</varlistentry> |
| 14663 |
|
| 14664 |
<varlistentry> |
| 14665 |
<term><anchor id="chmodref"><command>chmod</command></term> |
| 14666 |
<indexterm> |
| 14667 |
<primary>chmod</primary> |
| 14668 |
</indexterm> |
| 14669 |
<indexterm> |
| 14670 |
<primary>command</primary> |
| 14671 |
<secondary>chmod</secondary> |
| 14672 |
</indexterm> |
| 14673 |
<listitem> |
| 14674 |
|
| 14675 |
<para>Changes the attributes of an existing file or directory |
| 14676 |
(see <xref linkend="ex44">).</para> |
| 14677 |
|
| 14678 |
<para><programlisting>chmod +x filename |
| 14679 |
# Makes "filename" executable for all users. |
| 14680 |
|
| 14681 |
chmod u+s filename |
| 14682 |
# Sets "suid" bit on "filename" permissions. |
| 14683 |
# An ordinary user may execute "filename" with same privileges as the file's owner. |
| 14684 |
# (This does not apply to shell scripts.)</programlisting></para> |
| 14685 |
|
| 14686 |
<para><programlisting>chmod 644 filename |
| 14687 |
# Makes "filename" readable/writable to owner, readable to others |
| 14688 |
#+ (octal mode). |
| 14689 |
|
| 14690 |
chmod 444 filename |
| 14691 |
# Makes "filename" read-only for all. |
| 14692 |
# Modifying the file (for example, with a text editor) |
| 14693 |
#+ not allowed for a user who does not own the file (except for root), |
| 14694 |
#+ and even the file owner must force a file-save |
| 14695 |
#+ if she modifies the file. |
| 14696 |
# Same restrictions apply for deleting the file.</programlisting></para> |
| 14697 |
|
| 14698 |
<para><programlisting>chmod 1777 directory-name |
| 14699 |
# Gives everyone read, write, and execute permission in directory, |
| 14700 |
#+ however also sets the "sticky bit". |
| 14701 |
# This means that only the owner of the directory, |
| 14702 |
#+ owner of the file, and, of course, root |
| 14703 |
#+ can delete any particular file in that directory. |
| 14704 |
|
| 14705 |
chmod 111 directory-name |
| 14706 |
# Gives everyone execute-only permission in a directory. |
| 14707 |
# This means that you can execute and READ the files in that directory |
| 14708 |
#+ (execute permission necessarily includes read permission |
| 14709 |
#+ because you can't execute a file without being able to read it). |
| 14710 |
# But you can't list the files or search for them with the "find" command. |
| 14711 |
# These restrictions do not apply to root. |
| 14712 |
|
| 14713 |
chmod 000 directory-name |
| 14714 |
# No permissions at all for that directory. |
| 14715 |
# Can't read, write, or execute files in it. |
| 14716 |
# Can't even list files in it or "cd" to it. |
| 14717 |
# But, you can rename (mv) the directory |
| 14718 |
#+ or delete it (rmdir) if it is empty. |
| 14719 |
# You can even symlink to files in the directory, |
| 14720 |
#+ but you can't read, write, or execute the symlinks. |
| 14721 |
# These restrictions do not apply to root.</programlisting></para> |
| 14722 |
|
| 14723 |
</listitem> |
| 14724 |
</varlistentry> |
| 14725 |
|
| 14726 |
<varlistentry> |
| 14727 |
<term><anchor id="chattrref"><command>chattr</command></term> |
| 14728 |
<indexterm> |
| 14729 |
<primary>chattr</primary> |
| 14730 |
</indexterm> |
| 14731 |
<indexterm> |
| 14732 |
<primary>command</primary> |
| 14733 |
<secondary>chattr</secondary> |
| 14734 |
</indexterm> |
| 14735 |
<listitem> |
| 14736 |
|
| 14737 |
<para><command>Ch</command>ange file |
| 14738 |
<command>attr</command>ibutes. This is analogous to |
| 14739 |
<command>chmod</command> above, but with different options |
| 14740 |
and a different invocation syntax, and it works only on |
| 14741 |
<firstterm>ext2/ext3</firstterm> filesystems.</para> |
| 14742 |
|
| 14743 |
<para>One particularly interesting <command>chattr</command> |
| 14744 |
option is <option>i</option>. A <command>chattr +i |
| 14745 |
<filename>filename</filename></command> marks the file |
| 14746 |
as immutable. The file cannot be modified, linked to, or |
| 14747 |
deleted, <emphasis>not even by root</emphasis>. This |
| 14748 |
file attribute can be set or removed only by |
| 14749 |
<firstterm>root</firstterm>. In a similar fashion, |
| 14750 |
the <option>a</option> option marks the file as append |
| 14751 |
only.</para> |
| 14752 |
|
| 14753 |
<para> |
| 14754 |
<screen> |
| 14755 |
<prompt>root# </prompt><userinput>chattr +i file1.txt</userinput> |
| 14756 |
|
| 14757 |
|
| 14758 |
<prompt>root# </prompt><userinput>rm file1.txt</userinput> |
| 14759 |
|
| 14760 |
<computeroutput>rm: remove write-protected regular file `file1.txt'? y |
| 14761 |
rm: cannot remove `file1.txt': Operation not permitted</computeroutput> |
| 14762 |
</screen> |
| 14763 |
</para> |
| 14764 |
|
| 14765 |
<para>If a file has the <option>s</option> (secure) |
| 14766 |
attribute set, then when it is deleted its block is |
| 14767 |
overwritten with binary zeroes. |
| 14768 |
<footnote><para>This particular feature may not yet be |
| 14769 |
implemented in the version of the ext2/ext3 filesystem |
| 14770 |
installed on your system. Check the documentation for |
| 14771 |
your Linux distro.</para></footnote> |
| 14772 |
</para> |
| 14773 |
|
| 14774 |
<para>If a file has the <option>u</option> (undelete) |
| 14775 |
attribute set, then when it is deleted, its contents can still |
| 14776 |
be retrieved (undeleted).</para> |
| 14777 |
|
| 14778 |
<para>If a file has the <option>c</option> (compress) |
| 14779 |
attribute set, then it will automatically be compressed |
| 14780 |
on writes to disk, and uncompressed on reads.</para> |
| 14781 |
|
| 14782 |
<note><para>The file attributes set with |
| 14783 |
<command>chattr</command> do not show in a file listing |
| 14784 |
(<command>ls -l</command>).</para></note> |
| 14785 |
|
| 14786 |
</listitem> |
| 14787 |
</varlistentry> |
| 14788 |
|
| 14789 |
<varlistentry> |
| 14790 |
<term><anchor id="linkref"><command>ln</command></term> |
| 14791 |
<listitem> |
| 14792 |
|
| 14793 |
<para>Creates links to pre-existings files. A <quote>link</quote> |
| 14794 |
is a reference to a file, an alternate name for it. |
| 14795 |
The <command>ln</command> command permits referencing |
| 14796 |
the linked file by more than one name and is a superior |
| 14797 |
alternative to aliasing (see <xref linkend="ex18">).</para> |
| 14798 |
|
| 14799 |
<para>The <command>ln</command> creates only a reference, a |
| 14800 |
pointer to the file only a few bytes in size.</para> |
| 14801 |
|
| 14802 |
<para><anchor id="symlinkref"></para> |
| 14803 |
|
| 14804 |
<para>The <command>ln</command> command is most often used |
| 14805 |
with the <option>-s</option>, symbolic or |
| 14806 |
<quote>soft</quote> link flag. Advantages of using the |
| 14807 |
<option>-s</option> flag are that it permits linking across |
| 14808 |
file systems or to directories.</para> |
| 14809 |
|
| 14810 |
<para>The syntax of the command is a bit tricky. For example: |
| 14811 |
<userinput>ln -s oldfile newfile</userinput> links the |
| 14812 |
previously existing <filename>oldfile</filename> to the |
| 14813 |
newly created link, <filename>newfile</filename>.</para> |
| 14814 |
|
| 14815 |
<caution><para>If a file named <filename>newfile</filename> has |
| 14816 |
previously existed, an error message will |
| 14817 |
result.</para></caution> |
| 14818 |
|
| 14819 |
|
| 14820 |
<sidebar><title>Which type of link to use?</title> |
| 14821 |
|
| 14822 |
<para>As John Macdonald explains it:</para> |
| 14823 |
|
| 14824 |
<para>Both of these [types of links] provide a certain measure of dual reference |
| 14825 |
-- if you edit the contents of the file using any name, |
| 14826 |
your changes will affect both the original name and either |
| 14827 |
a hard or soft new name. The differences between them |
| 14828 |
occurs when you work at a higher level. The advantage of |
| 14829 |
a hard link is that the new name is totally independent |
| 14830 |
of the old name -- if you remove or rename the old name, |
| 14831 |
that does not affect the hard link, which continues |
| 14832 |
to point to the data while it would leave a soft link |
| 14833 |
hanging pointing to the old name which is no longer |
| 14834 |
there. The advantage of a soft link is that it can refer |
| 14835 |
to a different file system (since it is just a reference |
| 14836 |
to a file name, not to actual data). And, unlike a hard |
| 14837 |
link, a symbolic link can refer to a directory.</para> |
| 14838 |
|
| 14839 |
</sidebar> |
| 14840 |
|
| 14841 |
<para><anchor id="linkminvok"></para> |
| 14842 |
<para>Links give the ability to invoke a script (or any other type |
| 14843 |
of executable) with multiple names, and having that script |
| 14844 |
behave according to how it was invoked.</para> |
| 14845 |
|
| 14846 |
<example id="hellol"> |
| 14847 |
<title>Hello or Good-bye</title> |
| 14848 |
<programlisting>&hellol;</programlisting> |
| 14849 |
</example> |
| 14850 |
|
| 14851 |
|
| 14852 |
</listitem> |
| 14853 |
</varlistentry> |
| 14854 |
|
| 14855 |
<varlistentry> |
| 14856 |
<term><anchor id="manref"><command>man</command></term> |
| 14857 |
<term><anchor id="inforef"><command>info</command></term> |
| 14858 |
<indexterm> |
| 14859 |
<primary>man</primary> |
| 14860 |
</indexterm> |
| 14861 |
<indexterm> |
| 14862 |
<primary>command</primary> |
| 14863 |
<secondary>man</secondary> |
| 14864 |
</indexterm> |
| 14865 |
<indexterm> |
| 14866 |
<primary>info</primary> |
| 14867 |
</indexterm> |
| 14868 |
<indexterm> |
| 14869 |
<primary>command</primary> |
| 14870 |
<secondary>info</secondary> |
| 14871 |
</indexterm> |
| 14872 |
<listitem> |
| 14873 |
|
| 14874 |
<para>These commands access the manual and information pages on |
| 14875 |
system commands and installed utilities. When available, the |
| 14876 |
<firstterm>info</firstterm> pages usually contain more detailed |
| 14877 |
descriptions than do the <firstterm>man</firstterm> pages.</para> |
| 14878 |
|
| 14879 |
<para>There have been various attempts at |
| 14880 |
<quote>automating</quote> the writing of <firstterm>man |
| 14881 |
pages</firstterm>. For a script that makes a tentative first |
| 14882 |
step in that direction, see <xref linkend="maned">.</para> |
| 14883 |
|
| 14884 |
</listitem> |
| 14885 |
</varlistentry> |
| 14886 |
|
| 14887 |
|
| 14888 |
</variablelist> |
| 14889 |
|
| 14890 |
|
| 14891 |
</sect1> <!-- End Basic Commands --> |
| 14892 |
|
| 14893 |
|
| 14894 |
<sect1 id="moreadv"> |
| 14895 |
<title>Complex Commands</title> |
| 14896 |
|
| 14897 |
<variablelist id="cclisting"> |
| 14898 |
<title><anchor id="cclisting1">Commands for more advanced users</title> |
| 14899 |
|
| 14900 |
<varlistentry> |
| 14901 |
<term><anchor id="findref"><command>find</command></term> |
| 14902 |
<indexterm> |
| 14903 |
<primary>find</primary> |
| 14904 |
</indexterm> |
| 14905 |
<indexterm> |
| 14906 |
<primary>command</primary> |
| 14907 |
<secondary>find</secondary> |
| 14908 |
</indexterm> |
| 14909 |
|
| 14910 |
<indexterm> |
| 14911 |
<primary>{}</primary> |
| 14912 |
</indexterm> |
| 14913 |
<indexterm> |
| 14914 |
<primary>special character</primary> |
| 14915 |
<secondary>{}</secondary> |
| 14916 |
</indexterm> |
| 14917 |
|
| 14918 |
<indexterm> |
| 14919 |
<primary>\;</primary> |
| 14920 |
</indexterm> |
| 14921 |
<indexterm> |
| 14922 |
<primary>escaped character</primary> |
| 14923 |
<secondary>\;</secondary> |
| 14924 |
</indexterm> |
| 14925 |
|
| 14926 |
<listitem> |
| 14927 |
<para><anchor id="findref0"></para> |
| 14928 |
<para>-exec <replaceable>COMMAND</replaceable> \;</para> |
| 14929 |
<para>Carries out <replaceable>COMMAND</replaceable> on |
| 14930 |
each file that <command>find</command> matches. The |
| 14931 |
command sequence terminates with <token>;</token> (the |
| 14932 |
<quote>;</quote> is <link linkend="escp">escaped</link> to |
| 14933 |
make certain the shell passes it to <command>find</command> |
| 14934 |
literally, without interpreting it as a special character).</para> |
| 14935 |
|
| 14936 |
|
| 14937 |
<para> |
| 14938 |
<screen><prompt>bash$ </prompt><userinput>find ~/ -name '*.txt'</userinput> |
| 14939 |
<computeroutput>/home/bozo/.kde/share/apps/karm/karmdata.txt |
| 14940 |
/home/bozo/misc/irmeyc.txt |
| 14941 |
/home/bozo/test-scripts/1.txt</computeroutput> |
| 14942 |
</screen> |
| 14943 |
</para> |
| 14944 |
|
| 14945 |
<para><anchor id="curlybracketsref"></para> |
| 14946 |
|
| 14947 |
<para>If <replaceable>COMMAND</replaceable> contains |
| 14948 |
<token>{}</token>, then <command>find</command> |
| 14949 |
substitutes the full path name of the selected file for |
| 14950 |
<quote>{}</quote>.</para> |
| 14951 |
|
| 14952 |
<para> |
| 14953 |
<programlisting>find ~/ -name 'core*' -exec rm {} \; |
| 14954 |
# Removes all core dump files from user's home directory.</programlisting> |
| 14955 |
</para> |
| 14956 |
|
| 14957 |
|
| 14958 |
<para> |
| 14959 |
<programlisting>find /home/bozo/projects -mtime -1 |
| 14960 |
# ^ Note minus sign! |
| 14961 |
# Lists all files in /home/bozo/projects directory tree |
| 14962 |
#+ that were modified within the last day (current_day - 1). |
| 14963 |
# |
| 14964 |
find /home/bozo/projects -mtime 1 |
| 14965 |
# Same as above, but modified *exactly* one day ago. |
| 14966 |
# |
| 14967 |
# mtime = last modification time of the target file |
| 14968 |
# ctime = last status change time (via 'chmod' or otherwise) |
| 14969 |
# atime = last access time |
| 14970 |
|
| 14971 |
DIR=/home/bozo/junk_files |
| 14972 |
find "$DIR" -type f -atime +5 -exec rm {} \; |
| 14973 |
# ^ ^^ |
| 14974 |
# Curly brackets are placeholder for the path name output by "find." |
| 14975 |
# |
| 14976 |
# Deletes all files in "/home/bozo/junk_files" |
| 14977 |
#+ that have not been accessed in *at least* 5 days (plus sign ... +5). |
| 14978 |
# |
| 14979 |
# "-type filetype", where |
| 14980 |
# f = regular file |
| 14981 |
# d = directory |
| 14982 |
# l = symbolic link, etc. |
| 14983 |
# |
| 14984 |
# (The 'find' manpage and info page have complete option listings.)</programlisting> |
| 14985 |
</para> |
| 14986 |
|
| 14987 |
|
| 14988 |
|
| 14989 |
<para><programlisting>find /etc -exec grep '[0-9][0-9]*[.][0-9][0-9]*[.][0-9][0-9]*[.][0-9][0-9]*' {} \; |
| 14990 |
|
| 14991 |
# Finds all IP addresses (xxx.xxx.xxx.xxx) in /etc directory files. |
| 14992 |
# There a few extraneous hits. Can they be filtered out? |
| 14993 |
|
| 14994 |
# Possibly by: |
| 14995 |
|
| 14996 |
find /etc -type f -exec cat '{}' \; | tr -c '.[:digit:]' '\n' \ |
| 14997 |
| grep '^[^.][^.]*\.[^.][^.]*\.[^.][^.]*\.[^.][^.]*$' |
| 14998 |
# |
| 14999 |
# [:digit:] is one of the character classes |
| 15000 |
#+ introduced with the POSIX 1003.2 standard. |
| 15001 |
|
| 15002 |
# Thanks, Stéphane Chazelas. |
| 15003 |
</programlisting></para> |
| 15004 |
|
| 15005 |
|
| 15006 |
<note><para>The <option>-exec</option> option to |
| 15007 |
<command>find</command> should not be confused with the <link |
| 15008 |
linkend="execref">exec</link> shell builtin.</para></note> |
| 15009 |
|
| 15010 |
<example id="ex57"> |
| 15011 |
<title><firstterm>Badname</firstterm>, eliminate file names |
| 15012 |
in current directory containing bad characters and <link |
| 15013 |
linkend="whitespaceref">whitespace</link>.</title> |
| 15014 |
<programlisting>&ex57;</programlisting> |
| 15015 |
</example> |
| 15016 |
|
| 15017 |
<example id="idelete"> |
| 15018 |
<title>Deleting a file by its <firstterm>inode</firstterm> |
| 15019 |
number</title> |
| 15020 |
<programlisting>&idelete;</programlisting> |
| 15021 |
</example> |
| 15022 |
|
| 15023 |
<para>The <command>find</command> command also works |
| 15024 |
without the <option>-exec</option> option.</para> |
| 15025 |
|
| 15026 |
<para> |
| 15027 |
<programlisting>#!/bin/bash |
| 15028 |
# Find suid root files. |
| 15029 |
# A strange suid file might indicate a security hole, |
| 15030 |
#+ or even a system intrusion. |
| 15031 |
|
| 15032 |
directory="/usr/sbin" |
| 15033 |
# Might also try /sbin, /bin, /usr/bin, /usr/local/bin, etc. |
| 15034 |
permissions="+4000" # suid root (dangerous!) |
| 15035 |
|
| 15036 |
|
| 15037 |
for file in $( find "$directory" -perm "$permissions" ) |
| 15038 |
do |
| 15039 |
ls -ltF --author "$file" |
| 15040 |
done</programlisting> |
| 15041 |
</para> |
| 15042 |
|
| 15043 |
<para>See <xref linkend="ex48">, <xref linkend="ex58">, |
| 15044 |
and <xref linkend="findstring"> for scripts using |
| 15045 |
<command>find</command>. Its <link |
| 15046 |
linkend="manref">manpage</link> provides more detail |
| 15047 |
on this complex and powerful command.</para> |
| 15048 |
|
| 15049 |
|
| 15050 |
</listitem> |
| 15051 |
</varlistentry> |
| 15052 |
|
| 15053 |
<varlistentry> |
| 15054 |
<term><anchor id="xargsref"><command>xargs</command></term> |
| 15055 |
<indexterm> |
| 15056 |
<primary>xargs</primary> |
| 15057 |
</indexterm> |
| 15058 |
<indexterm> |
| 15059 |
<primary>command</primary> |
| 15060 |
<secondary>xargs</secondary> |
| 15061 |
</indexterm> |
| 15062 |
<listitem> |
| 15063 |
<para>A filter for feeding arguments to a command, and also |
| 15064 |
a tool for assembling the commands themselves. It breaks |
| 15065 |
a data stream into small enough chunks for filters and |
| 15066 |
commands to process. Consider it as a powerful replacement |
| 15067 |
for <link linkend="backquotesref">backquotes</link>. |
| 15068 |
In situations where <link linkend="commandsubref">command |
| 15069 |
substitution</link> fails with a <errorname>too |
| 15070 |
many arguments</errorname> error, |
| 15071 |
substituting <command>xargs</command> often |
| 15072 |
works. |
| 15073 |
<footnote><para>And even when <firstterm>xargs</firstterm> is |
| 15074 |
not strictly necessary, it can speed up execution of a command |
| 15075 |
involving <link |
| 15076 |
linkend="batchprocref">batch-processing</link> of multiple |
| 15077 |
files.</para></footnote> |
| 15078 |
Normally, <command>xargs</command> reads from |
| 15079 |
<filename>stdin</filename> or from a pipe, but it can also |
| 15080 |
be given the output of a file.</para> |
| 15081 |
|
| 15082 |
<para>The default command for <command>xargs</command> is |
| 15083 |
<link linkend="echoref">echo</link>. This means that input |
| 15084 |
piped to <command>xargs</command> may have linefeeds and |
| 15085 |
other whitespace characters stripped out.</para> |
| 15086 |
|
| 15087 |
<para> |
| 15088 |
<screen> |
| 15089 |
<prompt>bash$ </prompt><userinput>ls -l</userinput> |
| 15090 |
<computeroutput>total 0 |
| 15091 |
-rw-rw-r-- 1 bozo bozo 0 Jan 29 23:58 file1 |
| 15092 |
-rw-rw-r-- 1 bozo bozo 0 Jan 29 23:58 file2</computeroutput> |
| 15093 |
|
| 15094 |
|
| 15095 |
|
| 15096 |
<prompt>bash$ </prompt><userinput>ls -l | xargs</userinput> |
| 15097 |
<computeroutput>total 0 -rw-rw-r-- 1 bozo bozo 0 Jan 29 23:58 file1 -rw-rw-r-- 1 bozo bozo 0 Jan...</computeroutput> |
| 15098 |
|
| 15099 |
|
| 15100 |
|
| 15101 |
<prompt>bash$ </prompt><userinput>find ~/mail -type f | xargs grep "Linux"</userinput> |
| 15102 |
<computeroutput>./misc:User-Agent: slrn/0.9.8.1 (Linux) |
| 15103 |
./sent-mail-jul-2005: hosted by the Linux Documentation Project. |
| 15104 |
./sent-mail-jul-2005: (Linux Documentation Project Site, rtf version) |
| 15105 |
./sent-mail-jul-2005: Subject: Criticism of Bozo's Windows/Linux article |
| 15106 |
./sent-mail-jul-2005: while mentioning that the Linux ext2/ext3 filesystem |
| 15107 |
. . .</computeroutput> |
| 15108 |
</screen> |
| 15109 |
</para> |
| 15110 |
|
| 15111 |
<para><userinput>ls | xargs -p -l gzip</userinput> <link |
| 15112 |
linkend="gzipref">gzips</link> every file in current |
| 15113 |
directory, one at a time, prompting before each |
| 15114 |
operation.</para> |
| 15115 |
|
| 15116 |
<para><anchor id="xargsoneatatime"></para> |
| 15117 |
<note> |
| 15118 |
<para>Note that <firstterm>xargs</firstterm> processes the |
| 15119 |
arguments passed to it sequentially, <emphasis>one at |
| 15120 |
a time</emphasis>.</para> |
| 15121 |
|
| 15122 |
<para><screen> |
| 15123 |
<prompt>bash$ </prompt><userinput>find /usr/bin | xargs file</userinput> |
| 15124 |
<computeroutput>/usr/bin: directory |
| 15125 |
/usr/bin/foomatic-ppd-options: perl script text executable |
| 15126 |
. . .</computeroutput> |
| 15127 |
</screen> |
| 15128 |
</para> |
| 15129 |
</note> |
| 15130 |
|
| 15131 |
<para><anchor id="xargslimargs"></para> |
| 15132 |
<tip> |
| 15133 |
<para>An interesting <firstterm>xargs</firstterm> |
| 15134 |
option is <option>-n <replaceable>NN</replaceable></option>, |
| 15135 |
which limits to <replaceable>NN</replaceable> the number |
| 15136 |
of arguments passed.</para> |
| 15137 |
<para><userinput>ls | xargs -n 8 echo</userinput> lists the files in the |
| 15138 |
current directory in <literal>8</literal> columns.</para> |
| 15139 |
</tip> |
| 15140 |
|
| 15141 |
|
| 15142 |
<para><anchor id="xargsws"></para> |
| 15143 |
<tip> |
| 15144 |
<para>Another useful option is |
| 15145 |
<option>-0</option>, in combination with <userinput>find |
| 15146 |
-print0</userinput> or <userinput>grep -lZ</userinput>. This |
| 15147 |
allows handling arguments containing whitespace or |
| 15148 |
quotes.</para> |
| 15149 |
|
| 15150 |
<para> |
| 15151 |
<userinput>find / -type f -print0 | xargs -0 grep -liwZ GUI | xargs -0 rm -f</userinput> |
| 15152 |
</para> |
| 15153 |
|
| 15154 |
<para> |
| 15155 |
<userinput>grep -rliwZ GUI / | xargs -0 rm -f</userinput> |
| 15156 |
</para> |
| 15157 |
|
| 15158 |
<para>Either of the above will remove any file containing <quote>GUI</quote>. |
| 15159 |
<emphasis>(Thanks, S.C.)</emphasis></para> |
| 15160 |
|
| 15161 |
<para>Or: |
| 15162 |
<programlisting>cat /proc/"$pid"/"$OPTION" | xargs -0 echo |
| 15163 |
# Formats output: ^^^^^^^^^^^^^^^ |
| 15164 |
# From Han Holl's fixup of "get-commandline.sh" |
| 15165 |
#+ script in "/dev and /proc" chapter.</programlisting></para> |
| 15166 |
</tip> |
| 15167 |
|
| 15168 |
<tip> |
| 15169 |
<para><anchor id="xargsmultiprocess"></para> |
| 15170 |
<para>The <option>-P</option> option to |
| 15171 |
<firstterm>xargs</firstterm> permits running |
| 15172 |
processes in parallel. This speeds up execution |
| 15173 |
in a machine with a multicore CPU.</para> |
| 15174 |
<para><programlisting>#!/bin/bash |
| 15175 |
|
| 15176 |
ls *gif | xargs -t -n1 -P2 gif2png |
| 15177 |
# Converts all the gif images in current directory to png. |
| 15178 |
|
| 15179 |
# Options: |
| 15180 |
# ======= |
| 15181 |
# -t Print command to stderr. |
| 15182 |
# -n1 At most 1 argument per command line. |
| 15183 |
# -P2 Run up to 2 processes simultaneously. |
| 15184 |
|
| 15185 |
# Thank you, Roberto Polli, for the inspiration.</programlisting></para> |
| 15186 |
</tip> |
| 15187 |
|
| 15188 |
|
| 15189 |
<example id="ex41"> |
| 15190 |
<title>Logfile: Using <firstterm>xargs</firstterm> to monitor system log</title> |
| 15191 |
<programlisting>&ex41;</programlisting> |
| 15192 |
</example> |
| 15193 |
|
| 15194 |
<para><anchor id="xargscurlyref"></para> |
| 15195 |
<para><link linkend="curlybracketsref">As in |
| 15196 |
<command>find</command></link>, a curly bracket |
| 15197 |
pair serves as a placeholder for replacement text.</para> |
| 15198 |
|
| 15199 |
<example id="ex42"> |
| 15200 |
<title>Copying files in current directory to another</title> |
| 15201 |
<programlisting>&ex42;</programlisting> |
| 15202 |
</example> |
| 15203 |
|
| 15204 |
<example id="killbyname"> |
| 15205 |
<title>Killing processes by name</title> |
| 15206 |
<programlisting>&killbyname;</programlisting> |
| 15207 |
</example> |
| 15208 |
|
| 15209 |
<example id="wf2"> |
| 15210 |
<title>Word frequency analysis using |
| 15211 |
<firstterm>xargs</firstterm></title> |
| 15212 |
<programlisting>&wf2;</programlisting> |
| 15213 |
</example> |
| 15214 |
|
| 15215 |
</listitem> |
| 15216 |
</varlistentry> |
| 15217 |
|
| 15218 |
<varlistentry> |
| 15219 |
<term><anchor id="exprref"><userinput>expr</userinput></term> |
| 15220 |
<indexterm> |
| 15221 |
<primary>expr</primary> |
| 15222 |
</indexterm> |
| 15223 |
<indexterm> |
| 15224 |
<primary>command</primary> |
| 15225 |
<secondary>expr</secondary> |
| 15226 |
</indexterm> |
| 15227 |
<listitem> |
| 15228 |
<para>All-purpose expression evaluator: |
| 15229 |
Concatenates and evaluates the arguments according |
| 15230 |
to the operation given (arguments must be separated |
| 15231 |
by spaces). Operations may be arithmetic, comparison, |
| 15232 |
string, or logical.</para> |
| 15233 |
|
| 15234 |
<variablelist> |
| 15235 |
<varlistentry> |
| 15236 |
<term><userinput>expr 3 + 5</userinput></term> |
| 15237 |
<listitem> |
| 15238 |
<para>returns <literal>8</literal></para> |
| 15239 |
</listitem> |
| 15240 |
</varlistentry> |
| 15241 |
|
| 15242 |
<varlistentry> |
| 15243 |
<term><userinput>expr 5 % 3</userinput></term> |
| 15244 |
<listitem> |
| 15245 |
<para>returns 2</para> |
| 15246 |
</listitem> |
| 15247 |
</varlistentry> |
| 15248 |
|
| 15249 |
<varlistentry> |
| 15250 |
<term><userinput>expr 1 / 0</userinput></term> |
| 15251 |
<listitem> |
| 15252 |
<para>returns the error message, <errorcode>expr: division by |
| 15253 |
zero</errorcode></para> |
| 15254 |
<para>Illegal arithmetic operations not allowed.</para> |
| 15255 |
</listitem> |
| 15256 |
</varlistentry> |
| 15257 |
|
| 15258 |
<varlistentry> |
| 15259 |
<term><userinput>expr 5 \* 3</userinput></term> |
| 15260 |
<listitem> |
| 15261 |
<para>returns 15</para> |
| 15262 |
<para>The multiplication operator |
| 15263 |
must be escaped when used in an arithmetic expression |
| 15264 |
with <command>expr</command>.</para> |
| 15265 |
</listitem> |
| 15266 |
</varlistentry> |
| 15267 |
|
| 15268 |
<varlistentry> |
| 15269 |
<term><userinput>y=`expr $y + 1`</userinput></term> |
| 15270 |
<listitem> |
| 15271 |
<para>Increment a variable, with the same effect |
| 15272 |
as <userinput>let y=y+1</userinput> and |
| 15273 |
<userinput>y=$(($y+1))</userinput>. This is an |
| 15274 |
example of <link linkend="arithexpref">arithmetic |
| 15275 |
expansion</link>.</para> |
| 15276 |
</listitem> |
| 15277 |
</varlistentry> |
| 15278 |
|
| 15279 |
<varlistentry> |
| 15280 |
<term><anchor id="expextrsub"><userinput>z=`expr substr |
| 15281 |
$string $position $length`</userinput></term> |
| 15282 |
<listitem> |
| 15283 |
<para>Extract substring of $length characters, starting |
| 15284 |
at $position.</para> |
| 15285 |
</listitem> |
| 15286 |
</varlistentry> |
| 15287 |
</variablelist> |
| 15288 |
|
| 15289 |
<example id="ex45"> |
| 15290 |
<title>Using <firstterm>expr</firstterm></title> |
| 15291 |
<programlisting>&ex45;</programlisting> |
| 15292 |
</example> |
| 15293 |
|
| 15294 |
<important> |
| 15295 |
<para>The <link linkend="nullref">: |
| 15296 |
(<firstterm>null</firstterm>)</link> operator |
| 15297 |
can substitute for <command>match</command>. For example, |
| 15298 |
<userinput>b=`expr $a : [0-9]*`</userinput> is the |
| 15299 |
exact equivalent of <userinput>b=`expr match $a |
| 15300 |
[0-9]*`</userinput> in the above listing.</para> |
| 15301 |
|
| 15302 |
<para><programlisting>&ex45a;</programlisting></para> |
| 15303 |
</important> |
| 15304 |
|
| 15305 |
</listitem> |
| 15306 |
</varlistentry> |
| 15307 |
|
| 15308 |
</variablelist> |
| 15309 |
|
| 15310 |
<para>The above script illustrates how |
| 15311 |
<command>expr</command> uses the <firstterm>escaped |
| 15312 |
parentheses -- \( ... \) --</firstterm> grouping operator |
| 15313 |
in tandem with <link linkend="regexref">regular |
| 15314 |
expression</link> parsing to match a substring. |
| 15315 |
Here is a another example, this time from <quote>real |
| 15316 |
life.</quote> |
| 15317 |
|
| 15318 |
<programlisting># Strip the whitespace from the beginning and end. |
| 15319 |
LRFDATE=`expr "$LRFDATE" : '[[:space:]]*\(.*\)[[:space:]]*$'` |
| 15320 |
|
| 15321 |
# From Peter Knowles' "booklistgen.sh" script |
| 15322 |
#+ for converting files to Sony Librie/PRS-50X format. |
| 15323 |
# (http://booklistgensh.peterknowles.com)</programlisting> |
| 15324 |
|
| 15325 |
</para> |
| 15326 |
|
| 15327 |
|
| 15328 |
<para><link linkend="perlref">Perl</link>, |
| 15329 |
<link linkend="sedref">sed</link>, and <link |
| 15330 |
linkend="awkref">awk</link> have far superior string |
| 15331 |
parsing facilities. A short <command>sed</command> or |
| 15332 |
<command>awk</command> <quote>subroutine</quote> within |
| 15333 |
a script (see <xref linkend="wrapper">) is an attractive |
| 15334 |
alternative to <command>expr</command>.</para> |
| 15335 |
|
| 15336 |
|
| 15337 |
<para>See <xref linkend="String-Manipulation"> for more on |
| 15338 |
using <command>expr</command> in string operations.</para> |
| 15339 |
|
| 15340 |
|
| 15341 |
</sect1> <!-- End Complex Commands --> |
| 15342 |
|
| 15343 |
|
| 15344 |
|
| 15345 |
<sect1 id="timedate"> |
| 15346 |
<title>Time / Date Commands</title> |
| 15347 |
|
| 15348 |
<variablelist id="tdlisting"> |
| 15349 |
<title><anchor id="tdlisting1">Time/date and timing</title> |
| 15350 |
|
| 15351 |
<varlistentry> |
| 15352 |
<term><anchor id="dateref"><command>date</command></term> |
| 15353 |
<indexterm> |
| 15354 |
<primary>date</primary> |
| 15355 |
</indexterm> |
| 15356 |
<indexterm> |
| 15357 |
<primary>command</primary> |
| 15358 |
<secondary>date</secondary> |
| 15359 |
</indexterm> |
| 15360 |
<listitem> |
| 15361 |
<para>Simply invoked, <command>date</command> prints the date and |
| 15362 |
time to <filename>stdout</filename>. Where this command gets |
| 15363 |
interesting is in its formatting and parsing options.</para> |
| 15364 |
|
| 15365 |
<example id="ex51"> |
| 15366 |
<title>Using <firstterm>date</firstterm></title> |
| 15367 |
<programlisting>&ex51;</programlisting> |
| 15368 |
</example> |
| 15369 |
|
| 15370 |
|
| 15371 |
<para>The <option>-u</option> option gives the UTC (Universal |
| 15372 |
Coordinated Time).</para> |
| 15373 |
|
| 15374 |
<para> |
| 15375 |
<screen> |
| 15376 |
<prompt>bash$ </prompt><userinput>date</userinput> |
| 15377 |
<computeroutput>Fri Mar 29 21:07:39 MST 2002</computeroutput> |
| 15378 |
|
| 15379 |
|
| 15380 |
|
| 15381 |
<prompt>bash$ </prompt><userinput>date -u</userinput> |
| 15382 |
<computeroutput>Sat Mar 30 04:07:42 UTC 2002</computeroutput> |
| 15383 |
</screen> |
| 15384 |
</para> |
| 15385 |
|
| 15386 |
<para>This option facilitates calculating the time between |
| 15387 |
different dates.</para> |
| 15388 |
|
| 15389 |
<example id="datecalc"> |
| 15390 |
<title><firstterm>Date</firstterm> calculations</title> |
| 15391 |
<programlisting>&datecalc;</programlisting> |
| 15392 |
</example> |
| 15393 |
|
| 15394 |
|
| 15395 |
<para><anchor id="daterandref"></para> |
| 15396 |
<para>The <firstterm>date</firstterm> command has quite a |
| 15397 |
number of <firstterm>output</firstterm> options. For |
| 15398 |
example <option>%N</option> gives the nanosecond portion |
| 15399 |
of the current time. One interesting use for this is to |
| 15400 |
generate random integers. |
| 15401 |
|
| 15402 |
<programlisting>date +%N | sed -e 's/000$//' -e 's/^0//' |
| 15403 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 15404 |
# Strip off leading and trailing zeroes, if present. |
| 15405 |
# Length of generated integer depends on |
| 15406 |
#+ how many zeroes stripped off. |
| 15407 |
|
| 15408 |
# 115281032 |
| 15409 |
# 63408725 |
| 15410 |
# 394504284</programlisting> |
| 15411 |
</para> |
| 15412 |
|
| 15413 |
<para>There are many more options (try <command>man |
| 15414 |
date</command>).</para> |
| 15415 |
|
| 15416 |
<para><programlisting>date +%j |
| 15417 |
# Echoes day of the year (days elapsed since January 1). |
| 15418 |
|
| 15419 |
date +%k%M |
| 15420 |
# Echoes hour and minute in 24-hour format, as a single digit string. |
| 15421 |
|
| 15422 |
|
| 15423 |
|
| 15424 |
# The 'TZ' parameter permits overriding the default time zone. |
| 15425 |
date # Mon Mar 28 21:42:16 MST 2005 |
| 15426 |
TZ=EST date # Mon Mar 28 23:42:16 EST 2005 |
| 15427 |
# Thanks, Frank Kannemann and Pete Sjoberg, for the tip. |
| 15428 |
|
| 15429 |
|
| 15430 |
SixDaysAgo=$(date --date='6 days ago') |
| 15431 |
OneMonthAgo=$(date --date='1 month ago') # Four weeks back (not a month!) |
| 15432 |
OneYearAgo=$(date --date='1 year ago')</programlisting></para> |
| 15433 |
|
| 15434 |
<para>See also <xref linkend="ex58"> and <xref |
| 15435 |
linkend="stopwatch">.</para> |
| 15436 |
|
| 15437 |
</listitem> |
| 15438 |
</varlistentry> |
| 15439 |
|
| 15440 |
<varlistentry> |
| 15441 |
<term><anchor id="zdumpref"><command>zdump</command></term> |
| 15442 |
<indexterm> |
| 15443 |
<primary>zdump</primary> |
| 15444 |
</indexterm> |
| 15445 |
<indexterm> |
| 15446 |
<primary>command</primary> |
| 15447 |
<secondary>time zone dump</secondary> |
| 15448 |
</indexterm> |
| 15449 |
<listitem> |
| 15450 |
<para>Time zone dump: echoes the time in a specified time zone.</para> |
| 15451 |
<para> |
| 15452 |
<screen><prompt>bash$ </prompt><userinput>zdump EST</userinput> |
| 15453 |
<computeroutput>EST Tue Sep 18 22:09:22 2001 EST</computeroutput> |
| 15454 |
</screen> |
| 15455 |
</para> |
| 15456 |
</listitem> |
| 15457 |
</varlistentry> |
| 15458 |
|
| 15459 |
<varlistentry> |
| 15460 |
<term><anchor id="timref"><command>time</command></term> |
| 15461 |
<indexterm> |
| 15462 |
<primary>time</primary> |
| 15463 |
</indexterm> |
| 15464 |
<indexterm> |
| 15465 |
<primary>command</primary> |
| 15466 |
<secondary>time</secondary> |
| 15467 |
</indexterm> |
| 15468 |
<listitem> |
| 15469 |
|
| 15470 |
<para>Outputs verbose timing statistics for executing a command.</para> |
| 15471 |
|
| 15472 |
<para><userinput>time ls -l /</userinput> gives something |
| 15473 |
like this:</para> |
| 15474 |
|
| 15475 |
<para> |
| 15476 |
<screen><computeroutput>real 0m0.067s |
| 15477 |
user 0m0.004s |
| 15478 |
sys 0m0.005s</computeroutput></screen> |
| 15479 |
</para> |
| 15480 |
|
| 15481 |
<para>See also the very similar <link |
| 15482 |
linkend="timesref">times</link> command in the previous |
| 15483 |
section.</para> |
| 15484 |
|
| 15485 |
<note><para>As of <link linkend="bash2ref">version 2.0</link> |
| 15486 |
of Bash, <command>time</command> became a shell reserved word, |
| 15487 |
with slightly altered behavior in a pipeline.</para></note> |
| 15488 |
|
| 15489 |
</listitem> |
| 15490 |
</varlistentry> |
| 15491 |
|
| 15492 |
<varlistentry> |
| 15493 |
<term><anchor id="touchref"><command>touch</command></term> |
| 15494 |
<indexterm> |
| 15495 |
<primary>touch</primary> |
| 15496 |
</indexterm> |
| 15497 |
<indexterm> |
| 15498 |
<primary>command</primary> |
| 15499 |
<secondary>touch</secondary> |
| 15500 |
</indexterm> |
| 15501 |
<listitem> |
| 15502 |
|
| 15503 |
<para>Utility for updating access/modification times of a |
| 15504 |
file to current system time or other specified time, |
| 15505 |
but also useful for creating a new file. The command |
| 15506 |
<userinput>touch zzz</userinput> will create a new file |
| 15507 |
of zero length, named <filename>zzz</filename>, assuming |
| 15508 |
that <filename>zzz</filename> did not previously exist. |
| 15509 |
Time-stamping empty files in this way is useful for |
| 15510 |
storing date information, for example in keeping track of |
| 15511 |
modification times on a project. |
| 15512 |
</para> |
| 15513 |
|
| 15514 |
<note><para>The <command>touch</command> command is |
| 15515 |
equivalent to <userinput>: >> newfile</userinput> |
| 15516 |
or <userinput>>> newfile</userinput> (for ordinary |
| 15517 |
files).</para></note> |
| 15518 |
|
| 15519 |
<tip> |
| 15520 |
<para>Before doing a <link linkend="cpref">cp -u</link> |
| 15521 |
(<firstterm>copy/update</firstterm>), use |
| 15522 |
<command>touch</command> to update the time stamp of files |
| 15523 |
you don't wish overwritten.</para> |
| 15524 |
<para>As an example, if the directory <filename |
| 15525 |
class="directory">/home/bozo/tax_audit</filename> contains the |
| 15526 |
files <filename>spreadsheet-051606.data</filename>, |
| 15527 |
<filename>spreadsheet-051706.data</filename>, and |
| 15528 |
<filename>spreadsheet-051806.data</filename>, then |
| 15529 |
doing a <command>touch spreadsheet*.data</command> |
| 15530 |
will protect these files from being overwritten |
| 15531 |
by files with the same names during a |
| 15532 |
<command>cp -u /home/bozo/financial_info/spreadsheet*data |
| 15533 |
/home/bozo/tax_audit</command>.</para> |
| 15534 |
</tip> |
| 15535 |
|
| 15536 |
</listitem> |
| 15537 |
</varlistentry> |
| 15538 |
|
| 15539 |
<varlistentry> |
| 15540 |
<term><anchor id="atref"><command>at</command></term> |
| 15541 |
<indexterm> |
| 15542 |
<primary>at</primary> |
| 15543 |
</indexterm> |
| 15544 |
<indexterm> |
| 15545 |
<primary>command</primary> |
| 15546 |
<secondary>at</secondary> |
| 15547 |
</indexterm> |
| 15548 |
<indexterm> |
| 15549 |
<primary>cron</primary> |
| 15550 |
</indexterm> |
| 15551 |
<indexterm> |
| 15552 |
<primary>command</primary> |
| 15553 |
<secondary>cron</secondary> |
| 15554 |
</indexterm> |
| 15555 |
<listitem> |
| 15556 |
<para>The <command>at</command> job control command executes |
| 15557 |
a given set of commands at a specified time. Superficially, |
| 15558 |
it resembles <link linkend="cronref">cron</link>, however, |
| 15559 |
<command>at</command> is chiefly useful for one-time execution |
| 15560 |
of a command set.</para> |
| 15561 |
|
| 15562 |
<para><userinput>at 2pm January 15</userinput> prompts for a set of |
| 15563 |
commands to execute at that time. These commands should be |
| 15564 |
shell-script compatible, since, for all practical |
| 15565 |
purposes, the user is typing in an executable shell |
| 15566 |
script a line at a time. Input terminates with a <link |
| 15567 |
linkend="ctldref">Ctl-D</link>.</para> |
| 15568 |
|
| 15569 |
<para>Using either the <option>-f</option> option or input |
| 15570 |
redirection (<token><</token>), <command>at</command> |
| 15571 |
reads a command list from a file. This file is an |
| 15572 |
executable shell script, though it should, of course, |
| 15573 |
be non-interactive. Particularly clever is including the |
| 15574 |
<link linkend="runpartsref">run-parts</link> command in |
| 15575 |
the file to execute a different set of scripts.</para> |
| 15576 |
|
| 15577 |
<para> |
| 15578 |
<screen><prompt>bash$ </prompt><userinput>at 2:30 am Friday < at-jobs.list</userinput> |
| 15579 |
<computeroutput>job 2 at 2000-10-27 02:30</computeroutput> |
| 15580 |
</screen> |
| 15581 |
</para> |
| 15582 |
</listitem> |
| 15583 |
</varlistentry> |
| 15584 |
|
| 15585 |
<varlistentry> |
| 15586 |
<term><anchor id="batchref"><command>batch</command></term> |
| 15587 |
<indexterm> |
| 15588 |
<primary>batch</primary> |
| 15589 |
</indexterm> |
| 15590 |
<indexterm> |
| 15591 |
<primary>command</primary> |
| 15592 |
<secondary>batch</secondary> |
| 15593 |
</indexterm> |
| 15594 |
<indexterm> |
| 15595 |
<primary>at</primary> |
| 15596 |
</indexterm> |
| 15597 |
<indexterm> |
| 15598 |
<primary>command</primary> |
| 15599 |
<secondary>at</secondary> |
| 15600 |
</indexterm> |
| 15601 |
<listitem> |
| 15602 |
|
| 15603 |
<para>The <command>batch</command> job control command is similar to |
| 15604 |
<command>at</command>, but it runs a command list when the system |
| 15605 |
load drops below <literal>.8</literal>. Like |
| 15606 |
<command>at</command>, it can read commands from a file with the |
| 15607 |
<option>-f</option> option.</para> |
| 15608 |
|
| 15609 |
<para><anchor id="batchprocref"></para> |
| 15610 |
|
| 15611 |
<sidebar> |
| 15612 |
<para>The concept of <firstterm>batch processing</firstterm> |
| 15613 |
dates back to the era of mainframe computers. It means |
| 15614 |
running a set of commands without user intervention.</para> |
| 15615 |
</sidebar> |
| 15616 |
|
| 15617 |
</listitem> |
| 15618 |
</varlistentry> |
| 15619 |
|
| 15620 |
<varlistentry> |
| 15621 |
<term><anchor id="calref"><command>cal</command></term> |
| 15622 |
<indexterm> |
| 15623 |
<primary>cal</primary> |
| 15624 |
</indexterm> |
| 15625 |
<indexterm> |
| 15626 |
<primary>command</primary> |
| 15627 |
<secondary>cal</secondary> |
| 15628 |
</indexterm> |
| 15629 |
<listitem> |
| 15630 |
<para>Prints a neatly formatted monthly calendar to |
| 15631 |
<filename>stdout</filename>. Will do current year or a large |
| 15632 |
range of past and future years.</para> |
| 15633 |
</listitem> |
| 15634 |
</varlistentry> |
| 15635 |
|
| 15636 |
<varlistentry> |
| 15637 |
<term><anchor id="sleepref"><command>sleep</command></term> |
| 15638 |
<indexterm> |
| 15639 |
<primary>sleep</primary> |
| 15640 |
</indexterm> |
| 15641 |
<indexterm> |
| 15642 |
<primary>command</primary> |
| 15643 |
<secondary>sleep</secondary> |
| 15644 |
</indexterm> |
| 15645 |
<listitem> |
| 15646 |
<para>This is the shell equivalent of a <firstterm>wait |
| 15647 |
loop</firstterm>. It pauses for a specified number of |
| 15648 |
seconds, doing nothing. It can be useful for timing or |
| 15649 |
in processes running in the background, checking for |
| 15650 |
a specific event every so often (polling), as in <xref |
| 15651 |
linkend="online">. <programlisting>sleep 3 # Pauses 3 seconds.</programlisting> |
| 15652 |
</para> |
| 15653 |
|
| 15654 |
<note><para>The <command>sleep</command> command defaults to |
| 15655 |
seconds, but minute, hours, or days may also be specified. |
| 15656 |
<programlisting>sleep 3 h # Pauses 3 hours!</programlisting> |
| 15657 |
</para></note> |
| 15658 |
|
| 15659 |
<note><para>The <link linkend="watchref">watch</link> command may |
| 15660 |
be a better choice than <command>sleep</command> for running |
| 15661 |
commands at timed intervals.</para></note> |
| 15662 |
|
| 15663 |
</listitem> |
| 15664 |
</varlistentry> |
| 15665 |
|
| 15666 |
<varlistentry> |
| 15667 |
<term><anchor id="usleepref"><command>usleep</command></term> |
| 15668 |
<indexterm> |
| 15669 |
<primary>usleep</primary> |
| 15670 |
</indexterm> |
| 15671 |
<indexterm> |
| 15672 |
<primary>command</primary> |
| 15673 |
<secondary>usleep</secondary> |
| 15674 |
</indexterm> |
| 15675 |
<listitem> |
| 15676 |
<para><firstterm>Microsleep</firstterm> (the |
| 15677 |
<firstterm>u</firstterm> may be read as the Greek |
| 15678 |
<firstterm>mu</firstterm>, or <firstterm>micro-</firstterm> |
| 15679 |
prefix). This is the same as <command>sleep</command>, |
| 15680 |
above, but <quote>sleeps</quote> in microsecond |
| 15681 |
intervals. It can be used for fine-grained timing, |
| 15682 |
or for polling an ongoing process at very frequent |
| 15683 |
intervals.</para> |
| 15684 |
|
| 15685 |
<para> |
| 15686 |
<programlisting>usleep 30 # Pauses 30 microseconds.</programlisting> |
| 15687 |
</para> |
| 15688 |
|
| 15689 |
<para>This command is part of the Red Hat |
| 15690 |
<firstterm>initscripts / rc-scripts</firstterm> package.</para> |
| 15691 |
|
| 15692 |
<caution><para>The <command>usleep</command> command does not |
| 15693 |
provide particularly accurate timing, and is therefore |
| 15694 |
unsuitable for critical timing loops.</para></caution> |
| 15695 |
</listitem> |
| 15696 |
</varlistentry> |
| 15697 |
|
| 15698 |
<varlistentry> |
| 15699 |
<term><anchor id="hwclockref"><command>hwclock</command></term> |
| 15700 |
<term><anchor id="clockref"><command>clock</command></term> |
| 15701 |
<indexterm> |
| 15702 |
<primary>hwclock</primary> |
| 15703 |
</indexterm> |
| 15704 |
<indexterm> |
| 15705 |
<primary>command</primary> |
| 15706 |
<secondary>hwclock</secondary> |
| 15707 |
</indexterm> |
| 15708 |
<indexterm> |
| 15709 |
<primary>clock</primary> |
| 15710 |
</indexterm> |
| 15711 |
<indexterm> |
| 15712 |
<primary>command</primary> |
| 15713 |
<secondary>clock</secondary> |
| 15714 |
</indexterm> |
| 15715 |
<listitem> |
| 15716 |
<para>The <command>hwclock</command> command accesses or |
| 15717 |
adjusts the machine's hardware clock. Some options |
| 15718 |
require <firstterm>root</firstterm> privileges. The |
| 15719 |
<filename>/etc/rc.d/rc.sysinit</filename> startup file |
| 15720 |
uses <command>hwclock</command> to set the system time |
| 15721 |
from the hardware clock at bootup.</para> |
| 15722 |
|
| 15723 |
<para>The <command>clock</command> command is a synonym for |
| 15724 |
<command>hwclock</command>.</para> |
| 15725 |
</listitem> |
| 15726 |
</varlistentry> |
| 15727 |
|
| 15728 |
|
| 15729 |
</variablelist> |
| 15730 |
|
| 15731 |
</sect1> <!-- End Time / Date Commands --> |
| 15732 |
|
| 15733 |
|
| 15734 |
|
| 15735 |
|
| 15736 |
<sect1 id="textproc"> |
| 15737 |
<title>Text Processing Commands</title> |
| 15738 |
|
| 15739 |
<variablelist id="tpcommandlisting"> |
| 15740 |
<title><anchor id="tpcommandlisting1">Commands affecting text and |
| 15741 |
text files</title> |
| 15742 |
|
| 15743 |
<varlistentry> |
| 15744 |
<term><anchor id="sortref"><command>sort</command></term> |
| 15745 |
<indexterm> |
| 15746 |
<primary>sort</primary> |
| 15747 |
</indexterm> |
| 15748 |
<indexterm> |
| 15749 |
<primary>command</primary> |
| 15750 |
<secondary>sort</secondary> |
| 15751 |
</indexterm> |
| 15752 |
<listitem> |
| 15753 |
<para>File sort utility, often used as a filter in a pipe. This |
| 15754 |
command sorts a <firstterm>text stream</firstterm> |
| 15755 |
or file forwards or backwards, or according to various |
| 15756 |
keys or character positions. Using the <option>-m</option> |
| 15757 |
option, it merges presorted input files. The <firstterm>info |
| 15758 |
page</firstterm> lists its many capabilities and options. See |
| 15759 |
<xref linkend="findstring">, <xref linkend="symlinks">, |
| 15760 |
and <xref linkend="makedict">.</para> |
| 15761 |
</listitem> |
| 15762 |
</varlistentry> |
| 15763 |
|
| 15764 |
<varlistentry> |
| 15765 |
<term><anchor id="tsortref"><command>tsort</command></term> |
| 15766 |
<indexterm> |
| 15767 |
<primary>tsort</primary> |
| 15768 |
</indexterm> |
| 15769 |
<indexterm> |
| 15770 |
<primary>command</primary> |
| 15771 |
<secondary>topological sort</secondary> |
| 15772 |
</indexterm> |
| 15773 |
<listitem> |
| 15774 |
|
| 15775 |
<para><firstterm>Topological sort</firstterm>, reading in |
| 15776 |
pairs of whitespace-separated strings and sorting |
| 15777 |
according to input patterns. The original purpose of |
| 15778 |
<command>tsort</command> was to sort a list of dependencies |
| 15779 |
for an obsolete version of the <firstterm>ld</firstterm> |
| 15780 |
linker in an <quote>ancient</quote> version of UNIX.</para> |
| 15781 |
|
| 15782 |
<para>The results of a <firstterm>tsort</firstterm> will usually |
| 15783 |
differ markedly from those of the standard |
| 15784 |
<command>sort</command> command, above.</para> |
| 15785 |
</listitem> |
| 15786 |
</varlistentry> |
| 15787 |
|
| 15788 |
<varlistentry> |
| 15789 |
<term><anchor id="uniqref"><command>uniq</command></term> |
| 15790 |
<indexterm> |
| 15791 |
<primary>uniq</primary> |
| 15792 |
</indexterm> |
| 15793 |
<indexterm> |
| 15794 |
<primary>command</primary> |
| 15795 |
<secondary>uniq</secondary> |
| 15796 |
</indexterm> |
| 15797 |
<listitem> |
| 15798 |
<para>This filter removes duplicate lines from a sorted |
| 15799 |
file. It is often seen in a pipe coupled with |
| 15800 |
<link linkend="sortref">sort</link>.</para> |
| 15801 |
|
| 15802 |
<para><programlisting>cat list-1 list-2 list-3 | sort | uniq > final.list |
| 15803 |
# Concatenates the list files, |
| 15804 |
# sorts them, |
| 15805 |
# removes duplicate lines, |
| 15806 |
# and finally writes the result to an output file.</programlisting></para> |
| 15807 |
|
| 15808 |
<para>The useful <option>-c</option> option prefixes each line of |
| 15809 |
the input file with its number of occurrences.</para> |
| 15810 |
<para> |
| 15811 |
<screen> |
| 15812 |
<prompt>bash$ </prompt><userinput>cat testfile</userinput> |
| 15813 |
<computeroutput>This line occurs only once. |
| 15814 |
This line occurs twice. |
| 15815 |
This line occurs twice. |
| 15816 |
This line occurs three times. |
| 15817 |
This line occurs three times. |
| 15818 |
This line occurs three times.</computeroutput> |
| 15819 |
|
| 15820 |
|
| 15821 |
<prompt>bash$ </prompt><userinput>uniq -c testfile</userinput> |
| 15822 |
<computeroutput> 1 This line occurs only once. |
| 15823 |
2 This line occurs twice. |
| 15824 |
3 This line occurs three times.</computeroutput> |
| 15825 |
|
| 15826 |
|
| 15827 |
<prompt>bash$ </prompt><userinput>sort testfile | uniq -c | sort -nr</userinput> |
| 15828 |
<computeroutput> 3 This line occurs three times. |
| 15829 |
2 This line occurs twice. |
| 15830 |
1 This line occurs only once.</computeroutput> |
| 15831 |
</screen> |
| 15832 |
</para> |
| 15833 |
|
| 15834 |
<para>The <userinput>sort INPUTFILE | uniq -c | sort -nr</userinput> |
| 15835 |
command string produces a <firstterm>frequency |
| 15836 |
of occurrence</firstterm> listing on the |
| 15837 |
<filename>INPUTFILE</filename> file (the |
| 15838 |
<option>-nr</option> options to <command>sort</command> |
| 15839 |
cause a reverse numerical sort). This template finds |
| 15840 |
use in analysis of log files and dictionary lists, and |
| 15841 |
wherever the lexical structure of a document needs to |
| 15842 |
be examined.</para> |
| 15843 |
|
| 15844 |
<example id="wf"> |
| 15845 |
<title>Word Frequency Analysis</title> |
| 15846 |
<programlisting>&wf;</programlisting> |
| 15847 |
</example> |
| 15848 |
|
| 15849 |
<para> |
| 15850 |
<screen> |
| 15851 |
<prompt>bash$ </prompt><userinput>cat testfile</userinput> |
| 15852 |
<computeroutput>This line occurs only once. |
| 15853 |
This line occurs twice. |
| 15854 |
This line occurs twice. |
| 15855 |
This line occurs three times. |
| 15856 |
This line occurs three times. |
| 15857 |
This line occurs three times.</computeroutput> |
| 15858 |
|
| 15859 |
|
| 15860 |
<prompt>bash$ </prompt><userinput>./wf.sh testfile</userinput> |
| 15861 |
<computeroutput> 6 this |
| 15862 |
6 occurs |
| 15863 |
6 line |
| 15864 |
3 times |
| 15865 |
3 three |
| 15866 |
2 twice |
| 15867 |
1 only |
| 15868 |
1 once</computeroutput> |
| 15869 |
</screen> |
| 15870 |
</para> |
| 15871 |
|
| 15872 |
</listitem> |
| 15873 |
</varlistentry> |
| 15874 |
|
| 15875 |
<varlistentry> |
| 15876 |
<term><anchor id="expandref"><command>expand</command></term> |
| 15877 |
<term><command>unexpand</command></term> |
| 15878 |
<indexterm> |
| 15879 |
<primary>expand</primary> |
| 15880 |
</indexterm> |
| 15881 |
<indexterm> |
| 15882 |
<primary>command</primary> |
| 15883 |
<secondary>expand</secondary> |
| 15884 |
</indexterm> |
| 15885 |
<indexterm> |
| 15886 |
<primary>unexpand</primary> |
| 15887 |
</indexterm> |
| 15888 |
<indexterm> |
| 15889 |
<primary>command</primary> |
| 15890 |
<secondary>unexpand</secondary> |
| 15891 |
</indexterm> |
| 15892 |
<listitem> |
| 15893 |
<para>The <command>expand</command> filter converts tabs to |
| 15894 |
spaces. It is often used in a <link |
| 15895 |
linkend="piperef">pipe</link>.</para> |
| 15896 |
<para>The <command>unexpand</command> filter |
| 15897 |
converts spaces to tabs. This reverses the effect of |
| 15898 |
<command>expand</command>.</para> |
| 15899 |
</listitem> |
| 15900 |
</varlistentry> |
| 15901 |
|
| 15902 |
<varlistentry> |
| 15903 |
<term><anchor id="cutref"><command>cut</command></term> |
| 15904 |
<indexterm> |
| 15905 |
<primary>cut</primary> |
| 15906 |
</indexterm> |
| 15907 |
<indexterm> |
| 15908 |
<primary>command</primary> |
| 15909 |
<secondary>cut</secondary> |
| 15910 |
</indexterm> |
| 15911 |
<indexterm> |
| 15912 |
<primary>awk</primary> |
| 15913 |
</indexterm> |
| 15914 |
<indexterm> |
| 15915 |
<primary>command</primary> |
| 15916 |
<secondary>awk</secondary> |
| 15917 |
</indexterm> |
| 15918 |
<listitem> |
| 15919 |
<para>A tool for extracting <link |
| 15920 |
linkend="fieldref">fields</link> from files. It is similar |
| 15921 |
to the <userinput>print $N</userinput> command set in <link |
| 15922 |
linkend="awkref">awk</link>, but more limited. It may be |
| 15923 |
simpler to use <firstterm>cut</firstterm> in a script than |
| 15924 |
<firstterm>awk</firstterm>. Particularly important are the |
| 15925 |
<option>-d</option> (delimiter) and <option>-f</option> |
| 15926 |
(field specifier) options.</para> |
| 15927 |
|
| 15928 |
<para>Using <command>cut</command> to obtain a listing of the |
| 15929 |
mounted filesystems: |
| 15930 |
<programlisting>cut -d ' ' -f1,2 /etc/mtab</programlisting></para> |
| 15931 |
|
| 15932 |
<para>Using <command>cut</command> to list the OS and kernel version: |
| 15933 |
<programlisting>uname -a | cut -d" " -f1,3,11,12</programlisting></para> |
| 15934 |
|
| 15935 |
<para>Using <command>cut</command> to extract message headers from |
| 15936 |
an e-mail folder: |
| 15937 |
|
| 15938 |
<screen><prompt>bash$ </prompt><userinput>grep '^Subject:' read-messages | cut -c10-80</userinput> |
| 15939 |
<computeroutput>Re: Linux suitable for mission-critical apps? |
| 15940 |
MAKE MILLIONS WORKING AT HOME!!! |
| 15941 |
Spam complaint |
| 15942 |
Re: Spam complaint</computeroutput></screen> |
| 15943 |
</para> |
| 15944 |
|
| 15945 |
<para>Using <command>cut</command> to parse a file: |
| 15946 |
<programlisting># List all the users in /etc/passwd. |
| 15947 |
|
| 15948 |
FILENAME=/etc/passwd |
| 15949 |
|
| 15950 |
for user in $(cut -d: -f1 $FILENAME) |
| 15951 |
do |
| 15952 |
echo $user |
| 15953 |
done |
| 15954 |
|
| 15955 |
# Thanks, Oleg Philon for suggesting this.</programlisting></para> |
| 15956 |
|
| 15957 |
<para><userinput>cut -d ' ' -f2,3 filename</userinput> is equivalent to |
| 15958 |
<userinput>awk -F'[ ]' '{ print $2, $3 }' filename</userinput></para> |
| 15959 |
|
| 15960 |
<note> |
| 15961 |
<para>It is even possible to specify a linefeed as a |
| 15962 |
delimiter. The trick is to actually embed a linefeed |
| 15963 |
(<keycap>RETURN</keycap>) in the command sequence.</para> |
| 15964 |
<para> |
| 15965 |
<screen><prompt>bash$ </prompt><userinput>cut -d' |
| 15966 |
' -f3,7,19 testfile</userinput> |
| 15967 |
<computeroutput>This is line 3 of testfile. |
| 15968 |
This is line 7 of testfile. |
| 15969 |
This is line 19 of testfile.</computeroutput> |
| 15970 |
</screen> |
| 15971 |
</para> |
| 15972 |
|
| 15973 |
<para>Thank you, Jaka Kranjc, for pointing this out.</para> |
| 15974 |
</note> |
| 15975 |
|
| 15976 |
<para>See also <xref linkend="base">.</para> |
| 15977 |
|
| 15978 |
</listitem> |
| 15979 |
</varlistentry> |
| 15980 |
|
| 15981 |
<varlistentry> |
| 15982 |
<term><anchor id="pasteref"><command>paste</command></term> |
| 15983 |
<indexterm> |
| 15984 |
<primary>paste</primary> |
| 15985 |
</indexterm> |
| 15986 |
<indexterm> |
| 15987 |
<primary>command</primary> |
| 15988 |
<secondary>paste</secondary> |
| 15989 |
</indexterm> |
| 15990 |
<indexterm> |
| 15991 |
<primary>cut</primary> |
| 15992 |
</indexterm> |
| 15993 |
<indexterm> |
| 15994 |
<primary>command</primary> |
| 15995 |
<secondary>cut</secondary> |
| 15996 |
</indexterm> |
| 15997 |
<listitem> |
| 15998 |
<para>Tool for merging together different files into a single, |
| 15999 |
multi-column file. In combination with |
| 16000 |
<link linkend="cutref">cut</link>, useful for creating system log |
| 16001 |
files. |
| 16002 |
</para> |
| 16003 |
|
| 16004 |
|
| 16005 |
<para> |
| 16006 |
<screen><prompt>bash$ </prompt><userinput>cat items</userinput> |
| 16007 |
<computeroutput>alphabet blocks |
| 16008 |
building blocks |
| 16009 |
cables</computeroutput> |
| 16010 |
|
| 16011 |
<prompt>bash$ </prompt><userinput>cat prices</userinput> |
| 16012 |
<computeroutput>$1.00/dozen |
| 16013 |
$2.50 ea. |
| 16014 |
$3.75</computeroutput> |
| 16015 |
|
| 16016 |
<prompt>bash$ </prompt><userinput>paste items prices</userinput> |
| 16017 |
<computeroutput>alphabet blocks $1.00/dozen |
| 16018 |
building blocks $2.50 ea. |
| 16019 |
cables $3.75</computeroutput></screen> |
| 16020 |
</para> |
| 16021 |
|
| 16022 |
</listitem> |
| 16023 |
</varlistentry> |
| 16024 |
|
| 16025 |
<varlistentry> |
| 16026 |
<term><anchor id="joinref"><command>join</command></term> |
| 16027 |
<indexterm> |
| 16028 |
<primary>join</primary> |
| 16029 |
</indexterm> |
| 16030 |
<indexterm> |
| 16031 |
<primary>command</primary> |
| 16032 |
<secondary>join</secondary> |
| 16033 |
</indexterm> |
| 16034 |
<listitem> |
| 16035 |
<para>Consider this a special-purpose cousin of |
| 16036 |
<command>paste</command>. This powerful utility allows |
| 16037 |
merging two files in a meaningful fashion, which essentially |
| 16038 |
creates a simple version of a relational database.</para> |
| 16039 |
|
| 16040 |
<para>The <command>join</command> command operates on |
| 16041 |
exactly two files, but pastes together only those lines |
| 16042 |
with a common tagged <link linkend="fieldref">field</link> |
| 16043 |
(usually a numerical label), and writes the result to |
| 16044 |
<filename>stdout</filename>. The files to be joined should |
| 16045 |
be sorted according to the tagged field for the matchups |
| 16046 |
to work properly.</para> |
| 16047 |
|
| 16048 |
<para><programlisting>File: 1.data |
| 16049 |
|
| 16050 |
100 Shoes |
| 16051 |
200 Laces |
| 16052 |
300 Socks</programlisting></para> |
| 16053 |
|
| 16054 |
<para><programlisting>File: 2.data |
| 16055 |
|
| 16056 |
100 $40.00 |
| 16057 |
200 $1.00 |
| 16058 |
300 $2.00</programlisting></para> |
| 16059 |
|
| 16060 |
<para> |
| 16061 |
<screen><prompt>bash$ </prompt><userinput>join 1.data 2.data</userinput> |
| 16062 |
<computeroutput>File: 1.data 2.data |
| 16063 |
|
| 16064 |
100 Shoes $40.00 |
| 16065 |
200 Laces $1.00 |
| 16066 |
300 Socks $2.00</computeroutput> |
| 16067 |
</screen> |
| 16068 |
</para> |
| 16069 |
|
| 16070 |
<note><para>The tagged field appears only once in the |
| 16071 |
output.</para></note> |
| 16072 |
|
| 16073 |
</listitem> |
| 16074 |
</varlistentry> |
| 16075 |
|
| 16076 |
<varlistentry> |
| 16077 |
<term><anchor id="headref"><command>head</command></term> |
| 16078 |
<indexterm> |
| 16079 |
<primary>head</primary> |
| 16080 |
</indexterm> |
| 16081 |
<indexterm> |
| 16082 |
<primary>command</primary> |
| 16083 |
<secondary>head</secondary> |
| 16084 |
</indexterm> |
| 16085 |
<listitem> |
| 16086 |
<para>lists the beginning of a file to <filename>stdout</filename>. |
| 16087 |
The default is <literal>10</literal> lines, but a different |
| 16088 |
number can be specified. The command has a number of |
| 16089 |
interesting options. |
| 16090 |
|
| 16091 |
<example id="scriptdetector"> |
| 16092 |
<title>Which files are scripts?</title> |
| 16093 |
<programlisting>&scriptdetector;</programlisting> |
| 16094 |
</example> |
| 16095 |
|
| 16096 |
<example id="rnd"> |
| 16097 |
<title>Generating 10-digit random numbers</title> |
| 16098 |
<programlisting>&rnd;</programlisting> |
| 16099 |
</example> |
| 16100 |
|
| 16101 |
See also <xref linkend="ex52">.</para> |
| 16102 |
</listitem> |
| 16103 |
</varlistentry> |
| 16104 |
|
| 16105 |
<varlistentry> |
| 16106 |
<term><anchor id="tailref"><command>tail</command></term> |
| 16107 |
<indexterm> |
| 16108 |
<primary>tail</primary> |
| 16109 |
</indexterm> |
| 16110 |
<indexterm> |
| 16111 |
<primary>command</primary> |
| 16112 |
<secondary>tail</secondary> |
| 16113 |
</indexterm> |
| 16114 |
<listitem> |
| 16115 |
<para>lists the (tail) end of a file to <filename>stdout</filename>. |
| 16116 |
The default is <literal>10</literal> lines, but this can |
| 16117 |
be changed with the <option>-n</option> option. |
| 16118 |
Commonly used to keep track of |
| 16119 |
changes to a system logfile, using the <option>-f</option> |
| 16120 |
option, which outputs lines appended to the file.</para> |
| 16121 |
|
| 16122 |
<example id="ex12"> |
| 16123 |
<title>Using <firstterm>tail</firstterm> to monitor the system log</title> |
| 16124 |
<programlisting>&ex12;</programlisting> |
| 16125 |
</example> |
| 16126 |
|
| 16127 |
<tip> |
| 16128 |
<para>To list a specific line of a text file, |
| 16129 |
<link linkend="piperef">pipe</link> the output of |
| 16130 |
<command>head</command> to <command>tail -n 1</command>. |
| 16131 |
For example <userinput>head -n 8 database.txt | tail |
| 16132 |
-n 1</userinput> lists the 8th line of the file |
| 16133 |
<filename>database.txt</filename>.</para> |
| 16134 |
<para>To set a variable to a given block of a text file: |
| 16135 |
<programlisting>var=$(head -n $m $filename | tail -n $n) |
| 16136 |
|
| 16137 |
# filename = name of file |
| 16138 |
# m = from beginning of file, number of lines to end of block |
| 16139 |
# n = number of lines to set variable to (trim from end of block)</programlisting></para> |
| 16140 |
</tip> |
| 16141 |
|
| 16142 |
<note> |
| 16143 |
<para>Newer implementations of <command>tail</command> |
| 16144 |
deprecate the older <command>tail -$LINES |
| 16145 |
filename</command> usage. The standard <command>tail -n $LINES |
| 16146 |
filename</command> is correct.</para> |
| 16147 |
</note> |
| 16148 |
|
| 16149 |
<para>See also <xref linkend="ex41">, <xref linkend="ex52"> and |
| 16150 |
<xref linkend="online">.</para> |
| 16151 |
|
| 16152 |
</listitem> |
| 16153 |
</varlistentry> |
| 16154 |
|
| 16155 |
<varlistentry> |
| 16156 |
<term><anchor id="grepref"><command>grep</command></term> |
| 16157 |
<indexterm> |
| 16158 |
<primary>grep</primary> |
| 16159 |
</indexterm> |
| 16160 |
<indexterm> |
| 16161 |
<primary>command</primary> |
| 16162 |
<secondary>grep</secondary> |
| 16163 |
</indexterm> |
| 16164 |
<listitem> |
| 16165 |
<para>A multi-purpose file search tool that uses |
| 16166 |
<link linkend="regexref">Regular Expressions</link>. |
| 16167 |
It was originally a command/filter in the |
| 16168 |
venerable <command>ed</command> line editor: |
| 16169 |
<userinput>g/re/p</userinput> -- <firstterm>global - |
| 16170 |
regular expression - print</firstterm>.</para> |
| 16171 |
|
| 16172 |
<para><cmdsynopsis> |
| 16173 |
<command>grep</command> <arg |
| 16174 |
choice="plain"><replaceable>pattern</replaceable></arg> |
| 16175 |
<arg choice="opt" |
| 16176 |
rep="repeat"><replaceable>file</replaceable></arg> |
| 16177 |
</cmdsynopsis>Search the target file(s) for |
| 16178 |
occurrences of <replaceable>pattern</replaceable>, where |
| 16179 |
<replaceable>pattern</replaceable> may be literal text |
| 16180 |
or a Regular Expression.</para> |
| 16181 |
|
| 16182 |
<para> |
| 16183 |
<screen><prompt>bash$ </prompt><userinput>grep '[rst]ystem.$' osinfo.txt</userinput> |
| 16184 |
<computeroutput>The GPL governs the distribution of the Linux operating system.</computeroutput> |
| 16185 |
</screen> |
| 16186 |
</para> |
| 16187 |
|
| 16188 |
<para>If no target file(s) specified, <command>grep</command> |
| 16189 |
works as a filter on <filename>stdout</filename>, as in |
| 16190 |
a <link linkend="piperef">pipe</link>.</para> |
| 16191 |
|
| 16192 |
<para> |
| 16193 |
<screen><prompt>bash$ </prompt><userinput>ps ax | grep clock</userinput> |
| 16194 |
<computeroutput>765 tty1 S 0:00 xclock |
| 16195 |
901 pts/1 S 0:00 grep clock</computeroutput> |
| 16196 |
</screen> |
| 16197 |
</para> |
| 16198 |
|
| 16199 |
<para>The <option>-i</option> option causes a case-insensitive |
| 16200 |
search.</para> |
| 16201 |
|
| 16202 |
<para>The <option>-w</option> option matches only whole |
| 16203 |
words.</para> |
| 16204 |
|
| 16205 |
<para>The <option>-l</option> option lists only the files in which |
| 16206 |
matches were found, but not the matching lines.</para> |
| 16207 |
|
| 16208 |
<para>The <option>-r</option> (recursive) option searches files in |
| 16209 |
the current working directory and all subdirectories below |
| 16210 |
it.</para> |
| 16211 |
|
| 16212 |
<para>The <option>-n</option> option lists the matching lines, |
| 16213 |
together with line numbers.</para> |
| 16214 |
|
| 16215 |
<para> |
| 16216 |
<screen><prompt>bash$ </prompt><userinput>grep -n Linux osinfo.txt</userinput> |
| 16217 |
<computeroutput>2:This is a file containing information about Linux. |
| 16218 |
6:The GPL governs the distribution of the Linux operating system.</computeroutput> |
| 16219 |
</screen> |
| 16220 |
</para> |
| 16221 |
|
| 16222 |
<para>The <option>-v</option> (or <option>--invert-match</option>) |
| 16223 |
option <firstterm>filters out</firstterm> matches. |
| 16224 |
<programlisting>grep pattern1 *.txt | grep -v pattern2 |
| 16225 |
|
| 16226 |
# Matches all lines in "*.txt" files containing "pattern1", |
| 16227 |
# but ***not*** "pattern2". |
| 16228 |
</programlisting></para> |
| 16229 |
|
| 16230 |
<para>The <option>-c</option> (<option>--count</option>) |
| 16231 |
option gives a numerical count of matches, rather than |
| 16232 |
actually listing the matches. |
| 16233 |
|
| 16234 |
<programlisting>grep -c txt *.sgml # (number of occurrences of "txt" in "*.sgml" files) |
| 16235 |
|
| 16236 |
|
| 16237 |
# grep -cz . |
| 16238 |
# ^ dot |
| 16239 |
# means count (-c) zero-separated (-z) items matching "." |
| 16240 |
# that is, non-empty ones (containing at least 1 character). |
| 16241 |
# |
| 16242 |
printf 'a b\nc d\n\n\n\n\n\000\n\000e\000\000\nf' | grep -cz . # 3 |
| 16243 |
printf 'a b\nc d\n\n\n\n\n\000\n\000e\000\000\nf' | grep -cz '$' # 5 |
| 16244 |
printf 'a b\nc d\n\n\n\n\n\000\n\000e\000\000\nf' | grep -cz '^' # 5 |
| 16245 |
# |
| 16246 |
printf 'a b\nc d\n\n\n\n\n\000\n\000e\000\000\nf' | grep -c '$' # 9 |
| 16247 |
# By default, newline chars (\n) separate items to match. |
| 16248 |
|
| 16249 |
# Note that the -z option is GNU "grep" specific. |
| 16250 |
|
| 16251 |
|
| 16252 |
# Thanks, S.C.</programlisting> |
| 16253 |
</para> |
| 16254 |
|
| 16255 |
<para>The <option>--color</option> (or <option>--colour</option>) |
| 16256 |
option marks the matching string in color (on the console |
| 16257 |
or in an <firstterm>xterm</firstterm> window). Since |
| 16258 |
<firstterm>grep</firstterm> prints out each entire line |
| 16259 |
containing the matching pattern, this lets you see exactly |
| 16260 |
<emphasis>what</emphasis> is being matched. See also |
| 16261 |
the <option>-o</option> option, which shows only the |
| 16262 |
matching portion of the line(s).</para> |
| 16263 |
|
| 16264 |
|
| 16265 |
<example id="fromsh"> |
| 16266 |
<title>Printing out the <firstterm>From</firstterm> lines in |
| 16267 |
stored e-mail messages</title> |
| 16268 |
<programlisting>&fromsh;</programlisting> |
| 16269 |
</example> |
| 16270 |
|
| 16271 |
<para>When invoked with more than one target file given, |
| 16272 |
<command>grep</command> specifies which file contains |
| 16273 |
matches.</para> |
| 16274 |
|
| 16275 |
<para> |
| 16276 |
<screen><prompt>bash$ </prompt><userinput>grep Linux osinfo.txt misc.txt</userinput> |
| 16277 |
<computeroutput>osinfo.txt:This is a file containing information about Linux. |
| 16278 |
osinfo.txt:The GPL governs the distribution of the Linux operating system. |
| 16279 |
misc.txt:The Linux operating system is steadily gaining in popularity.</computeroutput> |
| 16280 |
</screen> |
| 16281 |
</para> |
| 16282 |
|
| 16283 |
|
| 16284 |
<tip> |
| 16285 |
<para>To force <command>grep</command> to show the filename |
| 16286 |
when searching only one target file, simply give |
| 16287 |
<filename>/dev/null</filename> as the second file.</para> |
| 16288 |
<para> |
| 16289 |
<screen><prompt>bash$ </prompt><userinput>grep Linux osinfo.txt /dev/null</userinput> |
| 16290 |
<computeroutput>osinfo.txt:This is a file containing information about Linux. |
| 16291 |
osinfo.txt:The GPL governs the distribution of the Linux operating system.</computeroutput> |
| 16292 |
</screen> |
| 16293 |
</para> |
| 16294 |
</tip> |
| 16295 |
|
| 16296 |
|
| 16297 |
<para>If there is a successful match, <command>grep</command> |
| 16298 |
returns an <link linkend="exitstatusref">exit status</link> |
| 16299 |
of 0, which makes it useful in a condition test in a |
| 16300 |
script, especially in combination with the <option>-q</option> |
| 16301 |
option to suppress output. |
| 16302 |
<programlisting>SUCCESS=0 # if grep lookup succeeds |
| 16303 |
word=Linux |
| 16304 |
filename=data.file |
| 16305 |
|
| 16306 |
grep -q "$word" "$filename" # The "-q" option |
| 16307 |
#+ causes nothing to echo to stdout. |
| 16308 |
if [ $? -eq $SUCCESS ] |
| 16309 |
# if grep -q "$word" "$filename" can replace lines 5 - 7. |
| 16310 |
then |
| 16311 |
echo "$word found in $filename" |
| 16312 |
else |
| 16313 |
echo "$word not found in $filename" |
| 16314 |
fi</programlisting> |
| 16315 |
</para> |
| 16316 |
|
| 16317 |
|
| 16318 |
<para><xref linkend="online"> demonstrates how to use |
| 16319 |
<command>grep</command> to search for a word pattern in |
| 16320 |
a system logfile.</para> |
| 16321 |
|
| 16322 |
|
| 16323 |
<example id="grp"> |
| 16324 |
<title>Emulating <firstterm>grep</firstterm> in a script</title> |
| 16325 |
<programlisting>&grp;</programlisting> |
| 16326 |
</example> |
| 16327 |
|
| 16328 |
<para>How can <command>grep</command> search for two (or |
| 16329 |
more) separate patterns? What if you want |
| 16330 |
<command>grep</command> to display all lines in a file |
| 16331 |
or files that contain both <quote>pattern1</quote> |
| 16332 |
<emphasis>and</emphasis> <quote>pattern2</quote>?</para> |
| 16333 |
|
| 16334 |
<para>One method is to <link |
| 16335 |
linkend="piperef">pipe</link> the result of <command>grep |
| 16336 |
pattern1</command> to <command>grep pattern2</command>.</para> |
| 16337 |
|
| 16338 |
<para>For example, given the following file:</para> |
| 16339 |
|
| 16340 |
<para> |
| 16341 |
<programlisting># Filename: tstfile |
| 16342 |
|
| 16343 |
This is a sample file. |
| 16344 |
This is an ordinary text file. |
| 16345 |
This file does not contain any unusual text. |
| 16346 |
This file is not unusual. |
| 16347 |
Here is some text.</programlisting> |
| 16348 |
</para> |
| 16349 |
|
| 16350 |
<para>Now, let's search this file for lines containing |
| 16351 |
<emphasis>both</emphasis> <quote>file</quote> and |
| 16352 |
<quote>text</quote> . . . </para> |
| 16353 |
|
| 16354 |
<screen><prompt>bash$ </prompt><userinput>grep file tstfile</userinput> |
| 16355 |
<computeroutput># Filename: tstfile |
| 16356 |
This is a sample file. |
| 16357 |
This is an ordinary text file. |
| 16358 |
This file does not contain any unusual text. |
| 16359 |
This file is not unusual.</computeroutput> |
| 16360 |
|
| 16361 |
<prompt>bash$ </prompt><userinput>grep file tstfile | grep text</userinput> |
| 16362 |
<computeroutput>This is an ordinary text file. |
| 16363 |
This file does not contain any unusual text.</computeroutput></screen> |
| 16364 |
|
| 16365 |
<para>Now, for an interesting recreational use |
| 16366 |
of <firstterm>grep</firstterm> . . .</para> |
| 16367 |
|
| 16368 |
|
| 16369 |
<example id="cwsolver"> |
| 16370 |
<title>Crossword puzzle solver</title> |
| 16371 |
<programlisting>&cwsolver;</programlisting> |
| 16372 |
</example> |
| 16373 |
|
| 16374 |
|
| 16375 |
|
| 16376 |
<para><anchor id="egrepref"><command>egrep</command> |
| 16377 |
-- <firstterm>extended grep</firstterm> -- is the same |
| 16378 |
as <command>grep -E</command>. This uses a somewhat |
| 16379 |
different, extended set of <link linkend="regexref">Regular |
| 16380 |
Expressions</link>, which can make the search a bit more |
| 16381 |
flexible. It also allows the boolean | |
| 16382 |
(<firstterm>or</firstterm>) operator. |
| 16383 |
<screen><prompt>bash $ </prompt><userinput>egrep 'matches|Matches' file.txt</userinput> |
| 16384 |
<computeroutput>Line 1 matches. |
| 16385 |
Line 3 Matches. |
| 16386 |
Line 4 contains matches, but also Matches</computeroutput> |
| 16387 |
</screen> |
| 16388 |
</para> |
| 16389 |
|
| 16390 |
<para><anchor id="fgrepref"><command>fgrep</command> -- |
| 16391 |
<firstterm>fast grep</firstterm> -- is the same as |
| 16392 |
<command>grep -F</command>. It does a literal string search |
| 16393 |
(no <link linkend="regexref">Regular Expressions</link>), |
| 16394 |
which generally speeds things up a bit.</para> |
| 16395 |
|
| 16396 |
<note><para>On some Linux distros, <command>egrep</command> and |
| 16397 |
<command>fgrep</command> are symbolic links to, or aliases for |
| 16398 |
<command>grep</command>, but invoked with the |
| 16399 |
<option>-E</option> and <option>-F</option> options, |
| 16400 |
respectively.</para></note> |
| 16401 |
|
| 16402 |
<example id="dictlookup"> |
| 16403 |
<title>Looking up definitions in <citetitle |
| 16404 |
pubwork="book">Webster's 1913 Dictionary</citetitle></title> |
| 16405 |
<programlisting>&dictlookup;</programlisting> |
| 16406 |
</example> |
| 16407 |
|
| 16408 |
<note><para>See also <xref linkend="qky"> for an example |
| 16409 |
of speedy <firstterm>fgrep</firstterm> lookup on a large |
| 16410 |
text file.</para></note> |
| 16411 |
|
| 16412 |
<para><anchor id="agrepref"></para> |
| 16413 |
<para><command>agrep</command> (<firstterm>approximate |
| 16414 |
grep</firstterm>) extends the capabilities of |
| 16415 |
<command>grep</command> to approximate matching. The search |
| 16416 |
string may differ by a specified number of characters |
| 16417 |
from the resulting matches. This utility is not part of |
| 16418 |
the core Linux distribution.</para> |
| 16419 |
|
| 16420 |
|
| 16421 |
<para><anchor id="zegrepref"></para> |
| 16422 |
<tip> <para>To search compressed files, use |
| 16423 |
<command>zgrep</command>, <command>zegrep</command>, or |
| 16424 |
<command>zfgrep</command>. These also work on non-compressed |
| 16425 |
files, though slower than plain <command>grep</command>, |
| 16426 |
<command>egrep</command>, <command>fgrep</command>. |
| 16427 |
They are handy for searching through a mixed set of files, |
| 16428 |
some compressed, some not.</para> |
| 16429 |
<para><anchor id="bzgrepref"></para> |
| 16430 |
<para>To search <link linkend="bzipref">bzipped</link> |
| 16431 |
files, use <command>bzgrep</command>.</para> </tip> |
| 16432 |
|
| 16433 |
</listitem> |
| 16434 |
</varlistentry> |
| 16435 |
|
| 16436 |
|
| 16437 |
<varlistentry> |
| 16438 |
<term><anchor id="lookref"><command>look</command></term> |
| 16439 |
<indexterm> |
| 16440 |
<primary>look</primary> |
| 16441 |
</indexterm> |
| 16442 |
<indexterm> |
| 16443 |
<primary>command</primary> |
| 16444 |
<secondary>look</secondary> |
| 16445 |
</indexterm> |
| 16446 |
<listitem> |
| 16447 |
<para>The command <command>look</command> works like |
| 16448 |
<command>grep</command>, but does a lookup on |
| 16449 |
a <quote>dictionary,</quote> a sorted word list. |
| 16450 |
By default, <command>look</command> searches for a match |
| 16451 |
in <filename>/usr/dict/words</filename>, but a different |
| 16452 |
dictionary file may be specified.</para> |
| 16453 |
|
| 16454 |
<example id="lookup"> |
| 16455 |
<title>Checking words in a list for validity</title> |
| 16456 |
<programlisting>&lookup;</programlisting> |
| 16457 |
</example> |
| 16458 |
|
| 16459 |
</listitem> |
| 16460 |
</varlistentry> |
| 16461 |
|
| 16462 |
|
| 16463 |
<varlistentry> |
| 16464 |
<term><command>sed</command></term> |
| 16465 |
<term><command>awk</command></term> |
| 16466 |
<indexterm> |
| 16467 |
<primary>sed</primary> |
| 16468 |
</indexterm> |
| 16469 |
<indexterm> |
| 16470 |
<primary>command</primary> |
| 16471 |
<secondary>sed</secondary> |
| 16472 |
</indexterm> |
| 16473 |
<indexterm> |
| 16474 |
<primary>awk</primary> |
| 16475 |
</indexterm> |
| 16476 |
<indexterm> |
| 16477 |
<primary>command</primary> |
| 16478 |
<secondary>awk</secondary> |
| 16479 |
</indexterm> |
| 16480 |
<listitem> |
| 16481 |
<para>Scripting languages especially suited for parsing text |
| 16482 |
files and command output. May be embedded singly or in |
| 16483 |
combination in pipes and shell scripts.</para> |
| 16484 |
</listitem> |
| 16485 |
</varlistentry> |
| 16486 |
|
| 16487 |
<varlistentry> |
| 16488 |
<term><command><link linkend="sedref">sed</link></command></term> |
| 16489 |
<listitem> |
| 16490 |
<para>Non-interactive <quote>stream editor</quote>, permits using |
| 16491 |
many <command>ex</command> commands in <link |
| 16492 |
linkend="batchprocref">batch</link> mode. It finds many |
| 16493 |
uses in shell scripts.</para> |
| 16494 |
</listitem> |
| 16495 |
</varlistentry> |
| 16496 |
|
| 16497 |
<varlistentry> |
| 16498 |
<term><command><link linkend="awkref">awk</link></command></term> |
| 16499 |
<listitem> |
| 16500 |
<para>Programmable file extractor and formatter, good for |
| 16501 |
manipulating and/or extracting <link |
| 16502 |
linkend="fieldref">fields</link> (columns) in structured |
| 16503 |
text files. Its syntax is similar to C.</para> |
| 16504 |
</listitem> |
| 16505 |
</varlistentry> |
| 16506 |
|
| 16507 |
<varlistentry> |
| 16508 |
<term><anchor id="wcref"><command>wc</command></term> |
| 16509 |
<indexterm> |
| 16510 |
<primary>wc</primary> |
| 16511 |
</indexterm> |
| 16512 |
<indexterm> |
| 16513 |
<primary>command</primary> |
| 16514 |
<secondary>wc</secondary> |
| 16515 |
</indexterm> |
| 16516 |
<listitem> |
| 16517 |
|
| 16518 |
<para><firstterm>wc</firstterm> gives a <quote>word |
| 16519 |
count</quote> on a file or I/O stream: |
| 16520 |
|
| 16521 |
<screen><prompt>bash $ </prompt><userinput>wc /usr/share/doc/sed-4.1.2/README</userinput> |
| 16522 |
<computeroutput>13 70 447 README</computeroutput> |
| 16523 |
[13 lines 70 words 447 characters]</screen></para> |
| 16524 |
|
| 16525 |
<para><userinput>wc -w</userinput> gives only the word count.</para> |
| 16526 |
<para><userinput>wc -l</userinput> gives only the line count.</para> |
| 16527 |
<para><userinput>wc -c</userinput> gives only the byte count.</para> |
| 16528 |
<para><userinput>wc -m</userinput> gives only the character count.</para> |
| 16529 |
<para><userinput>wc -L</userinput> gives only the length of the longest line.</para> |
| 16530 |
|
| 16531 |
<para>Using <command>wc</command> to count how many |
| 16532 |
<filename>.txt</filename> files are in current working directory: |
| 16533 |
<programlisting>$ ls *.txt | wc -l |
| 16534 |
# Will work as long as none of the "*.txt" files |
| 16535 |
#+ have a linefeed embedded in their name. |
| 16536 |
|
| 16537 |
# Alternative ways of doing this are: |
| 16538 |
# find . -maxdepth 1 -name \*.txt -print0 | grep -cz . |
| 16539 |
# (shopt -s nullglob; set -- *.txt; echo $#) |
| 16540 |
|
| 16541 |
# Thanks, S.C.</programlisting> |
| 16542 |
</para> |
| 16543 |
|
| 16544 |
<para>Using <command>wc</command> to total up the size of all the |
| 16545 |
files whose names begin with letters in the range d - h |
| 16546 |
<screen><prompt>bash$ </prompt><userinput>wc [d-h]* | grep total | awk '{print $3}'</userinput> |
| 16547 |
<computeroutput>71832</computeroutput> |
| 16548 |
</screen> |
| 16549 |
</para> |
| 16550 |
|
| 16551 |
<para>Using <command>wc</command> to count the instances of the |
| 16552 |
word <quote>Linux</quote> in the main source file for |
| 16553 |
this book. |
| 16554 |
<screen><prompt>bash$ </prompt><userinput>grep Linux abs-book.sgml | wc -l</userinput> |
| 16555 |
<computeroutput>138</computeroutput> |
| 16556 |
</screen> |
| 16557 |
</para> |
| 16558 |
|
| 16559 |
<para>See also <xref linkend="ex52"> and <xref |
| 16560 |
linkend="redir4">.</para> |
| 16561 |
|
| 16562 |
<para>Certain commands include some of the |
| 16563 |
functionality of <command>wc</command> as options. |
| 16564 |
|
| 16565 |
<programlisting>... | grep foo | wc -l |
| 16566 |
# This frequently used construct can be more concisely rendered. |
| 16567 |
|
| 16568 |
... | grep -c foo |
| 16569 |
# Just use the "-c" (or "--count") option of grep. |
| 16570 |
|
| 16571 |
# Thanks, S.C.</programlisting></para> |
| 16572 |
|
| 16573 |
</listitem> |
| 16574 |
</varlistentry> |
| 16575 |
|
| 16576 |
|
| 16577 |
<varlistentry> |
| 16578 |
<term><anchor id="trref"><command>tr</command></term> |
| 16579 |
<indexterm> |
| 16580 |
<primary>tr</primary> |
| 16581 |
</indexterm> |
| 16582 |
<indexterm> |
| 16583 |
<primary>command</primary> |
| 16584 |
<secondary>tr</secondary> |
| 16585 |
</indexterm> |
| 16586 |
<listitem> |
| 16587 |
<para>character translation filter.</para> |
| 16588 |
|
| 16589 |
<caution><para><link linkend="ucref">Must use quoting and/or |
| 16590 |
brackets</link>, as appropriate. Quotes prevent the |
| 16591 |
shell from reinterpreting the special characters in |
| 16592 |
<command>tr</command> command sequences. Brackets should be |
| 16593 |
quoted to prevent expansion by the shell. </para></caution> |
| 16594 |
|
| 16595 |
<para>Either <userinput>tr "A-Z" "*" <filename</userinput> |
| 16596 |
or <userinput>tr A-Z \* <filename</userinput> changes |
| 16597 |
all the uppercase letters in <filename>filename</filename> |
| 16598 |
to asterisks (writes to <filename>stdout</filename>). |
| 16599 |
On some systems this may not work, but <userinput>tr A-Z |
| 16600 |
'[**]'</userinput> will.</para> |
| 16601 |
|
| 16602 |
<para><anchor id="troptions"></para> |
| 16603 |
<para>The <option>-d</option> option deletes a range of |
| 16604 |
characters. |
| 16605 |
<programlisting>echo "abcdef" # abcdef |
| 16606 |
echo "abcdef" | tr -d b-d # aef |
| 16607 |
|
| 16608 |
|
| 16609 |
tr -d 0-9 <filename |
| 16610 |
# Deletes all digits from the file "filename".</programlisting></para> |
| 16611 |
|
| 16612 |
<para>The <option>--squeeze-repeats</option> (or |
| 16613 |
<option>-s</option>) option deletes all but the |
| 16614 |
first instance of a string of consecutive characters. |
| 16615 |
This option is useful for removing excess <link |
| 16616 |
linkend="whitespaceref">whitespace</link>. |
| 16617 |
|
| 16618 |
|
| 16619 |
|
| 16620 |
<screen><prompt>bash$ </prompt><userinput>echo "XXXXX" | tr --squeeze-repeats 'X'</userinput> |
| 16621 |
<computeroutput>X</computeroutput></screen></para> |
| 16622 |
|
| 16623 |
<para>The <option>-c</option> <quote>complement</quote> |
| 16624 |
option <firstterm>inverts</firstterm> the character set to |
| 16625 |
match. With this option, <command>tr</command> acts only |
| 16626 |
upon those characters <emphasis>not</emphasis> matching |
| 16627 |
the specified set.</para> |
| 16628 |
|
| 16629 |
<para> |
| 16630 |
<screen><prompt>bash$ </prompt><userinput>echo "acfdeb123" | tr -c b-d +</userinput> |
| 16631 |
<computeroutput>+c+d+b++++</computeroutput></screen> |
| 16632 |
</para> |
| 16633 |
|
| 16634 |
|
| 16635 |
|
| 16636 |
<para>Note that <command>tr</command> recognizes <link |
| 16637 |
linkend="posixref">POSIX character classes</link>. |
| 16638 |
<footnote><para>This is only true of the GNU version of |
| 16639 |
<command>tr</command>, not the generic version often found on |
| 16640 |
commercial UNIX systems.</para></footnote> |
| 16641 |
</para> |
| 16642 |
|
| 16643 |
<para> |
| 16644 |
<screen><prompt>bash$ </prompt><userinput>echo "abcd2ef1" | tr '[:alpha:]' -</userinput> |
| 16645 |
<computeroutput>----2--1</computeroutput> |
| 16646 |
</screen> |
| 16647 |
</para> |
| 16648 |
|
| 16649 |
<example id="ex49"> |
| 16650 |
<title><firstterm>toupper</firstterm>: Transforms a file |
| 16651 |
to all uppercase.</title> |
| 16652 |
<programlisting>&ex49;</programlisting> |
| 16653 |
</example> |
| 16654 |
|
| 16655 |
|
| 16656 |
<example id="lowercase"> |
| 16657 |
<title><firstterm>lowercase</firstterm>: Changes all |
| 16658 |
filenames in working directory to lowercase.</title> |
| 16659 |
<programlisting>&lowercase;</programlisting> |
| 16660 |
</example> |
| 16661 |
|
| 16662 |
<para><anchor id="trd2u"></para> |
| 16663 |
<example id="du"> |
| 16664 |
<title><firstterm>du</firstterm>: DOS to UNIX text file conversion.</title> |
| 16665 |
<programlisting>&du;</programlisting> |
| 16666 |
</example> |
| 16667 |
|
| 16668 |
<example id="rot13"> |
| 16669 |
<title><firstterm>rot13</firstterm>: ultra-weak encryption.</title> |
| 16670 |
<programlisting>&rot13;</programlisting> |
| 16671 |
</example> |
| 16672 |
|
| 16673 |
<example id="cryptoquote"> |
| 16674 |
<title>Generating <quote>Crypto-Quote</quote> Puzzles</title> |
| 16675 |
<programlisting>&cryptoquote;</programlisting> |
| 16676 |
</example> |
| 16677 |
|
| 16678 |
<para><anchor id="jabh">Of course, <firstterm>tr</firstterm> |
| 16679 |
lends itself to <firstterm>code |
| 16680 |
obfuscation</firstterm>.</para> |
| 16681 |
|
| 16682 |
<para><programlisting>#!/bin/bash |
| 16683 |
# jabh.sh |
| 16684 |
|
| 16685 |
x="wftedskaebjgdBstbdbsmnjgz" |
| 16686 |
echo $x | tr "a-z" 'oh, turtleneck Phrase Jar!' |
| 16687 |
|
| 16688 |
# Based on the Wikipedia "Just another Perl hacker" article.</programlisting></para> |
| 16689 |
|
| 16690 |
|
| 16691 |
<para><anchor id="trvariants"></para> |
| 16692 |
<sidebar><title><firstterm>tr</firstterm> variants</title> |
| 16693 |
<para> |
| 16694 |
The <command>tr</command> utility has two historic |
| 16695 |
variants. The BSD version does not use brackets |
| 16696 |
(<userinput>tr a-z A-Z</userinput>), but the SysV one does |
| 16697 |
(<userinput>tr '[a-z]' '[A-Z]'</userinput>). The GNU version |
| 16698 |
of <command>tr</command> resembles the BSD one. |
| 16699 |
</para> |
| 16700 |
</sidebar> |
| 16701 |
|
| 16702 |
</listitem> |
| 16703 |
</varlistentry> |
| 16704 |
|
| 16705 |
<varlistentry> |
| 16706 |
<term><anchor id="foldref"><command>fold</command></term> |
| 16707 |
<indexterm> |
| 16708 |
<primary>fold</primary> |
| 16709 |
</indexterm> |
| 16710 |
<indexterm> |
| 16711 |
<primary>command</primary> |
| 16712 |
<secondary>fold</secondary> |
| 16713 |
</indexterm> |
| 16714 |
<listitem> |
| 16715 |
<para>A filter that wraps lines of input to a specified width. |
| 16716 |
This is especially useful with the <option>-s</option> |
| 16717 |
option, which breaks lines at word spaces (see <xref |
| 16718 |
linkend="ex50"> and <xref linkend="mailformat">).</para> |
| 16719 |
</listitem> |
| 16720 |
</varlistentry> |
| 16721 |
|
| 16722 |
<varlistentry> |
| 16723 |
<term><anchor id="fmtref"><command>fmt</command></term> |
| 16724 |
<indexterm> |
| 16725 |
<primary>fmt</primary> |
| 16726 |
</indexterm> |
| 16727 |
<indexterm> |
| 16728 |
<primary>command</primary> |
| 16729 |
<secondary>fmt</secondary> |
| 16730 |
</indexterm> |
| 16731 |
<listitem> |
| 16732 |
<para>Simple-minded file formatter, used as a filter in a |
| 16733 |
pipe to <quote>wrap</quote> long lines of text |
| 16734 |
output.</para> |
| 16735 |
|
| 16736 |
<example id="ex50"> |
| 16737 |
<title>Formatted file listing.</title> |
| 16738 |
<programlisting>&ex50;</programlisting> |
| 16739 |
</example> |
| 16740 |
|
| 16741 |
<para>See also <xref linkend="ex41">.</para> |
| 16742 |
|
| 16743 |
<tip><para>A powerful alternative to <command>fmt</command> is |
| 16744 |
Kamil Toman's <command>par</command> |
| 16745 |
utility, available from <ulink |
| 16746 |
url="http://www.cs.berkeley.edu/~amc/Par/">http://www.cs.berkeley.edu/~amc/Par/</ulink>. |
| 16747 |
</para></tip> |
| 16748 |
|
| 16749 |
</listitem> |
| 16750 |
</varlistentry> |
| 16751 |
|
| 16752 |
<varlistentry> |
| 16753 |
<term><anchor id="colref"><command>col</command></term> |
| 16754 |
<indexterm> |
| 16755 |
<primary>col</primary> |
| 16756 |
</indexterm> |
| 16757 |
<indexterm> |
| 16758 |
<primary>command</primary> |
| 16759 |
<secondary>reverse line feed</secondary> |
| 16760 |
</indexterm> |
| 16761 |
<listitem> |
| 16762 |
<para>This deceptively named filter removes reverse line feeds |
| 16763 |
from an input stream. It also attempts to replace |
| 16764 |
whitespace with equivalent tabs. The chief use of |
| 16765 |
<command>col</command> is in filtering the output |
| 16766 |
from certain text processing utilities, such as |
| 16767 |
<command>groff</command> and <command>tbl</command>.</para> |
| 16768 |
</listitem> |
| 16769 |
</varlistentry> |
| 16770 |
|
| 16771 |
<varlistentry> |
| 16772 |
<term><anchor id="columnref"><command>column</command></term> |
| 16773 |
<indexterm> |
| 16774 |
<primary>column</primary> |
| 16775 |
</indexterm> |
| 16776 |
<indexterm> |
| 16777 |
<primary>command</primary> |
| 16778 |
<secondary>column</secondary> |
| 16779 |
</indexterm> |
| 16780 |
<listitem> |
| 16781 |
<para>Column formatter. This filter transforms list-type |
| 16782 |
text output into a <quote>pretty-printed</quote> table |
| 16783 |
by inserting tabs at appropriate places.</para> |
| 16784 |
|
| 16785 |
<example id="col"> |
| 16786 |
<title>Using <firstterm>column</firstterm> to format a directory |
| 16787 |
listing</title> |
| 16788 |
<programlisting>&colm;</programlisting> |
| 16789 |
</example> |
| 16790 |
|
| 16791 |
</listitem> |
| 16792 |
</varlistentry> |
| 16793 |
|
| 16794 |
<varlistentry> |
| 16795 |
<term><anchor id="colrmref"><command>colrm</command></term> |
| 16796 |
<indexterm> |
| 16797 |
<primary>colrm</primary> |
| 16798 |
</indexterm> |
| 16799 |
<indexterm> |
| 16800 |
<primary>command</primary> |
| 16801 |
<secondary>colrm</secondary> |
| 16802 |
</indexterm> |
| 16803 |
<listitem> |
| 16804 |
<para>Column removal filter. This removes columns (characters) |
| 16805 |
from a file and writes the file, lacking the range of |
| 16806 |
specified columns, back to <filename>stdout</filename>. |
| 16807 |
<userinput>colrm 2 4 <filename</userinput> removes the |
| 16808 |
second through fourth characters from each line of the |
| 16809 |
text file <filename>filename</filename>.</para> |
| 16810 |
<caution><para>If the file contains tabs or nonprintable |
| 16811 |
characters, this may cause unpredictable |
| 16812 |
behavior. In such cases, consider using |
| 16813 |
<link linkend="expandref">expand</link> and |
| 16814 |
<command>unexpand</command> in a pipe preceding |
| 16815 |
<command>colrm</command>.</para></caution> |
| 16816 |
</listitem> |
| 16817 |
</varlistentry> |
| 16818 |
|
| 16819 |
<varlistentry> |
| 16820 |
<term><anchor id="nlref"><command>nl</command></term> |
| 16821 |
<indexterm> |
| 16822 |
<primary>nl</primary> |
| 16823 |
</indexterm> |
| 16824 |
<indexterm> |
| 16825 |
<primary>command</primary> |
| 16826 |
<secondary>fmt</secondary> |
| 16827 |
</indexterm> |
| 16828 |
<listitem> |
| 16829 |
<para>Line numbering filter: <userinput>nl filename</userinput> |
| 16830 |
lists <filename>filename</filename> to |
| 16831 |
<filename>stdout</filename>, but inserts consecutive |
| 16832 |
numbers at the beginning of each non-blank line. If |
| 16833 |
<filename>filename</filename> omitted, operates on |
| 16834 |
<filename>stdin.</filename></para> |
| 16835 |
|
| 16836 |
<para>The output of <command>nl</command> is very similar to |
| 16837 |
<userinput>cat -b</userinput>, since, by default |
| 16838 |
<command>nl</command> does not list blank lines.</para> |
| 16839 |
|
| 16840 |
<example id="lnum"> |
| 16841 |
<title><firstterm>nl</firstterm>: A self-numbering script.</title> |
| 16842 |
<programlisting>&lnum;</programlisting> |
| 16843 |
</example> |
| 16844 |
|
| 16845 |
</listitem> |
| 16846 |
</varlistentry> |
| 16847 |
|
| 16848 |
|
| 16849 |
<varlistentry> |
| 16850 |
<term><anchor id="prref"><command>pr</command></term> |
| 16851 |
<indexterm> |
| 16852 |
<primary>pr</primary> |
| 16853 |
</indexterm> |
| 16854 |
<indexterm> |
| 16855 |
<primary>command</primary> |
| 16856 |
<secondary>pr</secondary> |
| 16857 |
</indexterm> |
| 16858 |
<listitem> |
| 16859 |
<para>Print formatting filter. This will paginate files |
| 16860 |
(or <filename>stdout</filename>) into sections suitable for |
| 16861 |
hard copy printing or viewing on screen. Various options |
| 16862 |
permit row and column manipulation, joining lines, setting |
| 16863 |
margins, numbering lines, adding page headers, and merging |
| 16864 |
files, among other things. The <command>pr</command> |
| 16865 |
command combines much of the functionality of |
| 16866 |
<command>nl</command>, <command>paste</command>, |
| 16867 |
<command>fold</command>, <command>column</command>, and |
| 16868 |
<command>expand</command>.</para> |
| 16869 |
|
| 16870 |
<para><userinput>pr -o 5 --width=65 fileZZZ | more</userinput> |
| 16871 |
gives a nice paginated listing to screen of |
| 16872 |
<filename>fileZZZ</filename> with margins set at 5 and |
| 16873 |
65.</para> |
| 16874 |
|
| 16875 |
<para>A particularly useful option is <option>-d</option>, |
| 16876 |
forcing double-spacing (same effect as <command>sed |
| 16877 |
-G</command>).</para> |
| 16878 |
|
| 16879 |
</listitem> |
| 16880 |
</varlistentry> |
| 16881 |
|
| 16882 |
<varlistentry> |
| 16883 |
<term><anchor id="gettextref"><command>gettext</command></term> |
| 16884 |
<indexterm> |
| 16885 |
<primary>gettext</primary> |
| 16886 |
</indexterm> |
| 16887 |
<indexterm> |
| 16888 |
<primary>command</primary> |
| 16889 |
<secondary>localization</secondary> |
| 16890 |
</indexterm> |
| 16891 |
<listitem> |
| 16892 |
|
| 16893 |
<para>The GNU <command>gettext</command> package is a set of |
| 16894 |
utilities for <link linkend="localization">localizing</link> |
| 16895 |
and translating the text output of programs into foreign |
| 16896 |
languages. While originally intended for C programs, it |
| 16897 |
now supports quite a number of programming and scripting |
| 16898 |
languages.</para> |
| 16899 |
|
| 16900 |
<para>The <command>gettext</command> |
| 16901 |
<emphasis>program</emphasis> works on shell scripts. See |
| 16902 |
the <replaceable>info page</replaceable>.</para> |
| 16903 |
|
| 16904 |
</listitem> |
| 16905 |
</varlistentry> |
| 16906 |
|
| 16907 |
<varlistentry> |
| 16908 |
<term><anchor id="msgfmtref"><command>msgfmt</command></term> |
| 16909 |
<indexterm> |
| 16910 |
<primary>msgfmt</primary> |
| 16911 |
</indexterm> |
| 16912 |
<indexterm> |
| 16913 |
<primary>command</primary> |
| 16914 |
<secondary>localization</secondary> |
| 16915 |
</indexterm> |
| 16916 |
<listitem> |
| 16917 |
<para>A program for generating binary |
| 16918 |
message catalogs. It is used for <link |
| 16919 |
linkend="localization">localization</link>.</para> |
| 16920 |
</listitem> |
| 16921 |
</varlistentry> |
| 16922 |
|
| 16923 |
<varlistentry> |
| 16924 |
<term><anchor id="iconvref"><command>iconv</command></term> |
| 16925 |
<indexterm> |
| 16926 |
<primary>iconv</primary> |
| 16927 |
</indexterm> |
| 16928 |
<indexterm> |
| 16929 |
<primary>command</primary> |
| 16930 |
<secondary>encoding</secondary> |
| 16931 |
</indexterm> |
| 16932 |
<listitem> |
| 16933 |
|
| 16934 |
<para>A utility for converting file(s) to a different encoding |
| 16935 |
(character set). Its chief use is for <link |
| 16936 |
linkend="localization">localization</link>.</para> |
| 16937 |
|
| 16938 |
<para> |
| 16939 |
<programlisting># Convert a string from UTF-8 to UTF-16 and print to the BookList |
| 16940 |
function write_utf8_string { |
| 16941 |
STRING=$1 |
| 16942 |
BOOKLIST=$2 |
| 16943 |
echo -n "$STRING" | iconv -f UTF8 -t UTF16 | \ |
| 16944 |
cut -b 3- | tr -d \\n >> "$BOOKLIST" |
| 16945 |
} |
| 16946 |
|
| 16947 |
# From Peter Knowles' "booklistgen.sh" script |
| 16948 |
#+ for converting files to Sony Librie/PRS-50X format. |
| 16949 |
# (http://booklistgensh.peterknowles.com)</programlisting> |
| 16950 |
</para> |
| 16951 |
|
| 16952 |
</listitem> |
| 16953 |
</varlistentry> |
| 16954 |
|
| 16955 |
<varlistentry> |
| 16956 |
<term><anchor id="recoderef"><command>recode</command></term> |
| 16957 |
<indexterm> |
| 16958 |
<primary>recode</primary> |
| 16959 |
</indexterm> |
| 16960 |
<indexterm> |
| 16961 |
<primary>command</primary> |
| 16962 |
<secondary>encoding</secondary> |
| 16963 |
</indexterm> |
| 16964 |
<listitem> |
| 16965 |
<para>Consider this a fancier version of |
| 16966 |
<command>iconv</command>, above. This very versatile utility |
| 16967 |
for converting a file to a different encoding scheme. |
| 16968 |
Note that <firstterm>recode</firstterm> is not part of the |
| 16969 |
standard Linux installation.</para> |
| 16970 |
</listitem> |
| 16971 |
</varlistentry> |
| 16972 |
|
| 16973 |
<varlistentry> |
| 16974 |
<term><anchor id="texref"><command>TeX</command></term> |
| 16975 |
<term><anchor id="gsref"><command>gs</command></term> |
| 16976 |
<indexterm> |
| 16977 |
<primary>TeX</primary> |
| 16978 |
</indexterm> |
| 16979 |
<indexterm> |
| 16980 |
<primary>command</primary> |
| 16981 |
<secondary>TeX</secondary> |
| 16982 |
</indexterm> |
| 16983 |
<indexterm> |
| 16984 |
<primary>gs</primary> |
| 16985 |
</indexterm> |
| 16986 |
<indexterm> |
| 16987 |
<primary>command</primary> |
| 16988 |
<secondary>Postscript</secondary> |
| 16989 |
</indexterm> |
| 16990 |
<listitem> |
| 16991 |
|
| 16992 |
<para><command>TeX</command> and <command>Postscript</command> |
| 16993 |
are text markup languages used for preparing copy for |
| 16994 |
printing or formatted video display.</para> |
| 16995 |
|
| 16996 |
<para><command>TeX</command> is Donald Knuth's elaborate |
| 16997 |
typsetting system. It is often convenient to write a |
| 16998 |
shell script encapsulating all the options and arguments |
| 16999 |
passed to one of these markup languages.</para> |
| 17000 |
|
| 17001 |
<para><firstterm>Ghostscript</firstterm> |
| 17002 |
(<command>gs</command>) is a GPL-ed Postscript |
| 17003 |
interpreter.</para> |
| 17004 |
|
| 17005 |
</listitem> |
| 17006 |
</varlistentry> |
| 17007 |
|
| 17008 |
<varlistentry> |
| 17009 |
<term><anchor id="texexecref"><command>texexec</command></term> |
| 17010 |
<indexterm> |
| 17011 |
<primary>texexec</primary> |
| 17012 |
</indexterm> |
| 17013 |
<indexterm> |
| 17014 |
<primary>command</primary> |
| 17015 |
<secondary>pdf</secondary> |
| 17016 |
</indexterm> |
| 17017 |
<listitem> |
| 17018 |
|
| 17019 |
<para>Utility for processing <firstterm>TeX</firstterm> and |
| 17020 |
<firstterm>pdf</firstterm> files. Found in |
| 17021 |
<filename class="directory">/usr/bin</filename> |
| 17022 |
on many Linux distros, it is actually a <link |
| 17023 |
linkend="shwrapper">shell wrapper</link> that |
| 17024 |
calls <link linkend="perlref">Perl</link> to invoke |
| 17025 |
<firstterm>Tex</firstterm>.</para> |
| 17026 |
|
| 17027 |
<para> |
| 17028 |
<programlisting>texexec --pdfarrange --result=Concatenated.pdf *pdf |
| 17029 |
|
| 17030 |
# Concatenates all the pdf files in the current working directory |
| 17031 |
#+ into the merged file, Concatenated.pdf . . . |
| 17032 |
# (The --pdfarrange option repaginates a pdf file. See also --pdfcombine.) |
| 17033 |
# The above command-line could be parameterized and put into a shell script.</programlisting> |
| 17034 |
</para> |
| 17035 |
</listitem> |
| 17036 |
</varlistentry> |
| 17037 |
|
| 17038 |
<varlistentry> |
| 17039 |
<term><anchor id="enscriptref"><command>enscript</command></term> |
| 17040 |
<indexterm> |
| 17041 |
<primary>enscript</primary> |
| 17042 |
</indexterm> |
| 17043 |
<indexterm> |
| 17044 |
<primary>command</primary> |
| 17045 |
<secondary>PostScript</secondary> |
| 17046 |
</indexterm> |
| 17047 |
<listitem> |
| 17048 |
<para>Utility for converting plain text file to PostScript</para> |
| 17049 |
<para>For example, <command>enscript filename.txt -p filename.ps</command> |
| 17050 |
produces the PostScript output file |
| 17051 |
<filename>filename.ps</filename>.</para> |
| 17052 |
</listitem> |
| 17053 |
</varlistentry> |
| 17054 |
|
| 17055 |
<varlistentry> |
| 17056 |
<term><anchor id="groffref"><command>groff</command></term> |
| 17057 |
<term><anchor id="tblref"><command>tbl</command></term> |
| 17058 |
<term><anchor id="eqnref"><command>eqn</command></term> |
| 17059 |
<indexterm> |
| 17060 |
<primary>groff</primary> |
| 17061 |
</indexterm> |
| 17062 |
<indexterm> |
| 17063 |
<primary>command</primary> |
| 17064 |
<secondary>groff</secondary> |
| 17065 |
</indexterm> |
| 17066 |
<indexterm> |
| 17067 |
<primary>tbl</primary> |
| 17068 |
</indexterm> |
| 17069 |
<indexterm> |
| 17070 |
<primary>command</primary> |
| 17071 |
<secondary>table</secondary> |
| 17072 |
</indexterm> |
| 17073 |
<indexterm> |
| 17074 |
<primary>eqn</primary> |
| 17075 |
</indexterm> |
| 17076 |
<indexterm> |
| 17077 |
<primary>command</primary> |
| 17078 |
<secondary>equation</secondary> |
| 17079 |
</indexterm> |
| 17080 |
<listitem> |
| 17081 |
|
| 17082 |
<para>Yet another text markup and display formatting language |
| 17083 |
is <command>groff</command>. This is the enhanced GNU version |
| 17084 |
of the venerable UNIX <command>roff/troff</command> display |
| 17085 |
and typesetting package. <link linkend="manref">Manpages</link> |
| 17086 |
use <command>groff</command>.</para> |
| 17087 |
|
| 17088 |
<para>The <command>tbl</command> table processing utility |
| 17089 |
is considered part of <command>groff</command>, as its |
| 17090 |
function is to convert table markup into |
| 17091 |
<command>groff</command> commands.</para> |
| 17092 |
|
| 17093 |
<para>The <command>eqn</command> equation processing utility |
| 17094 |
is likewise part of <command>groff</command>, and |
| 17095 |
its function is to convert equation markup into |
| 17096 |
<command>groff</command> commands.</para> |
| 17097 |
|
| 17098 |
<example id="manview"> |
| 17099 |
<title><firstterm>manview</firstterm>: Viewing formatted manpages</title> |
| 17100 |
<programlisting>&manview;</programlisting> |
| 17101 |
</example> |
| 17102 |
|
| 17103 |
<para>See also <xref linkend="maned">.</para> |
| 17104 |
|
| 17105 |
</listitem> |
| 17106 |
</varlistentry> |
| 17107 |
|
| 17108 |
|
| 17109 |
<varlistentry> |
| 17110 |
<term><anchor id="lexref"><command>lex</command></term> |
| 17111 |
<term><anchor id="yaccref"><command>yacc</command></term> |
| 17112 |
<indexterm> |
| 17113 |
<primary>lex</primary> |
| 17114 |
</indexterm> |
| 17115 |
<indexterm> |
| 17116 |
<primary>command</primary> |
| 17117 |
<secondary>flex</secondary> |
| 17118 |
</indexterm> |
| 17119 |
<indexterm> |
| 17120 |
<primary>yacc</primary> |
| 17121 |
</indexterm> |
| 17122 |
<indexterm> |
| 17123 |
<primary>command</primary> |
| 17124 |
<secondary>bison</secondary> |
| 17125 |
</indexterm> |
| 17126 |
<listitem> |
| 17127 |
<para><anchor id="flexref"></para> |
| 17128 |
<para>The <command>lex</command> lexical analyzer produces |
| 17129 |
programs for pattern matching. This has been replaced |
| 17130 |
by the nonproprietary <command>flex</command> on Linux |
| 17131 |
systems.</para> |
| 17132 |
<para><anchor id="bisonref"></para> |
| 17133 |
<para>The <command>yacc</command> utility creates a |
| 17134 |
parser based on a set of specifications. This has been |
| 17135 |
replaced by the nonproprietary <command>bison</command> |
| 17136 |
on Linux systems.</para> |
| 17137 |
</listitem> |
| 17138 |
</varlistentry> |
| 17139 |
|
| 17140 |
</variablelist> |
| 17141 |
|
| 17142 |
</sect1> <!-- End Text Processing Commands --> |
| 17143 |
|
| 17144 |
|
| 17145 |
<sect1 id="filearchiv"> |
| 17146 |
<title>File and Archiving Commands</title> |
| 17147 |
|
| 17148 |
<variablelist id="faarchiving"> |
| 17149 |
<title><anchor id="faarchiving1">Archiving</title> |
| 17150 |
|
| 17151 |
<varlistentry> |
| 17152 |
<term><anchor id="tarref"><command>tar</command></term> |
| 17153 |
<indexterm> |
| 17154 |
<primary>tar</primary> |
| 17155 |
</indexterm> |
| 17156 |
<indexterm> |
| 17157 |
<primary>command</primary> |
| 17158 |
<secondary>tar</secondary> |
| 17159 |
</indexterm> |
| 17160 |
<listitem> |
| 17161 |
|
| 17162 |
<para>The standard UNIX archiving utility. |
| 17163 |
|
| 17164 |
<footnote><para>An <firstterm>archive</firstterm>, |
| 17165 |
in the sense discussed here, is simply a set of related |
| 17166 |
files stored in a single location.</para></footnote> |
| 17167 |
|
| 17168 |
Originally a |
| 17169 |
<wordasword>Tape ARchiving</wordasword> program, it has |
| 17170 |
developed into a general purpose package that can handle |
| 17171 |
all manner of archiving with all types of destination |
| 17172 |
devices, ranging from tape drives to regular files to even |
| 17173 |
<filename>stdout</filename> (see <xref linkend="ex58">). GNU |
| 17174 |
<firstterm>tar</firstterm> has been patched to accept |
| 17175 |
various compression filters, for example: <command>tar |
| 17176 |
czvf archive_name.tar.gz *</command>, which recursively |
| 17177 |
archives and <link linkend="gzipref">gzips</link> |
| 17178 |
all files in a directory tree except <link |
| 17179 |
linkend="dotfilesref">dotfiles</link> in the current |
| 17180 |
working directory (<link linkend="pwdref">$PWD</link>). |
| 17181 |
|
| 17182 |
<footnote> |
| 17183 |
<para> |
| 17184 |
A <replaceable>tar czvf ArchiveName.tar.gz *</replaceable> |
| 17185 |
<emphasis>will</emphasis> include dotfiles in |
| 17186 |
subdirectories <emphasis>below</emphasis> the current |
| 17187 |
working directory. This is an undocumented GNU |
| 17188 |
<command>tar</command> <quote>feature.</quote> |
| 17189 |
</para> |
| 17190 |
</footnote> |
| 17191 |
</para> |
| 17192 |
|
| 17193 |
|
| 17194 |
<para>Some useful <command>tar</command> options: |
| 17195 |
<orderedlist> |
| 17196 |
|
| 17197 |
<listitem><para><option>-c</option> create (a new |
| 17198 |
archive)</para></listitem> |
| 17199 |
|
| 17200 |
<listitem><para><option>-x</option> extract (files from |
| 17201 |
existing archive)</para></listitem> |
| 17202 |
|
| 17203 |
<listitem> |
| 17204 |
<para><option>--delete</option> delete (files |
| 17205 |
from existing archive)</para> |
| 17206 |
<caution><para>This option will not work on magnetic tape |
| 17207 |
devices.</para></caution> |
| 17208 |
</listitem> |
| 17209 |
|
| 17210 |
<listitem><para><option>-r</option> append (files to |
| 17211 |
existing archive)</para></listitem> |
| 17212 |
|
| 17213 |
<listitem><para><option>-A</option> append |
| 17214 |
(<firstterm>tar</firstterm> files to |
| 17215 |
existing archive)</para></listitem> |
| 17216 |
|
| 17217 |
<listitem><para><option>-t</option> list (contents of |
| 17218 |
existing archive)</para></listitem> |
| 17219 |
|
| 17220 |
<listitem><para><option>-u</option> update archive</para></listitem> |
| 17221 |
|
| 17222 |
<listitem><para><option>-d</option> compare archive with |
| 17223 |
specified filesystem</para></listitem> |
| 17224 |
|
| 17225 |
<listitem><para><option>--after-date</option> only process |
| 17226 |
files with a date stamp <emphasis>after</emphasis> |
| 17227 |
specified date</para></listitem> |
| 17228 |
|
| 17229 |
<listitem> |
| 17230 |
<para><option>-z</option> <link |
| 17231 |
linkend="gzipref">gzip</link> the archive</para> |
| 17232 |
<para>(compress or uncompress, depending on whether |
| 17233 |
combined with the <option>-c</option> or |
| 17234 |
<option>-x</option>) option</para> |
| 17235 |
</listitem> |
| 17236 |
|
| 17237 |
<listitem><para><option>-j</option> |
| 17238 |
<link linkend="bzipref">bzip2</link> the |
| 17239 |
archive</para></listitem> |
| 17240 |
|
| 17241 |
</orderedlist> |
| 17242 |
</para> |
| 17243 |
|
| 17244 |
<caution><para>It may be difficult to recover data from a |
| 17245 |
corrupted <firstterm>gzipped</firstterm> tar |
| 17246 |
archive. When archiving important files, make multiple |
| 17247 |
backups.</para></caution> |
| 17248 |
|
| 17249 |
</listitem> |
| 17250 |
</varlistentry> |
| 17251 |
|
| 17252 |
<varlistentry> |
| 17253 |
<term><anchor id="sharref"><command>shar</command></term> |
| 17254 |
<indexterm> |
| 17255 |
<primary>shar</primary> |
| 17256 |
</indexterm> |
| 17257 |
<indexterm> |
| 17258 |
<primary>command</primary> |
| 17259 |
<secondary>archive</secondary> |
| 17260 |
</indexterm> |
| 17261 |
<listitem> |
| 17262 |
|
| 17263 |
<para><firstterm>Shell archiving</firstterm> utility. |
| 17264 |
The text and/or binary files in a shell archive are |
| 17265 |
concatenated without compression, and the resultant |
| 17266 |
archive is essentially a shell script, complete with |
| 17267 |
<token>#!/bin/sh</token> header, containing all the |
| 17268 |
necessary unarchiving commands, as well as the files |
| 17269 |
themselves. Unprintable binary characters in the target |
| 17270 |
file(s) are converted to printable ASCII characters in the |
| 17271 |
output <firstterm>shar</firstterm> file. <firstterm>Shar |
| 17272 |
archives</firstterm> still show up in Usenet newsgroups, |
| 17273 |
but otherwise <command>shar</command> has been replaced |
| 17274 |
by <command>tar</command>/<command>gzip</command>. |
| 17275 |
The <command>unshar</command> command unpacks |
| 17276 |
<firstterm>shar</firstterm> archives.</para> <para>The |
| 17277 |
<command>mailshar</command> command is a Bash script that |
| 17278 |
uses <command>shar</command> to concatenate multiple files |
| 17279 |
into a single one for e-mailing. |
| 17280 |
This script supports compression and <link |
| 17281 |
linkend="uuencoderef">uuencoding</link>.</para> |
| 17282 |
|
| 17283 |
</listitem> |
| 17284 |
</varlistentry> |
| 17285 |
|
| 17286 |
<varlistentry> |
| 17287 |
<term><anchor id="arref"><command>ar</command></term> |
| 17288 |
<indexterm> |
| 17289 |
<primary>ar</primary> |
| 17290 |
</indexterm> |
| 17291 |
<indexterm> |
| 17292 |
<primary>command</primary> |
| 17293 |
<secondary>archive</secondary> |
| 17294 |
</indexterm> |
| 17295 |
<listitem> |
| 17296 |
<para>Creation and manipulation utility for archives, mainly |
| 17297 |
used for binary object file libraries.</para> |
| 17298 |
</listitem> |
| 17299 |
</varlistentry> |
| 17300 |
|
| 17301 |
<varlistentry> |
| 17302 |
<term><anchor id="rpmref"><command>rpm</command></term> |
| 17303 |
<indexterm> |
| 17304 |
<primary>rpm</primary> |
| 17305 |
</indexterm> |
| 17306 |
<indexterm> |
| 17307 |
<primary>command</primary> |
| 17308 |
<secondary>package manager</secondary> |
| 17309 |
</indexterm> |
| 17310 |
<listitem> |
| 17311 |
|
| 17312 |
<para>The <firstterm>Red Hat Package Manager</firstterm>, or |
| 17313 |
<command>rpm</command> utility provides a wrapper for |
| 17314 |
source or binary archives. It includes commands for |
| 17315 |
installing and checking the integrity of packages, among |
| 17316 |
other things.</para> |
| 17317 |
|
| 17318 |
<para>A simple <command>rpm -i package_name.rpm</command> |
| 17319 |
usually suffices to install a package, though there are many |
| 17320 |
more options available.</para> |
| 17321 |
|
| 17322 |
|
| 17323 |
<tip> |
| 17324 |
<para><userinput>rpm -qf</userinput> identifies which package a |
| 17325 |
file originates from.</para> |
| 17326 |
|
| 17327 |
<para> |
| 17328 |
<screen> |
| 17329 |
<prompt>bash$ </prompt><userinput>rpm -qf /bin/ls</userinput> |
| 17330 |
<computeroutput>coreutils-5.2.1-31</computeroutput> |
| 17331 |
</screen> |
| 17332 |
</para> |
| 17333 |
</tip> |
| 17334 |
|
| 17335 |
<tip> |
| 17336 |
<para><userinput>rpm -qa</userinput> gives a |
| 17337 |
complete list of all installed <firstterm>rpm</firstterm> packages |
| 17338 |
on a given system. An <userinput>rpm -qa package_name</userinput> |
| 17339 |
lists only the package(s) corresponding to |
| 17340 |
<filename>package_name</filename>.</para> |
| 17341 |
|
| 17342 |
<para> |
| 17343 |
<screen> |
| 17344 |
<prompt>bash$ </prompt><userinput>rpm -qa</userinput> |
| 17345 |
<computeroutput>redhat-logos-1.1.3-1 |
| 17346 |
glibc-2.2.4-13 |
| 17347 |
cracklib-2.7-12 |
| 17348 |
dosfstools-2.7-1 |
| 17349 |
gdbm-1.8.0-10 |
| 17350 |
ksymoops-2.4.1-1 |
| 17351 |
mktemp-1.5-11 |
| 17352 |
perl-5.6.0-17 |
| 17353 |
reiserfs-utils-3.x.0j-2 |
| 17354 |
...</computeroutput> |
| 17355 |
|
| 17356 |
|
| 17357 |
<prompt>bash$ </prompt><userinput>rpm -qa docbook-utils</userinput> |
| 17358 |
<computeroutput>docbook-utils-0.6.9-2</computeroutput> |
| 17359 |
|
| 17360 |
|
| 17361 |
<prompt>bash$ </prompt><userinput>rpm -qa docbook | grep docbook</userinput> |
| 17362 |
<computeroutput>docbook-dtd31-sgml-1.0-10 |
| 17363 |
docbook-style-dsssl-1.64-3 |
| 17364 |
docbook-dtd30-sgml-1.0-10 |
| 17365 |
docbook-dtd40-sgml-1.0-11 |
| 17366 |
docbook-utils-pdf-0.6.9-2 |
| 17367 |
docbook-dtd41-sgml-1.0-10 |
| 17368 |
docbook-utils-0.6.9-2</computeroutput> |
| 17369 |
</screen> |
| 17370 |
</para> |
| 17371 |
</tip> |
| 17372 |
|
| 17373 |
</listitem> |
| 17374 |
</varlistentry> |
| 17375 |
|
| 17376 |
<varlistentry> |
| 17377 |
<term><anchor id="cpioref"><command>cpio</command></term> |
| 17378 |
<indexterm> |
| 17379 |
<primary>cpio</primary> |
| 17380 |
</indexterm> |
| 17381 |
<indexterm> |
| 17382 |
<primary>command</primary> |
| 17383 |
<secondary>cpio</secondary> |
| 17384 |
</indexterm> |
| 17385 |
<listitem> |
| 17386 |
<para>This specialized archiving copy command |
| 17387 |
(<command>c</command>o<command>p</command>y |
| 17388 |
<command>i</command>nput and <command>o</command>utput) |
| 17389 |
is rarely seen any more, having been supplanted by |
| 17390 |
<command>tar</command>/<command>gzip</command>. It still |
| 17391 |
has its uses, such as moving a directory tree. With an |
| 17392 |
appropriate block size (for copying) specified, it |
| 17393 |
can be appreciably faster than <command>tar</command>.</para> |
| 17394 |
|
| 17395 |
<example id="ex48"> |
| 17396 |
<title>Using <firstterm>cpio</firstterm> to move a directory tree</title> |
| 17397 |
<programlisting>&ex48;</programlisting> |
| 17398 |
</example> |
| 17399 |
|
| 17400 |
</listitem> |
| 17401 |
</varlistentry> |
| 17402 |
|
| 17403 |
<varlistentry> |
| 17404 |
<term><anchor id="rpm2cpioref"><command>rpm2cpio</command></term> |
| 17405 |
<indexterm> |
| 17406 |
<primary>rpm</primary> |
| 17407 |
</indexterm> |
| 17408 |
<indexterm> |
| 17409 |
<primary>command</primary> |
| 17410 |
<secondary>cpio</secondary> |
| 17411 |
</indexterm> |
| 17412 |
<listitem> |
| 17413 |
<para>This command extracts a |
| 17414 |
<command>cpio</command> archive from an <link |
| 17415 |
linkend="rpmref">rpm</link> one.</para> |
| 17416 |
|
| 17417 |
<example id="derpm"> |
| 17418 |
<title>Unpacking an <firstterm>rpm</firstterm> archive</title> |
| 17419 |
<programlisting>&derpm;</programlisting> |
| 17420 |
</example> |
| 17421 |
|
| 17422 |
</listitem> |
| 17423 |
</varlistentry> |
| 17424 |
|
| 17425 |
<varlistentry> |
| 17426 |
<term><anchor id="paxref"><command>pax</command></term> |
| 17427 |
<indexterm> |
| 17428 |
<primary>pax</primary> |
| 17429 |
</indexterm> |
| 17430 |
<indexterm> |
| 17431 |
<primary>command</primary> |
| 17432 |
<secondary>archive</secondary> |
| 17433 |
</indexterm> |
| 17434 |
<listitem> |
| 17435 |
|
| 17436 |
<para>The <firstterm>pax</firstterm> |
| 17437 |
<command>p</command>ortable <command>a</command>rchive |
| 17438 |
e<command>x</command>change toolkit facilitates periodic |
| 17439 |
file backups and is designed to be cross-compatible |
| 17440 |
between various flavors of UNIX. It was designed |
| 17441 |
to replace <link linkend="tarref">tar</link> and <link |
| 17442 |
linkend="cpioref">cpio</link>.</para> |
| 17443 |
|
| 17444 |
<para> |
| 17445 |
<programlisting>pax -wf daily_backup.pax ~/linux-server/files |
| 17446 |
# Creates a tar archive of all files in the target directory. |
| 17447 |
# Note that the options to pax must be in the correct order -- |
| 17448 |
#+ pax -fw has an entirely different effect. |
| 17449 |
|
| 17450 |
pax -f daily_backup.pax |
| 17451 |
# Lists the files in the archive. |
| 17452 |
|
| 17453 |
pax -rf daily_backup.pax ~/bsd-server/files |
| 17454 |
# Restores the backed-up files from the Linux machine |
| 17455 |
#+ onto a BSD one.</programlisting> |
| 17456 |
</para> |
| 17457 |
|
| 17458 |
<para>Note that <firstterm>pax</firstterm> handles many of |
| 17459 |
the standard archiving and compression commands.</para> |
| 17460 |
|
| 17461 |
</listitem> |
| 17462 |
</varlistentry> |
| 17463 |
|
| 17464 |
</variablelist> |
| 17465 |
|
| 17466 |
|
| 17467 |
<variablelist id="facompression"> |
| 17468 |
<title><anchor id="facompression1">Compression</title> |
| 17469 |
|
| 17470 |
<varlistentry> |
| 17471 |
<term><anchor id="gzipref"><command>gzip</command></term> |
| 17472 |
<indexterm> |
| 17473 |
<primary>gzip</primary> |
| 17474 |
</indexterm> |
| 17475 |
<indexterm> |
| 17476 |
<primary>command</primary> |
| 17477 |
<secondary>gzip</secondary> |
| 17478 |
</indexterm> |
| 17479 |
<listitem> |
| 17480 |
|
| 17481 |
<para>The standard GNU/UNIX compression utility, replacing |
| 17482 |
the inferior and proprietary |
| 17483 |
<command>compress</command>. The corresponding decompression |
| 17484 |
command is <command>gunzip</command>, which is the equivalent of |
| 17485 |
<command>gzip -d</command>.</para> |
| 17486 |
|
| 17487 |
<note><para>The <option>-c</option> option sends the output of |
| 17488 |
<command>gzip</command> to <filename>stdout</filename>. This |
| 17489 |
is useful when <link linkend="piperef">piping</link> to other |
| 17490 |
commands.</para></note> |
| 17491 |
|
| 17492 |
<para><anchor id="zcatref"></para> |
| 17493 |
<para>The <command>zcat</command> filter decompresses a |
| 17494 |
<firstterm>gzipped</firstterm> file to |
| 17495 |
<filename>stdout</filename>, as possible input to a pipe or |
| 17496 |
redirection. This is, in effect, a <command>cat</command> |
| 17497 |
command that works on compressed files (including files |
| 17498 |
processed with the older <link |
| 17499 |
linkend="compressref">compress</link> |
| 17500 |
utility). The <command>zcat</command> command is equivalent to |
| 17501 |
<command>gzip -dc</command>.</para> |
| 17502 |
|
| 17503 |
|
| 17504 |
<caution><para>On some commercial UNIX systems, <command>zcat</command> |
| 17505 |
is a synonym for <command>uncompress -c</command>, |
| 17506 |
and will not work on <firstterm>gzipped</firstterm> |
| 17507 |
files.</para></caution> |
| 17508 |
|
| 17509 |
<para>See also <xref linkend="ex14">.</para> |
| 17510 |
|
| 17511 |
</listitem> |
| 17512 |
</varlistentry> |
| 17513 |
|
| 17514 |
<varlistentry> |
| 17515 |
<term><anchor id="bzipref"><command>bzip2</command></term> |
| 17516 |
<indexterm> |
| 17517 |
<primary>bzip2</primary> |
| 17518 |
</indexterm> |
| 17519 |
<indexterm> |
| 17520 |
<primary>command</primary> |
| 17521 |
<secondary>bzip2</secondary> |
| 17522 |
</indexterm> |
| 17523 |
<listitem> |
| 17524 |
|
| 17525 |
<para>An alternate compression utility, usually more efficient |
| 17526 |
(but slower) than <command>gzip</command>, especially on |
| 17527 |
large files. The corresponding decompression command is |
| 17528 |
<command>bunzip2</command>.</para> |
| 17529 |
|
| 17530 |
<para>Similar to the <command>zcat</command> command, |
| 17531 |
<command>bzcat</command> decompresses a |
| 17532 |
<firstterm>bzipped2-ed</firstterm> file to |
| 17533 |
<filename>stdout</filename>.</para> |
| 17534 |
|
| 17535 |
<note><para>Newer versions of <link |
| 17536 |
linkend="tarref">tar</link> have been patched with |
| 17537 |
<command>bzip2</command> support.</para></note> |
| 17538 |
|
| 17539 |
</listitem> |
| 17540 |
</varlistentry> |
| 17541 |
|
| 17542 |
<varlistentry> |
| 17543 |
<term><anchor id="compressref"><command>compress</command></term> |
| 17544 |
<term><anchor id="uncompressref"><command>uncompress</command></term> |
| 17545 |
<indexterm> |
| 17546 |
<primary>compress</primary> |
| 17547 |
</indexterm> |
| 17548 |
<indexterm> |
| 17549 |
<primary>command</primary> |
| 17550 |
<secondary>compress</secondary> |
| 17551 |
</indexterm> |
| 17552 |
<indexterm> |
| 17553 |
<primary>uncompress</primary> |
| 17554 |
</indexterm> |
| 17555 |
<indexterm> |
| 17556 |
<primary>command</primary> |
| 17557 |
<secondary>uncompress</secondary> |
| 17558 |
</indexterm> |
| 17559 |
<listitem> |
| 17560 |
<para>This is an older, proprietary compression |
| 17561 |
utility found in commercial UNIX distributions. The |
| 17562 |
more efficient <command>gzip</command> has largely |
| 17563 |
replaced it. Linux distributions generally include a |
| 17564 |
<command>compress</command> workalike for compatibility, |
| 17565 |
although <command>gunzip</command> can unarchive files |
| 17566 |
treated with <command>compress</command>.</para> |
| 17567 |
|
| 17568 |
<tip><para>The <command>znew</command> command transforms |
| 17569 |
<firstterm>compressed</firstterm> files into |
| 17570 |
<firstterm>gzipped</firstterm> ones.</para></tip> |
| 17571 |
</listitem> |
| 17572 |
</varlistentry> |
| 17573 |
|
| 17574 |
<varlistentry> |
| 17575 |
<term><anchor id="sqref"><command>sq</command></term> |
| 17576 |
<indexterm> |
| 17577 |
<primary>sq</primary> |
| 17578 |
</indexterm> |
| 17579 |
<indexterm> |
| 17580 |
<primary>command</primary> |
| 17581 |
<secondary>sq</secondary> |
| 17582 |
</indexterm> |
| 17583 |
<listitem> |
| 17584 |
|
| 17585 |
<para>Yet another compression (<command>sq</command>ueeze) |
| 17586 |
utility, a filter that works only on sorted |
| 17587 |
<link linkend="asciidef">ASCII</link> word lists. It |
| 17588 |
uses the standard invocation syntax for a filter, |
| 17589 |
<command>sq < input-file > output-file</command>. |
| 17590 |
Fast, but not nearly as efficient as <link |
| 17591 |
linkend="gzipref">gzip</link>. The corresponding |
| 17592 |
uncompression filter is <command>unsq</command>, invoked |
| 17593 |
like <command>sq</command>.</para> |
| 17594 |
|
| 17595 |
<tip><para>The output of <command>sq</command> may be |
| 17596 |
piped to <command>gzip</command> for further |
| 17597 |
compression.</para></tip> |
| 17598 |
|
| 17599 |
</listitem> |
| 17600 |
</varlistentry> |
| 17601 |
|
| 17602 |
<varlistentry> |
| 17603 |
<term><anchor id="zipref"><command>zip</command></term> |
| 17604 |
<term><command>unzip</command></term> |
| 17605 |
<indexterm> |
| 17606 |
<primary>zip</primary> |
| 17607 |
</indexterm> |
| 17608 |
<indexterm> |
| 17609 |
<primary>command</primary> |
| 17610 |
<secondary>pkzip.exe</secondary> |
| 17611 |
</indexterm> |
| 17612 |
<indexterm> |
| 17613 |
<primary>unzip</primary> |
| 17614 |
</indexterm> |
| 17615 |
<indexterm> |
| 17616 |
<primary>command</primary> |
| 17617 |
<secondary>unzip</secondary> |
| 17618 |
</indexterm> |
| 17619 |
<listitem> |
| 17620 |
<para>Cross-platform file archiving and compression utility |
| 17621 |
compatible with DOS <firstterm>pkzip.exe</firstterm>. |
| 17622 |
<quote>Zipped</quote> archives seem to be a more |
| 17623 |
common medium of file exchange on the Internet than |
| 17624 |
<quote>tarballs.</quote></para> |
| 17625 |
</listitem> |
| 17626 |
</varlistentry> |
| 17627 |
|
| 17628 |
<varlistentry> |
| 17629 |
<term><anchor id="unarcref"><command>unarc</command></term> |
| 17630 |
<term><command>unarj</command></term> |
| 17631 |
<term><command>unrar</command></term> |
| 17632 |
<indexterm> |
| 17633 |
<primary>unarc</primary> |
| 17634 |
</indexterm> |
| 17635 |
<indexterm> |
| 17636 |
<primary>command</primary> |
| 17637 |
<secondary>arc.exe</secondary> |
| 17638 |
</indexterm> |
| 17639 |
<indexterm> |
| 17640 |
<primary>unarj</primary> |
| 17641 |
</indexterm> |
| 17642 |
<indexterm> |
| 17643 |
<primary>command</primary> |
| 17644 |
<secondary>arj.exe</secondary> |
| 17645 |
</indexterm> |
| 17646 |
<indexterm> |
| 17647 |
<primary>unrar</primary> |
| 17648 |
</indexterm> |
| 17649 |
<indexterm> |
| 17650 |
<primary>command</primary> |
| 17651 |
<secondary>rar.exe</secondary> |
| 17652 |
</indexterm> |
| 17653 |
<listitem> |
| 17654 |
<para>These Linux utilities permit unpacking archives |
| 17655 |
compressed with the DOS <firstterm>arc.exe</firstterm>, |
| 17656 |
<firstterm>arj.exe</firstterm>, and |
| 17657 |
<firstterm>rar.exe</firstterm> programs.</para> |
| 17658 |
|
| 17659 |
</listitem> |
| 17660 |
</varlistentry> |
| 17661 |
|
| 17662 |
<varlistentry> |
| 17663 |
<term><anchor id="lzmaref"><command>lzma</command></term> |
| 17664 |
<term><command>unlzma</command></term> |
| 17665 |
<term><command>lzcat</command></term> |
| 17666 |
<indexterm> |
| 17667 |
<primary>lzma</primary> |
| 17668 |
</indexterm> |
| 17669 |
<indexterm> |
| 17670 |
<primary>command</primary> |
| 17671 |
<secondary>lzma</secondary> |
| 17672 |
</indexterm> |
| 17673 |
<indexterm> |
| 17674 |
<primary>unlzma</primary> |
| 17675 |
</indexterm> |
| 17676 |
<indexterm> |
| 17677 |
<primary>command</primary> |
| 17678 |
<secondary>unlzma</secondary> |
| 17679 |
</indexterm> |
| 17680 |
<indexterm> |
| 17681 |
<primary>lzcat</primary> |
| 17682 |
</indexterm> |
| 17683 |
<indexterm> |
| 17684 |
<primary>command</primary> |
| 17685 |
<secondary>lzcat</secondary> |
| 17686 |
</indexterm> |
| 17687 |
<listitem> |
| 17688 |
<para>Highly efficient Lempel-Ziv-Markov compression. |
| 17689 |
The syntax of <firstterm>lzma</firstterm> is similar to |
| 17690 |
that of <firstterm>gzip</firstterm>. The <ulink |
| 17691 |
url="http://www.7-zip.org/sdk.html">7-zip Website</ulink> |
| 17692 |
has more information.</para> |
| 17693 |
|
| 17694 |
</listitem> |
| 17695 |
</varlistentry> |
| 17696 |
|
| 17697 |
|
| 17698 |
<varlistentry> |
| 17699 |
<term><anchor id="xzref"><command>xz</command></term> |
| 17700 |
<term><command>unxz</command></term> |
| 17701 |
<term><command>xzcat</command></term> |
| 17702 |
<indexterm> |
| 17703 |
<primary>xz</primary> |
| 17704 |
</indexterm> |
| 17705 |
<indexterm> |
| 17706 |
<primary>command</primary> |
| 17707 |
<secondary>xz</secondary> |
| 17708 |
</indexterm> |
| 17709 |
<indexterm> |
| 17710 |
<primary>unxz</primary> |
| 17711 |
</indexterm> |
| 17712 |
<indexterm> |
| 17713 |
<primary>command</primary> |
| 17714 |
<secondary>unxz</secondary> |
| 17715 |
</indexterm> |
| 17716 |
<indexterm> |
| 17717 |
<primary>xzcat</primary> |
| 17718 |
</indexterm> |
| 17719 |
<indexterm> |
| 17720 |
<primary>command</primary> |
| 17721 |
<secondary>xzcat</secondary> |
| 17722 |
</indexterm> |
| 17723 |
<listitem> |
| 17724 |
<para>A new high-efficiency compression tool, backward compatible |
| 17725 |
with <firstterm>lzma</firstterm>, and with an invocation |
| 17726 |
syntax similar to <firstterm>gzip</firstterm>. For |
| 17727 |
more information, see the <ulink |
| 17728 |
url="http://en.wikipedia.org/wiki/Xz">Wikipedia |
| 17729 |
entry</ulink>.</para> |
| 17730 |
|
| 17731 |
</listitem> |
| 17732 |
</varlistentry> |
| 17733 |
|
| 17734 |
|
| 17735 |
</variablelist> |
| 17736 |
|
| 17737 |
|
| 17738 |
<variablelist id="fainformation"> |
| 17739 |
<title><anchor id="fainformation1">File Information</title> |
| 17740 |
|
| 17741 |
<varlistentry> |
| 17742 |
<term><anchor id="fileref"><command>file</command></term> |
| 17743 |
<indexterm> |
| 17744 |
<primary>file</primary> |
| 17745 |
</indexterm> |
| 17746 |
<indexterm> |
| 17747 |
<primary>command</primary> |
| 17748 |
<secondary>file</secondary> |
| 17749 |
</indexterm> |
| 17750 |
<listitem> |
| 17751 |
|
| 17752 |
<para>A utility for identifying file types. The command |
| 17753 |
<userinput>file file-name</userinput> will return a |
| 17754 |
file specification for <filename>file-name</filename>, |
| 17755 |
such as <computeroutput>ascii text</computeroutput> or |
| 17756 |
<computeroutput>data</computeroutput>. It references |
| 17757 |
the <link linkend="magnumref">magic numbers</link> |
| 17758 |
found in <filename>/usr/share/magic</filename>, |
| 17759 |
<filename>/etc/magic</filename>, or |
| 17760 |
<filename>/usr/lib/magic</filename>, depending on the |
| 17761 |
Linux/UNIX distribution.</para> |
| 17762 |
|
| 17763 |
<para>The <option>-f</option> option causes |
| 17764 |
<command>file</command> to run in <link |
| 17765 |
linkend="batchprocref">batch</link> mode, to read from |
| 17766 |
a designated file a list of filenames to analyze. The |
| 17767 |
<option>-z</option> option, when used on a compressed |
| 17768 |
target file, forces an attempt to analyze the uncompressed |
| 17769 |
file type.</para> |
| 17770 |
|
| 17771 |
<para> |
| 17772 |
<screen><prompt>bash$ </prompt><userinput>file test.tar.gz</userinput> |
| 17773 |
<computeroutput>test.tar.gz: gzip compressed data, deflated, |
| 17774 |
last modified: Sun Sep 16 13:34:51 2001, os: Unix</computeroutput> |
| 17775 |
|
| 17776 |
<prompt>bash </prompt><userinput>file -z test.tar.gz</userinput> |
| 17777 |
<computeroutput>test.tar.gz: GNU tar archive (gzip compressed data, deflated, |
| 17778 |
last modified: Sun Sep 16 13:34:51 2001, os: Unix)</computeroutput> |
| 17779 |
</screen> |
| 17780 |
</para> |
| 17781 |
|
| 17782 |
<para> |
| 17783 |
<programlisting># Find sh and Bash scripts in a given directory: |
| 17784 |
|
| 17785 |
DIRECTORY=/usr/local/bin |
| 17786 |
KEYWORD=Bourne |
| 17787 |
# Bourne and Bourne-Again shell scripts |
| 17788 |
|
| 17789 |
file $DIRECTORY/* | fgrep $KEYWORD |
| 17790 |
|
| 17791 |
# Output: |
| 17792 |
|
| 17793 |
# /usr/local/bin/burn-cd: Bourne-Again shell script text executable |
| 17794 |
# /usr/local/bin/burnit: Bourne-Again shell script text executable |
| 17795 |
# /usr/local/bin/cassette.sh: Bourne shell script text executable |
| 17796 |
# /usr/local/bin/copy-cd: Bourne-Again shell script text executable |
| 17797 |
# . . .</programlisting> |
| 17798 |
</para> |
| 17799 |
|
| 17800 |
<example id="stripc"> |
| 17801 |
<title>Stripping comments from C program files</title> |
| 17802 |
<programlisting>&stripc;</programlisting> |
| 17803 |
</example> |
| 17804 |
|
| 17805 |
</listitem> |
| 17806 |
</varlistentry> |
| 17807 |
|
| 17808 |
<varlistentry> |
| 17809 |
<term><anchor id="whichref"><command>which</command></term> |
| 17810 |
<indexterm> |
| 17811 |
<primary>which</primary> |
| 17812 |
</indexterm> |
| 17813 |
<indexterm> |
| 17814 |
<primary>command</primary> |
| 17815 |
<secondary>which</secondary> |
| 17816 |
</indexterm> |
| 17817 |
<listitem> |
| 17818 |
<para><command>which command</command> gives the full path |
| 17819 |
to <quote>command.</quote> This is useful for finding |
| 17820 |
out whether a particular command or utility is installed |
| 17821 |
on the system.</para> |
| 17822 |
<para><userinput>$bash which rm</userinput> |
| 17823 |
<screen><computeroutput>/usr/bin/rm</computeroutput></screen> |
| 17824 |
</para> |
| 17825 |
<para>For an interesting use of this command, see <xref |
| 17826 |
linkend="horserace">.</para> |
| 17827 |
</listitem> |
| 17828 |
</varlistentry> |
| 17829 |
|
| 17830 |
<varlistentry> |
| 17831 |
<term><anchor id="whereisref"><command>whereis</command></term> |
| 17832 |
<indexterm> |
| 17833 |
<primary>whereis</primary> |
| 17834 |
</indexterm> |
| 17835 |
<indexterm> |
| 17836 |
<primary>command</primary> |
| 17837 |
<secondary>whereis</secondary> |
| 17838 |
</indexterm> |
| 17839 |
<listitem> |
| 17840 |
<para>Similar to <command>which</command>, above, |
| 17841 |
<command>whereis command</command> gives the |
| 17842 |
full path to <quote>command,</quote> but also to its |
| 17843 |
<link linkend="manref">manpage</link>.</para> |
| 17844 |
<para><userinput>$bash whereis rm</userinput> |
| 17845 |
<screen><computeroutput>rm: /bin/rm /usr/share/man/man1/rm.1.bz2</computeroutput></screen> |
| 17846 |
</para> |
| 17847 |
|
| 17848 |
</listitem> |
| 17849 |
</varlistentry> |
| 17850 |
|
| 17851 |
<varlistentry> |
| 17852 |
<term><anchor id="whatisref"><command>whatis</command></term> |
| 17853 |
<indexterm> |
| 17854 |
<primary>whatis</primary> |
| 17855 |
</indexterm> |
| 17856 |
<indexterm> |
| 17857 |
<primary>command</primary> |
| 17858 |
<secondary>whatis</secondary> |
| 17859 |
</indexterm> |
| 17860 |
<listitem> |
| 17861 |
<para><command>whatis command</command> looks up |
| 17862 |
<quote>command</quote> in the |
| 17863 |
<replaceable>whatis</replaceable> database. This is useful |
| 17864 |
for identifying system commands and important configuration |
| 17865 |
files. Consider it a simplified <command>man</command> |
| 17866 |
command.</para> |
| 17867 |
<para><userinput>$bash whatis whatis</userinput> |
| 17868 |
<screen><computeroutput>whatis (1) - search the whatis database for complete words</computeroutput></screen> |
| 17869 |
</para> |
| 17870 |
|
| 17871 |
<example id="what"> |
| 17872 |
<title>Exploring <filename |
| 17873 |
class="directory">/usr/X11R6/bin</filename></title> |
| 17874 |
<programlisting>&what;</programlisting> |
| 17875 |
</example> |
| 17876 |
|
| 17877 |
<para>See also <xref linkend="fileinfo">.</para> |
| 17878 |
|
| 17879 |
</listitem> |
| 17880 |
</varlistentry> |
| 17881 |
|
| 17882 |
<varlistentry> |
| 17883 |
<term><anchor id="vdirref"><command>vdir</command></term> |
| 17884 |
<indexterm> |
| 17885 |
<primary>vdir</primary> |
| 17886 |
</indexterm> |
| 17887 |
<indexterm> |
| 17888 |
<primary>command</primary> |
| 17889 |
<secondary>ls</secondary> |
| 17890 |
</indexterm> |
| 17891 |
<listitem> |
| 17892 |
<para>Show a detailed directory listing. The effect is similar to |
| 17893 |
<link linkend="lsref">ls -lb</link>.</para> |
| 17894 |
<para>This is one of the GNU |
| 17895 |
<firstterm>fileutils</firstterm>.</para> |
| 17896 |
|
| 17897 |
<para> |
| 17898 |
<screen><prompt>bash$ </prompt><userinput>vdir</userinput> |
| 17899 |
<computeroutput>total 10 |
| 17900 |
-rw-r--r-- 1 bozo bozo 4034 Jul 18 22:04 data1.xrolo |
| 17901 |
-rw-r--r-- 1 bozo bozo 4602 May 25 13:58 data1.xrolo.bak |
| 17902 |
-rw-r--r-- 1 bozo bozo 877 Dec 17 2000 employment.xrolo</computeroutput> |
| 17903 |
|
| 17904 |
<prompt>bash </prompt><userinput>ls -l</userinput> |
| 17905 |
<computeroutput>total 10 |
| 17906 |
-rw-r--r-- 1 bozo bozo 4034 Jul 18 22:04 data1.xrolo |
| 17907 |
-rw-r--r-- 1 bozo bozo 4602 May 25 13:58 data1.xrolo.bak |
| 17908 |
-rw-r--r-- 1 bozo bozo 877 Dec 17 2000 employment.xrolo</computeroutput> |
| 17909 |
</screen> |
| 17910 |
</para> |
| 17911 |
</listitem> |
| 17912 |
</varlistentry> |
| 17913 |
|
| 17914 |
<varlistentry> |
| 17915 |
<term><anchor id="locateref"><command>locate</command></term> |
| 17916 |
<term><anchor id="slocateref"><command>slocate</command></term> |
| 17917 |
<indexterm> |
| 17918 |
<primary>locate</primary> |
| 17919 |
</indexterm> |
| 17920 |
<indexterm> |
| 17921 |
<primary>command</primary> |
| 17922 |
<secondary>locate</secondary> |
| 17923 |
</indexterm> |
| 17924 |
<indexterm> |
| 17925 |
<primary>slocate</primary> |
| 17926 |
</indexterm> |
| 17927 |
<indexterm> |
| 17928 |
<primary>command</primary> |
| 17929 |
<secondary>slocate</secondary> |
| 17930 |
</indexterm> |
| 17931 |
<listitem> |
| 17932 |
<para>The <command>locate</command> command searches for |
| 17933 |
files using a database stored for just that purpose. The |
| 17934 |
<command>slocate</command> command is the secure version of |
| 17935 |
<command>locate</command> (which may be aliased to |
| 17936 |
<command>slocate</command>).</para> |
| 17937 |
<para><userinput>$bash locate hickson</userinput> |
| 17938 |
<screen><computeroutput>/usr/lib/xephem/catalogs/hickson.edb</computeroutput></screen></para> |
| 17939 |
</listitem> |
| 17940 |
</varlistentry> |
| 17941 |
|
| 17942 |
<varlistentry> |
| 17943 |
<term><anchor id="getfaclref"><command>getfacl</command></term> |
| 17944 |
<term><anchor id="setfaclref"><command>setfacl</command></term> |
| 17945 |
<indexterm> |
| 17946 |
<primary>getfacl</primary> |
| 17947 |
</indexterm> |
| 17948 |
<indexterm> |
| 17949 |
<primary>command</primary> |
| 17950 |
<secondary>getfacl</secondary> |
| 17951 |
</indexterm> |
| 17952 |
<indexterm> |
| 17953 |
<primary>setfacl</primary> |
| 17954 |
</indexterm> |
| 17955 |
<indexterm> |
| 17956 |
<primary>command</primary> |
| 17957 |
<secondary>setfacl</secondary> |
| 17958 |
</indexterm> |
| 17959 |
<listitem> |
| 17960 |
<para>These commands <firstterm>retrieve</firstterm> or |
| 17961 |
<firstterm>set</firstterm> the <command>f</command>ile |
| 17962 |
<command>a</command>ccess <command>c</command>ontrol |
| 17963 |
<command>l</command>ist -- the <firstterm>owner</firstterm>, |
| 17964 |
<firstterm>group</firstterm>, and file permissions.</para> |
| 17965 |
|
| 17966 |
<para> |
| 17967 |
<screen><prompt>bash$ </prompt><userinput>getfacl *</userinput> |
| 17968 |
<computeroutput># file: test1.txt |
| 17969 |
# owner: bozo |
| 17970 |
# group: bozgrp |
| 17971 |
user::rw- |
| 17972 |
group::rw- |
| 17973 |
other::r-- |
| 17974 |
|
| 17975 |
# file: test2.txt |
| 17976 |
# owner: bozo |
| 17977 |
# group: bozgrp |
| 17978 |
user::rw- |
| 17979 |
group::rw- |
| 17980 |
other::r--</computeroutput> |
| 17981 |
|
| 17982 |
|
| 17983 |
|
| 17984 |
<prompt>bash$ </prompt><userinput>setfacl -m u:bozo:rw yearly_budget.csv</userinput> |
| 17985 |
<prompt>bash$ </prompt><userinput>getfacl yearly_budget.csv</userinput> |
| 17986 |
<computeroutput># file: yearly_budget.csv |
| 17987 |
# owner: accountant |
| 17988 |
# group: budgetgrp |
| 17989 |
user::rw- |
| 17990 |
user:bozo:rw- |
| 17991 |
user:accountant:rw- |
| 17992 |
group::rw- |
| 17993 |
mask::rw- |
| 17994 |
other::r--</computeroutput> |
| 17995 |
</screen> |
| 17996 |
</para> |
| 17997 |
|
| 17998 |
|
| 17999 |
</listitem> |
| 18000 |
</varlistentry> |
| 18001 |
|
| 18002 |
|
| 18003 |
<varlistentry> |
| 18004 |
<term><anchor id="readlinkref"><command>readlink</command></term> |
| 18005 |
<indexterm> |
| 18006 |
<primary>readlink</primary> |
| 18007 |
</indexterm> |
| 18008 |
<indexterm> |
| 18009 |
<primary>command</primary> |
| 18010 |
<secondary>link</secondary> |
| 18011 |
</indexterm> |
| 18012 |
<listitem> |
| 18013 |
<para>Disclose the file that a symbolic link points to.</para> |
| 18014 |
<para> |
| 18015 |
<screen><prompt>bash$ </prompt><userinput>readlink /usr/bin/awk</userinput> |
| 18016 |
<computeroutput>../../bin/gawk</computeroutput> |
| 18017 |
</screen> |
| 18018 |
</para> |
| 18019 |
</listitem> |
| 18020 |
</varlistentry> |
| 18021 |
|
| 18022 |
<varlistentry> |
| 18023 |
<term><anchor id="stringsref"><command>strings</command></term> |
| 18024 |
<indexterm> |
| 18025 |
<primary>strings</primary> |
| 18026 |
</indexterm> |
| 18027 |
<indexterm> |
| 18028 |
<primary>command</primary> |
| 18029 |
<secondary>strings</secondary> |
| 18030 |
</indexterm> |
| 18031 |
<listitem> |
| 18032 |
<para>Use the <command>strings</command> command to find |
| 18033 |
printable strings in a binary or data file. It will list |
| 18034 |
sequences of printable characters found in the target |
| 18035 |
file. This might be handy for a quick 'n dirty examination |
| 18036 |
of a core dump or for looking at an unknown graphic image |
| 18037 |
file (<userinput>strings image-file | more</userinput> might |
| 18038 |
show something like <firstterm>JFIF</firstterm>, |
| 18039 |
which would identify the file as a <firstterm>jpeg</firstterm> |
| 18040 |
graphic). In a script, you would probably |
| 18041 |
parse the output of <command>strings</command> |
| 18042 |
with <link linkend="grepref">grep</link> or <link |
| 18043 |
linkend="sedref">sed</link>. See <xref linkend="bingrep"> |
| 18044 |
and <xref linkend="findstring">.</para> |
| 18045 |
|
| 18046 |
<example id="wstrings"> |
| 18047 |
<title>An <quote>improved</quote> |
| 18048 |
<firstterm>strings</firstterm> command</title> |
| 18049 |
<programlisting>&wstrings;</programlisting> |
| 18050 |
</example> |
| 18051 |
</listitem> |
| 18052 |
|
| 18053 |
</varlistentry> |
| 18054 |
|
| 18055 |
</variablelist> |
| 18056 |
|
| 18057 |
<variablelist id="comparisonn"> |
| 18058 |
<title><anchor id="comparisonn1">Comparison</title> |
| 18059 |
|
| 18060 |
<varlistentry> |
| 18061 |
<term><anchor id="diffref"><command>diff</command></term> |
| 18062 |
<term><command>patch</command></term> |
| 18063 |
<indexterm> |
| 18064 |
<primary>diff</primary> |
| 18065 |
</indexterm> |
| 18066 |
<indexterm> |
| 18067 |
<primary>command</primary> |
| 18068 |
<secondary>diff</secondary> |
| 18069 |
</indexterm> |
| 18070 |
<indexterm> |
| 18071 |
<primary>patch</primary> |
| 18072 |
</indexterm> |
| 18073 |
<indexterm> |
| 18074 |
<primary>command</primary> |
| 18075 |
<secondary>patch</secondary> |
| 18076 |
</indexterm> |
| 18077 |
<listitem> |
| 18078 |
<para><command>diff</command>: flexible file comparison |
| 18079 |
utility. It compares the target files line-by-line |
| 18080 |
sequentially. In some applications, such as comparing |
| 18081 |
word dictionaries, it may be helpful to filter the |
| 18082 |
files through <link linkend="sortref">sort</link> |
| 18083 |
and <command>uniq</command> before piping them |
| 18084 |
to <command>diff</command>. <userinput>diff file-1 |
| 18085 |
file-2</userinput> outputs the lines in the files that |
| 18086 |
differ, with carets showing which file each particular |
| 18087 |
line belongs to.</para> |
| 18088 |
|
| 18089 |
<para>The <option>--side-by-side</option> option to |
| 18090 |
<command>diff</command> outputs each compared file, line by |
| 18091 |
line, in separate columns, with non-matching lines marked. The |
| 18092 |
<option>-c</option> and <option>-u</option> options likewise |
| 18093 |
make the output of the command easier to interpret.</para> |
| 18094 |
|
| 18095 |
<para>There are available various fancy frontends for |
| 18096 |
<command>diff</command>, such as <command>sdiff</command>, |
| 18097 |
<command>wdiff</command>, <command>xdiff</command>, and |
| 18098 |
<command>mgdiff</command>. </para> |
| 18099 |
|
| 18100 |
<tip><para><anchor id="differr2">The <command>diff</command> |
| 18101 |
command returns an exit status of <errorcode>0</errorcode> |
| 18102 |
if the compared files are identical, and |
| 18103 |
<errorcode>1</errorcode> if they differ (or |
| 18104 |
<errorcode>2</errorcode> when <firstterm>binary</firstterm> |
| 18105 |
files are being compared). This permits use of |
| 18106 |
<command>diff</command> in a test construct within a shell |
| 18107 |
script (see below).</para></tip> |
| 18108 |
|
| 18109 |
|
| 18110 |
<para>A common use for <command>diff</command> is generating |
| 18111 |
difference files to be used with <command>patch</command> |
| 18112 |
The <option>-e</option> option outputs files suitable |
| 18113 |
for <command>ed</command> or <command>ex</command> |
| 18114 |
scripts.</para> |
| 18115 |
|
| 18116 |
|
| 18117 |
<para><anchor id="patchref"></para> |
| 18118 |
<para><command>patch</command>: flexible versioning |
| 18119 |
utility. Given a difference file generated by |
| 18120 |
<command>diff</command>, <command>patch</command> can |
| 18121 |
upgrade a previous version of a package to a newer version. |
| 18122 |
It is much more convenient to distribute a relatively |
| 18123 |
small <quote>diff</quote> file than the entire body of a |
| 18124 |
newly revised package. Kernel <quote>patches</quote> have |
| 18125 |
become the preferred method of distributing the frequent |
| 18126 |
releases of the Linux kernel.</para> |
| 18127 |
|
| 18128 |
<para><programlisting>patch -p1 <patch-file |
| 18129 |
# Takes all the changes listed in 'patch-file' |
| 18130 |
# and applies them to the files referenced therein. |
| 18131 |
# This upgrades to a newer version of the package.</programlisting></para> |
| 18132 |
|
| 18133 |
<para>Patching the kernel:</para> |
| 18134 |
<para><programlisting>cd /usr/src |
| 18135 |
gzip -cd patchXX.gz | patch -p0 |
| 18136 |
# Upgrading kernel source using 'patch'. |
| 18137 |
# From the Linux kernel docs "README", |
| 18138 |
# by anonymous author (Alan Cox?).</programlisting></para> |
| 18139 |
|
| 18140 |
|
| 18141 |
|
| 18142 |
<note> |
| 18143 |
<para>The <command>diff</command> command can also |
| 18144 |
recursively compare directories (for the filenames |
| 18145 |
present).</para> |
| 18146 |
|
| 18147 |
<para> |
| 18148 |
<screen><prompt>bash$ </prompt><userinput>diff -r ~/notes1 ~/notes2</userinput> |
| 18149 |
<computeroutput>Only in /home/bozo/notes1: file02 |
| 18150 |
Only in /home/bozo/notes1: file03 |
| 18151 |
Only in /home/bozo/notes2: file04</computeroutput> |
| 18152 |
</screen> |
| 18153 |
</para> |
| 18154 |
</note> |
| 18155 |
|
| 18156 |
<tip> |
| 18157 |
<para><anchor id="zdiffref"></para> |
| 18158 |
<para>Use <command>zdiff</command> to compare |
| 18159 |
<firstterm>gzipped</firstterm> files.</para> |
| 18160 |
</tip> |
| 18161 |
|
| 18162 |
<tip> |
| 18163 |
<para><anchor id="diffstatref"></para> |
| 18164 |
<para>Use <command>diffstat</command> to create |
| 18165 |
a histogram (point-distribution graph) of output from |
| 18166 |
<command>diff</command>.</para> |
| 18167 |
</tip> |
| 18168 |
|
| 18169 |
</listitem> |
| 18170 |
</varlistentry> |
| 18171 |
|
| 18172 |
<varlistentry> |
| 18173 |
<term><anchor id="diff3ref"><command>diff3</command></term> |
| 18174 |
<term><command>merge</command></term> |
| 18175 |
<indexterm> |
| 18176 |
<primary>diff3</primary> |
| 18177 |
</indexterm> |
| 18178 |
<indexterm> |
| 18179 |
<primary>command</primary> |
| 18180 |
<secondary>diff3</secondary> |
| 18181 |
</indexterm> |
| 18182 |
<indexterm> |
| 18183 |
<primary>merge</primary> |
| 18184 |
</indexterm> |
| 18185 |
<indexterm> |
| 18186 |
<primary>command</primary> |
| 18187 |
<secondary>merge</secondary> |
| 18188 |
</indexterm> |
| 18189 |
|
| 18190 |
<listitem> |
| 18191 |
<para>An extended version of <command>diff</command> that compares |
| 18192 |
three files at a time. This command returns an exit value |
| 18193 |
of 0 upon successful execution, but unfortunately this gives |
| 18194 |
no information about the results of the comparison.</para> |
| 18195 |
<para> |
| 18196 |
<screen><prompt>bash$ </prompt><userinput>diff3 file-1 file-2 file-3</userinput> |
| 18197 |
<computeroutput>==== |
| 18198 |
1:1c |
| 18199 |
This is line 1 of "file-1". |
| 18200 |
2:1c |
| 18201 |
This is line 1 of "file-2". |
| 18202 |
3:1c |
| 18203 |
This is line 1 of "file-3"</computeroutput> |
| 18204 |
</screen> |
| 18205 |
</para> |
| 18206 |
|
| 18207 |
<para><anchor id="mergeref">The <command>merge</command> |
| 18208 |
(3-way file merge) command is an interesting adjunct to |
| 18209 |
<firstterm>diff3</firstterm>. Its syntax is |
| 18210 |
<userinput>merge Mergefile file1 file2</userinput>. |
| 18211 |
The result is to output to <filename>Mergefile</filename> |
| 18212 |
the changes that lead from <filename>file1</filename> |
| 18213 |
to <filename>file2</filename>. Consider this command |
| 18214 |
a stripped-down version of <firstterm>patch</firstterm>.</para> |
| 18215 |
|
| 18216 |
</listitem> |
| 18217 |
</varlistentry> |
| 18218 |
|
| 18219 |
<varlistentry> |
| 18220 |
<term><anchor id="sdiffref"><command>sdiff</command></term> |
| 18221 |
<indexterm> |
| 18222 |
<primary>sdiff</primary> |
| 18223 |
</indexterm> |
| 18224 |
<indexterm> |
| 18225 |
<primary>command</primary> |
| 18226 |
<secondary>sdiff</secondary> |
| 18227 |
</indexterm> |
| 18228 |
<listitem> |
| 18229 |
<para>Compare and/or edit two files in order to merge |
| 18230 |
them into an output file. Because of its interactive nature, |
| 18231 |
this command would find little use in a script.</para> |
| 18232 |
</listitem> |
| 18233 |
</varlistentry> |
| 18234 |
|
| 18235 |
<varlistentry> |
| 18236 |
<term><anchor id="cmpref"><command>cmp</command></term> |
| 18237 |
<indexterm> |
| 18238 |
<primary>cmp</primary> |
| 18239 |
</indexterm> |
| 18240 |
<indexterm> |
| 18241 |
<primary>command</primary> |
| 18242 |
<secondary>cmp</secondary> |
| 18243 |
</indexterm> |
| 18244 |
<listitem> |
| 18245 |
<para>The <command>cmp</command> command is a simpler version of |
| 18246 |
<command>diff</command>, above. Whereas <command>diff</command> |
| 18247 |
reports the differences between two files, |
| 18248 |
<command>cmp</command> merely shows at what point they |
| 18249 |
differ.</para> |
| 18250 |
|
| 18251 |
<note><para>Like <command>diff</command>, <command>cmp</command> |
| 18252 |
returns an exit status of 0 if the compared files are |
| 18253 |
identical, and 1 if they differ. This permits use in a test |
| 18254 |
construct within a shell script.</para></note> |
| 18255 |
|
| 18256 |
<example id="filecomp"> |
| 18257 |
<title>Using <firstterm>cmp</firstterm> to compare two files |
| 18258 |
within a script.</title> |
| 18259 |
<programlisting>&filecomp;</programlisting> |
| 18260 |
</example> |
| 18261 |
|
| 18262 |
<tip><para>Use <command>zcmp</command> on |
| 18263 |
<firstterm>gzipped</firstterm> files.</para></tip> |
| 18264 |
|
| 18265 |
</listitem> |
| 18266 |
</varlistentry> |
| 18267 |
|
| 18268 |
<varlistentry> |
| 18269 |
<term><anchor id="commref"><command>comm</command></term> |
| 18270 |
<indexterm> |
| 18271 |
<primary>comm</primary> |
| 18272 |
</indexterm> |
| 18273 |
<indexterm> |
| 18274 |
<primary>command</primary> |
| 18275 |
<secondary>comm</secondary> |
| 18276 |
</indexterm> |
| 18277 |
<listitem> |
| 18278 |
<para>Versatile file comparison utility. The files must be |
| 18279 |
sorted for this to be useful.</para> |
| 18280 |
|
| 18281 |
<para><command>comm |
| 18282 |
<replaceable>-options</replaceable> |
| 18283 |
<replaceable>first-file</replaceable> |
| 18284 |
<replaceable>second-file</replaceable></command></para> |
| 18285 |
|
| 18286 |
<para><userinput>comm file-1 file-2</userinput> outputs three columns: |
| 18287 |
<itemizedlist> |
| 18288 |
<listitem><para>column 1 = lines unique to <filename>file-1</filename></para> |
| 18289 |
</listitem> |
| 18290 |
|
| 18291 |
<listitem><para>column 2 = lines unique to <filename>file-2</filename></para> |
| 18292 |
</listitem> |
| 18293 |
|
| 18294 |
<listitem><para>column 3 = lines common to both.</para> |
| 18295 |
</listitem> |
| 18296 |
</itemizedlist></para> |
| 18297 |
|
| 18298 |
<para>The options allow suppressing output of one or more columns. |
| 18299 |
<itemizedlist> |
| 18300 |
<listitem><para><option>-1</option> suppresses column |
| 18301 |
<literal>1</literal></para> |
| 18302 |
</listitem> |
| 18303 |
<listitem><para><option>-2</option> suppresses column |
| 18304 |
<literal>2</literal></para> |
| 18305 |
</listitem> |
| 18306 |
<listitem><para><option>-3</option> suppresses column |
| 18307 |
<literal>3</literal></para> |
| 18308 |
</listitem> |
| 18309 |
<listitem><para><option>-12</option> suppresses both columns |
| 18310 |
<literal>1</literal> and <literal>2</literal>, etc.</para> |
| 18311 |
</listitem> |
| 18312 |
</itemizedlist> |
| 18313 |
</para> |
| 18314 |
|
| 18315 |
<para>This command is useful for comparing |
| 18316 |
<quote>dictionaries</quote> or <firstterm>word |
| 18317 |
lists</firstterm> -- sorted text files with one word per |
| 18318 |
line.</para> |
| 18319 |
|
| 18320 |
</listitem> |
| 18321 |
</varlistentry> |
| 18322 |
|
| 18323 |
|
| 18324 |
</variablelist> |
| 18325 |
|
| 18326 |
<variablelist id="fautils"> |
| 18327 |
<title><anchor id="fautils1">Utilities</title> |
| 18328 |
|
| 18329 |
<varlistentry> |
| 18330 |
<term><anchor id="basenameref"><command>basename</command></term> |
| 18331 |
<indexterm> |
| 18332 |
<primary>basename</primary> |
| 18333 |
</indexterm> |
| 18334 |
<indexterm> |
| 18335 |
<primary>command</primary> |
| 18336 |
<secondary>basename</secondary> |
| 18337 |
</indexterm> |
| 18338 |
<listitem><para>Strips the path information from a file name, printing |
| 18339 |
only the file name. The construction <userinput>basename |
| 18340 |
$0</userinput> lets the script know its name, that is, the name it |
| 18341 |
was invoked by. This can be used for <quote>usage</quote> messages if, |
| 18342 |
for example a script is called with missing arguments: |
| 18343 |
<programlisting>echo "Usage: `basename $0` arg1 arg2 ... argn"</programlisting> |
| 18344 |
</para> |
| 18345 |
|
| 18346 |
</listitem> |
| 18347 |
</varlistentry> |
| 18348 |
|
| 18349 |
<varlistentry> |
| 18350 |
<term><anchor id="dirnameref"><command>dirname</command></term> |
| 18351 |
<indexterm> |
| 18352 |
<primary>dirname</primary> |
| 18353 |
</indexterm> |
| 18354 |
<indexterm> |
| 18355 |
<primary>command</primary> |
| 18356 |
<secondary>dirname</secondary> |
| 18357 |
</indexterm> |
| 18358 |
<listitem><para>Strips the <command>basename</command> from |
| 18359 |
a filename, printing only the path information.</para> |
| 18360 |
<note> |
| 18361 |
<para><command>basename</command> and <command>dirname</command> |
| 18362 |
can operate on any arbitrary string. The argument |
| 18363 |
does not need to refer to an existing file, or |
| 18364 |
even be a filename for that matter (see <xref |
| 18365 |
linkend="daysbetween">).</para> |
| 18366 |
</note> |
| 18367 |
|
| 18368 |
<example id="ex35"> |
| 18369 |
<title><firstterm>basename</firstterm> and |
| 18370 |
<firstterm>dirname</firstterm></title> |
| 18371 |
<programlisting>&ex35;</programlisting> |
| 18372 |
</example> |
| 18373 |
|
| 18374 |
</listitem> |
| 18375 |
</varlistentry> |
| 18376 |
|
| 18377 |
<varlistentry> |
| 18378 |
<term><anchor id="splitref"><command>split</command></term> |
| 18379 |
<term><anchor id="csplitref"><command>csplit</command></term> |
| 18380 |
<indexterm> |
| 18381 |
<primary>split</primary> |
| 18382 |
</indexterm> |
| 18383 |
<indexterm> |
| 18384 |
<primary>command</primary> |
| 18385 |
<secondary>split</secondary> |
| 18386 |
</indexterm> |
| 18387 |
<indexterm> |
| 18388 |
<primary>csplit</primary> |
| 18389 |
</indexterm> |
| 18390 |
<indexterm> |
| 18391 |
<primary>command</primary> |
| 18392 |
<secondary>csplit</secondary> |
| 18393 |
</indexterm> |
| 18394 |
|
| 18395 |
<listitem> |
| 18396 |
<para>These are utilities for splitting a file into smaller |
| 18397 |
chunks. Their usual use is for splitting up large files |
| 18398 |
in order to back them up on floppies or preparatory to |
| 18399 |
e-mailing or uploading them.</para> |
| 18400 |
|
| 18401 |
<para>The <command>csplit</command> command splits a file |
| 18402 |
according to <firstterm>context</firstterm>, the split occuring |
| 18403 |
where patterns are matched.</para> |
| 18404 |
|
| 18405 |
<example id="splitcopy"> |
| 18406 |
<title>A script that copies itself in sections</title> |
| 18407 |
<programlisting>&splitcopy;</programlisting> |
| 18408 |
</example> |
| 18409 |
|
| 18410 |
</listitem> |
| 18411 |
</varlistentry> |
| 18412 |
|
| 18413 |
</variablelist> |
| 18414 |
|
| 18415 |
|
| 18416 |
|
| 18417 |
<variablelist id="faencencr"> |
| 18418 |
<title><anchor id="faencencr1">Encoding and Encryption</title> |
| 18419 |
|
| 18420 |
<varlistentry> |
| 18421 |
<term><anchor id="sumref"><command>sum</command></term> |
| 18422 |
<term><anchor id="cksumref"><command>cksum</command></term> |
| 18423 |
<term><anchor id="md5sumref"><command>md5sum</command></term> |
| 18424 |
<term><anchor id="sha1sumref"><command>sha1sum</command></term> |
| 18425 |
<indexterm> |
| 18426 |
<primary>sum</primary> |
| 18427 |
</indexterm> |
| 18428 |
<indexterm> |
| 18429 |
<primary>command</primary> |
| 18430 |
<secondary>sum</secondary> |
| 18431 |
</indexterm> |
| 18432 |
<indexterm> |
| 18433 |
<primary>cksum</primary> |
| 18434 |
</indexterm> |
| 18435 |
<indexterm> |
| 18436 |
<primary>command</primary> |
| 18437 |
<secondary>cksum</secondary> |
| 18438 |
</indexterm> |
| 18439 |
<indexterm> |
| 18440 |
<primary>md5sum</primary> |
| 18441 |
</indexterm> |
| 18442 |
<indexterm> |
| 18443 |
<primary>command</primary> |
| 18444 |
<secondary>md5sum</secondary> |
| 18445 |
</indexterm> |
| 18446 |
<indexterm> |
| 18447 |
<primary>command</primary> |
| 18448 |
<secondary>sha1sum</secondary> |
| 18449 |
</indexterm> |
| 18450 |
<listitem> |
| 18451 |
<para><anchor id="checksumref">These are utilities for |
| 18452 |
generating <firstterm>checksums</firstterm>. A |
| 18453 |
<firstterm>checksum</firstterm> is a number |
| 18454 |
|
| 18455 |
<footnote><para>The checksum may be expressed as a |
| 18456 |
<firstterm>hexadecimal</firstterm> number, or to some |
| 18457 |
other base.</para></footnote> |
| 18458 |
|
| 18459 |
mathematically calculated from the contents of a file, |
| 18460 |
for the purpose of checking its integrity. A script might |
| 18461 |
refer to a list of checksums for security purposes, such |
| 18462 |
as ensuring that the contents of key system files have not |
| 18463 |
been altered or corrupted. For security applications, use |
| 18464 |
the <command>md5sum</command> (<command>m</command>essage |
| 18465 |
<command>d</command>igest <command>5</command> |
| 18466 |
check<command>sum</command>) command, or better yet, the |
| 18467 |
newer <command>sha1sum</command> (Secure Hash Algorithm). |
| 18468 |
|
| 18469 |
<footnote><para>For even <emphasis>better</emphasis> |
| 18470 |
security, use the <firstterm>sha256sum</firstterm>, |
| 18471 |
<firstterm>sha512</firstterm>, and |
| 18472 |
<firstterm>sha1pass</firstterm> |
| 18473 |
commands.</para></footnote> |
| 18474 |
</para> |
| 18475 |
|
| 18476 |
|
| 18477 |
<para> |
| 18478 |
<screen> |
| 18479 |
<prompt>bash$ </prompt><userinput>cksum /boot/vmlinuz</userinput> |
| 18480 |
<computeroutput>1670054224 804083 /boot/vmlinuz</computeroutput> |
| 18481 |
|
| 18482 |
<prompt>bash$ </prompt><userinput>echo -n "Top Secret" | cksum</userinput> |
| 18483 |
<computeroutput>3391003827 10</computeroutput> |
| 18484 |
|
| 18485 |
|
| 18486 |
|
| 18487 |
<prompt>bash$ </prompt><userinput>md5sum /boot/vmlinuz</userinput> |
| 18488 |
<computeroutput>0f43eccea8f09e0a0b2b5cf1dcf333ba /boot/vmlinuz</computeroutput> |
| 18489 |
|
| 18490 |
<prompt>bash$ </prompt><userinput>echo -n "Top Secret" | md5sum</userinput> |
| 18491 |
<computeroutput>8babc97a6f62a4649716f4df8d61728f -</computeroutput> |
| 18492 |
</screen> |
| 18493 |
</para> |
| 18494 |
|
| 18495 |
|
| 18496 |
<note> |
| 18497 |
<para>The <command>cksum</command> command shows the size, |
| 18498 |
in bytes, of its target, whether file or |
| 18499 |
<filename>stdout</filename>.</para> |
| 18500 |
<para>The <command>md5sum</command> and |
| 18501 |
<command>sha1sum</command> commands display a |
| 18502 |
<link linkend="dashref2">dash</link> when they receive their input from |
| 18503 |
<filename>stdout</filename>.</para> |
| 18504 |
</note> |
| 18505 |
|
| 18506 |
<example id="fileintegrity"> |
| 18507 |
<title>Checking file integrity</title> |
| 18508 |
<programlisting>&fileintegrity;</programlisting> |
| 18509 |
</example> |
| 18510 |
|
| 18511 |
<para>Also see <xref linkend="directoryinfo">, <xref |
| 18512 |
linkend="horserace">, and <xref linkend="randstring"> for |
| 18513 |
creative uses of the <command>md5sum</command> command.</para> |
| 18514 |
|
| 18515 |
<note> |
| 18516 |
|
| 18517 |
<para> |
| 18518 |
There have been reports that the 128-bit |
| 18519 |
<command>md5sum</command> can be cracked, so the more secure |
| 18520 |
160-bit <command>sha1sum</command> is a welcome new addition |
| 18521 |
to the checksum toolkit. |
| 18522 |
</para> |
| 18523 |
|
| 18524 |
<screen><prompt>bash$ </prompt><userinput>md5sum testfile</userinput> |
| 18525 |
<computeroutput>e181e2c8720c60522c4c4c981108e367 testfile</computeroutput> |
| 18526 |
|
| 18527 |
|
| 18528 |
<prompt>bash$ </prompt><userinput>sha1sum testfile</userinput> |
| 18529 |
<computeroutput>5d7425a9c08a66c3177f1e31286fa40986ffc996 testfile</computeroutput> |
| 18530 |
</screen></note> |
| 18531 |
|
| 18532 |
<para>Security consultants have demonstrated that even |
| 18533 |
<command>sha1sum</command> can be compromised. Fortunately, |
| 18534 |
newer Linux distros include longer bit-length |
| 18535 |
<command>sha224sum</command>, |
| 18536 |
<command>sha256sum</command>, |
| 18537 |
<command>sha384sum</command>, and |
| 18538 |
<command>sha512sum</command> commands.</para> |
| 18539 |
|
| 18540 |
|
| 18541 |
</listitem> |
| 18542 |
</varlistentry> |
| 18543 |
|
| 18544 |
|
| 18545 |
|
| 18546 |
<varlistentry> |
| 18547 |
<term><anchor id="uuencoderef"><command>uuencode</command></term> |
| 18548 |
<indexterm> |
| 18549 |
<primary>uuencode</primary> |
| 18550 |
</indexterm> |
| 18551 |
<indexterm> |
| 18552 |
<primary>command</primary> |
| 18553 |
<secondary>uuencode</secondary> |
| 18554 |
</indexterm> |
| 18555 |
<listitem> |
| 18556 |
<para>This utility encodes binary files (images, sound files, |
| 18557 |
compressed files, etc.) into <link |
| 18558 |
linkend="asciidef">ASCII</link> characters, making |
| 18559 |
them suitable for transmission in the body of an |
| 18560 |
e-mail message or in a newsgroup posting. This is |
| 18561 |
especially useful where MIME (multimedia) encoding |
| 18562 |
is not available.</para> |
| 18563 |
</listitem> |
| 18564 |
</varlistentry> |
| 18565 |
|
| 18566 |
<varlistentry> |
| 18567 |
<term><anchor id="uudecoderef"><command>uudecode</command></term> |
| 18568 |
<indexterm> |
| 18569 |
<primary>uudecode</primary> |
| 18570 |
</indexterm> |
| 18571 |
<indexterm> |
| 18572 |
<primary>command</primary> |
| 18573 |
<secondary>uudecode</secondary> |
| 18574 |
</indexterm> |
| 18575 |
<listitem> |
| 18576 |
<para>This reverses the encoding, decoding |
| 18577 |
<firstterm>uuencoded</firstterm> files back into the |
| 18578 |
original binaries.</para> |
| 18579 |
|
| 18580 |
<example id="ex52"> |
| 18581 |
<title>Uudecoding encoded files</title> |
| 18582 |
<programlisting>&ex52;</programlisting> |
| 18583 |
</example> |
| 18584 |
|
| 18585 |
<tip><para>The <link linkend="foldref">fold -s</link> command |
| 18586 |
may be useful (possibly in a pipe) to process long uudecoded |
| 18587 |
text messages downloaded from Usenet newsgroups.</para></tip> |
| 18588 |
|
| 18589 |
</listitem> |
| 18590 |
</varlistentry> |
| 18591 |
|
| 18592 |
<varlistentry> |
| 18593 |
<term><anchor id="mimencoderef"><command>mimencode</command></term> |
| 18594 |
<term><anchor id="mmencoderef"><command>mmencode</command></term> |
| 18595 |
<indexterm> |
| 18596 |
<primary>mimencode</primary> |
| 18597 |
</indexterm> |
| 18598 |
<indexterm> |
| 18599 |
<primary>command</primary> |
| 18600 |
<secondary>mime</secondary> |
| 18601 |
</indexterm> |
| 18602 |
<indexterm> |
| 18603 |
<primary>mmencode</primary> |
| 18604 |
</indexterm> |
| 18605 |
<indexterm> |
| 18606 |
<primary>command</primary> |
| 18607 |
<secondary>encode</secondary> |
| 18608 |
</indexterm> |
| 18609 |
<listitem> |
| 18610 |
<para>The <command>mimencode</command> and |
| 18611 |
<command>mmencode</command> commands process |
| 18612 |
multimedia-encoded e-mail attachments. Although |
| 18613 |
<firstterm>mail user agents</firstterm> (such as |
| 18614 |
<firstterm>pine</firstterm> or <firstterm>kmail</firstterm>) |
| 18615 |
normally handle this automatically, these particular |
| 18616 |
utilities permit manipulating such attachments manually from |
| 18617 |
the command-line or in <link linkend="batchprocref">batch |
| 18618 |
processing mode</link> by means of a shell script.</para> |
| 18619 |
</listitem> |
| 18620 |
</varlistentry> |
| 18621 |
|
| 18622 |
<varlistentry> |
| 18623 |
<term><anchor id="cryptref"><command>crypt</command></term> |
| 18624 |
<indexterm> |
| 18625 |
<primary>crypt</primary> |
| 18626 |
</indexterm> |
| 18627 |
<indexterm> |
| 18628 |
<primary>command</primary> |
| 18629 |
<secondary>crypt</secondary> |
| 18630 |
</indexterm> |
| 18631 |
<listitem> |
| 18632 |
<para>At one time, this was the standard UNIX file encryption |
| 18633 |
utility. |
| 18634 |
|
| 18635 |
<footnote><para>This is a symmetric block cipher, used to |
| 18636 |
encrypt files on a single system or local network, |
| 18637 |
as opposed to the <firstterm>public key</firstterm> |
| 18638 |
cipher class, of which <firstterm>pgp</firstterm> is a |
| 18639 |
well-known example.</para></footnote> |
| 18640 |
|
| 18641 |
Politically-motivated government regulations |
| 18642 |
prohibiting the export of encryption software resulted |
| 18643 |
in the disappearance of <command>crypt</command> |
| 18644 |
from much of the UNIX world, and it is still |
| 18645 |
missing from most Linux distributions. Fortunately, |
| 18646 |
programmers have come up with a number of decent |
| 18647 |
alternatives to it, among them the author's very own <ulink |
| 18648 |
url="ftp://metalab.unc.edu/pub/Linux/utils/file/cruft-0.2.tar.gz">cruft</ulink> |
| 18649 |
(see <xref linkend="encryptedpw">). </para> |
| 18650 |
|
| 18651 |
</listitem> |
| 18652 |
</varlistentry> |
| 18653 |
|
| 18654 |
|
| 18655 |
|
| 18656 |
<varlistentry> |
| 18657 |
<term><anchor id="opensslref"><command>openssl</command></term> |
| 18658 |
<indexterm> |
| 18659 |
<primary>openssl</primary> |
| 18660 |
</indexterm> |
| 18661 |
<indexterm> |
| 18662 |
<primary>command</primary> |
| 18663 |
<secondary>SSL</secondary> |
| 18664 |
</indexterm> |
| 18665 |
<listitem> |
| 18666 |
<para>This is an Open Source implementation of |
| 18667 |
<firstterm>Secure Sockets Layer</firstterm> encryption. |
| 18668 |
|
| 18669 |
<programlisting># To encrypt a file: |
| 18670 |
openssl aes-128-ecb -salt -in file.txt -out file.encrypted \ |
| 18671 |
-pass pass:my_password |
| 18672 |
# ^^^^^^^^^^^ User-selected password. |
| 18673 |
# aes-128-ecb is the encryption method chosen. |
| 18674 |
|
| 18675 |
# To decrypt an openssl-encrypted file: |
| 18676 |
openssl aes-128-ecb -d -salt -in file.encrypted -out file.txt \ |
| 18677 |
-pass pass:my_password |
| 18678 |
# ^^^^^^^^^^^ User-selected password.</programlisting></para> |
| 18679 |
|
| 18680 |
<para><link linkend="piperef">Piping</link> |
| 18681 |
<firstterm>openssl</firstterm> to/from <link |
| 18682 |
linkend="tarref">tar</link> makes it possible to encrypt |
| 18683 |
an entire directory tree. |
| 18684 |
|
| 18685 |
<programlisting># To encrypt a directory: |
| 18686 |
|
| 18687 |
sourcedir="/home/bozo/testfiles" |
| 18688 |
encrfile="encr-dir.tar.gz" |
| 18689 |
password=my_secret_password |
| 18690 |
|
| 18691 |
tar czvf - "$sourcedir" | |
| 18692 |
openssl des3 -salt -out "$encrfile" -pass pass:"$password" |
| 18693 |
# ^^^^ Uses des3 encryption. |
| 18694 |
# Writes encrypted file "encr-dir.tar.gz" in current working directory. |
| 18695 |
|
| 18696 |
# To decrypt the resulting tarball: |
| 18697 |
openssl des3 -d -salt -in "$encrfile" -pass pass:"$password" | |
| 18698 |
tar -xzv |
| 18699 |
# Decrypts and unpacks into current working directory.</programlisting> |
| 18700 |
|
| 18701 |
</para> |
| 18702 |
|
| 18703 |
<para>Of course, <firstterm>openssl</firstterm> has many other uses, |
| 18704 |
such as obtaining signed <firstterm>certificates</firstterm> |
| 18705 |
for Web sites. See the <link linkend="inforef">info</link> |
| 18706 |
page.</para> |
| 18707 |
|
| 18708 |
</listitem> |
| 18709 |
</varlistentry> |
| 18710 |
|
| 18711 |
|
| 18712 |
|
| 18713 |
<varlistentry> |
| 18714 |
<term><anchor id="shredref"><command>shred</command></term> |
| 18715 |
<indexterm> |
| 18716 |
<primary>shred</primary> |
| 18717 |
</indexterm> |
| 18718 |
<indexterm> |
| 18719 |
<primary>command</primary> |
| 18720 |
<secondary>secure delete</secondary> |
| 18721 |
</indexterm> |
| 18722 |
<listitem> |
| 18723 |
|
| 18724 |
<para>Securely erase a file by overwriting it multiple times with |
| 18725 |
random bit patterns before deleting it. This command has |
| 18726 |
the same effect as <xref linkend="blotout">, but does it |
| 18727 |
in a more thorough and elegant manner.</para> |
| 18728 |
|
| 18729 |
<para>This is one of the GNU |
| 18730 |
<firstterm>fileutils</firstterm>.</para> |
| 18731 |
|
| 18732 |
<caution><para>Advanced forensic technology may still be able to |
| 18733 |
recover the contents of a file, even after application of |
| 18734 |
<command>shred</command>.</para></caution> |
| 18735 |
|
| 18736 |
</listitem> |
| 18737 |
</varlistentry> |
| 18738 |
|
| 18739 |
|
| 18740 |
</variablelist> |
| 18741 |
|
| 18742 |
|
| 18743 |
<variablelist id="famisc"> |
| 18744 |
<title><anchor id="famisc1">Miscellaneous</title> |
| 18745 |
|
| 18746 |
<varlistentry> |
| 18747 |
<term><anchor id="mktempref"><command>mktemp</command></term> |
| 18748 |
<indexterm> |
| 18749 |
<primary>temporary</primary> |
| 18750 |
</indexterm> |
| 18751 |
<indexterm> |
| 18752 |
<primary>command</primary> |
| 18753 |
<secondary>filename</secondary> |
| 18754 |
</indexterm> |
| 18755 |
<listitem> |
| 18756 |
<para>Create a <firstterm>temporary file</firstterm> |
| 18757 |
|
| 18758 |
<footnote><para>Creates a temporary |
| 18759 |
<firstterm>directory</firstterm> when invoked with the |
| 18760 |
<option>-d</option> option.</para></footnote> |
| 18761 |
|
| 18762 |
with a <quote>unique</quote> filename. When invoked |
| 18763 |
from the command-line without additional arguments, |
| 18764 |
it creates a zero-length file in the <filename |
| 18765 |
class="directory">/tmp</filename> directory.</para> |
| 18766 |
|
| 18767 |
|
| 18768 |
<para> |
| 18769 |
<screen><prompt>bash$ </prompt><userinput>mktemp</userinput> |
| 18770 |
<computeroutput>/tmp/tmp.zzsvql3154</computeroutput> |
| 18771 |
</screen> |
| 18772 |
</para> |
| 18773 |
|
| 18774 |
<para><programlisting>PREFIX=filename |
| 18775 |
tempfile=`mktemp $PREFIX.XXXXXX` |
| 18776 |
# ^^^^^^ Need at least 6 placeholders |
| 18777 |
#+ in the filename template. |
| 18778 |
# If no filename template supplied, |
| 18779 |
#+ "tmp.XXXXXXXXXX" is the default. |
| 18780 |
|
| 18781 |
echo "tempfile name = $tempfile" |
| 18782 |
# tempfile name = filename.QA2ZpY |
| 18783 |
# or something similar... |
| 18784 |
|
| 18785 |
# Creates a file of that name in the current working directory |
| 18786 |
#+ with 600 file permissions. |
| 18787 |
# A "umask 177" is therefore unnecessary, |
| 18788 |
#+ but it's good programming practice nevertheless.</programlisting></para> |
| 18789 |
</listitem> |
| 18790 |
</varlistentry> |
| 18791 |
|
| 18792 |
<varlistentry> |
| 18793 |
<term><anchor id="makeref"><command>make</command></term> |
| 18794 |
<indexterm> |
| 18795 |
<primary>make</primary> |
| 18796 |
</indexterm> |
| 18797 |
<indexterm> |
| 18798 |
<primary>command</primary> |
| 18799 |
<secondary>Makefile</secondary> |
| 18800 |
</indexterm> |
| 18801 |
<listitem> |
| 18802 |
<para><anchor id="makefileref"></para> |
| 18803 |
<para>Utility for building and compiling binary packages. |
| 18804 |
This can also be used for any set of operations triggered |
| 18805 |
by incremental changes in source files.</para> |
| 18806 |
<para>The <firstterm>make</firstterm> command checks a |
| 18807 |
<filename>Makefile</filename>, a list of file dependencies and |
| 18808 |
operations to be carried out.</para> |
| 18809 |
<para>The <firstterm>make</firstterm> utility is, in effect, |
| 18810 |
a powerful scripting language similar in many ways to |
| 18811 |
<firstterm>Bash</firstterm>, but with the capability of |
| 18812 |
recognizing <firstterm>dependencies</firstterm>. For in-depth |
| 18813 |
coverage of this useful tool set, see the <ulink |
| 18814 |
url="http://www.gnu.org/manual/manual.html">GNU software |
| 18815 |
documentation site</ulink>.</para> |
| 18816 |
</listitem> |
| 18817 |
</varlistentry> |
| 18818 |
|
| 18819 |
<varlistentry> |
| 18820 |
<term><anchor id="installref"><command>install</command></term> |
| 18821 |
<indexterm> |
| 18822 |
<primary>install</primary> |
| 18823 |
</indexterm> |
| 18824 |
<indexterm> |
| 18825 |
<primary>command</primary> |
| 18826 |
<secondary>install</secondary> |
| 18827 |
</indexterm> |
| 18828 |
<listitem> |
| 18829 |
<para>Special purpose file copying command, similar to |
| 18830 |
<link linkend="cpref">cp</link>, but capable of |
| 18831 |
setting permissions and attributes of the copied |
| 18832 |
files. This command seems tailormade for installing |
| 18833 |
software packages, and as such it shows up frequently in |
| 18834 |
<filename>Makefiles</filename> (in the <replaceable>make |
| 18835 |
install :</replaceable> section). It could likewise prove |
| 18836 |
useful in installation scripts.</para> |
| 18837 |
</listitem> |
| 18838 |
</varlistentry> |
| 18839 |
|
| 18840 |
<varlistentry> |
| 18841 |
<term><anchor id="dos2unixref"><command>dos2unix</command></term> |
| 18842 |
<indexterm> |
| 18843 |
<primary>dos2unix</primary> |
| 18844 |
</indexterm> |
| 18845 |
<indexterm> |
| 18846 |
<primary>command</primary> |
| 18847 |
<secondary>file converter</secondary> |
| 18848 |
</indexterm> |
| 18849 |
<listitem> |
| 18850 |
<para>This utility, written by Benjamin Lin and collaborators, |
| 18851 |
converts DOS-formatted text files (lines terminated by |
| 18852 |
CR-LF) to UNIX format (lines terminated by LF only), |
| 18853 |
and <link linkend="dosnewlines">vice-versa</link>.</para> |
| 18854 |
</listitem> |
| 18855 |
</varlistentry> |
| 18856 |
|
| 18857 |
<varlistentry> |
| 18858 |
<term><anchor id="ptxref"><command>ptx</command></term> |
| 18859 |
<indexterm> |
| 18860 |
<primary>ptx</primary> |
| 18861 |
</indexterm> |
| 18862 |
<indexterm> |
| 18863 |
<primary>command</primary> |
| 18864 |
<secondary>index</secondary> |
| 18865 |
</indexterm> |
| 18866 |
<listitem> |
| 18867 |
<para>The <command>ptx [targetfile]</command> command |
| 18868 |
outputs a permuted index (cross-reference list) of the |
| 18869 |
targetfile. This may be further filtered and formatted in a |
| 18870 |
pipe, if necessary.</para> |
| 18871 |
</listitem> |
| 18872 |
</varlistentry> |
| 18873 |
|
| 18874 |
<varlistentry> |
| 18875 |
<term><anchor id="moreref"><command>more</command></term> |
| 18876 |
<term><anchor id="lessref"><command>less</command></term> |
| 18877 |
<indexterm> |
| 18878 |
<primary>more</primary> |
| 18879 |
</indexterm> |
| 18880 |
<indexterm> |
| 18881 |
<primary>command</primary> |
| 18882 |
<secondary>more</secondary> |
| 18883 |
</indexterm> |
| 18884 |
<indexterm> |
| 18885 |
<primary>less</primary> |
| 18886 |
</indexterm> |
| 18887 |
<indexterm> |
| 18888 |
<primary>command</primary> |
| 18889 |
<secondary>less</secondary> |
| 18890 |
</indexterm> |
| 18891 |
<listitem> |
| 18892 |
|
| 18893 |
<para>Pagers that display a text file or stream to |
| 18894 |
<filename>stdout</filename>, one screenful at a time. |
| 18895 |
These may be used to filter the output of |
| 18896 |
<filename>stdout</filename> . . . or of a script.</para> |
| 18897 |
|
| 18898 |
<para> |
| 18899 |
An interesting application of <firstterm>more</firstterm> |
| 18900 |
is to <quote>test drive</quote> a command sequence, |
| 18901 |
to forestall potentially unpleasant consequences. |
| 18902 |
<programlisting>ls /home/bozo | awk '{print "rm -rf " $1}' | more |
| 18903 |
# ^^^^ |
| 18904 |
|
| 18905 |
# Testing the effect of the following (disastrous) command-line: |
| 18906 |
# ls /home/bozo | awk '{print "rm -rf " $1}' | sh |
| 18907 |
# Hand off to the shell to execute . . . ^^</programlisting> |
| 18908 |
</para> |
| 18909 |
|
| 18910 |
<para>The <firstterm>less</firstterm> pager has the |
| 18911 |
interesting property of doing a formatted display of |
| 18912 |
<firstterm>man page</firstterm> source. See <xref |
| 18913 |
linkend="maned">.</para> |
| 18914 |
|
| 18915 |
</listitem> |
| 18916 |
</varlistentry> |
| 18917 |
|
| 18918 |
</variablelist> |
| 18919 |
|
| 18920 |
</sect1> <!-- End File and Archiving Commands --> |
| 18921 |
|
| 18922 |
|
| 18923 |
|
| 18924 |
<sect1 id="communications"> |
| 18925 |
<title>Communications Commands</title> |
| 18926 |
|
| 18927 |
<para>Certain of the following commands find use in |
| 18928 |
network data transfer and analysis, as well as in |
| 18929 |
<link linkend="cspammers">chasing spammers</link>.</para> |
| 18930 |
|
| 18931 |
<variablelist id="communinfo"> |
| 18932 |
<title><anchor id="communinfo1">Information and Statistics</title> |
| 18933 |
|
| 18934 |
<varlistentry> |
| 18935 |
<term><anchor id="hostref"><command>host</command></term> |
| 18936 |
<indexterm> |
| 18937 |
<primary>host</primary> |
| 18938 |
</indexterm> |
| 18939 |
<indexterm> |
| 18940 |
<primary>command</primary> |
| 18941 |
<secondary>host</secondary> |
| 18942 |
</indexterm> |
| 18943 |
<listitem> |
| 18944 |
<para>Searches for information about an Internet host by name or |
| 18945 |
IP address, using DNS.</para> |
| 18946 |
<para> |
| 18947 |
<screen><prompt>bash$ </prompt><userinput>host surfacemail.com</userinput> |
| 18948 |
<computeroutput>surfacemail.com. has address 202.92.42.236</computeroutput> |
| 18949 |
</screen> |
| 18950 |
</para> |
| 18951 |
</listitem> |
| 18952 |
</varlistentry> |
| 18953 |
|
| 18954 |
<varlistentry> |
| 18955 |
<term><anchor id="ipcalcref"><command>ipcalc</command></term> |
| 18956 |
<indexterm> |
| 18957 |
<primary>ipcalc</primary> |
| 18958 |
</indexterm> |
| 18959 |
<indexterm> |
| 18960 |
<primary>command</primary> |
| 18961 |
<secondary>ipcalc</secondary> |
| 18962 |
</indexterm> |
| 18963 |
<listitem> |
| 18964 |
<para>Displays IP information for a host. |
| 18965 |
With the <option>-h</option> option, |
| 18966 |
<command>ipcalc</command> does a reverse DNS lookup, finding |
| 18967 |
the name of the host (server) from the IP address.</para> |
| 18968 |
<para> |
| 18969 |
<screen><prompt>bash$ </prompt><userinput>ipcalc -h 202.92.42.236</userinput> |
| 18970 |
<computeroutput>HOSTNAME=surfacemail.com</computeroutput> |
| 18971 |
</screen> |
| 18972 |
</para> |
| 18973 |
</listitem> |
| 18974 |
</varlistentry> |
| 18975 |
|
| 18976 |
<varlistentry> |
| 18977 |
<term><anchor id="nslookupref"><command>nslookup</command></term> |
| 18978 |
<indexterm> |
| 18979 |
<primary>nslookup</primary> |
| 18980 |
</indexterm> |
| 18981 |
<indexterm> |
| 18982 |
<primary>command</primary> |
| 18983 |
<secondary>name server lookup</secondary> |
| 18984 |
</indexterm> |
| 18985 |
<listitem> |
| 18986 |
|
| 18987 |
<para>Do an Internet <quote>name server lookup</quote> |
| 18988 |
on a host by IP address. This is essentially equivalent |
| 18989 |
to <command>ipcalc -h</command> or <command>dig -x |
| 18990 |
</command>. The command may be run either interactively |
| 18991 |
or noninteractively, i.e., from within a script.</para> |
| 18992 |
|
| 18993 |
<para>The <command>nslookup</command> command has allegedly |
| 18994 |
been <quote>deprecated,</quote> but it is still useful.</para> |
| 18995 |
|
| 18996 |
<para> |
| 18997 |
<screen><prompt>bash$ </prompt><userinput>nslookup -sil 66.97.104.180</userinput> |
| 18998 |
<computeroutput>nslookup kuhleersparnis.ch |
| 18999 |
Server: 135.116.137.2 |
| 19000 |
Address: 135.116.137.2#53 |
| 19001 |
|
| 19002 |
Non-authoritative answer: |
| 19003 |
Name: kuhleersparnis.ch</computeroutput> |
| 19004 |
</screen> |
| 19005 |
</para> |
| 19006 |
|
| 19007 |
</listitem> |
| 19008 |
</varlistentry> |
| 19009 |
|
| 19010 |
<varlistentry> |
| 19011 |
<term><anchor id="digref"><command>dig</command></term> |
| 19012 |
<indexterm> |
| 19013 |
<primary>dig</primary> |
| 19014 |
</indexterm> |
| 19015 |
<indexterm> |
| 19016 |
<primary>command</primary> |
| 19017 |
<secondary>domain information groper</secondary> |
| 19018 |
</indexterm> |
| 19019 |
<listitem> |
| 19020 |
|
| 19021 |
<para><command>D</command>omain <command>I</command>nformation |
| 19022 |
<command>G</command>roper. Similar to |
| 19023 |
<command>nslookup</command>, <firstterm>dig</firstterm> does |
| 19024 |
an Internet <firstterm>name server lookup</firstterm> on a host. |
| 19025 |
May be run from the command-line or from within a script.</para> |
| 19026 |
|
| 19027 |
<para>Some interesting options to <firstterm>dig</firstterm> are |
| 19028 |
<option>+time=N</option> for setting a query timeout to |
| 19029 |
<parameter>N</parameter> seconds, <option>+nofail</option> for |
| 19030 |
continuing to query servers until a reply is received, and |
| 19031 |
<option>-x</option> for doing a reverse address lookup.</para> |
| 19032 |
|
| 19033 |
<para>Compare the output of <command>dig -x</command> with |
| 19034 |
<command>ipcalc -h</command> and |
| 19035 |
<command>nslookup</command>.</para> |
| 19036 |
|
| 19037 |
<para> |
| 19038 |
<screen><prompt>bash$ </prompt><userinput>dig -x 81.9.6.2</userinput> |
| 19039 |
<computeroutput>;; Got answer: |
| 19040 |
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 11649 |
| 19041 |
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0 |
| 19042 |
|
| 19043 |
;; QUESTION SECTION: |
| 19044 |
;2.6.9.81.in-addr.arpa. IN PTR |
| 19045 |
|
| 19046 |
;; AUTHORITY SECTION: |
| 19047 |
6.9.81.in-addr.arpa. 3600 IN SOA ns.eltel.net. noc.eltel.net. |
| 19048 |
2002031705 900 600 86400 3600 |
| 19049 |
|
| 19050 |
;; Query time: 537 msec |
| 19051 |
;; SERVER: 135.116.137.2#53(135.116.137.2) |
| 19052 |
;; WHEN: Wed Jun 26 08:35:24 2002 |
| 19053 |
;; MSG SIZE rcvd: 91</computeroutput> |
| 19054 |
</screen> |
| 19055 |
</para> |
| 19056 |
|
| 19057 |
<para><anchor id="spamlookup_0"></para> |
| 19058 |
<example id="spamlookup"> |
| 19059 |
<title>Finding out where to report a spammer</title> |
| 19060 |
<programlisting>&spamlookup;</programlisting> |
| 19061 |
</example> |
| 19062 |
|
| 19063 |
<para><anchor id="isspammer_0"></para> |
| 19064 |
<example id="isspammer"> |
| 19065 |
<title>Analyzing a spam domain</title> |
| 19066 |
<programlisting>&isspammer;</programlisting> |
| 19067 |
</example> |
| 19068 |
|
| 19069 |
<para>For a much more elaborate version of the above script, see |
| 19070 |
<xref linkend="isspammer2">.</para> |
| 19071 |
|
| 19072 |
</listitem> |
| 19073 |
</varlistentry> |
| 19074 |
|
| 19075 |
<varlistentry> |
| 19076 |
<term><anchor id="tracerouteref"><command>traceroute</command></term> |
| 19077 |
<indexterm> |
| 19078 |
<primary>traceroute</primary> |
| 19079 |
</indexterm> |
| 19080 |
<indexterm> |
| 19081 |
<primary>command</primary> |
| 19082 |
<secondary>traceroute</secondary> |
| 19083 |
</indexterm> |
| 19084 |
<listitem> |
| 19085 |
|
| 19086 |
<para>Trace the route taken by packets sent to a remote host. This |
| 19087 |
command works within a LAN, WAN, or over the |
| 19088 |
Internet. The remote host may be specified by an IP |
| 19089 |
address. The output of this command may be filtered |
| 19090 |
by <link linkend="grepref">grep</link> or <link |
| 19091 |
linkend="sedref">sed</link> in a pipe.</para> |
| 19092 |
|
| 19093 |
<para> |
| 19094 |
<screen><prompt>bash$ </prompt><userinput>traceroute 81.9.6.2</userinput> |
| 19095 |
<computeroutput>traceroute to 81.9.6.2 (81.9.6.2), 30 hops max, 38 byte packets |
| 19096 |
1 tc43.xjbnnbrb.com (136.30.178.8) 191.303 ms 179.400 ms 179.767 ms |
| 19097 |
2 or0.xjbnnbrb.com (136.30.178.1) 179.536 ms 179.534 ms 169.685 ms |
| 19098 |
3 192.168.11.101 (192.168.11.101) 189.471 ms 189.556 ms * |
| 19099 |
...</computeroutput> |
| 19100 |
</screen> |
| 19101 |
</para> |
| 19102 |
|
| 19103 |
</listitem> |
| 19104 |
</varlistentry> |
| 19105 |
|
| 19106 |
<varlistentry> |
| 19107 |
<term><anchor id="pingref"><command>ping</command></term> |
| 19108 |
<indexterm> |
| 19109 |
<primary>ping</primary> |
| 19110 |
</indexterm> |
| 19111 |
<indexterm> |
| 19112 |
<primary>command</primary> |
| 19113 |
<secondary>ping</secondary> |
| 19114 |
</indexterm> |
| 19115 |
<listitem> |
| 19116 |
|
| 19117 |
<para>Broadcast an <replaceable>ICMP |
| 19118 |
ECHO_REQUEST</replaceable> packet to another machine, |
| 19119 |
either on a local or remote network. This is a |
| 19120 |
diagnostic tool for testing network connections, |
| 19121 |
and it should be used with caution.</para> |
| 19122 |
|
| 19123 |
|
| 19124 |
<para> |
| 19125 |
<screen><prompt>bash$ </prompt><userinput>ping localhost</userinput> |
| 19126 |
<computeroutput>PING localhost.localdomain (127.0.0.1) from 127.0.0.1 : 56(84) bytes of data. |
| 19127 |
64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=0 ttl=255 time=709 usec |
| 19128 |
64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=1 ttl=255 time=286 usec |
| 19129 |
|
| 19130 |
--- localhost.localdomain ping statistics --- |
| 19131 |
2 packets transmitted, 2 packets received, 0% packet loss |
| 19132 |
round-trip min/avg/max/mdev = 0.286/0.497/0.709/0.212 ms</computeroutput> |
| 19133 |
</screen> |
| 19134 |
</para> |
| 19135 |
|
| 19136 |
<para>A successful <firstterm>ping</firstterm> returns |
| 19137 |
an <link linkend="exitstatusref">exit status</link> of |
| 19138 |
<errorcode>0</errorcode>. This can be tested for in a |
| 19139 |
script.</para> |
| 19140 |
<para><anchor id="ping0"></para> |
| 19141 |
|
| 19142 |
<para><programlisting> HNAME=news-15.net # Notorious spammer. |
| 19143 |
# HNAME=$HOST # Debug: test for localhost. |
| 19144 |
count=2 # Send only two pings. |
| 19145 |
|
| 19146 |
if [[ `ping -c $count "$HNAME"` ]] |
| 19147 |
then |
| 19148 |
echo ""$HNAME" still up and broadcasting spam your way." |
| 19149 |
else |
| 19150 |
echo ""$HNAME" seems to be down. Pity." |
| 19151 |
fi</programlisting></para> |
| 19152 |
|
| 19153 |
</listitem> |
| 19154 |
</varlistentry> |
| 19155 |
|
| 19156 |
<varlistentry> |
| 19157 |
<term><anchor id="whoisref"><command>whois</command></term> |
| 19158 |
<indexterm> |
| 19159 |
<primary>whois</primary> |
| 19160 |
</indexterm> |
| 19161 |
<indexterm> |
| 19162 |
<primary>command</primary> |
| 19163 |
<secondary>domain name server</secondary> |
| 19164 |
</indexterm> |
| 19165 |
<listitem> |
| 19166 |
<para>Perform a DNS (Domain Name System) lookup. |
| 19167 |
The <option>-h</option> option permits specifying which |
| 19168 |
particular <firstterm>whois</firstterm> server to query. See |
| 19169 |
<xref linkend="ex18"> and <xref linkend="spamlookup">.</para> |
| 19170 |
</listitem> |
| 19171 |
</varlistentry> |
| 19172 |
|
| 19173 |
<varlistentry> |
| 19174 |
<term><anchor id="fingerref"><command>finger</command></term> |
| 19175 |
<indexterm> |
| 19176 |
<primary>finger</primary> |
| 19177 |
</indexterm> |
| 19178 |
<indexterm> |
| 19179 |
<primary>command</primary> |
| 19180 |
<secondary>finger</secondary> |
| 19181 |
</indexterm> |
| 19182 |
<listitem> |
| 19183 |
|
| 19184 |
<para>Retrieve information about users on a |
| 19185 |
network. Optionally, this command can display |
| 19186 |
a user's <filename>~/.plan</filename>, |
| 19187 |
<filename>~/.project</filename>, and |
| 19188 |
<filename>~/.forward</filename> files, if present.</para> |
| 19189 |
|
| 19190 |
<para> |
| 19191 |
<screen> |
| 19192 |
<prompt>bash$ </prompt><userinput>finger</userinput> |
| 19193 |
<computeroutput>Login Name Tty Idle Login Time Office Office Phone |
| 19194 |
bozo Bozo Bozeman tty1 8 Jun 25 16:59 (:0) |
| 19195 |
bozo Bozo Bozeman ttyp0 Jun 25 16:59 (:0.0) |
| 19196 |
bozo Bozo Bozeman ttyp1 Jun 25 17:07 (:0.0)</computeroutput> |
| 19197 |
|
| 19198 |
|
| 19199 |
|
| 19200 |
<prompt>bash$ </prompt><userinput>finger bozo</userinput> |
| 19201 |
<computeroutput>Login: bozo Name: Bozo Bozeman |
| 19202 |
Directory: /home/bozo Shell: /bin/bash |
| 19203 |
Office: 2355 Clown St., 543-1234 |
| 19204 |
On since Fri Aug 31 20:13 (MST) on tty1 1 hour 38 minutes idle |
| 19205 |
On since Fri Aug 31 20:13 (MST) on pts/0 12 seconds idle |
| 19206 |
On since Fri Aug 31 20:13 (MST) on pts/1 |
| 19207 |
On since Fri Aug 31 20:31 (MST) on pts/2 1 hour 16 minutes idle |
| 19208 |
Mail last read Tue Jul 3 10:08 2007 (MST) |
| 19209 |
No Plan.</computeroutput> |
| 19210 |
</screen> |
| 19211 |
</para> |
| 19212 |
|
| 19213 |
<para>Out of security considerations, many networks disable |
| 19214 |
<command>finger</command> and its associated daemon. |
| 19215 |
<footnote> |
| 19216 |
<para><anchor id="daemonref"></para> |
| 19217 |
<para>A <firstterm>daemon</firstterm> is a background |
| 19218 |
process not attached to a terminal session. Daemons |
| 19219 |
perform designated services either at specified times |
| 19220 |
or explicitly triggered by certain events.</para> |
| 19221 |
<para>The word <quote>daemon</quote> means ghost in |
| 19222 |
Greek, and there is certainly something mysterious, |
| 19223 |
almost supernatural, about the way UNIX daemons |
| 19224 |
wander about behind the scenes, silently carrying |
| 19225 |
out their appointed tasks.</para> |
| 19226 |
</footnote> |
| 19227 |
</para> |
| 19228 |
|
| 19229 |
</listitem> |
| 19230 |
</varlistentry> |
| 19231 |
|
| 19232 |
<varlistentry> |
| 19233 |
<term><anchor id="chfnref"><command>chfn</command></term> |
| 19234 |
<indexterm> |
| 19235 |
<primary>chfn</primary> |
| 19236 |
</indexterm> |
| 19237 |
<indexterm> |
| 19238 |
<primary>command</primary> |
| 19239 |
<secondary>finger</secondary> |
| 19240 |
</indexterm> |
| 19241 |
<listitem> |
| 19242 |
<para>Change information disclosed by the |
| 19243 |
<command>finger</command> command.</para> |
| 19244 |
</listitem> |
| 19245 |
</varlistentry> |
| 19246 |
|
| 19247 |
<varlistentry> |
| 19248 |
<term><anchor id="vrfyref"><command>vrfy</command></term> |
| 19249 |
<indexterm> |
| 19250 |
<primary>vrfy</primary> |
| 19251 |
</indexterm> |
| 19252 |
<indexterm> |
| 19253 |
<primary>command</primary> |
| 19254 |
<secondary>vrfy</secondary> |
| 19255 |
</indexterm> |
| 19256 |
<listitem> |
| 19257 |
<para>Verify an Internet e-mail address.</para> |
| 19258 |
|
| 19259 |
<para>This command seems to be missing from newer Linux |
| 19260 |
distros.</para> |
| 19261 |
</listitem> |
| 19262 |
</varlistentry> |
| 19263 |
|
| 19264 |
</variablelist> |
| 19265 |
|
| 19266 |
|
| 19267 |
<variablelist id="commremote"> |
| 19268 |
<title><anchor id="commremote1">Remote Host Access</title> |
| 19269 |
|
| 19270 |
<varlistentry> |
| 19271 |
<term><anchor id="rxref"><command>sx</command></term> |
| 19272 |
<term><command>rx</command></term> |
| 19273 |
<indexterm> |
| 19274 |
<primary>sx</primary> |
| 19275 |
</indexterm> |
| 19276 |
<indexterm> |
| 19277 |
<primary>command</primary> |
| 19278 |
<secondary>sx</secondary> |
| 19279 |
</indexterm> |
| 19280 |
<indexterm> |
| 19281 |
<primary>rx</primary> |
| 19282 |
</indexterm> |
| 19283 |
<indexterm> |
| 19284 |
<primary>command</primary> |
| 19285 |
<secondary>rx</secondary> |
| 19286 |
</indexterm> |
| 19287 |
<listitem> |
| 19288 |
<para>The <command>sx</command> and <command>rx</command> |
| 19289 |
command set serves to transfer files to and from a remote |
| 19290 |
host using the <firstterm>xmodem</firstterm> protocol. These |
| 19291 |
are generally part of a communications package, such as |
| 19292 |
<command>minicom</command>.</para> |
| 19293 |
</listitem> |
| 19294 |
</varlistentry> |
| 19295 |
|
| 19296 |
<varlistentry> |
| 19297 |
<term><anchor id="rzref"><command>sz</command></term> |
| 19298 |
<term><command>rz</command></term> |
| 19299 |
<indexterm> |
| 19300 |
<primary>sz</primary> |
| 19301 |
</indexterm> |
| 19302 |
<indexterm> |
| 19303 |
<primary>command</primary> |
| 19304 |
<secondary>sz</secondary> |
| 19305 |
</indexterm> |
| 19306 |
<indexterm> |
| 19307 |
<primary>rz</primary> |
| 19308 |
</indexterm> |
| 19309 |
<indexterm> |
| 19310 |
<primary>command</primary> |
| 19311 |
<secondary>rz</secondary> |
| 19312 |
</indexterm> |
| 19313 |
<listitem> |
| 19314 |
<para>The <command>sz</command> and <command>rz</command> |
| 19315 |
command set serves to transfer files to and from a remote |
| 19316 |
host using the <firstterm>zmodem</firstterm> protocol. |
| 19317 |
<firstterm>Zmodem</firstterm> has certain advantages over |
| 19318 |
<firstterm>xmodem</firstterm>, such as faster transmission |
| 19319 |
rate and resumption of interrupted file transfers. |
| 19320 |
Like <command>sx</command> and <command>rx</command>, |
| 19321 |
these are generally part of a communications package.</para> |
| 19322 |
|
| 19323 |
</listitem> |
| 19324 |
</varlistentry> |
| 19325 |
|
| 19326 |
|
| 19327 |
<varlistentry> |
| 19328 |
<term><anchor id="ftpref"><command>ftp</command></term> |
| 19329 |
<indexterm> |
| 19330 |
<primary>ftp</primary> |
| 19331 |
</indexterm> |
| 19332 |
<indexterm> |
| 19333 |
<primary>command</primary> |
| 19334 |
<secondary>file transfer</secondary> |
| 19335 |
</indexterm> |
| 19336 |
<listitem> |
| 19337 |
<para>Utility and protocol for uploading / downloading |
| 19338 |
files to or from a remote host. An ftp session can be automated |
| 19339 |
in a script (see <xref linkend="ex72"> and <xref |
| 19340 |
linkend="encryptedpw">).</para> |
| 19341 |
</listitem> |
| 19342 |
</varlistentry> |
| 19343 |
|
| 19344 |
|
| 19345 |
<varlistentry> |
| 19346 |
<term><anchor id="uucpref"><command>uucp</command></term> |
| 19347 |
<term><anchor id="uuxref"><command>uux</command></term> |
| 19348 |
<term><anchor id="curef"><command>cu</command></term> |
| 19349 |
<indexterm> |
| 19350 |
<primary>uucp</primary> |
| 19351 |
</indexterm> |
| 19352 |
<indexterm> |
| 19353 |
<primary>command</primary> |
| 19354 |
<secondary>uucp</secondary> |
| 19355 |
</indexterm> |
| 19356 |
<indexterm> |
| 19357 |
<primary>uux</primary> |
| 19358 |
<secondary>unix to unix execute</secondary> |
| 19359 |
</indexterm> |
| 19360 |
<indexterm> |
| 19361 |
<primary>cu</primary> |
| 19362 |
<secondary>call up</secondary> |
| 19363 |
</indexterm> |
| 19364 |
<indexterm> |
| 19365 |
<primary>command</primary> |
| 19366 |
<secondary>uucp</secondary> |
| 19367 |
</indexterm> |
| 19368 |
<listitem> |
| 19369 |
|
| 19370 |
<para><command>uucp</command>: <firstterm>UNIX to UNIX |
| 19371 |
copy</firstterm>. This is a communications package for |
| 19372 |
transferring files between UNIX servers. A shell script |
| 19373 |
is an effective way to handle a <command>uucp</command> |
| 19374 |
command sequence.</para> |
| 19375 |
|
| 19376 |
<para>Since the advent of the Internet and e-mail, |
| 19377 |
<command>uucp</command> seems to have faded into obscurity, |
| 19378 |
but it still exists and remains perfectly workable in |
| 19379 |
situations where an Internet connection is not available |
| 19380 |
or appropriate. The advantage of <command>uucp</command> |
| 19381 |
is that it is fault-tolerant, so even if there is a service |
| 19382 |
interruption the copy operation will resume where it left |
| 19383 |
off when the connection is restored.</para> |
| 19384 |
|
| 19385 |
<para>---</para> |
| 19386 |
|
| 19387 |
<para><command>uux</command>: <firstterm>UNIX to UNIX |
| 19388 |
execute</firstterm>. Execute a command on a remote system. |
| 19389 |
This command is part of the <command>uucp</command> |
| 19390 |
package.</para> |
| 19391 |
|
| 19392 |
<para>---</para> |
| 19393 |
|
| 19394 |
<para><command>cu</command>: <command>C</command>all |
| 19395 |
<command>U</command>p a remote system and connect as a |
| 19396 |
simple terminal. It is a sort of dumbed-down version of |
| 19397 |
<link linkend="telnetref">telnet</link>. This command is |
| 19398 |
part of the <command>uucp</command> package.</para> |
| 19399 |
</listitem> |
| 19400 |
</varlistentry> |
| 19401 |
|
| 19402 |
|
| 19403 |
<varlistentry> |
| 19404 |
<term><anchor id="telnetref"><command>telnet</command></term> |
| 19405 |
<indexterm> |
| 19406 |
<primary>telnet</primary> |
| 19407 |
</indexterm> |
| 19408 |
<indexterm> |
| 19409 |
<primary>command</primary> |
| 19410 |
<secondary>telnet</secondary> |
| 19411 |
</indexterm> |
| 19412 |
<listitem> |
| 19413 |
<para>Utility and protocol for connecting to a remote host.</para> |
| 19414 |
<caution><para>The <firstterm>telnet</firstterm> protocol |
| 19415 |
contains security holes and should therefore probably be |
| 19416 |
avoided. Its use within a shell script is |
| 19417 |
<emphasis>not</emphasis> recommended.</para></caution> |
| 19418 |
</listitem> |
| 19419 |
</varlistentry> |
| 19420 |
|
| 19421 |
<varlistentry> |
| 19422 |
<term><anchor id="wgetref"><command>wget</command></term> |
| 19423 |
<indexterm> |
| 19424 |
<primary>wget</primary> |
| 19425 |
</indexterm> |
| 19426 |
<indexterm> |
| 19427 |
<primary>command</primary> |
| 19428 |
<secondary>download</secondary> |
| 19429 |
</indexterm> |
| 19430 |
<listitem> |
| 19431 |
|
| 19432 |
<para>The <command>wget</command> utility |
| 19433 |
<firstterm>noninteractively</firstterm> retrieves or |
| 19434 |
downloads files from a Web or ftp site. It works well in a |
| 19435 |
script.</para> |
| 19436 |
|
| 19437 |
<para><programlisting>wget -p http://www.xyz23.com/file01.html |
| 19438 |
# The -p or --page-requisite option causes wget to fetch all files |
| 19439 |
#+ required to display the specified page. |
| 19440 |
|
| 19441 |
wget -r ftp://ftp.xyz24.net/~bozo/project_files/ -O $SAVEFILE |
| 19442 |
# The -r option recursively follows and retrieves all links |
| 19443 |
#+ on the specified site. |
| 19444 |
|
| 19445 |
wget -c ftp://ftp.xyz25.net/bozofiles/filename.tar.bz2 |
| 19446 |
# The -c option lets wget resume an interrupted download. |
| 19447 |
# This works with ftp servers and many HTTP sites.</programlisting></para> |
| 19448 |
|
| 19449 |
<example id="quotefetch"> |
| 19450 |
<title>Getting a stock quote</title> |
| 19451 |
<programlisting>"efetch;</programlisting> |
| 19452 |
</example> |
| 19453 |
|
| 19454 |
|
| 19455 |
<para>See also <xref linkend="wgetter2"> and <xref |
| 19456 |
linkend="bashpodder">.</para> |
| 19457 |
|
| 19458 |
</listitem> |
| 19459 |
</varlistentry> |
| 19460 |
|
| 19461 |
<varlistentry> |
| 19462 |
<term><anchor id="lynxref"><command>lynx</command></term> |
| 19463 |
<indexterm> |
| 19464 |
<primary>lynx</primary> |
| 19465 |
</indexterm> |
| 19466 |
<indexterm> |
| 19467 |
<primary>command</primary> |
| 19468 |
<secondary>browser</secondary> |
| 19469 |
</indexterm> |
| 19470 |
<listitem> |
| 19471 |
|
| 19472 |
<para>The <command>lynx</command> Web and file browser |
| 19473 |
can be used inside a script (with the |
| 19474 |
<option>-dump</option> option) to retrieve a file from a Web or |
| 19475 |
ftp site noninteractively.</para> |
| 19476 |
|
| 19477 |
<para> |
| 19478 |
<programlisting>lynx -dump http://www.xyz23.com/file01.html >$SAVEFILE</programlisting> |
| 19479 |
</para> |
| 19480 |
|
| 19481 |
<para>With the <option>-traversal</option> option, |
| 19482 |
<command>lynx</command> starts at the HTTP URL specified |
| 19483 |
as an argument, then <quote>crawls</quote> through all |
| 19484 |
links located on that particular server. Used together |
| 19485 |
with the <option>-crawl</option> option, outputs page text |
| 19486 |
to a log file.</para> |
| 19487 |
|
| 19488 |
</listitem> |
| 19489 |
</varlistentry> |
| 19490 |
|
| 19491 |
<varlistentry> |
| 19492 |
<term><anchor id="rloginref"><command>rlogin</command></term> |
| 19493 |
<indexterm> |
| 19494 |
<primary>rlogin</primary> |
| 19495 |
</indexterm> |
| 19496 |
<indexterm> |
| 19497 |
<primary>command</primary> |
| 19498 |
<secondary>remote login</secondary> |
| 19499 |
</indexterm> |
| 19500 |
<listitem> |
| 19501 |
<para><replaceable>Remote login</replaceable>, initates a |
| 19502 |
session on a remote host. This command has security issues, |
| 19503 |
so use <link linkend="sshref">ssh</link> instead.</para> |
| 19504 |
</listitem> |
| 19505 |
</varlistentry> |
| 19506 |
|
| 19507 |
<varlistentry> |
| 19508 |
<term><anchor id="rshref"><command>rsh</command></term> |
| 19509 |
<indexterm> |
| 19510 |
<primary>rsh</primary> |
| 19511 |
</indexterm> |
| 19512 |
<indexterm> |
| 19513 |
<primary>command</primary> |
| 19514 |
<secondary>remote shell</secondary> |
| 19515 |
</indexterm> |
| 19516 |
<listitem> |
| 19517 |
<para><replaceable>Remote shell</replaceable>, executes |
| 19518 |
command(s) on a remote host. This has security issues, |
| 19519 |
so use <command>ssh</command> instead.</para> |
| 19520 |
</listitem> |
| 19521 |
</varlistentry> |
| 19522 |
|
| 19523 |
<varlistentry> |
| 19524 |
<term><anchor id="rcpref"><command>rcp</command></term> |
| 19525 |
<indexterm> |
| 19526 |
<primary>rcp</primary> |
| 19527 |
</indexterm> |
| 19528 |
<indexterm> |
| 19529 |
<primary>command</primary> |
| 19530 |
<secondary>remote copy</secondary> |
| 19531 |
</indexterm> |
| 19532 |
<listitem> |
| 19533 |
<para><replaceable>Remote copy</replaceable>, copies files |
| 19534 |
between two different networked machines.</para> |
| 19535 |
</listitem> |
| 19536 |
</varlistentry> |
| 19537 |
|
| 19538 |
<varlistentry> |
| 19539 |
<term><anchor id="rsyncref"><command>rsync</command></term> |
| 19540 |
<indexterm> |
| 19541 |
<primary>rsync</primary> |
| 19542 |
</indexterm> |
| 19543 |
<indexterm> |
| 19544 |
<primary>command</primary> |
| 19545 |
<secondary>remote update</secondary> |
| 19546 |
</indexterm> |
| 19547 |
<listitem> |
| 19548 |
|
| 19549 |
<para><replaceable>Remote synchronize</replaceable>, updates |
| 19550 |
(synchronizes) files |
| 19551 |
between two different networked machines.</para> |
| 19552 |
|
| 19553 |
<para> |
| 19554 |
<screen><prompt>bash$ </prompt><userinput>rsync -a ~/sourcedir/*txt /node1/subdirectory/</userinput> |
| 19555 |
</screen> |
| 19556 |
</para> |
| 19557 |
|
| 19558 |
<example id="fc4upd"> |
| 19559 |
<title>Updating FC4</title> |
| 19560 |
<programlisting>&fc4upd;</programlisting> |
| 19561 |
</example> |
| 19562 |
|
| 19563 |
<para>See also <xref linkend="nightlybackup">.</para> |
| 19564 |
|
| 19565 |
<note><para>Using <link linkend="rcpref">rcp</link>, <link |
| 19566 |
linkend="rsyncref">rsync</link>, and similar |
| 19567 |
utilities with security implications in a shell |
| 19568 |
script may not be advisable. Consider, instead, using |
| 19569 |
<command>ssh</command>, <link linkend="scpref">scp</link>, |
| 19570 |
or an <command>expect</command> script.</para></note> |
| 19571 |
|
| 19572 |
</listitem> |
| 19573 |
</varlistentry> |
| 19574 |
|
| 19575 |
|
| 19576 |
<varlistentry> |
| 19577 |
<term><anchor id="sshref"><command>ssh</command></term> |
| 19578 |
<indexterm> |
| 19579 |
<primary>ssh</primary> |
| 19580 |
</indexterm> |
| 19581 |
<indexterm> |
| 19582 |
<primary>command</primary> |
| 19583 |
<secondary>secure shell</secondary> |
| 19584 |
</indexterm> |
| 19585 |
<listitem> |
| 19586 |
<para><replaceable>Secure shell</replaceable>, logs onto |
| 19587 |
a remote host and executes commands there. This |
| 19588 |
secure replacement for <command>telnet</command>, |
| 19589 |
<command>rlogin</command>, <command>rcp</command>, and |
| 19590 |
<command>rsh</command> uses identity authentication |
| 19591 |
and encryption. See its <link linkend="manref">manpage</link> |
| 19592 |
for details.</para> |
| 19593 |
|
| 19594 |
<example id="remote"> |
| 19595 |
<title>Using <firstterm>ssh</firstterm></title> |
| 19596 |
<programlisting>&remote;</programlisting> |
| 19597 |
</example> |
| 19598 |
|
| 19599 |
<caution> |
| 19600 |
<para>Within a loop, <command>ssh</command> may cause |
| 19601 |
unexpected behavior. According to a <ulink |
| 19602 |
url="http://groups-beta.google.com/group/comp.unix.shell/msg/dcb446b5fff7d230"> |
| 19603 |
Usenet post</ulink> in the comp.unix shell archives, |
| 19604 |
<command>ssh</command> inherits the loop's |
| 19605 |
<filename>stdin</filename>. To remedy this, pass |
| 19606 |
<command>ssh</command> either the <option>-n</option> |
| 19607 |
or <option>-f</option> option.</para> |
| 19608 |
<para>Thanks, Jason Bechtel, for pointing this out.</para> |
| 19609 |
</caution> |
| 19610 |
|
| 19611 |
</listitem> |
| 19612 |
</varlistentry> |
| 19613 |
|
| 19614 |
<varlistentry> |
| 19615 |
<term><anchor id="scpref"><command>scp</command></term> |
| 19616 |
<indexterm> |
| 19617 |
<primary>scp</primary> |
| 19618 |
</indexterm> |
| 19619 |
<indexterm> |
| 19620 |
<primary>command</primary> |
| 19621 |
<secondary>secure copy</secondary> |
| 19622 |
</indexterm> |
| 19623 |
<listitem> |
| 19624 |
<para><replaceable>Secure copy</replaceable>, similar in |
| 19625 |
function to <command>rcp</command>, copies files between |
| 19626 |
two different networked machines, but does so using |
| 19627 |
authentication, and with a security level similar to |
| 19628 |
<command>ssh</command>.</para> |
| 19629 |
</listitem> |
| 19630 |
</varlistentry> |
| 19631 |
|
| 19632 |
</variablelist> |
| 19633 |
|
| 19634 |
|
| 19635 |
<variablelist id="commlocal"> |
| 19636 |
<title><anchor id="commlocal1">Local Network</title> |
| 19637 |
|
| 19638 |
<varlistentry> |
| 19639 |
<term><anchor id="writeref"><command>write</command></term> |
| 19640 |
<indexterm> |
| 19641 |
<primary>write</primary> |
| 19642 |
</indexterm> |
| 19643 |
<indexterm> |
| 19644 |
<primary>command</primary> |
| 19645 |
<secondary>write</secondary> |
| 19646 |
</indexterm> |
| 19647 |
<listitem> |
| 19648 |
<para>This is a utility for terminal-to-terminal communication. |
| 19649 |
It allows sending lines from your terminal (console or |
| 19650 |
<firstterm>xterm</firstterm>) to that of another user. The |
| 19651 |
<link linkend="mesgref">mesg</link> command may, of course, |
| 19652 |
be used to disable write access to a terminal</para> |
| 19653 |
|
| 19654 |
<para>Since <command>write</command> is interactive, it |
| 19655 |
would not normally find use in a script.</para> |
| 19656 |
</listitem> |
| 19657 |
</varlistentry> |
| 19658 |
|
| 19659 |
<varlistentry> |
| 19660 |
<term><anchor id="netconfigref"><command>netconfig</command></term> |
| 19661 |
<indexterm> |
| 19662 |
<primary>netconfig</primary> |
| 19663 |
</indexterm> |
| 19664 |
<indexterm> |
| 19665 |
<primary>command</primary> |
| 19666 |
<secondary>network</secondary> |
| 19667 |
</indexterm> |
| 19668 |
<listitem> |
| 19669 |
<para>A command-line utility for configuring a network adapter |
| 19670 |
(using <firstterm>DHCP</firstterm>). This command is native |
| 19671 |
to Red Hat centric Linux distros.</para> |
| 19672 |
</listitem> |
| 19673 |
</varlistentry> |
| 19674 |
|
| 19675 |
</variablelist> |
| 19676 |
|
| 19677 |
|
| 19678 |
<variablelist id="commmail"> |
| 19679 |
<title><anchor id="commmail1">Mail</title> |
| 19680 |
|
| 19681 |
<varlistentry> |
| 19682 |
<term><command>mail</command></term> |
| 19683 |
<indexterm> |
| 19684 |
<primary>mail</primary> |
| 19685 |
</indexterm> |
| 19686 |
<indexterm> |
| 19687 |
<primary>command</primary> |
| 19688 |
<secondary>mail</secondary> |
| 19689 |
</indexterm> |
| 19690 |
<listitem> |
| 19691 |
|
| 19692 |
<para>Send or read e-mail messages.</para> |
| 19693 |
|
| 19694 |
<para>This stripped-down command-line mail client |
| 19695 |
works fine as a command embedded in a script.</para> |
| 19696 |
|
| 19697 |
<example id="selfmailer"> |
| 19698 |
<title>A script that mails itself</title> |
| 19699 |
<programlisting>&selfmailer;</programlisting> |
| 19700 |
</example> |
| 19701 |
|
| 19702 |
</listitem> |
| 19703 |
</varlistentry> |
| 19704 |
|
| 19705 |
<varlistentry> |
| 19706 |
<term><anchor id="mailtoref"><command>mailto</command></term> |
| 19707 |
<indexterm> |
| 19708 |
<primary>mailto</primary> |
| 19709 |
</indexterm> |
| 19710 |
<indexterm> |
| 19711 |
<primary>command</primary> |
| 19712 |
<secondary>MIME mail</secondary> |
| 19713 |
</indexterm> |
| 19714 |
<listitem> |
| 19715 |
<para>Similar to the <command>mail</command> command, |
| 19716 |
<command>mailto</command> sends e-mail messages |
| 19717 |
from the command-line or in a script. However, |
| 19718 |
<command>mailto</command> also permits sending MIME |
| 19719 |
(multimedia) messages.</para> |
| 19720 |
</listitem> |
| 19721 |
</varlistentry> |
| 19722 |
|
| 19723 |
<varlistentry> |
| 19724 |
<term><anchor id="mailstatsref"><command>mailstats</command></term> |
| 19725 |
<indexterm> |
| 19726 |
<primary>mailstats</primary> |
| 19727 |
</indexterm> |
| 19728 |
<indexterm> |
| 19729 |
<primary>command</primary> |
| 19730 |
<secondary>statistics</secondary> |
| 19731 |
</indexterm> |
| 19732 |
<listitem> |
| 19733 |
<para>Show <firstterm>mail statistics</firstterm>. This command |
| 19734 |
may be invoked only by <firstterm>root</firstterm>.</para> |
| 19735 |
|
| 19736 |
<para> |
| 19737 |
<screen><prompt>root# </prompt><userinput>mailstats</userinput> |
| 19738 |
<computeroutput>Statistics from Tue Jan 1 20:32:08 2008 |
| 19739 |
M msgsfr bytes_from msgsto bytes_to msgsrej msgsdis msgsqur Mailer |
| 19740 |
4 1682 24118K 0 0K 0 0 0 esmtp |
| 19741 |
9 212 640K 1894 25131K 0 0 0 local |
| 19742 |
===================================================================== |
| 19743 |
T 1894 24758K 1894 25131K 0 0 0 |
| 19744 |
C 414 0</computeroutput> |
| 19745 |
</screen> |
| 19746 |
</para> |
| 19747 |
</listitem> |
| 19748 |
</varlistentry> |
| 19749 |
|
| 19750 |
<varlistentry> |
| 19751 |
<term><anchor id="vacationref"><command>vacation</command></term> |
| 19752 |
<indexterm> |
| 19753 |
<primary>vacation</primary> |
| 19754 |
</indexterm> |
| 19755 |
<indexterm> |
| 19756 |
<primary>command</primary> |
| 19757 |
<secondary>mail</secondary> |
| 19758 |
</indexterm> |
| 19759 |
<listitem> |
| 19760 |
<para>This utility automatically replies to e-mails that |
| 19761 |
the intended recipient is on vacation and temporarily |
| 19762 |
unavailable. It runs on a network, in conjunction with |
| 19763 |
<command>sendmail</command>, and is not applicable to a |
| 19764 |
dial-up POPmail account.</para> |
| 19765 |
</listitem> |
| 19766 |
</varlistentry> |
| 19767 |
|
| 19768 |
|
| 19769 |
</variablelist> |
| 19770 |
|
| 19771 |
</sect1> <!-- End Communications Commands --> |
| 19772 |
|
| 19773 |
|
| 19774 |
|
| 19775 |
<sect1 id="terminalccmds"> |
| 19776 |
<title>Terminal Control Commands</title> |
| 19777 |
|
| 19778 |
<variablelist id="termcommandlisting"> |
| 19779 |
<title><anchor id="termcommandlisting1">Command affecting the console |
| 19780 |
or terminal</title> |
| 19781 |
|
| 19782 |
|
| 19783 |
<varlistentry> |
| 19784 |
<term><anchor id="tputref"><command>tput</command></term> |
| 19785 |
<indexterm> |
| 19786 |
<primary>tput</primary> |
| 19787 |
</indexterm> |
| 19788 |
<indexterm> |
| 19789 |
<primary>command</primary> |
| 19790 |
<secondary>terminal</secondary> |
| 19791 |
</indexterm> |
| 19792 |
<listitem> |
| 19793 |
|
| 19794 |
<para>Initialize terminal and/or fetch information about it from |
| 19795 |
<database>terminfo</database> data. Various options permit |
| 19796 |
certain terminal operations: <command>tput clear</command> |
| 19797 |
is the equivalent of <link linkend="clearref">clear</link>; |
| 19798 |
<command>tput reset</command> is the equivalent |
| 19799 |
of <link linkend="resetref">reset</link>.</para> |
| 19800 |
|
| 19801 |
<para> |
| 19802 |
<screen><prompt>bash$ </prompt><userinput>tput longname</userinput> |
| 19803 |
<computeroutput>xterm terminal emulator (X Window System)</computeroutput> |
| 19804 |
</screen> |
| 19805 |
</para> |
| 19806 |
|
| 19807 |
<para>Issuing a <command>tput cup X Y</command> moves |
| 19808 |
the cursor to the (X,Y) coordinates in the current |
| 19809 |
terminal. A <command>clear</command> to erase the terminal |
| 19810 |
screen would normally precede this.</para> |
| 19811 |
|
| 19812 |
|
| 19813 |
<para> |
| 19814 |
|
| 19815 |
Some interesting options to <firstterm>tput</firstterm> are: |
| 19816 |
|
| 19817 |
<itemizedlist> |
| 19818 |
<listitem><para><option>bold</option>, for high-intensity |
| 19819 |
text</para></listitem> |
| 19820 |
<listitem><para><option>smul</option>, to underline text |
| 19821 |
in the terminal</para></listitem> |
| 19822 |
<listitem><para><option>smso</option>, to render text in |
| 19823 |
reverse</para></listitem> |
| 19824 |
<listitem><para><option>sgr0</option>, to reset the terminal |
| 19825 |
parameters (to normal), without clearing the |
| 19826 |
screen</para></listitem> |
| 19827 |
</itemizedlist> |
| 19828 |
|
| 19829 |
</para> |
| 19830 |
|
| 19831 |
|
| 19832 |
<para>Example scripts using <firstterm>tput</firstterm>: |
| 19833 |
<orderedlist id="tputexamples"> |
| 19834 |
<listitem><para><xref linkend="colorecho"></para></listitem> |
| 19835 |
<listitem><para><xref linkend="ex30a"></para></listitem> |
| 19836 |
<listitem><para><xref linkend="homework"></para></listitem> |
| 19837 |
<listitem><para><xref linkend="nim"></para></listitem> |
| 19838 |
<listitem><para><xref linkend="poem"></para></listitem> |
| 19839 |
</orderedlist> |
| 19840 |
</para> |
| 19841 |
|
| 19842 |
<para>Note that <link linkend="sttyref">stty</link> offers |
| 19843 |
a more powerful command set for controlling a terminal.</para> |
| 19844 |
|
| 19845 |
</listitem> |
| 19846 |
</varlistentry> |
| 19847 |
|
| 19848 |
<varlistentry> |
| 19849 |
<term><anchor id="infocmpref"><command>infocmp</command></term> |
| 19850 |
<indexterm> |
| 19851 |
<primary>infocmp</primary> |
| 19852 |
</indexterm> |
| 19853 |
<indexterm> |
| 19854 |
<primary>command</primary> |
| 19855 |
<secondary>terminal</secondary> |
| 19856 |
</indexterm> |
| 19857 |
<listitem> |
| 19858 |
|
| 19859 |
<para>This command prints out extensive information about the |
| 19860 |
current terminal. It references the |
| 19861 |
<firstterm>terminfo</firstterm> database.</para> |
| 19862 |
|
| 19863 |
<para> |
| 19864 |
<screen><prompt>bash$ </prompt><userinput>infocmp</userinput> |
| 19865 |
<computeroutput># Reconstructed via infocmp from file: |
| 19866 |
/usr/share/terminfo/r/rxvt |
| 19867 |
rxvt|rxvt terminal emulator (X Window System), |
| 19868 |
am, bce, eo, km, mir, msgr, xenl, xon, |
| 19869 |
colors#8, cols#80, it#8, lines#24, pairs#64, |
| 19870 |
acsc=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~, |
| 19871 |
bel=^G, blink=\E[5m, bold=\E[1m, |
| 19872 |
civis=\E[?25l, |
| 19873 |
clear=\E[H\E[2J, cnorm=\E[?25h, cr=^M, |
| 19874 |
...</computeroutput> |
| 19875 |
</screen> |
| 19876 |
</para> |
| 19877 |
|
| 19878 |
</listitem> |
| 19879 |
</varlistentry> |
| 19880 |
|
| 19881 |
<varlistentry> |
| 19882 |
<term><anchor id="resetref"><command>reset</command></term> |
| 19883 |
<indexterm> |
| 19884 |
<primary>reset</primary> |
| 19885 |
</indexterm> |
| 19886 |
<indexterm> |
| 19887 |
<primary>command</primary> |
| 19888 |
<secondary>reset</secondary> |
| 19889 |
</indexterm> |
| 19890 |
<listitem> |
| 19891 |
<para>Reset terminal parameters and clear text screen. As with |
| 19892 |
<command>clear</command>, the cursor and prompt reappear in the |
| 19893 |
upper lefthand corner of the terminal.</para> |
| 19894 |
</listitem> |
| 19895 |
</varlistentry> |
| 19896 |
|
| 19897 |
<varlistentry> |
| 19898 |
<term><anchor id="clearref"><command>clear</command></term> |
| 19899 |
<indexterm> |
| 19900 |
<primary>clear</primary> |
| 19901 |
</indexterm> |
| 19902 |
<indexterm> |
| 19903 |
<primary>command</primary> |
| 19904 |
<secondary>clear</secondary> |
| 19905 |
</indexterm> |
| 19906 |
<listitem> |
| 19907 |
<para>The <command>clear</command> command simply clears |
| 19908 |
the text screen at the console or in an |
| 19909 |
<firstterm>xterm</firstterm>. The prompt and cursor |
| 19910 |
reappear at the upper lefthand corner of the screen or |
| 19911 |
xterm window. This command may be used either at the command |
| 19912 |
line or in a script. See <xref linkend="ex30">.</para> |
| 19913 |
</listitem> |
| 19914 |
</varlistentry> |
| 19915 |
|
| 19916 |
|
| 19917 |
<varlistentry> |
| 19918 |
<term><anchor id="resizeref"><command>resize</command></term> |
| 19919 |
<indexterm> |
| 19920 |
<primary>resize</primary> |
| 19921 |
</indexterm> |
| 19922 |
<indexterm> |
| 19923 |
<primary>command</primary> |
| 19924 |
<secondary>resize</secondary> |
| 19925 |
</indexterm> |
| 19926 |
<listitem> |
| 19927 |
<para>Echoes commands necessary to set <varname>$TERM</varname> |
| 19928 |
and <varname>$TERMCAP</varname> to duplicate the |
| 19929 |
<firstterm>size</firstterm> (dimensions) of the current |
| 19930 |
terminal.</para> |
| 19931 |
<para> |
| 19932 |
<screen><prompt>bash$ </prompt><userinput>resize</userinput> |
| 19933 |
<computeroutput>set noglob; |
| 19934 |
setenv COLUMNS '80'; |
| 19935 |
setenv LINES '24'; |
| 19936 |
unset noglob;</computeroutput> |
| 19937 |
</screen> |
| 19938 |
</para> |
| 19939 |
</listitem> |
| 19940 |
</varlistentry> |
| 19941 |
|
| 19942 |
<varlistentry> |
| 19943 |
<term><anchor id="scriptref"><command>script</command></term> |
| 19944 |
<indexterm> |
| 19945 |
<primary>script</primary> |
| 19946 |
</indexterm> |
| 19947 |
<indexterm> |
| 19948 |
<primary>command</primary> |
| 19949 |
<secondary>script</secondary> |
| 19950 |
</indexterm> |
| 19951 |
<listitem> |
| 19952 |
<para>This utility records (saves to a file) all the user keystrokes at |
| 19953 |
the command-line in a console or an xterm window. This, in effect, |
| 19954 |
creates a record of a session.</para> |
| 19955 |
</listitem> |
| 19956 |
</varlistentry> |
| 19957 |
|
| 19958 |
</variablelist> |
| 19959 |
|
| 19960 |
</sect1> <!-- End Terminal Control Commands --> |
| 19961 |
|
| 19962 |
|
| 19963 |
<sect1 id="mathc"> |
| 19964 |
<title>Math Commands</title> |
| 19965 |
|
| 19966 |
<variablelist id="mathcommandlisting"> |
| 19967 |
<title><anchor id="mathcommandlisting1"><quote>Doing the |
| 19968 |
numbers</quote></title> |
| 19969 |
|
| 19970 |
<varlistentry> |
| 19971 |
<term><anchor id="factorref"><command>factor</command></term> |
| 19972 |
<indexterm> |
| 19973 |
<primary>factor</primary> |
| 19974 |
</indexterm> |
| 19975 |
<indexterm> |
| 19976 |
<primary>command</primary> |
| 19977 |
<secondary>factor</secondary> |
| 19978 |
</indexterm> |
| 19979 |
<listitem><para>Decompose an integer into prime factors.</para> |
| 19980 |
|
| 19981 |
<para> |
| 19982 |
<screen><prompt>bash$ </prompt><userinput>factor 27417</userinput> |
| 19983 |
<computeroutput>27417: 3 13 19 37</computeroutput> |
| 19984 |
</screen> |
| 19985 |
</para> |
| 19986 |
|
| 19987 |
<example id="primes2"> |
| 19988 |
<title>Generating prime numbers</title> |
| 19989 |
<programlisting>&primes2;</programlisting> |
| 19990 |
</example> |
| 19991 |
|
| 19992 |
</listitem> |
| 19993 |
</varlistentry> |
| 19994 |
|
| 19995 |
<varlistentry> |
| 19996 |
<term><anchor id="bcref"><command>bc</command></term> |
| 19997 |
<indexterm> |
| 19998 |
<primary>bc</primary> |
| 19999 |
</indexterm> |
| 20000 |
<indexterm> |
| 20001 |
<primary>command</primary> |
| 20002 |
<secondary>bc</secondary> |
| 20003 |
</indexterm> |
| 20004 |
<listitem> |
| 20005 |
|
| 20006 |
<para>Bash can't handle floating point calculations, and |
| 20007 |
it lacks operators for certain important mathematical |
| 20008 |
functions. Fortunately, <command>bc</command> gallops to |
| 20009 |
the rescue.</para> |
| 20010 |
|
| 20011 |
<para>Not just a versatile, arbitrary precision calculation |
| 20012 |
utility, <command>bc</command> offers many of the facilities of |
| 20013 |
a programming language. It has a syntax vaguely resembling |
| 20014 |
<command>C</command>.</para> |
| 20015 |
|
| 20016 |
<para>Since it is a fairly well-behaved UNIX utility, and may |
| 20017 |
therefore be used in a <link linkend="piperef">pipe</link>, |
| 20018 |
<command>bc</command> comes in handy in scripts.</para> |
| 20019 |
|
| 20020 |
<para><anchor id="bctemplate"></para> |
| 20021 |
<para>Here is a simple template for using |
| 20022 |
<command>bc</command> to calculate a script |
| 20023 |
variable. This uses <link linkend="commandsubref">command |
| 20024 |
substitution</link>.</para> |
| 20025 |
|
| 20026 |
<para> |
| 20027 |
<screen> |
| 20028 |
<userinput>variable=$(echo "OPTIONS; OPERATIONS" | bc)</userinput> |
| 20029 |
</screen> |
| 20030 |
</para> |
| 20031 |
|
| 20032 |
<para><anchor id="monthlypmt0"></para> |
| 20033 |
<example id="monthlypmt"> |
| 20034 |
<title>Monthly Payment on a Mortgage</title> |
| 20035 |
<programlisting>&monthlypmt;</programlisting> |
| 20036 |
</example> |
| 20037 |
|
| 20038 |
<para><anchor id="base0"></para> |
| 20039 |
<example id="base"> |
| 20040 |
<title>Base Conversion</title> |
| 20041 |
<programlisting>&base;</programlisting> |
| 20042 |
</example> |
| 20043 |
|
| 20044 |
<para><anchor id="bcheredoc"></para> |
| 20045 |
<para>An alternate method of invoking <command>bc</command> |
| 20046 |
involves using a <link linkend="heredocref">here |
| 20047 |
document</link> embedded within a <link |
| 20048 |
linkend="commandsubref">command substitution</link> |
| 20049 |
block. This is especially appropriate when a script |
| 20050 |
needs to pass a list of options and commands to |
| 20051 |
<command>bc</command>.</para> |
| 20052 |
|
| 20053 |
<para> |
| 20054 |
<programlisting>variable=`bc << LIMIT_STRING |
| 20055 |
options |
| 20056 |
statements |
| 20057 |
operations |
| 20058 |
LIMIT_STRING |
| 20059 |
` |
| 20060 |
|
| 20061 |
...or... |
| 20062 |
|
| 20063 |
|
| 20064 |
variable=$(bc << LIMIT_STRING |
| 20065 |
options |
| 20066 |
statements |
| 20067 |
operations |
| 20068 |
LIMIT_STRING |
| 20069 |
)</programlisting> |
| 20070 |
</para> |
| 20071 |
|
| 20072 |
|
| 20073 |
<example id="altbc"> |
| 20074 |
<title>Invoking <firstterm>bc</firstterm> using a <firstterm>here |
| 20075 |
document</firstterm></title> |
| 20076 |
<programlisting>&altbc;</programlisting> |
| 20077 |
</example> |
| 20078 |
|
| 20079 |
<para><anchor id="cannonref"></para> |
| 20080 |
<example id="cannon"> |
| 20081 |
<title>Calculating PI</title> |
| 20082 |
<programlisting>&cannon;</programlisting> |
| 20083 |
</example> |
| 20084 |
|
| 20085 |
<para>See also <xref linkend="stddev">.</para> |
| 20086 |
|
| 20087 |
</listitem> |
| 20088 |
</varlistentry> |
| 20089 |
|
| 20090 |
<varlistentry> |
| 20091 |
<term><anchor id="dcref"><command>dc</command></term> |
| 20092 |
<indexterm> |
| 20093 |
<primary>dc</primary> |
| 20094 |
</indexterm> |
| 20095 |
<indexterm> |
| 20096 |
<primary>command</primary> |
| 20097 |
<secondary>dc</secondary> |
| 20098 |
</indexterm> |
| 20099 |
<listitem> |
| 20100 |
|
| 20101 |
<para>The <command>dc</command> (<command>d</command>esk |
| 20102 |
<command>c</command>alculator) utility is <link |
| 20103 |
linkend="stackdefref">stack-oriented</link> |
| 20104 |
and uses RPN (<firstterm>Reverse Polish Notation</firstterm>). |
| 20105 |
Like <command>bc</command>, it has much of the power of |
| 20106 |
a programming language.</para> |
| 20107 |
|
| 20108 |
<para>Similar to the procedure with <command>bc</command>, |
| 20109 |
<link linkend="echoref">echo</link> a command-string |
| 20110 |
to <command>dc</command>.</para> |
| 20111 |
|
| 20112 |
<para> |
| 20113 |
<programlisting>echo "[Printing a string ... ]P" | dc |
| 20114 |
# The P command prints the string between the preceding brackets. |
| 20115 |
|
| 20116 |
# And now for some simple arithmetic. |
| 20117 |
echo "7 8 * p" | dc # 56 |
| 20118 |
# Pushes 7, then 8 onto the stack, |
| 20119 |
#+ multiplies ("*" operator), then prints the result ("p" operator).</programlisting> |
| 20120 |
</para> |
| 20121 |
|
| 20122 |
<para>Most persons avoid <command>dc</command>, because |
| 20123 |
of its non-intuitive input and rather cryptic |
| 20124 |
operators. Yet, it has its uses.</para> |
| 20125 |
|
| 20126 |
<example id="hexconvert"> |
| 20127 |
<title>Converting a decimal number to hexadecimal</title> |
| 20128 |
<programlisting>&hexconvert;</programlisting> |
| 20129 |
</example> |
| 20130 |
|
| 20131 |
<para>Studying the <link linkend="inforef">info</link> page for |
| 20132 |
<command>dc</command> is a painful path to understanding its |
| 20133 |
intricacies. There seems to be a small, select group of |
| 20134 |
<emphasis>dc wizards</emphasis> who delight in showing off |
| 20135 |
their mastery of this powerful, but arcane utility.</para> |
| 20136 |
|
| 20137 |
<para> |
| 20138 |
<screen><prompt>bash$ </prompt><userinput>echo "16i[q]sa[ln0=aln100%Pln100/snlbx]sbA0D68736142snlbxq" | dc</userinput> |
| 20139 |
<computeroutput>Bash</computeroutput> |
| 20140 |
</screen> |
| 20141 |
</para> |
| 20142 |
|
| 20143 |
<para><anchor id="goldenratio"> |
| 20144 |
<programlisting>dc <<< 10k5v1+2/p # 1.6180339887 |
| 20145 |
# ^^^ Feed operations to dc using a Here String. |
| 20146 |
# ^^^ Pushes 10 and sets that as the precision (10k). |
| 20147 |
# ^^ Pushes 5 and takes its square root |
| 20148 |
# (5v, v = square root). |
| 20149 |
# ^^ Pushes 1 and adds it to the running total (1+). |
| 20150 |
# ^^ Pushes 2 and divides the running total by that (2/). |
| 20151 |
# ^ Pops and prints the result (p) |
| 20152 |
# The result is 1.6180339887 ... |
| 20153 |
# ... which happens to be the Pythagorean Golden Ratio, to 10 places.</programlisting> |
| 20154 |
</para> |
| 20155 |
|
| 20156 |
<example id="factr"> |
| 20157 |
<title>Factoring</title> |
| 20158 |
<programlisting>&factr;</programlisting> |
| 20159 |
</example> |
| 20160 |
|
| 20161 |
</listitem> |
| 20162 |
</varlistentry> |
| 20163 |
|
| 20164 |
|
| 20165 |
<varlistentry> |
| 20166 |
<term><anchor id="awkmath"><command>awk</command></term> |
| 20167 |
<indexterm> |
| 20168 |
<primary>awk</primary> |
| 20169 |
</indexterm> |
| 20170 |
<indexterm> |
| 20171 |
<primary>command</primary> |
| 20172 |
<secondary>math</secondary> |
| 20173 |
</indexterm> |
| 20174 |
<listitem> |
| 20175 |
<para>Yet another way of doing floating point math in |
| 20176 |
a script is using <link linkend="awkref">awk's</link> |
| 20177 |
built-in math functions in a <link linkend="shwrapper">shell |
| 20178 |
wrapper</link>.</para> |
| 20179 |
|
| 20180 |
<example id="hypot"> |
| 20181 |
<title>Calculating the hypotenuse of a triangle</title> |
| 20182 |
<programlisting>&hypot;</programlisting> |
| 20183 |
</example> |
| 20184 |
|
| 20185 |
</listitem> |
| 20186 |
</varlistentry> |
| 20187 |
|
| 20188 |
</variablelist> |
| 20189 |
|
| 20190 |
</sect1> <!-- End Math Commands --> |
| 20191 |
|
| 20192 |
|
| 20193 |
<sect1 id="extmisc"> |
| 20194 |
<title>Miscellaneous Commands</title> |
| 20195 |
|
| 20196 |
<variablelist id="misccommandlisting"> |
| 20197 |
<title><anchor id="misccommandlisting1">Command that fit in no |
| 20198 |
special category</title> |
| 20199 |
|
| 20200 |
<varlistentry> |
| 20201 |
<term><anchor id="jotref"><command>jot</command></term> |
| 20202 |
<term><anchor id="seqref"><command>seq</command></term> |
| 20203 |
<indexterm> |
| 20204 |
<primary>jot</primary> |
| 20205 |
</indexterm> |
| 20206 |
<indexterm> |
| 20207 |
<primary>command</primary> |
| 20208 |
<secondary>jot</secondary> |
| 20209 |
</indexterm> |
| 20210 |
<indexterm> |
| 20211 |
<primary>seq</primary> |
| 20212 |
</indexterm> |
| 20213 |
<indexterm> |
| 20214 |
<primary>command</primary> |
| 20215 |
<secondary>seq</secondary> |
| 20216 |
</indexterm> |
| 20217 |
<indexterm> |
| 20218 |
<primary>loop</primary> |
| 20219 |
<secondary>arguments</secondary> |
| 20220 |
</indexterm> |
| 20221 |
<listitem> |
| 20222 |
<para>These utilities emit a sequence of integers, with a |
| 20223 |
user-selectable increment.</para> |
| 20224 |
|
| 20225 |
<para>The default separator character between each integer is a |
| 20226 |
newline, but this can be changed with the <option>-s</option> |
| 20227 |
option.</para> |
| 20228 |
<para> |
| 20229 |
<screen> |
| 20230 |
<prompt>bash$ </prompt><userinput>seq 5</userinput> |
| 20231 |
<computeroutput>1 |
| 20232 |
2 |
| 20233 |
3 |
| 20234 |
4 |
| 20235 |
5</computeroutput> |
| 20236 |
|
| 20237 |
|
| 20238 |
|
| 20239 |
<prompt>bash$ </prompt><userinput>seq -s : 5</userinput> |
| 20240 |
<computeroutput>1:2:3:4:5</computeroutput> |
| 20241 |
</screen> |
| 20242 |
</para> |
| 20243 |
|
| 20244 |
|
| 20245 |
<para>Both <command>jot</command> and <command>seq</command> |
| 20246 |
come in handy in a <link linkend="forloopref1">for |
| 20247 |
loop</link>.</para> |
| 20248 |
|
| 20249 |
<example id="ex53"> |
| 20250 |
<title>Using <firstterm>seq</firstterm> to generate loop |
| 20251 |
arguments</title> |
| 20252 |
<programlisting>&ex53;</programlisting> |
| 20253 |
</example> |
| 20254 |
|
| 20255 |
<para>A simpler example:</para> |
| 20256 |
|
| 20257 |
<para><programlisting># Create a set of 10 files, |
| 20258 |
#+ named file.1, file.2 . . . file.10. |
| 20259 |
COUNT=10 |
| 20260 |
PREFIX=file |
| 20261 |
|
| 20262 |
for filename in `seq $COUNT` |
| 20263 |
do |
| 20264 |
touch $PREFIX.$filename |
| 20265 |
# Or, can do other operations, |
| 20266 |
#+ such as rm, grep, etc. |
| 20267 |
done</programlisting></para> |
| 20268 |
|
| 20269 |
<example id="lettercount"> |
| 20270 |
<title>Letter Count"</title> |
| 20271 |
<programlisting>&lettercount;</programlisting> |
| 20272 |
</example> |
| 20273 |
|
| 20274 |
<note> |
| 20275 |
<para>Somewhat more capable than <firstterm>seq</firstterm>, |
| 20276 |
<command>jot</command> is a classic UNIX |
| 20277 |
utility that is not normally included in a standard Linux |
| 20278 |
distro. However, the source <firstterm>rpm</firstterm> |
| 20279 |
is available for download from the <ulink |
| 20280 |
url="http://www.mit.edu/afs/athena/system/rhlinux/athena-9.0/free/SRPMS/athena-jot-9.0-3.src.rpm"> |
| 20281 |
MIT repository</ulink>.</para> |
| 20282 |
|
| 20283 |
<para><anchor id="jotrandom"></para> |
| 20284 |
<para>Unlike <firstterm>seq</firstterm>, <command>jot</command> can |
| 20285 |
generate a sequence of random numbers, using the <option>-r</option> |
| 20286 |
option.</para> |
| 20287 |
<para><screen><prompt>bash$ </prompt><userinput>jot -r 3 999</userinput> |
| 20288 |
<computeroutput>1069 |
| 20289 |
1272 |
| 20290 |
1428</computeroutput></screen></para> |
| 20291 |
</note> |
| 20292 |
|
| 20293 |
</listitem> |
| 20294 |
</varlistentry> |
| 20295 |
|
| 20296 |
<varlistentry> |
| 20297 |
<term><anchor id="getopty"><command>getopt</command></term> |
| 20298 |
<indexterm> |
| 20299 |
<primary>getopt</primary> |
| 20300 |
</indexterm> |
| 20301 |
<indexterm> |
| 20302 |
<primary>command</primary> |
| 20303 |
<secondary>option</secondary> |
| 20304 |
</indexterm> |
| 20305 |
<listitem> |
| 20306 |
<para>The <command>getopt</command> command |
| 20307 |
parses command-line options preceded by a <link |
| 20308 |
linkend="dashref">dash</link>. This external command |
| 20309 |
corresponds to the <link linkend="getoptsx">getopts</link> |
| 20310 |
Bash builtin. Using <command>getopt</command> permits |
| 20311 |
handling long options by means of the <option>-l</option> |
| 20312 |
flag, and this also allows parameter reshuffling.</para> |
| 20313 |
|
| 20314 |
<example id="ex33a"> |
| 20315 |
<title>Using <firstterm>getopt</firstterm> to parse command-line |
| 20316 |
options</title> |
| 20317 |
<programlisting>&ex33a;</programlisting> |
| 20318 |
</example> |
| 20319 |
|
| 20320 |
<note> |
| 20321 |
<para>As <emphasis>Peggy Russell</emphasis> points out:</para> |
| 20322 |
<para>It is often necessary to include an <link |
| 20323 |
linkend="evalref">eval</link> to correctly process |
| 20324 |
<link linkend="whitespaceref">whitespace</link> and |
| 20325 |
<firstterm>quotes</firstterm>. |
| 20326 |
<programlisting>args=$(getopt -o a:bc:d -- "$@") |
| 20327 |
eval set -- "$args"</programlisting></para> |
| 20328 |
</note> |
| 20329 |
|
| 20330 |
<para>See <xref linkend="getoptsimple"> for a simplified emulation |
| 20331 |
of <command>getopt</command>.</para> |
| 20332 |
|
| 20333 |
</listitem> |
| 20334 |
</varlistentry> |
| 20335 |
|
| 20336 |
<varlistentry> |
| 20337 |
<term><anchor id="runpartsref"><command>run-parts</command></term> |
| 20338 |
<indexterm> |
| 20339 |
<primary>run-parts</primary> |
| 20340 |
</indexterm> |
| 20341 |
<indexterm> |
| 20342 |
<primary>command</primary> |
| 20343 |
<secondary>run-parts</secondary> |
| 20344 |
</indexterm> |
| 20345 |
<listitem> |
| 20346 |
<para>The <command>run-parts</command> command |
| 20347 |
|
| 20348 |
<footnote><para>This is actually a script adapted from |
| 20349 |
the Debian Linux distribution.</para></footnote> |
| 20350 |
|
| 20351 |
executes all the scripts in a target directory, sequentially |
| 20352 |
in ASCII-sorted filename order. Of course, the scripts |
| 20353 |
need to have execute permission.</para> |
| 20354 |
|
| 20355 |
<para>The <link linkend="cronref">cron</link> <link |
| 20356 |
linkend="daemonref">daemon</link> invokes |
| 20357 |
<command>run-parts</command> to run the scripts in |
| 20358 |
the <filename class="directory">/etc/cron.*</filename> |
| 20359 |
directories.</para> |
| 20360 |
|
| 20361 |
</listitem> |
| 20362 |
</varlistentry> |
| 20363 |
|
| 20364 |
<varlistentry> |
| 20365 |
<term><anchor id="yesref"><command>yes</command></term> |
| 20366 |
<indexterm> |
| 20367 |
<primary>yes</primary> |
| 20368 |
</indexterm> |
| 20369 |
<indexterm> |
| 20370 |
<primary>command</primary> |
| 20371 |
<secondary>yes</secondary> |
| 20372 |
</indexterm> |
| 20373 |
<listitem> |
| 20374 |
|
| 20375 |
<para>In its default behavior the <command>yes</command> |
| 20376 |
command feeds a continuous string of the character |
| 20377 |
<computeroutput>y</computeroutput> followed |
| 20378 |
by a line feed to <filename>stdout</filename>. A |
| 20379 |
<keycombo><keycap>control</keycap><keycap>C</keycap></keycombo> |
| 20380 |
terminates the run. A different output string |
| 20381 |
may be specified, as in <userinput>yes different |
| 20382 |
string</userinput>, which would continually output |
| 20383 |
<computeroutput>different string</computeroutput> to |
| 20384 |
<filename>stdout</filename>.</para> |
| 20385 |
|
| 20386 |
<para>One might well ask the purpose of this. From the |
| 20387 |
command-line or in a script, the output of |
| 20388 |
<command>yes</command> can be redirected or piped into a |
| 20389 |
program expecting user input. In effect, this becomes a sort |
| 20390 |
of poor man's version of <firstterm>expect</firstterm>.</para> |
| 20391 |
|
| 20392 |
<para><userinput>yes | fsck /dev/hda1</userinput> runs |
| 20393 |
<command>fsck</command> non-interactively (careful!).</para> |
| 20394 |
|
| 20395 |
<para><userinput>yes | rm -r dirname</userinput> has same effect as |
| 20396 |
<userinput>rm -rf dirname</userinput> (careful!).</para> |
| 20397 |
|
| 20398 |
<warning><para>Caution advised when piping |
| 20399 |
<firstterm>yes</firstterm> to a potentially dangerous |
| 20400 |
system command, such as <link linkend="fsckref">fsck</link> |
| 20401 |
or <link linkend="fdiskref">fdisk</link>. It might have |
| 20402 |
unintended consequences.</para></warning> |
| 20403 |
|
| 20404 |
<note> |
| 20405 |
<para>The <firstterm>yes</firstterm> command parses variables, |
| 20406 |
or more accurately, it echoes parsed variables. |
| 20407 |
For example:</para> |
| 20408 |
<para> |
| 20409 |
<screen><prompt>bash$ </prompt><userinput>yes $BASH_VERSION</userinput> |
| 20410 |
<computeroutput>3.1.17(1)-release |
| 20411 |
3.1.17(1)-release |
| 20412 |
3.1.17(1)-release |
| 20413 |
3.1.17(1)-release |
| 20414 |
3.1.17(1)-release |
| 20415 |
. . .</computeroutput> |
| 20416 |
</screen> |
| 20417 |
</para> |
| 20418 |
|
| 20419 |
<para> |
| 20420 |
This particular <quote>feature</quote> may be used |
| 20421 |
to create a <emphasis>very large</emphasis> ASCII file on the fly: |
| 20422 |
<screen><prompt>bash$ </prompt><userinput>yes $PATH > huge_file.txt</userinput> |
| 20423 |
<userinput>Ctl-C</userinput> |
| 20424 |
</screen> |
| 20425 |
Hit <userinput>Ctl-C</userinput> <emphasis>very |
| 20426 |
quickly</emphasis>, or you just might get more than you |
| 20427 |
bargained for. . . . |
| 20428 |
</para> |
| 20429 |
</note> |
| 20430 |
|
| 20431 |
<para><anchor id="yesemu">The <firstterm>yes</firstterm> |
| 20432 |
command may be emulated in a very simple script <link |
| 20433 |
linkend="functionref">function</link>.</para> |
| 20434 |
|
| 20435 |
<para><programlisting>yes () |
| 20436 |
{ # Trivial emulation of "yes" ... |
| 20437 |
local DEFAULT_TEXT="y" |
| 20438 |
while [ true ] # Endless loop. |
| 20439 |
do |
| 20440 |
if [ -z "$1" ] |
| 20441 |
then |
| 20442 |
echo "$DEFAULT_TEXT" |
| 20443 |
else # If argument ... |
| 20444 |
echo "$1" # ... expand and echo it. |
| 20445 |
fi |
| 20446 |
done # The only things missing are the |
| 20447 |
} #+ --help and --version options.</programlisting></para> |
| 20448 |
|
| 20449 |
</listitem> |
| 20450 |
</varlistentry> |
| 20451 |
|
| 20452 |
<varlistentry> |
| 20453 |
<term><anchor id="bannerref"><command>banner</command></term> |
| 20454 |
<indexterm> |
| 20455 |
<primary>banner</primary> |
| 20456 |
</indexterm> |
| 20457 |
<indexterm> |
| 20458 |
<primary>command</primary> |
| 20459 |
<secondary>banner</secondary> |
| 20460 |
</indexterm> |
| 20461 |
<listitem> |
| 20462 |
<para>Prints arguments as a large vertical banner to |
| 20463 |
<filename>stdout</filename>, using an <link |
| 20464 |
linkend="asciidef">ASCII</link> character (default |
| 20465 |
'#'). This may be redirected to a printer for |
| 20466 |
hardcopy.</para> |
| 20467 |
|
| 20468 |
<para>Note that <firstterm>banner</firstterm> has been |
| 20469 |
dropped from many Linux distros, presumably because it |
| 20470 |
is no longer considered useful.</para> |
| 20471 |
</listitem> |
| 20472 |
</varlistentry> |
| 20473 |
|
| 20474 |
<varlistentry> |
| 20475 |
<term><anchor id="printenvref"><command>printenv</command></term> |
| 20476 |
<indexterm> |
| 20477 |
<primary>printenv</primary> |
| 20478 |
</indexterm> |
| 20479 |
<indexterm> |
| 20480 |
<primary>command</primary> |
| 20481 |
<secondary>environment</secondary> |
| 20482 |
</indexterm> |
| 20483 |
<listitem> |
| 20484 |
<para>Show all the <link linkend="envref">environmental |
| 20485 |
variables</link> set for a particular user.</para> |
| 20486 |
<para> |
| 20487 |
<screen><prompt>bash$ </prompt><userinput>printenv | grep HOME</userinput> |
| 20488 |
<computeroutput>HOME=/home/bozo</computeroutput> |
| 20489 |
</screen> |
| 20490 |
</para> |
| 20491 |
</listitem> |
| 20492 |
</varlistentry> |
| 20493 |
|
| 20494 |
<varlistentry> |
| 20495 |
<term><anchor id="lpref"><command>lp</command></term> |
| 20496 |
<indexterm> |
| 20497 |
<primary>lp</primary> |
| 20498 |
</indexterm> |
| 20499 |
<indexterm> |
| 20500 |
<primary>command</primary> |
| 20501 |
<secondary>lpr</secondary> |
| 20502 |
</indexterm> |
| 20503 |
<listitem> |
| 20504 |
<para>The <command>lp</command> and <command>lpr</command> |
| 20505 |
commands send file(s) to the print queue, to be printed as |
| 20506 |
hard copy. |
| 20507 |
|
| 20508 |
<footnote><para>The <firstterm>print queue</firstterm> is |
| 20509 |
the group of jobs <quote>waiting in line</quote> to be |
| 20510 |
printed.</para></footnote> |
| 20511 |
|
| 20512 |
These commands trace the origin of their names to the |
| 20513 |
line printers of another era. |
| 20514 |
<footnote><para>Large mechanical <firstterm>line |
| 20515 |
printers</firstterm> printed a single |
| 20516 |
line of type at a time onto joined |
| 20517 |
sheets of <firstterm>greenbar</firstterm> |
| 20518 |
paper, to the accompaniment of <ulink |
| 20519 |
url="http://www.columbia.edu/cu/computinghistory/1403.html">a |
| 20520 |
great deal of noise</ulink>. The hardcopy |
| 20521 |
thusly printed was referred to as a |
| 20522 |
<firstterm>printout</firstterm>.</para></footnote> |
| 20523 |
|
| 20524 |
</para> |
| 20525 |
|
| 20526 |
<para><prompt>bash$ </prompt><userinput>lp file1.txt</userinput> |
| 20527 |
or <prompt>bash </prompt><userinput>lp |
| 20528 |
<file1.txt</userinput></para> |
| 20529 |
|
| 20530 |
<para>It is often useful to pipe the formatted output from |
| 20531 |
<command>pr</command> to <command>lp</command>.</para> |
| 20532 |
|
| 20533 |
<para><prompt>bash$ </prompt><userinput>pr -options file1.txt | lp</userinput> |
| 20534 |
</para> |
| 20535 |
|
| 20536 |
<para>Formatting packages, such as <link |
| 20537 |
linkend="groffref">groff</link> and |
| 20538 |
<firstterm>Ghostscript</firstterm> may send their output |
| 20539 |
directly to <command>lp</command>.</para> |
| 20540 |
|
| 20541 |
<para><prompt>bash$ </prompt><userinput>groff -Tascii file.tr | lp</userinput> |
| 20542 |
</para> |
| 20543 |
|
| 20544 |
<para><prompt>bash$ </prompt><userinput>gs -options | lp file.ps</userinput> |
| 20545 |
</para> |
| 20546 |
|
| 20547 |
<para>Related commands are <command>lpq</command>, for viewing |
| 20548 |
the print queue, and <command>lprm</command>, for removing |
| 20549 |
jobs from the print queue.</para> |
| 20550 |
|
| 20551 |
</listitem> |
| 20552 |
</varlistentry> |
| 20553 |
|
| 20554 |
<varlistentry> |
| 20555 |
<term><anchor id="teeref"><command>tee</command></term> |
| 20556 |
<indexterm> |
| 20557 |
<primary>tee</primary> |
| 20558 |
</indexterm> |
| 20559 |
<indexterm> |
| 20560 |
<primary>command</primary> |
| 20561 |
<secondary>tee</secondary> |
| 20562 |
</indexterm> |
| 20563 |
<listitem> |
| 20564 |
<para>[UNIX borrows an idea from the plumbing trade.]</para> |
| 20565 |
<para>This is a redirection operator, but with a difference. Like the |
| 20566 |
plumber's <firstterm>tee,</firstterm> it permits <quote>siphoning |
| 20567 |
off</quote> <emphasis>to a file </emphasis>the output of a command |
| 20568 |
or commands within a pipe, but without affecting the result. This is |
| 20569 |
useful for printing an ongoing process to a file or paper, perhaps to |
| 20570 |
keep track of it for debugging purposes.</para> |
| 20571 |
|
| 20572 |
<screen> |
| 20573 |
(redirection) |
| 20574 |
|----> to file |
| 20575 |
| |
| 20576 |
==========================|==================== |
| 20577 |
command ---> command ---> |tee ---> command ---> ---> output of pipe |
| 20578 |
=============================================== |
| 20579 |
</screen> |
| 20580 |
|
| 20581 |
|
| 20582 |
<para><programlisting>cat listfile* | sort | tee check.file | uniq > result.file |
| 20583 |
# ^^^^^^^^^^^^^^ ^^^^ |
| 20584 |
|
| 20585 |
# The file "check.file" contains the concatenated sorted "listfiles," |
| 20586 |
#+ before the duplicate lines are removed by 'uniq.'</programlisting></para> |
| 20587 |
|
| 20588 |
</listitem> |
| 20589 |
</varlistentry> |
| 20590 |
|
| 20591 |
<varlistentry> |
| 20592 |
<term><anchor id="mkfiforef"><command>mkfifo</command></term> |
| 20593 |
<indexterm> |
| 20594 |
<primary>mkfifo</primary> |
| 20595 |
</indexterm> |
| 20596 |
<indexterm> |
| 20597 |
<primary>command</primary> |
| 20598 |
<secondary>mkfifo</secondary> |
| 20599 |
</indexterm> |
| 20600 |
<listitem> |
| 20601 |
<para><anchor id="namedpiperef">This obscure command |
| 20602 |
creates a <firstterm>named pipe</firstterm>, a temporary |
| 20603 |
<firstterm>first-in-first-out buffer</firstterm> for |
| 20604 |
transferring data between processes. |
| 20605 |
|
| 20606 |
<footnote><para>For an excellent overview of this |
| 20607 |
topic, see Andy Vaught's article, <ulink |
| 20608 |
url="http://www2.linuxjournal.com/lj-issues/issue41/2156.html">Introduction |
| 20609 |
to Named Pipes</ulink>, in the September, 1997 issue of |
| 20610 |
<ulink url="http://www.linuxjournal.com"><citetitle |
| 20611 |
pubwork="journal">Linux |
| 20612 |
Journal</citetitle></ulink>.</para></footnote> |
| 20613 |
|
| 20614 |
Typically, one process writes to the FIFO, and the other |
| 20615 |
reads from it. See <xref linkend="fifo">.</para> |
| 20616 |
|
| 20617 |
<para> |
| 20618 |
<programlisting>#!/bin/bash |
| 20619 |
# This short script by Omair Eshkenazi. |
| 20620 |
# Used in ABS Guide with permission (thanks!). |
| 20621 |
|
| 20622 |
mkfifo pipe1 # Yes, pipes can be given names. |
| 20623 |
mkfifo pipe2 # Hence the designation "named pipe." |
| 20624 |
|
| 20625 |
(cut -d' ' -f1 | tr "a-z" "A-Z") >pipe2 <pipe1 & |
| 20626 |
ls -l | tr -s ' ' | cut -d' ' -f3,9- | tee pipe1 | |
| 20627 |
cut -d' ' -f2 | paste - pipe2 |
| 20628 |
|
| 20629 |
rm -f pipe1 |
| 20630 |
rm -f pipe2 |
| 20631 |
|
| 20632 |
# No need to kill background processes when script terminates (why not?). |
| 20633 |
|
| 20634 |
exit $? |
| 20635 |
|
| 20636 |
Now, invoke the script and explain the output: |
| 20637 |
sh mkfifo-example.sh |
| 20638 |
|
| 20639 |
4830.tar.gz BOZO |
| 20640 |
pipe1 BOZO |
| 20641 |
pipe2 BOZO |
| 20642 |
mkfifo-example.sh BOZO |
| 20643 |
Mixed.msg BOZO</programlisting> |
| 20644 |
</para> |
| 20645 |
|
| 20646 |
</listitem> |
| 20647 |
</varlistentry> |
| 20648 |
|
| 20649 |
<varlistentry> |
| 20650 |
<term><anchor id="pathchkref"><command>pathchk</command></term> |
| 20651 |
<indexterm> |
| 20652 |
<primary>pathchk</primary> |
| 20653 |
</indexterm> |
| 20654 |
<indexterm> |
| 20655 |
<primary>command</primary> |
| 20656 |
<secondary>pathchk</secondary> |
| 20657 |
</indexterm> |
| 20658 |
<listitem> |
| 20659 |
<para>This command checks the validity of a filename. If the |
| 20660 |
filename exceeds the maximum allowable length (255 |
| 20661 |
characters) or one or more of the directories in |
| 20662 |
its path is not searchable, then an error message |
| 20663 |
results.</para> |
| 20664 |
|
| 20665 |
<para>Unfortunately, <command>pathchk</command> does |
| 20666 |
not return a recognizable error code, and it is therefore |
| 20667 |
pretty much useless in a script. Consider instead the |
| 20668 |
<link linkend="rtif">file test operators</link>.</para> |
| 20669 |
</listitem> |
| 20670 |
</varlistentry> |
| 20671 |
|
| 20672 |
|
| 20673 |
<varlistentry> |
| 20674 |
<term><anchor id="ddref"><command>dd</command></term> |
| 20675 |
<indexterm> |
| 20676 |
<primary>dd</primary> |
| 20677 |
</indexterm> |
| 20678 |
<indexterm> |
| 20679 |
<primary>command</primary> |
| 20680 |
<secondary>dd</secondary> |
| 20681 |
</indexterm> |
| 20682 |
<listitem> |
| 20683 |
<para>Though this somewhat obscure and much feared |
| 20684 |
<command>d</command>ata <command>d</command>uplicator |
| 20685 |
command originated as a utility for exchanging |
| 20686 |
data on magnetic tapes between UNIX minicomputers |
| 20687 |
and IBM mainframes, it still has its uses. |
| 20688 |
The <command>dd</command> command simply copies a |
| 20689 |
file (or <filename>stdin/stdout</filename>), but with |
| 20690 |
conversions. <anchor id="ddconversions">Possible conversions |
| 20691 |
include ASCII/EBCDIC, |
| 20692 |
|
| 20693 |
<footnote><para><acronym>EBCDIC</acronym> (pronounced |
| 20694 |
<quote>ebb-sid-ick</quote>) is an acronym for Extended |
| 20695 |
Binary Coded Decimal Interchange Code, an obsolete |
| 20696 |
IBM data format. A bizarre application of |
| 20697 |
the <option>conv=ebcdic</option> option of |
| 20698 |
<command>dd</command> is as a quick 'n easy, but not |
| 20699 |
very secure text file encoder. |
| 20700 |
<programlisting>cat $file | dd conv=swab,ebcdic > $file_encrypted |
| 20701 |
# Encode (looks like gibberish). |
| 20702 |
# Might as well switch bytes (swab), too, for a little extra obscurity. |
| 20703 |
|
| 20704 |
cat $file_encrypted | dd conv=swab,ascii > $file_plaintext |
| 20705 |
# Decode.</programlisting> |
| 20706 |
</para></footnote> |
| 20707 |
|
| 20708 |
upper/lower case, swapping of byte pairs between input |
| 20709 |
and output, and skipping and/or truncating the head or |
| 20710 |
tail of the input file.</para> |
| 20711 |
|
| 20712 |
<para> |
| 20713 |
<programlisting># Converting a file to all uppercase: |
| 20714 |
|
| 20715 |
dd if=$filename conv=ucase > $filename.uppercase |
| 20716 |
# lcase # For lower case conversion</programlisting> |
| 20717 |
</para> |
| 20718 |
|
| 20719 |
<para><anchor id="ddoptions"></para> |
| 20720 |
<para>Some basic options to <command>dd</command> are: |
| 20721 |
<itemizedlist> |
| 20722 |
<listitem> |
| 20723 |
<para>if=INFILE</para> |
| 20724 |
<para>INFILE is the <firstterm>source</firstterm> |
| 20725 |
file.</para> |
| 20726 |
</listitem> |
| 20727 |
<listitem> |
| 20728 |
<para>of=OUTFILE</para> |
| 20729 |
<para>OUTFILE is the <firstterm>target</firstterm> |
| 20730 |
file, the file that will have the data written to it.</para> |
| 20731 |
</listitem> |
| 20732 |
<listitem> |
| 20733 |
<para>bs=BLOCKSIZE</para> |
| 20734 |
<para>This is the size of each block of data being read |
| 20735 |
and written, usually a power of 2.</para> |
| 20736 |
</listitem> |
| 20737 |
<listitem> |
| 20738 |
<para>skip=BLOCKS</para> |
| 20739 |
<para>How many blocks of data to skip in INFILE before |
| 20740 |
starting to copy. This is useful when the INFILE has |
| 20741 |
<quote>garbage</quote> or garbled data in its |
| 20742 |
header or when it is desirable to copy only a portion |
| 20743 |
of the INFILE.</para> |
| 20744 |
</listitem> |
| 20745 |
<listitem> |
| 20746 |
<para>seek=BLOCKS</para> |
| 20747 |
<para>How many blocks of data to skip in OUTFILE before |
| 20748 |
starting to copy, leaving blank data at beginning |
| 20749 |
of OUTFILE.</para> |
| 20750 |
</listitem> |
| 20751 |
<listitem> |
| 20752 |
<para>count=BLOCKS</para> |
| 20753 |
<para>Copy only this many blocks of data, rather than the |
| 20754 |
entire INFILE.</para> |
| 20755 |
</listitem> |
| 20756 |
<listitem> |
| 20757 |
<para>conv=CONVERSION</para> |
| 20758 |
<para>Type of conversion to be applied to INFILE data |
| 20759 |
before copying operation.</para> |
| 20760 |
</listitem> |
| 20761 |
</itemizedlist> |
| 20762 |
</para> |
| 20763 |
|
| 20764 |
<para>A <userinput>dd --help</userinput> lists all the |
| 20765 |
options this powerful utility takes.</para> |
| 20766 |
|
| 20767 |
|
| 20768 |
<example id="selfcopy"> |
| 20769 |
<title>A script that copies itself</title> |
| 20770 |
<programlisting>&selfcopy;</programlisting> |
| 20771 |
</example> |
| 20772 |
|
| 20773 |
<example id="exercisingdd"> |
| 20774 |
<title>Exercising <firstterm>dd</firstterm></title> |
| 20775 |
<programlisting>&exercisingdd;</programlisting> |
| 20776 |
</example> |
| 20777 |
|
| 20778 |
|
| 20779 |
<para><anchor id="ddkeystrokes"></para> |
| 20780 |
<para>To demonstrate just how versatile <command>dd</command> is, |
| 20781 |
let's use it to capture keystrokes.</para> |
| 20782 |
|
| 20783 |
<example id="ddkeypress"> |
| 20784 |
<title>Capturing Keystrokes</title> |
| 20785 |
<programlisting>&ddkeypress;</programlisting> |
| 20786 |
</example> |
| 20787 |
|
| 20788 |
<para><anchor id="ddrandom"></para> |
| 20789 |
<para>The <command>dd</command> command can do random access on a |
| 20790 |
data stream. |
| 20791 |
|
| 20792 |
<programlisting>echo -n . | dd bs=1 seek=4 of=file conv=notrunc |
| 20793 |
# The "conv=notrunc" option means that the output file |
| 20794 |
#+ will not be truncated. |
| 20795 |
|
| 20796 |
# Thanks, S.C.</programlisting> |
| 20797 |
</para> |
| 20798 |
|
| 20799 |
|
| 20800 |
<para><anchor id="ddcopy"></para> |
| 20801 |
<para>The <command>dd</command> command can copy raw data |
| 20802 |
and disk images to and from devices, such as floppies and |
| 20803 |
tape drives (<xref linkend="copycd">). A common use is |
| 20804 |
creating boot floppies.</para> |
| 20805 |
<para> |
| 20806 |
<userinput>dd if=kernel-image of=/dev/fd0H1440</userinput> |
| 20807 |
</para> |
| 20808 |
|
| 20809 |
<para>Similarly, <command>dd</command> can copy the entire |
| 20810 |
contents of a floppy, even one formatted with a |
| 20811 |
<quote>foreign</quote> OS, to the hard drive as an |
| 20812 |
image file.</para> |
| 20813 |
<para> |
| 20814 |
<userinput>dd if=/dev/fd0 of=/home/bozo/projects/floppy.img</userinput> |
| 20815 |
</para> |
| 20816 |
|
| 20817 |
<para><anchor id="bfs">Likewise, <command>dd</command> |
| 20818 |
can create bootable flash drives and SD cards.</para> |
| 20819 |
|
| 20820 |
<para><userinput>dd if=image.iso of=/dev/sdb</userinput></para> |
| 20821 |
|
| 20822 |
<para><anchor id="rpsdcard01"></para> |
| 20823 |
<example id="rpsdcard"> |
| 20824 |
<title>Preparing a bootable SD card for the |
| 20825 |
<emphasis>Raspberry Pi</emphasis></title> |
| 20826 |
<programlisting>&rpsdcard;</programlisting> |
| 20827 |
</example> |
| 20828 |
|
| 20829 |
<para><anchor id="ddswap"></para> |
| 20830 |
<para> |
| 20831 |
Other applications of <command>dd</command> include |
| 20832 |
initializing temporary swap files (<xref linkend="ex73">) |
| 20833 |
and ramdisks (<xref linkend="ramdisk">). It can even do a |
| 20834 |
low-level copy of an entire hard drive partition, although |
| 20835 |
this is not necessarily recommended.</para> |
| 20836 |
|
| 20837 |
<para>People (with presumably nothing better to do with |
| 20838 |
their time) are constantly thinking of interesting |
| 20839 |
applications of <command>dd</command>.</para> |
| 20840 |
|
| 20841 |
<para><anchor id="ddfdel"></para> |
| 20842 |
<example id="blotout"> |
| 20843 |
<title>Securely deleting a file</title> |
| 20844 |
<programlisting>&blotout;</programlisting> |
| 20845 |
</example> |
| 20846 |
|
| 20847 |
<para>See also the <link linkend="ddlink">dd |
| 20848 |
thread</link> entry in the <link |
| 20849 |
linkend="biblioref">bibliography</link>.</para> |
| 20850 |
|
| 20851 |
</listitem> |
| 20852 |
</varlistentry> |
| 20853 |
|
| 20854 |
<varlistentry> |
| 20855 |
<term><anchor id="odref"><command>od</command></term> |
| 20856 |
<indexterm> |
| 20857 |
<primary>od</primary> |
| 20858 |
</indexterm> |
| 20859 |
<indexterm> |
| 20860 |
<primary>command</primary> |
| 20861 |
<secondary>od</secondary> |
| 20862 |
</indexterm> |
| 20863 |
<listitem> |
| 20864 |
<para>The <command>od</command>, or <firstterm>octal |
| 20865 |
dump</firstterm> filter converts input (or files) to octal |
| 20866 |
(base-8) or other bases. This is useful for viewing or |
| 20867 |
processing binary data files or otherwise unreadable system |
| 20868 |
<link linkend="devfileref">device files</link>, such as |
| 20869 |
<filename>/dev/urandom</filename>, and as a filter for |
| 20870 |
binary data.</para> |
| 20871 |
|
| 20872 |
<para> |
| 20873 |
<programlisting>head -c4 /dev/urandom | od -N4 -tu4 | sed -ne '1s/.* //p' |
| 20874 |
# Sample output: 1324725719, 3918166450, 2989231420, etc. |
| 20875 |
|
| 20876 |
# From rnd.sh example script, by Stéphane Chazelas</programlisting> |
| 20877 |
</para> |
| 20878 |
|
| 20879 |
<para>See also <xref linkend="seedingrandom"> and <xref |
| 20880 |
linkend="insertionsort">.</para> |
| 20881 |
|
| 20882 |
</listitem> |
| 20883 |
</varlistentry> |
| 20884 |
|
| 20885 |
<varlistentry> |
| 20886 |
<term><anchor id="hexdumpref"><command>hexdump</command></term> |
| 20887 |
<indexterm> |
| 20888 |
<primary>hexdump</primary> |
| 20889 |
</indexterm> |
| 20890 |
<indexterm> |
| 20891 |
<primary>command</primary> |
| 20892 |
<secondary>hexadecimal</secondary> |
| 20893 |
</indexterm> |
| 20894 |
<listitem> |
| 20895 |
<para>Performs a hexadecimal, octal, decimal, or ASCII |
| 20896 |
dump of a binary file. This command is the rough equivalent |
| 20897 |
of <command>od</command>, above, but not nearly as |
| 20898 |
useful. May be used to view the contents of a binary file, |
| 20899 |
in combination with <link linkend="ddref">dd</link> and <link |
| 20900 |
linkend="lessref">less</link>.</para> |
| 20901 |
|
| 20902 |
<para> |
| 20903 |
<programlisting>dd if=/bin/ls | hexdump -C | less |
| 20904 |
# The -C option nicely formats the output in tabular form.</programlisting> |
| 20905 |
</para> |
| 20906 |
|
| 20907 |
</listitem> |
| 20908 |
</varlistentry> |
| 20909 |
|
| 20910 |
|
| 20911 |
<varlistentry> |
| 20912 |
<term><anchor id="objdumpref"><command>objdump</command></term> |
| 20913 |
<indexterm> |
| 20914 |
<primary>objdump</primary> |
| 20915 |
</indexterm> |
| 20916 |
<indexterm> |
| 20917 |
<primary>command</primary> |
| 20918 |
<secondary>object binary dump</secondary> |
| 20919 |
</indexterm> |
| 20920 |
<listitem> |
| 20921 |
<para>Displays information about an object file or binary |
| 20922 |
executable in either hexadecimal form or as a disassembled |
| 20923 |
listing (with the <option>-d</option> option).</para> |
| 20924 |
|
| 20925 |
<para> |
| 20926 |
<screen><prompt>bash$ </prompt><userinput>objdump -d /bin/ls</userinput> |
| 20927 |
<computeroutput>/bin/ls: file format elf32-i386 |
| 20928 |
|
| 20929 |
Disassembly of section .init: |
| 20930 |
|
| 20931 |
080490bc <.init>: |
| 20932 |
80490bc: 55 push %ebp |
| 20933 |
80490bd: 89 e5 mov %esp,%ebp |
| 20934 |
. . .</computeroutput> |
| 20935 |
</screen> |
| 20936 |
</para> |
| 20937 |
</listitem> |
| 20938 |
</varlistentry> |
| 20939 |
|
| 20940 |
<varlistentry> |
| 20941 |
<term><anchor id="mcookieref"><command>mcookie</command></term> |
| 20942 |
<indexterm> |
| 20943 |
<primary>magic</primary> |
| 20944 |
</indexterm> |
| 20945 |
<indexterm> |
| 20946 |
<primary>command</primary> |
| 20947 |
<secondary>cookie</secondary> |
| 20948 |
</indexterm> |
| 20949 |
<listitem> |
| 20950 |
|
| 20951 |
<para>This command generates a <quote>magic cookie,</quote> a |
| 20952 |
128-bit (32-character) pseudorandom hexadecimal number, |
| 20953 |
normally used as an authorization <quote>signature</quote> |
| 20954 |
by the X server. This also available for use in a script |
| 20955 |
as a <quote>quick 'n dirty</quote> random number.</para> |
| 20956 |
|
| 20957 |
<para><programlisting>random000=$(mcookie)</programlisting></para> |
| 20958 |
|
| 20959 |
<para>Of course, a script could use <link |
| 20960 |
linkend="md5sumref">md5sum</link> for the same purpose.</para> |
| 20961 |
|
| 20962 |
<para><programlisting># Generate md5 checksum on the script itself. |
| 20963 |
random001=`md5sum $0 | awk '{print $1}'` |
| 20964 |
# Uses 'awk' to strip off the filename.</programlisting></para> |
| 20965 |
|
| 20966 |
<para>The <command>mcookie</command> command gives yet another way |
| 20967 |
to generate a <quote>unique</quote> filename.</para> |
| 20968 |
|
| 20969 |
<example id="tempfilename"> |
| 20970 |
<title>Filename generator</title> |
| 20971 |
<programlisting>&tempfilename;</programlisting> |
| 20972 |
</example> |
| 20973 |
|
| 20974 |
</listitem> |
| 20975 |
</varlistentry> |
| 20976 |
|
| 20977 |
<varlistentry> |
| 20978 |
<term><anchor id="unitsref"><command>units</command></term> |
| 20979 |
<indexterm> |
| 20980 |
<primary>units</primary> |
| 20981 |
</indexterm> |
| 20982 |
<indexterm> |
| 20983 |
<primary>command</primary> |
| 20984 |
<secondary>conversion</secondary> |
| 20985 |
</indexterm> |
| 20986 |
<listitem> |
| 20987 |
<para>This utility converts between different <firstterm>units |
| 20988 |
of measure</firstterm>. While normally invoked in interactive |
| 20989 |
mode, <command>units</command> may find use in a |
| 20990 |
script.</para> |
| 20991 |
<example id="unitconversion"> |
| 20992 |
<title>Converting meters to miles</title> |
| 20993 |
<programlisting>&unitconversion;</programlisting> |
| 20994 |
</example> |
| 20995 |
</listitem> |
| 20996 |
</varlistentry> |
| 20997 |
|
| 20998 |
|
| 20999 |
<varlistentry> |
| 21000 |
<term><anchor id="m4ref"><command>m4</command></term> |
| 21001 |
<indexterm> |
| 21002 |
<primary>m4</primary> |
| 21003 |
</indexterm> |
| 21004 |
<indexterm> |
| 21005 |
<primary>command</primary> |
| 21006 |
<secondary>macro</secondary> |
| 21007 |
</indexterm> |
| 21008 |
<listitem> |
| 21009 |
<para>A hidden treasure, <command>m4</command> is a |
| 21010 |
powerful macro |
| 21011 |
<footnote><para>A <firstterm>macro</firstterm> is a |
| 21012 |
symbolic constant that expands into a command string |
| 21013 |
or a set of operations on parameters. Simply put, |
| 21014 |
it's a shortcut or abbreviation.</para></footnote> |
| 21015 |
processing filter, virtually a complete language. |
| 21016 |
Although originally written as a pre-processor |
| 21017 |
for <firstterm>RatFor</firstterm>, <command>m4</command> |
| 21018 |
turned out to be useful as a stand-alone utility. In |
| 21019 |
fact, <command>m4</command> combines some of the |
| 21020 |
functionality of <link linkend="evalref">eval</link>, |
| 21021 |
<link linkend="trref">tr</link>, and <link |
| 21022 |
linkend="awkref">awk</link>, in addition to its extensive |
| 21023 |
macro expansion facilities.</para> |
| 21024 |
|
| 21025 |
<para>The April, 2002 issue of <ulink |
| 21026 |
url="http://www.linuxjournal.com"><citetitle |
| 21027 |
pubwork="journal">Linux Journal</citetitle></ulink> |
| 21028 |
has a very nice article on <command>m4</command> and |
| 21029 |
its uses.</para> |
| 21030 |
|
| 21031 |
<example id="m4"> |
| 21032 |
<title>Using <firstterm>m4</firstterm></title> |
| 21033 |
<programlisting>&m4;</programlisting> |
| 21034 |
</example> |
| 21035 |
|
| 21036 |
</listitem> |
| 21037 |
</varlistentry> |
| 21038 |
|
| 21039 |
<varlistentry> |
| 21040 |
<term><anchor id="xmessageref"><command>xmessage</command></term> |
| 21041 |
<indexterm> |
| 21042 |
<primary>xmessage</primary> |
| 21043 |
</indexterm> |
| 21044 |
<indexterm> |
| 21045 |
<primary>command</primary> |
| 21046 |
<secondary>macro</secondary> |
| 21047 |
</indexterm> |
| 21048 |
<listitem> |
| 21049 |
<para>This X-based variant of |
| 21050 |
<link linkend="echoref">echo</link> pops up a message/query |
| 21051 |
window on the desktop.</para> |
| 21052 |
<para> |
| 21053 |
<programlisting>xmessage Left click to continue -button okay</programlisting> |
| 21054 |
</para> |
| 21055 |
</listitem> |
| 21056 |
</varlistentry> |
| 21057 |
|
| 21058 |
<varlistentry> |
| 21059 |
<term><anchor id="zenityref"><command>zenity</command></term> |
| 21060 |
<indexterm> |
| 21061 |
<primary>zenity</primary> |
| 21062 |
</indexterm> |
| 21063 |
<indexterm> |
| 21064 |
<primary>command</primary> |
| 21065 |
<secondary>macro</secondary> |
| 21066 |
</indexterm> |
| 21067 |
<listitem> |
| 21068 |
<para>The |
| 21069 |
<ulink url="http://freshmeat.net/projects/zenity">zenity</ulink> |
| 21070 |
utility is adept at displaying |
| 21071 |
<firstterm>GTK+</firstterm> dialog <link |
| 21072 |
linkend="widgetref">widgets</link> and <link |
| 21073 |
linkend="zenityref2">very suitable for scripting |
| 21074 |
purposes</link>.</para> |
| 21075 |
</listitem> |
| 21076 |
</varlistentry> |
| 21077 |
|
| 21078 |
|
| 21079 |
<varlistentry> |
| 21080 |
<term><anchor id="doexecref"><command>doexec</command></term> |
| 21081 |
<indexterm> |
| 21082 |
<primary>doexec</primary> |
| 21083 |
</indexterm> |
| 21084 |
<indexterm> |
| 21085 |
<primary>command</primary> |
| 21086 |
<secondary>executable arg list</secondary> |
| 21087 |
</indexterm> |
| 21088 |
<listitem> |
| 21089 |
<para>The <command>doexec</command> command enables passing |
| 21090 |
an arbitrary list of arguments to a <firstterm>binary |
| 21091 |
executable</firstterm>. In particular, passing |
| 21092 |
<parameter>argv[0]</parameter> (which corresponds to <link |
| 21093 |
linkend="posparamref1">$0</link> in a script) lets the |
| 21094 |
executable be invoked by various names, and it can then |
| 21095 |
carry out different sets of actions, according to the name |
| 21096 |
by which it was called. What this amounts to is roundabout |
| 21097 |
way of passing options to an executable.</para> |
| 21098 |
|
| 21099 |
<para>For example, the <filename |
| 21100 |
class="directory">/usr/local/bin</filename> directory might |
| 21101 |
contain a binary called <quote>aaa</quote>. Invoking |
| 21102 |
<command>doexec /usr/local/bin/aaa list</command> |
| 21103 |
would <emphasis>list</emphasis> all those files |
| 21104 |
in the current working directory beginning with an |
| 21105 |
<quote>a</quote>, while invoking (the same executable |
| 21106 |
with) <command>doexec /usr/local/bin/aaa delete </command> |
| 21107 |
would <emphasis>delete</emphasis> those files.</para> |
| 21108 |
|
| 21109 |
<note><para>The various behaviors of the executable |
| 21110 |
must be defined within the code of the executable itself, |
| 21111 |
analogous to something like the following in a shell script: |
| 21112 |
<programlisting>case `basename $0` in |
| 21113 |
"name1" ) do_something;; |
| 21114 |
"name2" ) do_something_else;; |
| 21115 |
"name3" ) do_yet_another_thing;; |
| 21116 |
* ) bail_out;; |
| 21117 |
esac</programlisting></para></note> |
| 21118 |
|
| 21119 |
</listitem> |
| 21120 |
</varlistentry> |
| 21121 |
|
| 21122 |
|
| 21123 |
<varlistentry> |
| 21124 |
<term><command>dialog</command></term> |
| 21125 |
<indexterm> |
| 21126 |
<primary>dialog</primary> |
| 21127 |
</indexterm> |
| 21128 |
<indexterm> |
| 21129 |
<primary>command</primary> |
| 21130 |
<secondary>dialog</secondary> |
| 21131 |
</indexterm> |
| 21132 |
<listitem> |
| 21133 |
<para>The <link linkend="dialogref">dialog</link> family of tools |
| 21134 |
provide a method of calling interactive |
| 21135 |
<quote>dialog</quote> boxes from a script. The more |
| 21136 |
elaborate variations of <command>dialog</command> -- |
| 21137 |
<command>gdialog</command>, <command>Xdialog</command>, |
| 21138 |
and <command>kdialog</command> -- actually invoke X-Windows |
| 21139 |
<link linkend="widgetref">widgets</link>.</para> |
| 21140 |
</listitem> |
| 21141 |
</varlistentry> |
| 21142 |
|
| 21143 |
<varlistentry> |
| 21144 |
<term><anchor id="soxref"><command>sox</command></term> |
| 21145 |
<indexterm> |
| 21146 |
<primary>sox</primary> |
| 21147 |
</indexterm> |
| 21148 |
<indexterm> |
| 21149 |
<primary>command</primary> |
| 21150 |
<secondary>sound</secondary> |
| 21151 |
</indexterm> |
| 21152 |
<listitem> |
| 21153 |
|
| 21154 |
<para>The <command>sox</command>, or |
| 21155 |
<quote><command>so</command>und |
| 21156 |
e<command>x</command>change</quote> command plays and |
| 21157 |
performs transformations on sound files. In fact, |
| 21158 |
the <filename>/usr/bin/play</filename> executable |
| 21159 |
(now deprecated) is nothing but a shell wrapper for |
| 21160 |
<firstterm>sox</firstterm>.</para> |
| 21161 |
|
| 21162 |
<para>For example, <command>sox soundfile.wav |
| 21163 |
soundfile.au</command> changes a WAV sound file into a |
| 21164 |
(Sun audio format) AU sound file.</para> |
| 21165 |
|
| 21166 |
<para>Shell scripts are ideally suited for batch-processing |
| 21167 |
<command>sox</command> operations on |
| 21168 |
sound files. For examples, see the <ulink |
| 21169 |
url="http://osl.iu.edu/~tveldhui/radio/"> Linux Radio |
| 21170 |
Timeshift HOWTO</ulink> and the <ulink |
| 21171 |
url="http://savannah.nongnu.org/projects/audiodo">MP3do |
| 21172 |
Project</ulink>.</para> |
| 21173 |
|
| 21174 |
</listitem> |
| 21175 |
</varlistentry> |
| 21176 |
|
| 21177 |
</variablelist> |
| 21178 |
|
| 21179 |
|
| 21180 |
</sect1> <!-- End Miscellaneous Commands --> |
| 21181 |
|
| 21182 |
</chapter> <!-- External Filters, Programs and Commands --> |
| 21183 |
|
| 21184 |
|
| 21185 |
|
| 21186 |
<chapter id="system"> |
| 21187 |
<title>System and Administrative Commands</title> |
| 21188 |
|
| 21189 |
|
| 21190 |
<para>The startup and shutdown scripts in |
| 21191 |
<filename class="directory">/etc/rc.d</filename> illustrate the uses |
| 21192 |
(and usefulness) of many of these comands. These are usually |
| 21193 |
invoked by <firstterm>root</firstterm> and used for system |
| 21194 |
maintenance or emergency filesystem repairs. Use with caution, as |
| 21195 |
some of these commands may damage your system if misused.</para> |
| 21196 |
|
| 21197 |
<variablelist id="usersgroups"> |
| 21198 |
<title><anchor id="usersgroups1">Users and Groups</title> |
| 21199 |
|
| 21200 |
<varlistentry> |
| 21201 |
<term><anchor id="usersref"><command>users</command></term> |
| 21202 |
<indexterm> |
| 21203 |
<primary>users</primary> |
| 21204 |
</indexterm> |
| 21205 |
<indexterm> |
| 21206 |
<primary>command</primary> |
| 21207 |
<secondary>users</secondary> |
| 21208 |
</indexterm> |
| 21209 |
<listitem> |
| 21210 |
<para>Show all logged on users. This is the approximate |
| 21211 |
equivalent of <command>who -q</command>.</para> |
| 21212 |
</listitem> |
| 21213 |
</varlistentry> |
| 21214 |
|
| 21215 |
<varlistentry> |
| 21216 |
<term><anchor id="groupscmdref"><command>groups</command></term> |
| 21217 |
<indexterm> |
| 21218 |
<primary>groups</primary> |
| 21219 |
</indexterm> |
| 21220 |
<indexterm> |
| 21221 |
<primary>command</primary> |
| 21222 |
<secondary>groups</secondary> |
| 21223 |
</indexterm> |
| 21224 |
<listitem> |
| 21225 |
<para>Lists the current user and the groups she belongs to. |
| 21226 |
This corresponds to the <link |
| 21227 |
linkend="groupsref">$GROUPS</link> internal variable, |
| 21228 |
but gives the group names, rather than the numbers.</para> |
| 21229 |
<screen><prompt>bash$ </prompt><userinput>groups</userinput> |
| 21230 |
<computeroutput>bozita cdrom cdwriter audio xgrp</computeroutput> |
| 21231 |
|
| 21232 |
<prompt>bash$ </prompt><userinput>echo $GROUPS</userinput> |
| 21233 |
<computeroutput>501</computeroutput></screen> |
| 21234 |
</listitem> |
| 21235 |
</varlistentry> |
| 21236 |
|
| 21237 |
<varlistentry> |
| 21238 |
<term><anchor id="chownref"><command>chown</command></term> |
| 21239 |
<term><anchor id="chgrpref"><command>chgrp</command></term> |
| 21240 |
<indexterm> |
| 21241 |
<primary>chown</primary> |
| 21242 |
</indexterm> |
| 21243 |
<indexterm> |
| 21244 |
<primary>command</primary> |
| 21245 |
<secondary>chown</secondary> |
| 21246 |
</indexterm> |
| 21247 |
<indexterm> |
| 21248 |
<primary>chgrp</primary> |
| 21249 |
</indexterm> |
| 21250 |
<indexterm> |
| 21251 |
<primary>command</primary> |
| 21252 |
<secondary>chgrp</secondary> |
| 21253 |
</indexterm> |
| 21254 |
<listitem> |
| 21255 |
<para>The <command>chown</command> command changes the |
| 21256 |
ownership of a file or files. This command is a useful |
| 21257 |
method that <firstterm>root</firstterm> can use to |
| 21258 |
shift file ownership from one user to another. An ordinary |
| 21259 |
user may not change the ownership of files, not even her |
| 21260 |
own files. |
| 21261 |
<footnote><para>This is the case on a Linux machine or a UNIX |
| 21262 |
system with disk quotas.</para></footnote> |
| 21263 |
</para> |
| 21264 |
|
| 21265 |
<para> |
| 21266 |
<screen><prompt>root# </prompt><userinput>chown bozo *.txt</userinput> |
| 21267 |
<computeroutput></computeroutput> |
| 21268 |
</screen> |
| 21269 |
</para> |
| 21270 |
|
| 21271 |
<para>The <command>chgrp</command> command changes the |
| 21272 |
<replaceable>group</replaceable> ownership of a file or |
| 21273 |
files. You must be owner of the file(s) as well as a member |
| 21274 |
of the destination group (or <firstterm>root</firstterm>) |
| 21275 |
to use this operation. |
| 21276 |
<programlisting>chgrp --recursive dunderheads *.data |
| 21277 |
# The "dunderheads" group will now own all the "*.data" files |
| 21278 |
#+ all the way down the $PWD directory tree (that's what "recursive" means). |
| 21279 |
</programlisting></para> |
| 21280 |
|
| 21281 |
</listitem> |
| 21282 |
</varlistentry> |
| 21283 |
|
| 21284 |
<varlistentry> |
| 21285 |
<term><anchor id="useraddref"><command>useradd</command></term> |
| 21286 |
<term><command>userdel</command></term> |
| 21287 |
<indexterm> |
| 21288 |
<primary>useradd</primary> |
| 21289 |
</indexterm> |
| 21290 |
<indexterm> |
| 21291 |
<primary>command</primary> |
| 21292 |
<secondary>useradd</secondary> |
| 21293 |
</indexterm> |
| 21294 |
<indexterm> |
| 21295 |
<primary>userdel</primary> |
| 21296 |
</indexterm> |
| 21297 |
<indexterm> |
| 21298 |
<primary>command</primary> |
| 21299 |
<secondary>userdel</secondary> |
| 21300 |
</indexterm> |
| 21301 |
<listitem> |
| 21302 |
<para>The <command>useradd</command> administrative command |
| 21303 |
adds a user account to the system and creates a home |
| 21304 |
directory for that particular user, if so specified. The |
| 21305 |
corresponding <command>userdel</command> command removes |
| 21306 |
a user account from the system |
| 21307 |
<footnote><para>The <command>userdel</command> command |
| 21308 |
will fail if the particular user being deleted is |
| 21309 |
still logged on.</para></footnote> |
| 21310 |
and deletes associated files.</para> |
| 21311 |
|
| 21312 |
<note><para>The <command>adduser</command> command is a synonym |
| 21313 |
for <command>useradd</command> and is usually a symbolic link to |
| 21314 |
it.</para></note> |
| 21315 |
|
| 21316 |
</listitem> |
| 21317 |
</varlistentry> |
| 21318 |
|
| 21319 |
<varlistentry> |
| 21320 |
<term><anchor id="usermodref"><command>usermod</command></term> |
| 21321 |
<indexterm> |
| 21322 |
<primary>usermod</primary> |
| 21323 |
</indexterm> |
| 21324 |
<indexterm> |
| 21325 |
<primary>command</primary> |
| 21326 |
<secondary>usermod</secondary> |
| 21327 |
</indexterm> |
| 21328 |
<listitem> |
| 21329 |
<para>Modify a user account. Changes may be made to the password, |
| 21330 |
group membership, expiration date, and other attributes of |
| 21331 |
a given user's account. With this command, a user's password |
| 21332 |
may be locked, which has the effect of disabling the |
| 21333 |
account.</para> |
| 21334 |
</listitem> |
| 21335 |
</varlistentry> |
| 21336 |
|
| 21337 |
<varlistentry> |
| 21338 |
<term><anchor id="groupmodref"><command>groupmod</command></term> |
| 21339 |
<indexterm> |
| 21340 |
<primary>groupmod</primary> |
| 21341 |
</indexterm> |
| 21342 |
<indexterm> |
| 21343 |
<primary>command</primary> |
| 21344 |
<secondary>group</secondary> |
| 21345 |
</indexterm> |
| 21346 |
<listitem> |
| 21347 |
<para>Modify a given group. The group name and/or ID number may be |
| 21348 |
changed using this command.</para> |
| 21349 |
</listitem> |
| 21350 |
</varlistentry> |
| 21351 |
|
| 21352 |
|
| 21353 |
<varlistentry> |
| 21354 |
<term><anchor id="idref"><command>id</command></term> |
| 21355 |
<indexterm> |
| 21356 |
<primary>id</primary> |
| 21357 |
</indexterm> |
| 21358 |
<indexterm> |
| 21359 |
<primary>command</primary> |
| 21360 |
<secondary>id</secondary> |
| 21361 |
</indexterm> |
| 21362 |
<listitem> |
| 21363 |
|
| 21364 |
<para>The <command>id</command> command lists the real and |
| 21365 |
effective user IDs and the group IDs of the user |
| 21366 |
associated with the current process. This is the |
| 21367 |
counterpart to the <link linkend="uidref">$UID</link>, |
| 21368 |
<link linkend="euidref">$EUID</link>, and <link |
| 21369 |
linkend="groupsref">$GROUPS</link> internal Bash |
| 21370 |
variables.</para> |
| 21371 |
|
| 21372 |
<screen><prompt>bash$ </prompt><userinput>id</userinput> |
| 21373 |
<computeroutput>uid=501(bozo) gid=501(bozo) groups=501(bozo),22(cdrom),80(cdwriter),81(audio)</computeroutput> |
| 21374 |
|
| 21375 |
<prompt>bash$ </prompt><userinput>echo $UID</userinput> |
| 21376 |
<computeroutput>501</computeroutput></screen> |
| 21377 |
|
| 21378 |
<note><para>The <command>id</command> command shows the |
| 21379 |
<emphasis>effective</emphasis> IDs only when they differ |
| 21380 |
from the <emphasis>real</emphasis> ones.</para></note> |
| 21381 |
|
| 21382 |
<para>Also see <xref linkend="amiroot">.</para> |
| 21383 |
|
| 21384 |
</listitem> |
| 21385 |
</varlistentry> |
| 21386 |
|
| 21387 |
<varlistentry> |
| 21388 |
<term><anchor id="lidref"><command>lid</command></term> |
| 21389 |
<indexterm> |
| 21390 |
<primary>lid</primary> |
| 21391 |
</indexterm> |
| 21392 |
<indexterm> |
| 21393 |
<primary>command</primary> |
| 21394 |
<secondary>group</secondary> |
| 21395 |
</indexterm> |
| 21396 |
<listitem> |
| 21397 |
<para>The <firstterm>lid</firstterm> (list ID) command |
| 21398 |
shows the group(s) that a given user belongs to, or alternately, |
| 21399 |
the users belonging to a given group. May be invoked only by |
| 21400 |
root.</para> |
| 21401 |
<para> |
| 21402 |
<screen><prompt>root# </prompt><userinput>lid bozo</userinput> |
| 21403 |
<computeroutput> bozo(gid=500)</computeroutput> |
| 21404 |
|
| 21405 |
|
| 21406 |
<prompt>root# </prompt><userinput>lid daemon</userinput> |
| 21407 |
<computeroutput> bin(gid=1) |
| 21408 |
daemon(gid=2) |
| 21409 |
adm(gid=4) |
| 21410 |
lp(gid=7)</computeroutput> |
| 21411 |
</screen> |
| 21412 |
</para> |
| 21413 |
</listitem> |
| 21414 |
</varlistentry> |
| 21415 |
|
| 21416 |
<varlistentry> |
| 21417 |
<term><anchor id="whoref"><command>who</command></term> |
| 21418 |
<indexterm> |
| 21419 |
<primary>who</primary> |
| 21420 |
</indexterm> |
| 21421 |
<indexterm> |
| 21422 |
<primary>command</primary> |
| 21423 |
<secondary>whoami</secondary> |
| 21424 |
</indexterm> |
| 21425 |
<listitem> |
| 21426 |
|
| 21427 |
<para>Show all users logged on to the system.</para> |
| 21428 |
<para> |
| 21429 |
<screen><prompt>bash$ </prompt><userinput>who</userinput> |
| 21430 |
<computeroutput>bozo tty1 Apr 27 17:45 |
| 21431 |
bozo pts/0 Apr 27 17:46 |
| 21432 |
bozo pts/1 Apr 27 17:47 |
| 21433 |
bozo pts/2 Apr 27 17:49 |
| 21434 |
</computeroutput> |
| 21435 |
</screen> |
| 21436 |
</para> |
| 21437 |
|
| 21438 |
<para>The <option>-m</option> gives detailed information about |
| 21439 |
only the current user. Passing any two arguments to |
| 21440 |
<command>who</command> is the equivalent of <command>who |
| 21441 |
-m</command>, as in <command>who am i</command> or <command>who |
| 21442 |
The Man</command>.</para> |
| 21443 |
<para> |
| 21444 |
<screen><prompt>bash$ </prompt><userinput>who -m</userinput> |
| 21445 |
<computeroutput>localhost.localdomain!bozo pts/2 Apr 27 17:49</computeroutput> |
| 21446 |
</screen> |
| 21447 |
</para> |
| 21448 |
|
| 21449 |
|
| 21450 |
<para><anchor id="whoamiref"><command>whoami</command> is similar to <command>who |
| 21451 |
-m</command>, but only lists the user name.</para> |
| 21452 |
<para> |
| 21453 |
<screen><prompt>bash$ </prompt><userinput>whoami</userinput> |
| 21454 |
<computeroutput>bozo</computeroutput> |
| 21455 |
</screen> |
| 21456 |
</para> |
| 21457 |
|
| 21458 |
</listitem> |
| 21459 |
</varlistentry> |
| 21460 |
|
| 21461 |
<varlistentry> |
| 21462 |
<term><anchor id="wref"><command>w</command></term> |
| 21463 |
<indexterm> |
| 21464 |
<primary>w</primary> |
| 21465 |
</indexterm> |
| 21466 |
<indexterm> |
| 21467 |
<primary>command</primary> |
| 21468 |
<secondary>w</secondary> |
| 21469 |
</indexterm> |
| 21470 |
<listitem> |
| 21471 |
<para>Show all logged on users and the processes belonging to them. This is |
| 21472 |
an extended version of <command>who</command>. The output of <command>w</command> |
| 21473 |
may be piped to <link linkend="grepref">grep</link> to find |
| 21474 |
a specific user and/or process.</para> |
| 21475 |
|
| 21476 |
<screen><prompt>bash$ </prompt><userinput>w | grep startx</userinput> |
| 21477 |
<computeroutput>bozo tty1 - 4:22pm 6:41 4.47s 0.45s startx</computeroutput></screen> |
| 21478 |
</listitem> |
| 21479 |
</varlistentry> |
| 21480 |
|
| 21481 |
<varlistentry> |
| 21482 |
<term><anchor id="lognameref"><command>logname</command></term> |
| 21483 |
<indexterm> |
| 21484 |
<primary>logname</primary> |
| 21485 |
</indexterm> |
| 21486 |
<indexterm> |
| 21487 |
<primary>command</primary> |
| 21488 |
<secondary>logname</secondary> |
| 21489 |
</indexterm> |
| 21490 |
<listitem> |
| 21491 |
|
| 21492 |
<para>Show current user's login name (as found in |
| 21493 |
<filename>/var/run/utmp</filename>). This is a |
| 21494 |
near-equivalent to <link linkend="whoamiref">whoami</link>, |
| 21495 |
above.</para> |
| 21496 |
|
| 21497 |
<screen><prompt>bash$ </prompt><userinput>logname</userinput> |
| 21498 |
<computeroutput>bozo</computeroutput> |
| 21499 |
|
| 21500 |
<prompt>bash$ </prompt><userinput>whoami</userinput> |
| 21501 |
<computeroutput>bozo</computeroutput></screen> |
| 21502 |
|
| 21503 |
<para>However . . .</para> |
| 21504 |
|
| 21505 |
<para> |
| 21506 |
<screen><prompt>bash$ </prompt><userinput>su</userinput> |
| 21507 |
<computeroutput>Password: ......</computeroutput> |
| 21508 |
|
| 21509 |
<prompt>bash# </prompt><userinput>whoami</userinput> |
| 21510 |
<computeroutput>root</computeroutput> |
| 21511 |
<prompt>bash# </prompt><userinput>logname</userinput> |
| 21512 |
<computeroutput>bozo</computeroutput></screen> |
| 21513 |
</para> |
| 21514 |
|
| 21515 |
|
| 21516 |
<note><para>While <command>logname</command> prints the name |
| 21517 |
of the logged in user, <command>whoami</command> gives the |
| 21518 |
name of the user attached to the current process. As we have |
| 21519 |
just seen, sometimes these are not the same.</para></note> |
| 21520 |
|
| 21521 |
</listitem> |
| 21522 |
</varlistentry> |
| 21523 |
|
| 21524 |
<varlistentry> |
| 21525 |
<term><anchor id="suref"><command>su</command></term> |
| 21526 |
<indexterm> |
| 21527 |
<primary>su</primary> |
| 21528 |
</indexterm> |
| 21529 |
<indexterm> |
| 21530 |
<primary>command</primary> |
| 21531 |
<secondary>su</secondary> |
| 21532 |
</indexterm> |
| 21533 |
<listitem> |
| 21534 |
<para>Runs a program or script as a |
| 21535 |
<command>s</command>ubstitute <command>u</command>ser. |
| 21536 |
<command>su rjones</command> starts a shell as user |
| 21537 |
<emphasis>rjones</emphasis>. A naked <command>su</command> |
| 21538 |
defaults to <firstterm>root</firstterm>. See <xref |
| 21539 |
linkend="fifo">.</para> |
| 21540 |
</listitem> |
| 21541 |
</varlistentry> |
| 21542 |
|
| 21543 |
<varlistentry> |
| 21544 |
<term><anchor id="sudoref"><command>sudo</command></term> |
| 21545 |
<indexterm> |
| 21546 |
<primary>sudo</primary> |
| 21547 |
</indexterm> |
| 21548 |
<indexterm> |
| 21549 |
<primary>command</primary> |
| 21550 |
<secondary>sudo</secondary> |
| 21551 |
</indexterm> |
| 21552 |
<listitem> |
| 21553 |
<para>Runs a command as <firstterm>root</firstterm> (or |
| 21554 |
another user). This may be used in a script, thus permitting |
| 21555 |
a <firstterm>regular user</firstterm> to run the script.</para> |
| 21556 |
|
| 21557 |
<para><programlisting>#!/bin/bash |
| 21558 |
|
| 21559 |
# Some commands. |
| 21560 |
sudo cp /root/secretfile /home/bozo/secret |
| 21561 |
# Some more commands.</programlisting></para> |
| 21562 |
|
| 21563 |
<para>The file <filename>/etc/sudoers</filename> holds |
| 21564 |
the names of users permitted to invoke |
| 21565 |
<command>sudo</command>.</para> |
| 21566 |
</listitem> |
| 21567 |
</varlistentry> |
| 21568 |
|
| 21569 |
<varlistentry> |
| 21570 |
<term><anchor id="passwdref"><command>passwd</command></term> |
| 21571 |
<indexterm> |
| 21572 |
<primary>passwd</primary> |
| 21573 |
</indexterm> |
| 21574 |
<indexterm> |
| 21575 |
<primary>command</primary> |
| 21576 |
<secondary>password</secondary> |
| 21577 |
</indexterm> |
| 21578 |
<listitem> |
| 21579 |
|
| 21580 |
<para>Sets, changes, or manages a user's password.</para> |
| 21581 |
|
| 21582 |
<para>The <command>passwd</command> command can be used in |
| 21583 |
a script, but probably <emphasis>should not</emphasis> be.</para> |
| 21584 |
|
| 21585 |
<example id="setnewpw"> |
| 21586 |
<title>Setting a new password</title> |
| 21587 |
<programlisting>&setnewpw;</programlisting> |
| 21588 |
</example> |
| 21589 |
|
| 21590 |
<para>The <command>passwd</command> command's <option>-l</option>, |
| 21591 |
<option>-u</option>, and <option>-d</option> options permit |
| 21592 |
locking, unlocking, and deleting a user's password. Only |
| 21593 |
<firstterm>root</firstterm> may use these options.</para> |
| 21594 |
|
| 21595 |
|
| 21596 |
</listitem> |
| 21597 |
</varlistentry> |
| 21598 |
|
| 21599 |
<varlistentry> |
| 21600 |
<term><anchor id="acref"><command>ac</command></term> |
| 21601 |
<indexterm> |
| 21602 |
<primary>ac</primary> |
| 21603 |
</indexterm> |
| 21604 |
<indexterm> |
| 21605 |
<primary>command</primary> |
| 21606 |
<secondary>accounting</secondary> |
| 21607 |
</indexterm> |
| 21608 |
<listitem> |
| 21609 |
<para>Show users' logged in time, as read from |
| 21610 |
<filename>/var/log/wtmp</filename>. This is one of the GNU |
| 21611 |
accounting utilities.</para> |
| 21612 |
<screen><prompt>bash$ </prompt><userinput>ac</userinput> |
| 21613 |
<computeroutput> total 68.08</computeroutput></screen> |
| 21614 |
</listitem> |
| 21615 |
</varlistentry> |
| 21616 |
|
| 21617 |
<varlistentry> |
| 21618 |
<term><anchor id="lastref"><command>last</command></term> |
| 21619 |
<indexterm> |
| 21620 |
<primary>last</primary> |
| 21621 |
</indexterm> |
| 21622 |
<indexterm> |
| 21623 |
<primary>command</primary> |
| 21624 |
<secondary>logged in</secondary> |
| 21625 |
</indexterm> |
| 21626 |
<listitem> |
| 21627 |
|
| 21628 |
<para>List <emphasis>last</emphasis> logged in users, as read from |
| 21629 |
<filename>/var/log/wtmp</filename>. This command can also |
| 21630 |
show remote logins.</para> |
| 21631 |
|
| 21632 |
<para>For example, to show the last few times the system |
| 21633 |
rebooted:</para> |
| 21634 |
|
| 21635 |
<screen><prompt>bash$ </prompt><userinput>last reboot</userinput> |
| 21636 |
<computeroutput>reboot system boot 2.6.9-1.667 Fri Feb 4 18:18 (00:02) |
| 21637 |
reboot system boot 2.6.9-1.667 Fri Feb 4 15:20 (01:27) |
| 21638 |
reboot system boot 2.6.9-1.667 Fri Feb 4 12:56 (00:49) |
| 21639 |
reboot system boot 2.6.9-1.667 Thu Feb 3 21:08 (02:17) |
| 21640 |
. . . |
| 21641 |
|
| 21642 |
wtmp begins Tue Feb 1 12:50:09 2005</computeroutput></screen> |
| 21643 |
|
| 21644 |
</listitem> |
| 21645 |
</varlistentry> |
| 21646 |
|
| 21647 |
<varlistentry> |
| 21648 |
<term><anchor id="newgrpref"><command>newgrp</command></term> |
| 21649 |
<indexterm> |
| 21650 |
<primary>newgrp</primary> |
| 21651 |
</indexterm> |
| 21652 |
<indexterm> |
| 21653 |
<primary>command</primary> |
| 21654 |
<secondary>group</secondary> |
| 21655 |
</indexterm> |
| 21656 |
<listitem> |
| 21657 |
<para>Change user's <firstterm>group ID</firstterm> without |
| 21658 |
logging out. This permits access to the new group's |
| 21659 |
files. Since users may be members of multiple groups |
| 21660 |
simultaneously, this command finds only limited use.</para> |
| 21661 |
|
| 21662 |
<note><para>Kurt Glaesemann points out that the |
| 21663 |
<firstterm>newgrp</firstterm> command could prove helpful |
| 21664 |
in setting the default group permissions for files a user |
| 21665 |
writes. However, the <link linkend="chgrpref">chgrp</link> |
| 21666 |
command might be more convenient for this purpose.</para></note> |
| 21667 |
|
| 21668 |
</listitem> |
| 21669 |
</varlistentry> |
| 21670 |
|
| 21671 |
</variablelist> |
| 21672 |
|
| 21673 |
|
| 21674 |
<variablelist id="terminalssys"> |
| 21675 |
<title><anchor id="terminalssys1">Terminals</title> |
| 21676 |
|
| 21677 |
<varlistentry> |
| 21678 |
<term><anchor id="ttyref"><command>tty</command></term> |
| 21679 |
<indexterm> |
| 21680 |
<primary>tty</primary> |
| 21681 |
</indexterm> |
| 21682 |
<indexterm> |
| 21683 |
<primary>command</primary> |
| 21684 |
<secondary>tty</secondary> |
| 21685 |
</indexterm> |
| 21686 |
<listitem> |
| 21687 |
<para>Echoes the name (filename) of the current user's terminal. |
| 21688 |
Note that each separate <firstterm>xterm</firstterm> |
| 21689 |
window counts as a different terminal.</para> |
| 21690 |
<screen><prompt>bash$ </prompt><userinput>tty</userinput> |
| 21691 |
<computeroutput>/dev/pts/1</computeroutput></screen> |
| 21692 |
|
| 21693 |
</listitem> |
| 21694 |
</varlistentry> |
| 21695 |
|
| 21696 |
<varlistentry> |
| 21697 |
<term><anchor id="sttyref"><command>stty</command></term> |
| 21698 |
<indexterm> |
| 21699 |
<primary>stty</primary> |
| 21700 |
</indexterm> |
| 21701 |
<indexterm> |
| 21702 |
<primary>command</primary> |
| 21703 |
<secondary>stty</secondary> |
| 21704 |
</indexterm> |
| 21705 |
<listitem> |
| 21706 |
<para>Shows and/or changes terminal settings. This complex |
| 21707 |
command, used in a script, can control terminal behavior |
| 21708 |
and the way output displays. See the info page, and study |
| 21709 |
it carefully.</para> |
| 21710 |
|
| 21711 |
<example id="erase"> |
| 21712 |
<title>Setting an <firstterm>erase</firstterm> character</title> |
| 21713 |
<programlisting>&erase;</programlisting> |
| 21714 |
</example> |
| 21715 |
|
| 21716 |
<example id="secretpw"> |
| 21717 |
<title><firstterm>secret password</firstterm>: |
| 21718 |
Turning off terminal echoing </title> |
| 21719 |
<programlisting>&secretpw;</programlisting> |
| 21720 |
</example> |
| 21721 |
|
| 21722 |
<para>A creative use of <command>stty</command> is detecting a |
| 21723 |
user keypress (without hitting |
| 21724 |
<keycap>ENTER</keycap>).</para> |
| 21725 |
|
| 21726 |
<example id="keypress"> |
| 21727 |
<title>Keypress detection</title> |
| 21728 |
<programlisting>&keypress;</programlisting> |
| 21729 |
</example> |
| 21730 |
|
| 21731 |
<para>Also see <xref linkend="timeout"> and <xref |
| 21732 |
linkend="stopwatch">.</para> |
| 21733 |
|
| 21734 |
|
| 21735 |
<para><anchor id="terminalsref"></para> |
| 21736 |
<sidebar><title>terminals and modes</title> |
| 21737 |
|
| 21738 |
<para>Normally, a terminal works in the |
| 21739 |
<firstterm>canonical</firstterm> mode. When a user hits a |
| 21740 |
key, the resulting character does not immediately go to |
| 21741 |
the program actually running in this terminal. A buffer |
| 21742 |
local to the terminal stores keystrokes. When the user |
| 21743 |
hits the <keycap>ENTER</keycap> key, this sends all the |
| 21744 |
stored keystrokes to the program running. There is even |
| 21745 |
a basic line editor inside the terminal.</para> |
| 21746 |
|
| 21747 |
<para> |
| 21748 |
<screen><prompt>bash$ </prompt><userinput>stty -a</userinput> |
| 21749 |
<computeroutput>speed 9600 baud; rows 36; columns 96; line = 0; |
| 21750 |
intr = ^C; quit = ^\; erase = ^H; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; |
| 21751 |
start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; |
| 21752 |
... |
| 21753 |
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt</computeroutput> |
| 21754 |
</screen> |
| 21755 |
</para> |
| 21756 |
|
| 21757 |
<para>Using canonical mode, it is possible to redefine the |
| 21758 |
special keys for the local terminal line editor. |
| 21759 |
|
| 21760 |
<screen> |
| 21761 |
<prompt>bash$ </prompt><userinput>cat > filexxx</userinput> |
| 21762 |
<userinput>wha<ctl-W>I<ctl-H>foo bar<ctl-U>hello world<ENTER></userinput> |
| 21763 |
<userinput><ctl-D></userinput> |
| 21764 |
<prompt>bash$ </prompt><userinput>cat filexxx</userinput> |
| 21765 |
<computeroutput>hello world</computeroutput> |
| 21766 |
<prompt>bash$ </prompt><userinput>wc -c < filexxx</userinput> |
| 21767 |
<computeroutput>12</computeroutput> |
| 21768 |
</screen> |
| 21769 |
|
| 21770 |
The process controlling the terminal receives only 12 |
| 21771 |
characters (11 alphabetic ones, plus a newline), although |
| 21772 |
the user hit 26 keys. |
| 21773 |
</para> |
| 21774 |
|
| 21775 |
<para>In non-canonical (<quote>raw</quote>) mode, every |
| 21776 |
key hit (including special editing keys such as |
| 21777 |
<keycap>ctl-H</keycap>) sends a character immediately to |
| 21778 |
the controlling process.</para> |
| 21779 |
|
| 21780 |
|
| 21781 |
<para>The Bash prompt disables both <option>icanon</option> |
| 21782 |
and <option>echo</option>, since it replaces the basic |
| 21783 |
terminal line editor with its own more elaborate one. For |
| 21784 |
example, when you hit <keycap>ctl-A</keycap> at the Bash |
| 21785 |
prompt, there's no <keycap>^A</keycap> echoed by the |
| 21786 |
terminal, but Bash gets a <keycap>\1</keycap> character, |
| 21787 |
interprets it, and moves the cursor to the begining of |
| 21788 |
the line.</para> |
| 21789 |
|
| 21790 |
<para><emphasis>Stéphane Chazelas</emphasis></para> |
| 21791 |
|
| 21792 |
</sidebar> |
| 21793 |
|
| 21794 |
|
| 21795 |
</listitem> |
| 21796 |
</varlistentry> |
| 21797 |
|
| 21798 |
<varlistentry> |
| 21799 |
<term><anchor id="settermref"><command>setterm</command></term> |
| 21800 |
<indexterm> |
| 21801 |
<primary>setterm</primary> |
| 21802 |
</indexterm> |
| 21803 |
<indexterm> |
| 21804 |
<primary>command</primary> |
| 21805 |
<secondary>terminal</secondary> |
| 21806 |
</indexterm> |
| 21807 |
<listitem> |
| 21808 |
|
| 21809 |
<para>Set certain terminal attributes. This command writes |
| 21810 |
to its terminal's <filename>stdout</filename> a string that |
| 21811 |
changes the behavior of that terminal.</para> |
| 21812 |
|
| 21813 |
<para> |
| 21814 |
<screen><prompt>bash$ </prompt><userinput>setterm -cursor off</userinput> |
| 21815 |
<computeroutput>bash$</computeroutput> |
| 21816 |
</screen> |
| 21817 |
</para> |
| 21818 |
|
| 21819 |
<para>The <command>setterm</command> command can be used within a |
| 21820 |
script to change the appearance of text written to |
| 21821 |
<filename>stdout</filename>, although there are certainly |
| 21822 |
<link linkend="colorizingref">better tools</link> available |
| 21823 |
for this purpose.</para> |
| 21824 |
|
| 21825 |
<para><programlisting>setterm -bold on |
| 21826 |
echo bold hello |
| 21827 |
|
| 21828 |
setterm -bold off |
| 21829 |
echo normal hello</programlisting></para> |
| 21830 |
|
| 21831 |
</listitem> |
| 21832 |
</varlistentry> |
| 21833 |
|
| 21834 |
<varlistentry> |
| 21835 |
<term><anchor id="tsetref"><command>tset</command></term> |
| 21836 |
<indexterm> |
| 21837 |
<primary>tset</primary> |
| 21838 |
</indexterm> |
| 21839 |
<indexterm> |
| 21840 |
<primary>command</primary> |
| 21841 |
<secondary>tset</secondary> |
| 21842 |
</indexterm> |
| 21843 |
<listitem> |
| 21844 |
<para>Show or initialize terminal settings. |
| 21845 |
This is a less capable version of |
| 21846 |
<command>stty</command>.</para> |
| 21847 |
|
| 21848 |
<para> |
| 21849 |
<screen><prompt>bash$ </prompt><userinput>tset -r</userinput> |
| 21850 |
<computeroutput>Terminal type is xterm-xfree86. |
| 21851 |
Kill is control-U (^U). |
| 21852 |
Interrupt is control-C (^C).</computeroutput> |
| 21853 |
</screen> |
| 21854 |
</para> |
| 21855 |
|
| 21856 |
</listitem> |
| 21857 |
</varlistentry> |
| 21858 |
|
| 21859 |
<varlistentry> |
| 21860 |
<term><anchor id="setserialref"><command>setserial</command></term> |
| 21861 |
<indexterm> |
| 21862 |
<primary>setserial</primary> |
| 21863 |
</indexterm> |
| 21864 |
<indexterm> |
| 21865 |
<primary>command</primary> |
| 21866 |
<secondary>serial</secondary> |
| 21867 |
</indexterm> |
| 21868 |
<listitem> |
| 21869 |
|
| 21870 |
<para>Set or display serial port parameters. This command must be |
| 21871 |
run by <firstterm>root</firstterm> and is usually found in a |
| 21872 |
system setup script.</para> |
| 21873 |
|
| 21874 |
<para><programlisting># From /etc/pcmcia/serial script: |
| 21875 |
|
| 21876 |
IRQ=`setserial /dev/$DEVICE | sed -e 's/.*IRQ: //'` |
| 21877 |
setserial /dev/$DEVICE irq 0 ; setserial /dev/$DEVICE irq $IRQ</programlisting></para> |
| 21878 |
|
| 21879 |
</listitem> |
| 21880 |
</varlistentry> |
| 21881 |
|
| 21882 |
<varlistentry> |
| 21883 |
<term><anchor id="gettyref"><command>getty</command></term> |
| 21884 |
<term><anchor id="agettyref"><command>agetty</command></term> |
| 21885 |
<indexterm> |
| 21886 |
<primary>getty</primary> |
| 21887 |
</indexterm> |
| 21888 |
<indexterm> |
| 21889 |
<primary>command</primary> |
| 21890 |
<secondary>getty</secondary> |
| 21891 |
</indexterm> |
| 21892 |
<indexterm> |
| 21893 |
<primary>agetty</primary> |
| 21894 |
</indexterm> |
| 21895 |
<indexterm> |
| 21896 |
<primary>command</primary> |
| 21897 |
<secondary>agetty</secondary> |
| 21898 |
</indexterm> |
| 21899 |
<listitem> |
| 21900 |
<para>The initialization process for a terminal uses |
| 21901 |
<command>getty</command> or <command>agetty</command> |
| 21902 |
to set it up for login by a user. These commands are not |
| 21903 |
used within user shell scripts. Their scripting counterpart |
| 21904 |
is <command>stty</command>.</para> |
| 21905 |
</listitem> |
| 21906 |
</varlistentry> |
| 21907 |
|
| 21908 |
<varlistentry> |
| 21909 |
<term><anchor id="mesgref"><command>mesg</command></term> |
| 21910 |
<indexterm> |
| 21911 |
<primary>mesg</primary> |
| 21912 |
</indexterm> |
| 21913 |
<indexterm> |
| 21914 |
<primary>command</primary> |
| 21915 |
<secondary>mesg</secondary> |
| 21916 |
</indexterm> |
| 21917 |
<listitem> |
| 21918 |
<para>Enables or disables write access to the current user's |
| 21919 |
terminal. Disabling access would prevent another user |
| 21920 |
on the network to <link linkend="writeref">write</link> |
| 21921 |
to the terminal.</para> |
| 21922 |
|
| 21923 |
<tip><para>It can be quite annoying to have a message |
| 21924 |
about ordering pizza suddenly appear in the middle of |
| 21925 |
the text file you are editing. On a multi-user network, |
| 21926 |
you might therefore wish to disable write access to your |
| 21927 |
terminal when you need to avoid interruptions.</para></tip> |
| 21928 |
|
| 21929 |
</listitem> |
| 21930 |
</varlistentry> |
| 21931 |
|
| 21932 |
<varlistentry> |
| 21933 |
<term><anchor id="wallref"><command>wall</command></term> |
| 21934 |
<indexterm> |
| 21935 |
<primary>wall</primary> |
| 21936 |
</indexterm> |
| 21937 |
<indexterm> |
| 21938 |
<primary>command</primary> |
| 21939 |
<secondary>wall</secondary> |
| 21940 |
</indexterm> |
| 21941 |
<listitem> |
| 21942 |
<para>This is an acronym for <quote><link |
| 21943 |
linkend="writeref">write</link> all,</quote> i.e., sending |
| 21944 |
a message to all users at every terminal logged into the |
| 21945 |
network. It is primarily a system administrator's tool, |
| 21946 |
useful, for example, when warning everyone that the |
| 21947 |
system will shortly go down due to a problem (see <xref |
| 21948 |
linkend="ex70">).</para> |
| 21949 |
|
| 21950 |
<para> |
| 21951 |
<screen><prompt>bash$ </prompt><userinput>wall System going down for maintenance in 5 minutes!</userinput> |
| 21952 |
<computeroutput>Broadcast message from bozo (pts/1) Sun Jul 8 13:53:27 2001... |
| 21953 |
|
| 21954 |
System going down for maintenance in 5 minutes!</computeroutput> |
| 21955 |
</screen> |
| 21956 |
</para> |
| 21957 |
|
| 21958 |
<note><para>If write access to a particular terminal has been |
| 21959 |
disabled with <command>mesg</command>, then |
| 21960 |
<command>wall</command> cannot send a message to |
| 21961 |
that terminal.</para></note> |
| 21962 |
|
| 21963 |
</listitem> |
| 21964 |
</varlistentry> |
| 21965 |
|
| 21966 |
</variablelist> |
| 21967 |
|
| 21968 |
|
| 21969 |
<variablelist id="statisticssys"> |
| 21970 |
<title><anchor id="statisticssys1">Information and Statistics</title> |
| 21971 |
|
| 21972 |
<varlistentry> |
| 21973 |
<term><anchor id="unameref"><command>uname</command></term> |
| 21974 |
<indexterm> |
| 21975 |
<primary>uname</primary> |
| 21976 |
</indexterm> |
| 21977 |
<indexterm> |
| 21978 |
<primary>command</primary> |
| 21979 |
<secondary>uname</secondary> |
| 21980 |
</indexterm> |
| 21981 |
<listitem> |
| 21982 |
<para>Output system specifications (OS, kernel version, |
| 21983 |
etc.) to <filename>stdout</filename>. Invoked with the |
| 21984 |
<option>-a</option> option, gives verbose system info |
| 21985 |
(see <xref linkend="ex41">). The <option>-s</option> |
| 21986 |
option shows only the OS type.</para> |
| 21987 |
|
| 21988 |
<screen><prompt>bash$ </prompt><userinput>uname</userinput> |
| 21989 |
<computeroutput>Linux</computeroutput> |
| 21990 |
|
| 21991 |
<prompt>bash$ </prompt><userinput>uname -s</userinput> |
| 21992 |
<computeroutput>Linux</computeroutput> |
| 21993 |
|
| 21994 |
|
| 21995 |
<prompt>bash$ </prompt><userinput>uname -a</userinput> |
| 21996 |
<computeroutput>Linux iron.bozo 2.6.15-1.2054_FC5 #1 Tue Mar 14 15:48:33 EST 2006 |
| 21997 |
i686 i686 i386 GNU/Linux</computeroutput></screen> |
| 21998 |
|
| 21999 |
</listitem> |
| 22000 |
</varlistentry> |
| 22001 |
|
| 22002 |
<varlistentry> |
| 22003 |
<term><anchor id="archref"><command>arch</command></term> |
| 22004 |
<indexterm> |
| 22005 |
<primary>arch</primary> |
| 22006 |
</indexterm> |
| 22007 |
<indexterm> |
| 22008 |
<primary>command</primary> |
| 22009 |
<secondary>arch</secondary> |
| 22010 |
</indexterm> |
| 22011 |
<listitem> |
| 22012 |
<para>Show system architecture. |
| 22013 |
Equivalent to <command>uname -m</command>. See <xref |
| 22014 |
linkend="casecmd">.</para> |
| 22015 |
<screen><prompt>bash$ </prompt><userinput>arch</userinput> |
| 22016 |
<computeroutput>i686</computeroutput> |
| 22017 |
|
| 22018 |
<prompt>bash$ </prompt><userinput>uname -m</userinput> |
| 22019 |
<computeroutput>i686</computeroutput></screen> |
| 22020 |
</listitem> |
| 22021 |
</varlistentry> |
| 22022 |
|
| 22023 |
<varlistentry> |
| 22024 |
<term><anchor id="lastcommref"><command>lastcomm</command></term> |
| 22025 |
<indexterm> |
| 22026 |
<primary>lastcomm</primary> |
| 22027 |
</indexterm> |
| 22028 |
<indexterm> |
| 22029 |
<primary>command</primary> |
| 22030 |
<secondary>last</secondary> |
| 22031 |
</indexterm> |
| 22032 |
<listitem> |
| 22033 |
<para>Gives information about previous commands, as stored |
| 22034 |
in the <filename>/var/account/pacct</filename> file. Command |
| 22035 |
name and user name can be specified by options. This is |
| 22036 |
one of the GNU accounting utilities.</para> |
| 22037 |
</listitem> |
| 22038 |
</varlistentry> |
| 22039 |
|
| 22040 |
<varlistentry> |
| 22041 |
<term><anchor id="lastlogref"><command>lastlog</command></term> |
| 22042 |
<indexterm> |
| 22043 |
<primary>lastlog</primary> |
| 22044 |
</indexterm> |
| 22045 |
<indexterm> |
| 22046 |
<primary>command</primary> |
| 22047 |
<secondary>last</secondary> |
| 22048 |
</indexterm> |
| 22049 |
<listitem> |
| 22050 |
<para>List the last login time of all system users. This |
| 22051 |
references the <filename>/var/log/lastlog</filename> |
| 22052 |
file.</para> |
| 22053 |
<para> |
| 22054 |
<screen><prompt>bash$ </prompt><userinput>lastlog</userinput> |
| 22055 |
<computeroutput>root tty1 Fri Dec 7 18:43:21 -0700 2001 |
| 22056 |
bin **Never logged in** |
| 22057 |
daemon **Never logged in** |
| 22058 |
... |
| 22059 |
bozo tty1 Sat Dec 8 21:14:29 -0700 2001</computeroutput> |
| 22060 |
|
| 22061 |
|
| 22062 |
|
| 22063 |
<prompt>bash$ </prompt><userinput>lastlog | grep root</userinput> |
| 22064 |
<computeroutput>root tty1 Fri Dec 7 18:43:21 -0700 2001</computeroutput> |
| 22065 |
</screen> |
| 22066 |
</para> |
| 22067 |
|
| 22068 |
<caution><para>This command will fail if the user invoking |
| 22069 |
it does not have read permission for the |
| 22070 |
<filename>/var/log/lastlog</filename> file.</para></caution> |
| 22071 |
</listitem> |
| 22072 |
</varlistentry> |
| 22073 |
|
| 22074 |
<varlistentry> |
| 22075 |
<term><anchor id="lsofref"><command>lsof</command></term> |
| 22076 |
<indexterm> |
| 22077 |
<primary>lsof</primary> |
| 22078 |
</indexterm> |
| 22079 |
<indexterm> |
| 22080 |
<primary>command</primary> |
| 22081 |
<secondary>lsof</secondary> |
| 22082 |
</indexterm> |
| 22083 |
<listitem> |
| 22084 |
<para>List open files. This command outputs a detailed |
| 22085 |
table of all currently open files and gives information |
| 22086 |
about their owner, size, the processes associated with |
| 22087 |
them, and more. Of course, <command>lsof</command> may |
| 22088 |
be piped to <link linkend="grepref">grep</link> and/or |
| 22089 |
<link linkend="awkref">awk</link> to parse and analyze |
| 22090 |
its results.</para> |
| 22091 |
|
| 22092 |
<para> |
| 22093 |
<screen><prompt>bash$ </prompt><userinput>lsof</userinput> |
| 22094 |
<computeroutput>COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME |
| 22095 |
init 1 root mem REG 3,5 30748 30303 /sbin/init |
| 22096 |
init 1 root mem REG 3,5 73120 8069 /lib/ld-2.1.3.so |
| 22097 |
init 1 root mem REG 3,5 931668 8075 /lib/libc-2.1.3.so |
| 22098 |
cardmgr 213 root mem REG 3,5 36956 30357 /sbin/cardmgr |
| 22099 |
...</computeroutput> |
| 22100 |
</screen> |
| 22101 |
</para> |
| 22102 |
|
| 22103 |
<para>The <command>lsof</command> command is a useful, |
| 22104 |
if complex administrative tool. If you are unable to |
| 22105 |
dismount a filesystem and get an error message that it is |
| 22106 |
still in use, then running <firstterm>lsof</firstterm> helps |
| 22107 |
determine which files are still open on that filesystem. The |
| 22108 |
<option>-i</option> option lists open network socket files, |
| 22109 |
and this can help trace intrusion or hack attempts.</para> |
| 22110 |
|
| 22111 |
<para> |
| 22112 |
<screen><prompt>bash$ </prompt><userinput>lsof -an -i tcp</userinput> |
| 22113 |
<computeroutput>COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME |
| 22114 |
firefox 2330 bozo 32u IPv4 9956 TCP 66.0.118.137:57596->67.112.7.104:http ... |
| 22115 |
firefox 2330 bozo 38u IPv4 10535 TCP 66.0.118.137:57708->216.79.48.24:http ...</computeroutput> |
| 22116 |
</screen> |
| 22117 |
</para> |
| 22118 |
|
| 22119 |
<para>See <xref linkend="ipaddresses"> for an effective use |
| 22120 |
of <command>lsof</command>.</para> |
| 22121 |
|
| 22122 |
</listitem> |
| 22123 |
</varlistentry> |
| 22124 |
|
| 22125 |
<varlistentry> |
| 22126 |
<term><anchor id="straceref"><command>strace</command></term> |
| 22127 |
<indexterm> |
| 22128 |
<primary>strace</primary> |
| 22129 |
</indexterm> |
| 22130 |
<indexterm> |
| 22131 |
<primary>command</primary> |
| 22132 |
<secondary>trace</secondary> |
| 22133 |
</indexterm> |
| 22134 |
<listitem> |
| 22135 |
<para><command>S</command>ystem <command>trace</command>: |
| 22136 |
diagnostic and debugging tool for tracing <firstterm>system |
| 22137 |
calls</firstterm> and signals. This command and |
| 22138 |
<command>ltrace</command>, following, are useful for |
| 22139 |
diagnosing why a given program or package fails to |
| 22140 |
run . . . perhaps due to missing libraries or related |
| 22141 |
causes.</para> |
| 22142 |
|
| 22143 |
<para> |
| 22144 |
<screen><prompt>bash$ </prompt><userinput>strace df</userinput> |
| 22145 |
<computeroutput>execve("/bin/df", ["df"], [/* 45 vars */]) = 0 |
| 22146 |
uname({sys="Linux", node="bozo.localdomain", ...}) = 0 |
| 22147 |
brk(0) = 0x804f5e4 |
| 22148 |
|
| 22149 |
...</computeroutput> |
| 22150 |
</screen> |
| 22151 |
</para> |
| 22152 |
|
| 22153 |
<para>This is the Linux equivalent of |
| 22154 |
the Solaris <command>truss</command> command.</para> |
| 22155 |
|
| 22156 |
</listitem> |
| 22157 |
</varlistentry> |
| 22158 |
|
| 22159 |
<varlistentry> |
| 22160 |
<term><anchor id="ltraceref"><command>ltrace</command></term> |
| 22161 |
<indexterm> |
| 22162 |
<primary>ltrace</primary> |
| 22163 |
</indexterm> |
| 22164 |
<indexterm> |
| 22165 |
<primary>command</primary> |
| 22166 |
<secondary>trace</secondary> |
| 22167 |
</indexterm> |
| 22168 |
<listitem> |
| 22169 |
<para><command>L</command>ibrary <command>trace</command>: |
| 22170 |
diagnostic and debugging tool that traces <firstterm>library calls</firstterm> |
| 22171 |
invoked by a given command.</para> |
| 22172 |
|
| 22173 |
<para> |
| 22174 |
<screen><prompt>bash$ </prompt><userinput>ltrace df</userinput> |
| 22175 |
<computeroutput>__libc_start_main(0x804a910, 1, 0xbfb589a4, 0x804fb70, 0x804fb68 <unfinished ...>: |
| 22176 |
setlocale(6, "") = "en_US.UTF-8" |
| 22177 |
bindtextdomain("coreutils", "/usr/share/locale") = "/usr/share/locale" |
| 22178 |
textdomain("coreutils") = "coreutils" |
| 22179 |
__cxa_atexit(0x804b650, 0, 0, 0x8052bf0, 0xbfb58908) = 0 |
| 22180 |
getenv("DF_BLOCK_SIZE") = NULL |
| 22181 |
|
| 22182 |
...</computeroutput> |
| 22183 |
</screen> |
| 22184 |
</para> |
| 22185 |
|
| 22186 |
</listitem> |
| 22187 |
</varlistentry> |
| 22188 |
|
| 22189 |
|
| 22190 |
|
| 22191 |
<varlistentry> |
| 22192 |
<term><anchor id="ncref"><command>nc</command></term> |
| 22193 |
<indexterm> |
| 22194 |
<primary>nc</primary> |
| 22195 |
</indexterm> |
| 22196 |
<indexterm> |
| 22197 |
<primary>command</primary> |
| 22198 |
<secondary>nc</secondary> |
| 22199 |
</indexterm> |
| 22200 |
<listitem> |
| 22201 |
|
| 22202 |
<para>The <command>nc</command> (<firstterm>netcat</firstterm>) |
| 22203 |
utility is a complete toolkit for connecting to and |
| 22204 |
listening to TCP and UDP ports. It is useful as a diagnostic |
| 22205 |
and testing tool and as a component in simple script-based HTTP |
| 22206 |
clients and servers.</para> |
| 22207 |
|
| 22208 |
<para> |
| 22209 |
<screen><prompt>bash$ </prompt><userinput>nc localhost.localdomain 25</userinput> |
| 22210 |
<computeroutput>220 localhost.localdomain ESMTP Sendmail 8.13.1/8.13.1; |
| 22211 |
Thu, 31 Mar 2005 15:41:35 -0700</computeroutput></screen> |
| 22212 |
</para> |
| 22213 |
|
| 22214 |
<para>A real-life <link linkend="netcatexample">usage |
| 22215 |
example</link>.</para> |
| 22216 |
|
| 22217 |
<example id="iscan"> |
| 22218 |
<title>Checking a remote server for |
| 22219 |
<firstterm>identd</firstterm></title> |
| 22220 |
<programlisting>&iscan;</programlisting> |
| 22221 |
</example> |
| 22222 |
|
| 22223 |
<para> |
| 22224 |
And, of course, there's Dr. Andrew Tridgell's notorious |
| 22225 |
one-line script in the BitKeeper Affair: |
| 22226 |
<programlisting>echo clone | nc thunk.org 5000 > e2fsprogs.dat</programlisting> |
| 22227 |
</para> |
| 22228 |
|
| 22229 |
</listitem> |
| 22230 |
</varlistentry> |
| 22231 |
|
| 22232 |
<varlistentry> |
| 22233 |
<term><anchor id="freeref"><command>free</command></term> |
| 22234 |
<indexterm> |
| 22235 |
<primary>free</primary> |
| 22236 |
</indexterm> |
| 22237 |
<indexterm> |
| 22238 |
<primary>command</primary> |
| 22239 |
<secondary>free</secondary> |
| 22240 |
</indexterm> |
| 22241 |
<listitem> |
| 22242 |
<para>Shows memory and cache usage in tabular form. The |
| 22243 |
output of this command lends itself to parsing, using |
| 22244 |
<link linkend="grepref">grep</link>, <link |
| 22245 |
linkend="awkref">awk</link> or <command>Perl</command>. The |
| 22246 |
<command>procinfo</command> command shows all the |
| 22247 |
information that <command>free</command> does, and much |
| 22248 |
more.</para> |
| 22249 |
|
| 22250 |
<screen><prompt>bash$ </prompt><command>free</command> |
| 22251 |
<computeroutput> total used free shared buffers cached |
| 22252 |
Mem: 30504 28624 1880 15820 1608 16376 |
| 22253 |
-/+ buffers/cache: 10640 19864 |
| 22254 |
Swap: 68540 3128 65412</computeroutput></screen> |
| 22255 |
|
| 22256 |
<para>To show unused RAM memory:</para> |
| 22257 |
<screen><prompt>bash$ </prompt><command>free | grep Mem | awk '{ print $4 }'</command> |
| 22258 |
<computeroutput>1880</computeroutput></screen> |
| 22259 |
|
| 22260 |
</listitem> |
| 22261 |
</varlistentry> |
| 22262 |
|
| 22263 |
<varlistentry> |
| 22264 |
<term><anchor id="procinforef"><command>procinfo</command></term> |
| 22265 |
<indexterm> |
| 22266 |
<primary>procinfo</primary> |
| 22267 |
</indexterm> |
| 22268 |
<indexterm> |
| 22269 |
<primary>command</primary> |
| 22270 |
<secondary>procinfo</secondary> |
| 22271 |
</indexterm> |
| 22272 |
<listitem> |
| 22273 |
<para>Extract and list information and statistics from the |
| 22274 |
<link linkend="devprocref"><filename |
| 22275 |
class="directory">/proc</filename> |
| 22276 |
pseudo-filesystem</link>. This gives a very extensive and |
| 22277 |
detailed listing.</para> |
| 22278 |
|
| 22279 |
<screen><prompt>bash$ </prompt><userinput>procinfo | grep Bootup</userinput> |
| 22280 |
<computeroutput>Bootup: Wed Mar 21 15:15:50 2001 Load average: 0.04 0.21 0.34 3/47 6829</computeroutput> |
| 22281 |
</screen> |
| 22282 |
|
| 22283 |
</listitem> |
| 22284 |
</varlistentry> |
| 22285 |
|
| 22286 |
<varlistentry> |
| 22287 |
<term><anchor id="lsdevref"><command>lsdev</command></term> |
| 22288 |
<indexterm> |
| 22289 |
<primary>lsdev</primary> |
| 22290 |
</indexterm> |
| 22291 |
<indexterm> |
| 22292 |
<primary>command</primary> |
| 22293 |
<secondary>device</secondary> |
| 22294 |
</indexterm> |
| 22295 |
<listitem> |
| 22296 |
<para>List devices, that is, show installed hardware.</para> |
| 22297 |
<para> |
| 22298 |
<screen><prompt>bash$ </prompt><userinput>lsdev</userinput> |
| 22299 |
<computeroutput>Device DMA IRQ I/O Ports |
| 22300 |
------------------------------------------------ |
| 22301 |
cascade 4 2 |
| 22302 |
dma 0080-008f |
| 22303 |
dma1 0000-001f |
| 22304 |
dma2 00c0-00df |
| 22305 |
fpu 00f0-00ff |
| 22306 |
ide0 14 01f0-01f7 03f6-03f6 |
| 22307 |
...</computeroutput> |
| 22308 |
</screen> |
| 22309 |
</para> |
| 22310 |
</listitem> |
| 22311 |
</varlistentry> |
| 22312 |
|
| 22313 |
<varlistentry> |
| 22314 |
<term><anchor id="duref"><command>du</command></term> |
| 22315 |
<indexterm> |
| 22316 |
<primary>du</primary> |
| 22317 |
</indexterm> |
| 22318 |
<indexterm> |
| 22319 |
<primary>command</primary> |
| 22320 |
<secondary>du</secondary> |
| 22321 |
</indexterm> |
| 22322 |
<listitem> |
| 22323 |
<para>Show (disk) file usage, recursively. Defaults to current |
| 22324 |
working directory, unless otherwise specified.</para> |
| 22325 |
|
| 22326 |
<screen><prompt>bash$ </prompt><command>du -ach</command> |
| 22327 |
<computeroutput>1.0k ./wi.sh |
| 22328 |
1.0k ./tst.sh |
| 22329 |
1.0k ./random.file |
| 22330 |
6.0k . |
| 22331 |
6.0k total</computeroutput></screen> |
| 22332 |
</listitem> |
| 22333 |
</varlistentry> |
| 22334 |
|
| 22335 |
<varlistentry> |
| 22336 |
<term><anchor id="dfref"><command>df</command></term> |
| 22337 |
<indexterm> |
| 22338 |
<primary>df</primary> |
| 22339 |
</indexterm> |
| 22340 |
<indexterm> |
| 22341 |
<primary>command</primary> |
| 22342 |
<secondary>df</secondary> |
| 22343 |
</indexterm> |
| 22344 |
<listitem> |
| 22345 |
<para>Shows filesystem usage in tabular form.</para> |
| 22346 |
|
| 22347 |
<screen><prompt>bash$ </prompt><command>df</command> |
| 22348 |
<computeroutput>Filesystem 1k-blocks Used Available Use% Mounted on |
| 22349 |
/dev/hda5 273262 92607 166547 36% / |
| 22350 |
/dev/hda8 222525 123951 87085 59% /home |
| 22351 |
/dev/hda7 1408796 1075744 261488 80% /usr</computeroutput></screen> |
| 22352 |
</listitem> |
| 22353 |
</varlistentry> |
| 22354 |
|
| 22355 |
<varlistentry> |
| 22356 |
<term><anchor id="dmesgref"><command>dmesg</command></term> |
| 22357 |
<indexterm> |
| 22358 |
<primary>dmesg</primary> |
| 22359 |
</indexterm> |
| 22360 |
<indexterm> |
| 22361 |
<primary>command</primary> |
| 22362 |
<secondary>dmesg</secondary> |
| 22363 |
</indexterm> |
| 22364 |
<listitem> |
| 22365 |
<para>Lists all system bootup messages to |
| 22366 |
<filename>stdout</filename>. Handy for debugging and |
| 22367 |
ascertaining which device drivers were installed |
| 22368 |
and which system interrupts in use. The output |
| 22369 |
of <command>dmesg</command> may, of course, be |
| 22370 |
parsed with <link linkend="grepref">grep</link>, |
| 22371 |
<link linkend="sedref">sed</link>, or <link |
| 22372 |
linkend="awkref">awk</link> from within a script.</para> |
| 22373 |
<para> |
| 22374 |
<screen><prompt>bash$ </prompt><userinput>dmesg | grep hda</userinput> |
| 22375 |
<computeroutput>Kernel command line: ro root=/dev/hda2 |
| 22376 |
hda: IBM-DLGA-23080, ATA DISK drive |
| 22377 |
hda: 6015744 sectors (3080 MB) w/96KiB Cache, CHS=746/128/63 |
| 22378 |
hda: hda1 hda2 hda3 < hda5 hda6 hda7 > hda4</computeroutput> |
| 22379 |
</screen> |
| 22380 |
</para> |
| 22381 |
</listitem> |
| 22382 |
</varlistentry> |
| 22383 |
|
| 22384 |
<varlistentry> |
| 22385 |
<term><anchor id="statref"><command>stat</command></term> |
| 22386 |
<indexterm> |
| 22387 |
<primary>stat</primary> |
| 22388 |
</indexterm> |
| 22389 |
<indexterm> |
| 22390 |
<primary>command</primary> |
| 22391 |
<secondary>stat</secondary> |
| 22392 |
</indexterm> |
| 22393 |
<listitem> |
| 22394 |
<para>Gives detailed and verbose <emphasis>stat</emphasis>istics |
| 22395 |
on a given file (even a directory or device file) or set |
| 22396 |
of files.</para> |
| 22397 |
<para> |
| 22398 |
<screen><prompt>bash$ </prompt><userinput>stat test.cru</userinput> |
| 22399 |
<computeroutput> File: "test.cru" |
| 22400 |
Size: 49970 Allocated Blocks: 100 Filetype: Regular File |
| 22401 |
Mode: (0664/-rw-rw-r--) Uid: ( 501/ bozo) Gid: ( 501/ bozo) |
| 22402 |
Device: 3,8 Inode: 18185 Links: 1 |
| 22403 |
Access: Sat Jun 2 16:40:24 2001 |
| 22404 |
Modify: Sat Jun 2 16:40:24 2001 |
| 22405 |
Change: Sat Jun 2 16:40:24 2001</computeroutput> |
| 22406 |
</screen> |
| 22407 |
</para> |
| 22408 |
|
| 22409 |
<para>If the target file does not exist, <command>stat</command> |
| 22410 |
returns an error message.</para> |
| 22411 |
|
| 22412 |
<para> |
| 22413 |
<screen><prompt>bash$ </prompt><userinput>stat nonexistent-file</userinput> |
| 22414 |
<computeroutput>nonexistent-file: No such file or directory</computeroutput> |
| 22415 |
</screen> |
| 22416 |
</para> |
| 22417 |
|
| 22418 |
<para>In a script, you can use <command>stat</command> to extract |
| 22419 |
information about files (and filesystems) and set variables |
| 22420 |
accordingly.</para> |
| 22421 |
|
| 22422 |
<para> |
| 22423 |
<programlisting>#!/bin/bash |
| 22424 |
# fileinfo2.sh |
| 22425 |
|
| 22426 |
# Per suggestion of Joël Bourquard and . . . |
| 22427 |
# http://www.linuxquestions.org/questions/showthread.php?t=410766 |
| 22428 |
|
| 22429 |
|
| 22430 |
FILENAME=testfile.txt |
| 22431 |
file_name=$(stat -c%n "$FILENAME") # Same as "$FILENAME" of course. |
| 22432 |
file_owner=$(stat -c%U "$FILENAME") |
| 22433 |
file_size=$(stat -c%s "$FILENAME") |
| 22434 |
# Certainly easier than using "ls -l $FILENAME" |
| 22435 |
#+ and then parsing with sed. |
| 22436 |
file_inode=$(stat -c%i "$FILENAME") |
| 22437 |
file_type=$(stat -c%F "$FILENAME") |
| 22438 |
file_access_rights=$(stat -c%A "$FILENAME") |
| 22439 |
|
| 22440 |
echo "File name: $file_name" |
| 22441 |
echo "File owner: $file_owner" |
| 22442 |
echo "File size: $file_size" |
| 22443 |
echo "File inode: $file_inode" |
| 22444 |
echo "File type: $file_type" |
| 22445 |
echo "File access rights: $file_access_rights" |
| 22446 |
|
| 22447 |
exit 0 |
| 22448 |
|
| 22449 |
sh fileinfo2.sh |
| 22450 |
|
| 22451 |
File name: testfile.txt |
| 22452 |
File owner: bozo |
| 22453 |
File size: 418 |
| 22454 |
File inode: 1730378 |
| 22455 |
File type: regular file |
| 22456 |
File access rights: -rw-rw-r--</programlisting> |
| 22457 |
</para> |
| 22458 |
|
| 22459 |
</listitem> |
| 22460 |
</varlistentry> |
| 22461 |
|
| 22462 |
<varlistentry> |
| 22463 |
<term><anchor id="vmstatref"><command>vmstat</command></term> |
| 22464 |
<indexterm> |
| 22465 |
<primary>vmstat</primary> |
| 22466 |
</indexterm> |
| 22467 |
<indexterm> |
| 22468 |
<primary>command</primary> |
| 22469 |
<secondary>virtual memory</secondary> |
| 22470 |
</indexterm> |
| 22471 |
<listitem> |
| 22472 |
<para>Display virtual memory statistics.</para> |
| 22473 |
<para> |
| 22474 |
<screen><prompt>bash$ </prompt><userinput>vmstat</userinput> |
| 22475 |
<computeroutput> procs memory swap io system cpu |
| 22476 |
r b w swpd free buff cache si so bi bo in cs us sy id |
| 22477 |
0 0 0 0 11040 2636 38952 0 0 33 7 271 88 8 3 89</computeroutput> |
| 22478 |
</screen> |
| 22479 |
</para> |
| 22480 |
|
| 22481 |
</listitem> |
| 22482 |
</varlistentry> |
| 22483 |
|
| 22484 |
|
| 22485 |
<varlistentry> |
| 22486 |
<term><anchor id="uptimeref"><command>uptime</command></term> |
| 22487 |
<indexterm> |
| 22488 |
<primary>uptime</primary> |
| 22489 |
</indexterm> |
| 22490 |
<indexterm> |
| 22491 |
<primary>command</primary> |
| 22492 |
<secondary>uptime</secondary> |
| 22493 |
</indexterm> |
| 22494 |
<listitem> |
| 22495 |
|
| 22496 |
<para>Shows how long the system has been running, along with |
| 22497 |
associated statistics.</para> |
| 22498 |
|
| 22499 |
<para> |
| 22500 |
<screen><prompt>bash$ </prompt><userinput>uptime</userinput> |
| 22501 |
<computeroutput>10:28pm up 1:57, 3 users, load average: 0.17, 0.34, 0.27</computeroutput></screen> |
| 22502 |
</para> |
| 22503 |
|
| 22504 |
<note><para>A <firstterm>load average</firstterm> of 1 or less |
| 22505 |
indicates that the system handles processes immediately. A load |
| 22506 |
average greater than 1 means that processes are being queued. When |
| 22507 |
the load average gets above 3 (on a single-core processor), |
| 22508 |
then system performance is significantly degraded.</para></note> |
| 22509 |
|
| 22510 |
</listitem> |
| 22511 |
</varlistentry> |
| 22512 |
|
| 22513 |
<varlistentry> |
| 22514 |
<term><anchor id="hnameref"><command>hostname</command></term> |
| 22515 |
<indexterm> |
| 22516 |
<primary>hostname</primary> |
| 22517 |
</indexterm> |
| 22518 |
<indexterm> |
| 22519 |
<primary>command</primary> |
| 22520 |
<secondary>hostname</secondary> |
| 22521 |
</indexterm> |
| 22522 |
<listitem> |
| 22523 |
|
| 22524 |
<para>Lists the system's host name. This command sets the host |
| 22525 |
name in an <filename class="directory">/etc/rc.d</filename> |
| 22526 |
setup script (<filename>/etc/rc.d/rc.sysinit</filename> |
| 22527 |
or similar). It is equivalent to <command>uname |
| 22528 |
-n</command>, and a counterpart to the <link |
| 22529 |
linkend="hostnameref">$HOSTNAME</link> internal |
| 22530 |
variable.</para> |
| 22531 |
|
| 22532 |
<screen><prompt>bash$ </prompt><userinput>hostname</userinput> |
| 22533 |
<computeroutput>localhost.localdomain</computeroutput> |
| 22534 |
|
| 22535 |
<prompt>bash$ </prompt><userinput>echo $HOSTNAME</userinput> |
| 22536 |
<computeroutput>localhost.localdomain</computeroutput></screen> |
| 22537 |
|
| 22538 |
<para>Similar to the <command>hostname</command> command are the |
| 22539 |
<command>domainname</command>, |
| 22540 |
<command>dnsdomainname</command>, |
| 22541 |
<command>nisdomainname</command>, and |
| 22542 |
<command>ypdomainname</command> commands. Use these to |
| 22543 |
display or set the system DNS or NIS/YP domain name. Various |
| 22544 |
options to <command>hostname</command> also perform these |
| 22545 |
functions.</para> |
| 22546 |
|
| 22547 |
|
| 22548 |
</listitem> |
| 22549 |
</varlistentry> |
| 22550 |
|
| 22551 |
<varlistentry> |
| 22552 |
<term><anchor id="hostidref"><command>hostid</command></term> |
| 22553 |
<indexterm> |
| 22554 |
<primary>hostid</primary> |
| 22555 |
</indexterm> |
| 22556 |
<indexterm> |
| 22557 |
<primary>command</primary> |
| 22558 |
<secondary>host id</secondary> |
| 22559 |
</indexterm> |
| 22560 |
<listitem> |
| 22561 |
|
| 22562 |
<para>Echo a 32-bit hexadecimal numerical identifier for the |
| 22563 |
host machine.</para> |
| 22564 |
|
| 22565 |
<para> |
| 22566 |
<screen><prompt>bash$ </prompt><userinput>hostid</userinput> |
| 22567 |
<computeroutput>7f0100</computeroutput></screen> |
| 22568 |
</para> |
| 22569 |
|
| 22570 |
<note> |
| 22571 |
<para>This command allegedly fetches a <quote>unique</quote> |
| 22572 |
serial number for a particular system. Certain |
| 22573 |
product registration procedures use this number |
| 22574 |
to brand a particular user license. Unfortunately, |
| 22575 |
<command>hostid</command> only returns the machine |
| 22576 |
network address in hexadecimal, with pairs of bytes |
| 22577 |
transposed.</para> |
| 22578 |
|
| 22579 |
<para>The network address of a typical non-networked Linux |
| 22580 |
machine, is found in <filename>/etc/hosts</filename>.</para> |
| 22581 |
|
| 22582 |
<para> |
| 22583 |
<screen><prompt>bash$ </prompt><userinput>cat /etc/hosts</userinput> |
| 22584 |
<computeroutput>127.0.0.1 localhost.localdomain localhost</computeroutput> |
| 22585 |
</screen> |
| 22586 |
</para> |
| 22587 |
|
| 22588 |
<para>As it happens, transposing the bytes of |
| 22589 |
<userinput>127.0.0.1</userinput>, we get |
| 22590 |
<userinput>0.127.1.0</userinput>, which translates in |
| 22591 |
hex to <userinput>007f0100</userinput>, the exact equivalent |
| 22592 |
of what <command>hostid</command> returns, above. There |
| 22593 |
exist only a few million other Linux machines with this |
| 22594 |
identical <firstterm>hostid</firstterm>.</para> |
| 22595 |
</note> |
| 22596 |
|
| 22597 |
</listitem> |
| 22598 |
</varlistentry> |
| 22599 |
|
| 22600 |
<varlistentry> |
| 22601 |
<term><anchor id="sarref"><command>sar</command></term> |
| 22602 |
<indexterm> |
| 22603 |
<primary>sar</primary> |
| 22604 |
</indexterm> |
| 22605 |
<indexterm> |
| 22606 |
<primary>command</primary> |
| 22607 |
<secondary>system activity report</secondary> |
| 22608 |
</indexterm> |
| 22609 |
<listitem> |
| 22610 |
<para>Invoking <command>sar</command> (System Activity Reporter) |
| 22611 |
gives a very detailed rundown on system statistics. The |
| 22612 |
Santa Cruz Operation (<quote>Old</quote> SCO) released |
| 22613 |
<command>sar</command> as Open Source in June, 1999.</para> |
| 22614 |
|
| 22615 |
<para>This command is not part of the base Linux distribution, |
| 22616 |
but may be obtained as part of the<ulink |
| 22617 |
url="http://perso.wanadoo.fr/sebastien.godard/"> |
| 22618 |
sysstat utilities</ulink> package, written by <ulink |
| 22619 |
url="mailto:[email protected]">Sebastien |
| 22620 |
Godard</ulink>.</para> |
| 22621 |
|
| 22622 |
<screen> |
| 22623 |
<prompt>bash$ </prompt><userinput>sar</userinput> |
| 22624 |
<computeroutput>Linux 2.4.9 (brooks.seringas.fr) 09/26/03 |
| 22625 |
|
| 22626 |
10:30:00 CPU %user %nice %system %iowait %idle |
| 22627 |
10:40:00 all 2.21 10.90 65.48 0.00 21.41 |
| 22628 |
10:50:00 all 3.36 0.00 72.36 0.00 24.28 |
| 22629 |
11:00:00 all 1.12 0.00 80.77 0.00 18.11 |
| 22630 |
Average: all 2.23 3.63 72.87 0.00 21.27 |
| 22631 |
|
| 22632 |
14:32:30 LINUX RESTART |
| 22633 |
|
| 22634 |
15:00:00 CPU %user %nice %system %iowait %idle |
| 22635 |
15:10:00 all 8.59 2.40 17.47 0.00 71.54 |
| 22636 |
15:20:00 all 4.07 1.00 11.95 0.00 82.98 |
| 22637 |
15:30:00 all 0.79 2.94 7.56 0.00 88.71 |
| 22638 |
Average: all 6.33 1.70 14.71 0.00 77.26</computeroutput> |
| 22639 |
</screen> |
| 22640 |
</listitem> |
| 22641 |
</varlistentry> |
| 22642 |
|
| 22643 |
<varlistentry> |
| 22644 |
<term><anchor id="readelfref"><command>readelf</command></term> |
| 22645 |
<indexterm> |
| 22646 |
<primary>elf</primary> |
| 22647 |
</indexterm> |
| 22648 |
<indexterm> |
| 22649 |
<primary>command</primary> |
| 22650 |
<secondary>statistics</secondary> |
| 22651 |
</indexterm> |
| 22652 |
<listitem> |
| 22653 |
<para>Show information and statistics about a designated |
| 22654 |
<firstterm>elf</firstterm> binary. This is part of the |
| 22655 |
<firstterm>binutils</firstterm> package.</para> |
| 22656 |
<screen><prompt>bash$ </prompt><userinput>readelf -h /bin/bash</userinput> |
| 22657 |
<computeroutput>ELF Header: |
| 22658 |
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 |
| 22659 |
Class: ELF32 |
| 22660 |
Data: 2's complement, little endian |
| 22661 |
Version: 1 (current) |
| 22662 |
OS/ABI: UNIX - System V |
| 22663 |
ABI Version: 0 |
| 22664 |
Type: EXEC (Executable file) |
| 22665 |
. . .</computeroutput></screen> |
| 22666 |
</listitem> |
| 22667 |
</varlistentry> |
| 22668 |
|
| 22669 |
<varlistentry> |
| 22670 |
<term><anchor id="sizeref"><command>size</command></term> |
| 22671 |
<indexterm> |
| 22672 |
<primary>size</primary> |
| 22673 |
</indexterm> |
| 22674 |
<indexterm> |
| 22675 |
<primary>command</primary> |
| 22676 |
<secondary>segment</secondary> |
| 22677 |
</indexterm> |
| 22678 |
<listitem> |
| 22679 |
<para>The <command>size [/path/to/binary]</command> command |
| 22680 |
gives the segment sizes of a binary executable or archive file. |
| 22681 |
This is mainly of use to programmers.</para> |
| 22682 |
<para> |
| 22683 |
<screen><prompt>bash$ </prompt><userinput>size /bin/bash</userinput> |
| 22684 |
<computeroutput> text data bss dec hex filename |
| 22685 |
495971 22496 17392 535859 82d33 /bin/bash</computeroutput> |
| 22686 |
</screen> |
| 22687 |
</para> |
| 22688 |
</listitem> |
| 22689 |
</varlistentry> |
| 22690 |
|
| 22691 |
</variablelist> |
| 22692 |
|
| 22693 |
<variablelist id="syslog"> |
| 22694 |
<title><anchor id="syslog1">System Logs</title> |
| 22695 |
|
| 22696 |
<varlistentry> |
| 22697 |
<term><anchor id="loggerref"><command>logger</command></term> |
| 22698 |
<indexterm> |
| 22699 |
<primary>logger</primary> |
| 22700 |
</indexterm> |
| 22701 |
<indexterm> |
| 22702 |
<primary>command</primary> |
| 22703 |
<secondary>logger</secondary> |
| 22704 |
</indexterm> |
| 22705 |
<listitem> |
| 22706 |
|
| 22707 |
<para>Appends a user-generated message to the system log |
| 22708 |
(<filename>/var/log/messages</filename>). You do not have |
| 22709 |
to be <firstterm>root</firstterm> to invoke |
| 22710 |
<command>logger</command>.</para> |
| 22711 |
|
| 22712 |
<para> |
| 22713 |
<programlisting>logger Experiencing instability in network connection at 23:10, 05/21. |
| 22714 |
# Now, do a 'tail /var/log/messages'.</programlisting></para> |
| 22715 |
|
| 22716 |
<para>By embedding a <command>logger</command> command in a script, |
| 22717 |
it is possible to write debugging information to |
| 22718 |
<filename>/var/log/messages</filename>.</para> |
| 22719 |
|
| 22720 |
<para><programlisting>logger -t $0 -i Logging at line "$LINENO". |
| 22721 |
# The "-t" option specifies the tag for the logger entry. |
| 22722 |
# The "-i" option records the process ID. |
| 22723 |
|
| 22724 |
# tail /var/log/message |
| 22725 |
# ... |
| 22726 |
# Jul 7 20:48:58 localhost ./test.sh[1712]: Logging at line 3.</programlisting> |
| 22727 |
</para> |
| 22728 |
|
| 22729 |
</listitem> |
| 22730 |
</varlistentry> |
| 22731 |
|
| 22732 |
<varlistentry> |
| 22733 |
<term><anchor id="logrotateref"><command>logrotate</command></term> |
| 22734 |
<indexterm> |
| 22735 |
<primary>logrotate</primary> |
| 22736 |
</indexterm> |
| 22737 |
<indexterm> |
| 22738 |
<primary>command</primary> |
| 22739 |
<secondary>logrotate</secondary> |
| 22740 |
</indexterm> |
| 22741 |
<listitem> |
| 22742 |
|
| 22743 |
<para>This utility manages the system log files, rotating, |
| 22744 |
compressing, deleting, and/or e-mailing them, as appropriate. |
| 22745 |
This keeps the <filename class="directory">/var/log</filename> |
| 22746 |
from getting cluttered with old log files. |
| 22747 |
Usually <link linkend="cronref">cron</link> runs |
| 22748 |
<command>logrotate</command> on a daily basis.</para> |
| 22749 |
|
| 22750 |
<para>Adding an appropriate entry to |
| 22751 |
<filename>/etc/logrotate.conf</filename> makes it possible |
| 22752 |
to manage personal log files, as well as system-wide |
| 22753 |
ones.</para> |
| 22754 |
|
| 22755 |
<note><para>Stefano Falsetto has created <ulink |
| 22756 |
url="http://www.gnu.org/software/rottlog/">rottlog</ulink>, |
| 22757 |
which he considers to be an improved version of |
| 22758 |
<command>logrotate</command>.</para></note> |
| 22759 |
|
| 22760 |
</listitem> |
| 22761 |
</varlistentry> |
| 22762 |
|
| 22763 |
</variablelist> |
| 22764 |
|
| 22765 |
|
| 22766 |
<variablelist id="jobcontrolsys"> |
| 22767 |
<title><anchor id="jobcontrolsys1">Job Control</title> |
| 22768 |
|
| 22769 |
<varlistentry> |
| 22770 |
<term><anchor id="ppssref"><command>ps</command></term> |
| 22771 |
<indexterm> |
| 22772 |
<primary>ps</primary> |
| 22773 |
</indexterm> |
| 22774 |
<indexterm> |
| 22775 |
<primary>command</primary> |
| 22776 |
<secondary>ps</secondary> |
| 22777 |
</indexterm> |
| 22778 |
<listitem> |
| 22779 |
|
| 22780 |
<para><replaceable>P</replaceable>rocess |
| 22781 |
<replaceable>S</replaceable>tatistics: lists currently |
| 22782 |
executing processes by owner and PID (process ID). This |
| 22783 |
is usually invoked with <option>ax</option> or |
| 22784 |
<option>aux</option> options, |
| 22785 |
and may be piped to <link linkend="grepref">grep</link> |
| 22786 |
or <link linkend="sedref">sed</link> to search for a |
| 22787 |
specific process (see <xref linkend="ex44"> and <xref |
| 22788 |
linkend="pidid">).</para> |
| 22789 |
|
| 22790 |
<screen><prompt>bash$ </prompt><userinput> ps ax | grep sendmail</userinput> |
| 22791 |
<computeroutput>295 ? S 0:00 sendmail: accepting connections on port 25</computeroutput></screen> |
| 22792 |
|
| 22793 |
<para>To display system processes in graphical <quote>tree</quote> |
| 22794 |
format: <command>ps afjx</command> or |
| 22795 |
<command>ps ax --forest</command>.</para> |
| 22796 |
|
| 22797 |
</listitem> |
| 22798 |
</varlistentry> |
| 22799 |
|
| 22800 |
|
| 22801 |
<varlistentry> |
| 22802 |
<term><anchor id="pgrepref"><command>pgrep</command></term> |
| 22803 |
<term><anchor id="pkillref"><command>pkill</command></term> |
| 22804 |
<indexterm> |
| 22805 |
<primary>pgrep</primary> |
| 22806 |
</indexterm> |
| 22807 |
<indexterm> |
| 22808 |
<primary>command</primary> |
| 22809 |
<secondary>process grep</secondary> |
| 22810 |
</indexterm> |
| 22811 |
<indexterm> |
| 22812 |
<primary>pkill</primary> |
| 22813 |
</indexterm> |
| 22814 |
<indexterm> |
| 22815 |
<primary>command</primary> |
| 22816 |
<secondary>process kill</secondary> |
| 22817 |
</indexterm> |
| 22818 |
|
| 22819 |
<listitem> |
| 22820 |
<para>Combining the <command>ps</command> command |
| 22821 |
with <link linkend="grepref">grep</link> or |
| 22822 |
<link linkend="killref">kill</link>.</para> |
| 22823 |
|
| 22824 |
<para> |
| 22825 |
<screen><prompt>bash$ </prompt><userinput>ps a | grep mingetty</userinput> |
| 22826 |
<computeroutput>2212 tty2 Ss+ 0:00 /sbin/mingetty tty2 |
| 22827 |
2213 tty3 Ss+ 0:00 /sbin/mingetty tty3 |
| 22828 |
2214 tty4 Ss+ 0:00 /sbin/mingetty tty4 |
| 22829 |
2215 tty5 Ss+ 0:00 /sbin/mingetty tty5 |
| 22830 |
2216 tty6 Ss+ 0:00 /sbin/mingetty tty6 |
| 22831 |
4849 pts/2 S+ 0:00 grep mingetty</computeroutput> |
| 22832 |
|
| 22833 |
|
| 22834 |
<prompt>bash$ </prompt><userinput>pgrep mingetty</userinput> |
| 22835 |
<computeroutput>2212 mingetty |
| 22836 |
2213 mingetty |
| 22837 |
2214 mingetty |
| 22838 |
2215 mingetty |
| 22839 |
2216 mingetty</computeroutput> |
| 22840 |
</screen> |
| 22841 |
</para> |
| 22842 |
|
| 22843 |
<para>Compare the action of <command>pkill</command> with <link |
| 22844 |
linkend="killallref">killall</link>.</para> |
| 22845 |
|
| 22846 |
</listitem> |
| 22847 |
</varlistentry> |
| 22848 |
|
| 22849 |
|
| 22850 |
<varlistentry> |
| 22851 |
<term><anchor id="pstreeref"><command>pstree</command></term> |
| 22852 |
<indexterm> |
| 22853 |
<primary>pstree</primary> |
| 22854 |
</indexterm> |
| 22855 |
<indexterm> |
| 22856 |
<primary>command</primary> |
| 22857 |
<secondary>pstree</secondary> |
| 22858 |
</indexterm> |
| 22859 |
<listitem> |
| 22860 |
<para>Lists currently executing processes in |
| 22861 |
<quote>tree</quote> format. The <option>-p</option> option |
| 22862 |
shows the PIDs, as well as the process names.</para> |
| 22863 |
|
| 22864 |
</listitem> |
| 22865 |
</varlistentry> |
| 22866 |
|
| 22867 |
<varlistentry> |
| 22868 |
<term><anchor id="topref"><command>top</command></term> |
| 22869 |
<indexterm> |
| 22870 |
<primary>top</primary> |
| 22871 |
</indexterm> |
| 22872 |
<indexterm> |
| 22873 |
<primary>command</primary> |
| 22874 |
<secondary>processes</secondary> |
| 22875 |
</indexterm> |
| 22876 |
<listitem> |
| 22877 |
|
| 22878 |
<para>Continuously updated display of most cpu-intensive |
| 22879 |
processes. The <option>-b</option> option displays in text |
| 22880 |
mode, so that the output may be parsed or accessed from |
| 22881 |
a script.</para> |
| 22882 |
|
| 22883 |
<para> |
| 22884 |
<screen><prompt>bash$ </prompt><userinput>top -b</userinput> |
| 22885 |
<computeroutput> 8:30pm up 3 min, 3 users, load average: 0.49, 0.32, 0.13 |
| 22886 |
45 processes: 44 sleeping, 1 running, 0 zombie, 0 stopped |
| 22887 |
CPU states: 13.6% user, 7.3% system, 0.0% nice, 78.9% idle |
| 22888 |
Mem: 78396K av, 65468K used, 12928K free, 0K shrd, 2352K buff |
| 22889 |
Swap: 157208K av, 0K used, 157208K free 37244K cached |
| 22890 |
|
| 22891 |
PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME COMMAND |
| 22892 |
848 bozo 17 0 996 996 800 R 5.6 1.2 0:00 top |
| 22893 |
1 root 8 0 512 512 444 S 0.0 0.6 0:04 init |
| 22894 |
2 root 9 0 0 0 0 SW 0.0 0.0 0:00 keventd |
| 22895 |
...</computeroutput> |
| 22896 |
</screen> |
| 22897 |
</para> |
| 22898 |
|
| 22899 |
</listitem> |
| 22900 |
</varlistentry> |
| 22901 |
|
| 22902 |
<varlistentry> |
| 22903 |
<term><anchor id="niceref"><command>nice</command></term> |
| 22904 |
<indexterm> |
| 22905 |
<primary>nice</primary> |
| 22906 |
</indexterm> |
| 22907 |
<indexterm> |
| 22908 |
<primary>command</primary> |
| 22909 |
<secondary>nice</secondary> |
| 22910 |
</indexterm> |
| 22911 |
<listitem> |
| 22912 |
<para><anchor id="nice2ref"></para> |
| 22913 |
<para>Run a background job with an altered |
| 22914 |
priority. Priorities run from 19 (lowest) to -20 |
| 22915 |
(highest). Only <firstterm>root</firstterm> may set the |
| 22916 |
negative (higher) priorities. Related commands are |
| 22917 |
<command>renice</command> and <command>snice</command>, |
| 22918 |
which change the priority of a running process or |
| 22919 |
processes, and <command>skill</command>, which sends a |
| 22920 |
<link linkend="killref">kill</link> signal to a process |
| 22921 |
or processes.</para> |
| 22922 |
</listitem> |
| 22923 |
</varlistentry> |
| 22924 |
|
| 22925 |
<varlistentry> |
| 22926 |
<term><anchor id="nohupref"><command>nohup</command></term> |
| 22927 |
<indexterm> |
| 22928 |
<primary>nohup</primary> |
| 22929 |
</indexterm> |
| 22930 |
<indexterm> |
| 22931 |
<primary>command</primary> |
| 22932 |
<secondary>nohup</secondary> |
| 22933 |
</indexterm> |
| 22934 |
<listitem> |
| 22935 |
<para>Keeps a command running even after user logs off. |
| 22936 |
The command will run as a foreground process unless followed |
| 22937 |
by <token>&</token>. If you use <command>nohup</command> |
| 22938 |
within a script, consider coupling it with a <link |
| 22939 |
linkend="waitref">wait</link> to avoid creating an |
| 22940 |
<firstterm>orphan</firstterm> or |
| 22941 |
<link linkend="zombieref">zombie</link> process.</para> |
| 22942 |
</listitem> |
| 22943 |
</varlistentry> |
| 22944 |
|
| 22945 |
<varlistentry> |
| 22946 |
<term><anchor id="pidofref"><command>pidof</command></term> |
| 22947 |
<indexterm> |
| 22948 |
<primary>pidof</primary> |
| 22949 |
</indexterm> |
| 22950 |
<indexterm> |
| 22951 |
<primary>command</primary> |
| 22952 |
<secondary>process ID</secondary> |
| 22953 |
</indexterm> |
| 22954 |
<listitem> |
| 22955 |
<para>Identifies <firstterm>process ID (PID)</firstterm> of a |
| 22956 |
running job. Since job control commands, such as <link |
| 22957 |
linkend="killref">kill</link> and <link |
| 22958 |
linkend="nice2ref">renice</link> act on the |
| 22959 |
<firstterm>PID</firstterm> of a process (not its |
| 22960 |
name), it is sometimes necessary to identify that |
| 22961 |
<firstterm>PID</firstterm>. The <command>pidof</command> |
| 22962 |
command is the approximate counterpart to the <link |
| 22963 |
linkend="ppidref">$PPID</link> internal variable.</para> |
| 22964 |
|
| 22965 |
<para> |
| 22966 |
<screen><prompt>bash$ </prompt><userinput>pidof xclock</userinput> |
| 22967 |
<computeroutput>880</computeroutput> |
| 22968 |
</screen> |
| 22969 |
</para> |
| 22970 |
|
| 22971 |
<example id="killprocess"> |
| 22972 |
<title><firstterm>pidof</firstterm> helps kill a process</title> |
| 22973 |
<programlisting>&killprocess;</programlisting> |
| 22974 |
</example> |
| 22975 |
|
| 22976 |
</listitem> |
| 22977 |
</varlistentry> |
| 22978 |
|
| 22979 |
<varlistentry> |
| 22980 |
<term><anchor id="fuserref"><command>fuser</command></term> |
| 22981 |
<indexterm> |
| 22982 |
<primary>fuser</primary> |
| 22983 |
</indexterm> |
| 22984 |
<indexterm> |
| 22985 |
<primary>command</primary> |
| 22986 |
<secondary>fuser</secondary> |
| 22987 |
</indexterm> |
| 22988 |
<listitem> |
| 22989 |
<para>Identifies the processes (by PID) that are accessing |
| 22990 |
a given file, set of files, or directory. May also be |
| 22991 |
invoked with the <option>-k</option> option, which kills |
| 22992 |
those processes. This has interesting implications for |
| 22993 |
system security, especially in scripts preventing |
| 22994 |
unauthorized users from accessing system services.</para> |
| 22995 |
|
| 22996 |
<para> |
| 22997 |
<screen><prompt>bash$ </prompt><userinput>fuser -u /usr/bin/vim</userinput> |
| 22998 |
<computeroutput>/usr/bin/vim: 3207e(bozo)</computeroutput> |
| 22999 |
|
| 23000 |
|
| 23001 |
|
| 23002 |
<prompt>bash$ </prompt><userinput>fuser -u /dev/null</userinput> |
| 23003 |
<computeroutput>/dev/null: 3009(bozo) 3010(bozo) 3197(bozo) 3199(bozo)</computeroutput> |
| 23004 |
</screen> |
| 23005 |
</para> |
| 23006 |
|
| 23007 |
<para>One important application for <command>fuser</command> is |
| 23008 |
when physically inserting or removing storage media, such |
| 23009 |
as CD ROM disks or USB flash drives. Sometimes trying |
| 23010 |
a <link linkend="umountref">umount</link> fails with a |
| 23011 |
<errorname>device is busy</errorname> error message. This |
| 23012 |
means that some user(s) and/or process(es) are accessing |
| 23013 |
the device. An <command>fuser -um /dev/device_name</command> |
| 23014 |
will clear up the mystery, so you can kill any relevant |
| 23015 |
processes.</para> |
| 23016 |
|
| 23017 |
<para> |
| 23018 |
<screen><prompt>bash$ </prompt><userinput>umount /mnt/usbdrive</userinput> |
| 23019 |
<computeroutput>umount: /mnt/usbdrive: device is busy</computeroutput> |
| 23020 |
|
| 23021 |
|
| 23022 |
|
| 23023 |
<prompt>bash$ </prompt><userinput>fuser -um /dev/usbdrive</userinput> |
| 23024 |
<computeroutput>/mnt/usbdrive: 1772c(bozo)</computeroutput> |
| 23025 |
|
| 23026 |
<prompt>bash$ </prompt><userinput>kill -9 1772</userinput> |
| 23027 |
<prompt>bash$ </prompt><userinput>umount /mnt/usbdrive</userinput> |
| 23028 |
</screen> |
| 23029 |
</para> |
| 23030 |
|
| 23031 |
<para> The <command>fuser</command> command, invoked with the |
| 23032 |
<option>-n</option> option identifies the processes |
| 23033 |
accessing a <firstterm>port</firstterm>. This |
| 23034 |
is especially useful in combination with <link |
| 23035 |
linkend="nmapref">nmap</link>.</para> |
| 23036 |
|
| 23037 |
<para> |
| 23038 |
<screen><prompt>root# </prompt><userinput>nmap localhost.localdomain</userinput> |
| 23039 |
<computeroutput>PORT STATE SERVICE |
| 23040 |
25/tcp open smtp</computeroutput> |
| 23041 |
|
| 23042 |
|
| 23043 |
|
| 23044 |
<prompt>root# </prompt><userinput>fuser -un tcp 25</userinput> |
| 23045 |
<computeroutput>25/tcp: 2095(root)</computeroutput> |
| 23046 |
|
| 23047 |
<prompt>root# </prompt><userinput>ps ax | grep 2095 | grep -v grep</userinput> |
| 23048 |
<computeroutput>2095 ? Ss 0:00 sendmail: accepting connections</computeroutput> |
| 23049 |
</screen> |
| 23050 |
</para> |
| 23051 |
|
| 23052 |
|
| 23053 |
</listitem> |
| 23054 |
</varlistentry> |
| 23055 |
|
| 23056 |
<varlistentry> |
| 23057 |
<term><anchor id="cronref"><command>cron</command></term> |
| 23058 |
<indexterm> |
| 23059 |
<primary>cron</primary> |
| 23060 |
</indexterm> |
| 23061 |
<indexterm> |
| 23062 |
<primary>command</primary> |
| 23063 |
<secondary>crond</secondary> |
| 23064 |
</indexterm> |
| 23065 |
<listitem> |
| 23066 |
|
| 23067 |
<para>Administrative program scheduler, performing such |
| 23068 |
duties as cleaning up and deleting system log files and |
| 23069 |
updating the <database>slocate</database> database. This |
| 23070 |
is the <firstterm>superuser</firstterm> version of <link |
| 23071 |
linkend="atref">at</link> (although each user may have |
| 23072 |
their own <filename>crontab</filename> file which can be |
| 23073 |
changed with the <command>crontab</command> command). |
| 23074 |
It runs as a <link linkend="daemonref">daemon</link> |
| 23075 |
and executes scheduled entries from |
| 23076 |
<filename>/etc/crontab</filename>.</para> |
| 23077 |
|
| 23078 |
<note><para>Some flavors of Linux run |
| 23079 |
<command>crond</command>, Matthew Dillon's version of |
| 23080 |
<command>cron</command>.</para></note> |
| 23081 |
|
| 23082 |
</listitem> |
| 23083 |
</varlistentry> |
| 23084 |
|
| 23085 |
</variablelist> |
| 23086 |
|
| 23087 |
<variablelist id="runcontrolsys"> |
| 23088 |
<title><anchor id="runcontrolsys1">Process Control and Booting</title> |
| 23089 |
|
| 23090 |
<varlistentry> |
| 23091 |
<term><anchor id="initref"><command>init</command></term> |
| 23092 |
<indexterm> |
| 23093 |
<primary>init</primary> |
| 23094 |
</indexterm> |
| 23095 |
<indexterm> |
| 23096 |
<primary>command</primary> |
| 23097 |
<secondary>init</secondary> |
| 23098 |
</indexterm> |
| 23099 |
<listitem> |
| 23100 |
<para><anchor id="inittabref"></para> |
| 23101 |
<para>The <command>init</command> command is the <link |
| 23102 |
linkend="forkref">parent</link> of all processes. Called |
| 23103 |
in the final step of a bootup, <command>init</command> |
| 23104 |
determines the runlevel of the system from |
| 23105 |
<filename>/etc/inittab</filename>. Invoked by its alias |
| 23106 |
<command>telinit</command>, and by |
| 23107 |
<firstterm>root</firstterm> only.</para> |
| 23108 |
</listitem> |
| 23109 |
</varlistentry> |
| 23110 |
|
| 23111 |
<varlistentry> |
| 23112 |
<term><anchor id="telinitref"><command>telinit</command></term> |
| 23113 |
<indexterm> |
| 23114 |
<primary>telinit</primary> |
| 23115 |
</indexterm> |
| 23116 |
<indexterm> |
| 23117 |
<primary>command</primary> |
| 23118 |
<secondary>telinit</secondary> |
| 23119 |
</indexterm> |
| 23120 |
<listitem> |
| 23121 |
<para>Symlinked to <command>init</command>, this is a means of changing the system runlevel, |
| 23122 |
usually done for system maintenance or emergency filesystem |
| 23123 |
repairs. Invoked only by <firstterm>root</firstterm>. This |
| 23124 |
command can be dangerous -- be certain you understand it |
| 23125 |
well before using!</para> |
| 23126 |
</listitem> |
| 23127 |
</varlistentry> |
| 23128 |
|
| 23129 |
<varlistentry> |
| 23130 |
<term><anchor id="runlevelref"><command>runlevel</command></term> |
| 23131 |
<indexterm> |
| 23132 |
<primary>runlevel</primary> |
| 23133 |
</indexterm> |
| 23134 |
<indexterm> |
| 23135 |
<primary>command</primary> |
| 23136 |
<secondary>runlevel</secondary> |
| 23137 |
</indexterm> |
| 23138 |
<listitem> |
| 23139 |
<para>Shows the current and last runlevel, that is, whether the system |
| 23140 |
is halted (runlevel <literal>0</literal>), in single-user mode |
| 23141 |
(<literal>1</literal>), in multi-user mode (<literal>2</literal> |
| 23142 |
or <literal>3</literal>), in X Windows (<literal>5</literal>), or |
| 23143 |
rebooting (<literal>6</literal>). This command accesses the |
| 23144 |
<filename>/var/run/utmp</filename> file.</para> |
| 23145 |
</listitem> |
| 23146 |
</varlistentry> |
| 23147 |
|
| 23148 |
<varlistentry> |
| 23149 |
<term><anchor id="haltref"><command>halt</command></term> |
| 23150 |
<term><anchor id="shutdownref"><command>shutdown</command></term> |
| 23151 |
<term><anchor id="rebootref"><command>reboot</command></term> |
| 23152 |
<indexterm> |
| 23153 |
<primary>halt</primary> |
| 23154 |
</indexterm> |
| 23155 |
<indexterm> |
| 23156 |
<primary>command</primary> |
| 23157 |
<secondary>halt</secondary> |
| 23158 |
</indexterm> |
| 23159 |
<indexterm> |
| 23160 |
<primary>shutdown</primary> |
| 23161 |
</indexterm> |
| 23162 |
<indexterm> |
| 23163 |
<primary>command</primary> |
| 23164 |
<secondary>shutdown</secondary> |
| 23165 |
</indexterm> |
| 23166 |
<indexterm> |
| 23167 |
<primary>reboot</primary> |
| 23168 |
</indexterm> |
| 23169 |
<indexterm> |
| 23170 |
<primary>command</primary> |
| 23171 |
<secondary>reboot</secondary> |
| 23172 |
</indexterm> |
| 23173 |
<listitem> |
| 23174 |
<para>Command set to shut the system down, usually just prior to a power down.</para> |
| 23175 |
<warning><para>On some Linux distros, the <command>halt</command> command |
| 23176 |
has 755 permissions, so it can be invoked by a non-root user. |
| 23177 |
A careless <firstterm>halt</firstterm> in a terminal or a script |
| 23178 |
may shut down the system!</para></warning> |
| 23179 |
</listitem> |
| 23180 |
</varlistentry> |
| 23181 |
|
| 23182 |
<varlistentry> |
| 23183 |
<term><anchor id="serviceref"><command>service</command></term> |
| 23184 |
<indexterm> |
| 23185 |
<primary>service</primary> |
| 23186 |
</indexterm> |
| 23187 |
<indexterm> |
| 23188 |
<primary>command</primary> |
| 23189 |
<secondary>service</secondary> |
| 23190 |
</indexterm> |
| 23191 |
<listitem> |
| 23192 |
<para>Starts or stops a system <firstterm>service</firstterm>. |
| 23193 |
The startup scripts in <filename class="directory">/etc/init.d</filename> |
| 23194 |
and <filename class="directory">/etc/rc.d</filename> use this |
| 23195 |
command to start services at bootup.</para> |
| 23196 |
|
| 23197 |
<para><anchor id="iptables01"></para> |
| 23198 |
|
| 23199 |
<para> |
| 23200 |
<screen><prompt>root# </prompt><userinput>/sbin/service iptables stop</userinput> |
| 23201 |
<computeroutput>Flushing firewall rules: [ OK ] |
| 23202 |
Setting chains to policy ACCEPT: filter [ OK ] |
| 23203 |
Unloading iptables modules: [ OK ]</computeroutput> |
| 23204 |
</screen> |
| 23205 |
</para> |
| 23206 |
|
| 23207 |
</listitem> |
| 23208 |
</varlistentry> |
| 23209 |
|
| 23210 |
</variablelist> |
| 23211 |
|
| 23212 |
|
| 23213 |
<variablelist id="networksys"> |
| 23214 |
<title><anchor id="networksys1">Network</title> |
| 23215 |
|
| 23216 |
<varlistentry> |
| 23217 |
<term><anchor id="nmapref"><command>nmap</command></term> |
| 23218 |
<indexterm> |
| 23219 |
<primary>nmap</primary> |
| 23220 |
</indexterm> |
| 23221 |
<indexterm> |
| 23222 |
<primary>command</primary> |
| 23223 |
<secondary>port scan</secondary> |
| 23224 |
</indexterm> |
| 23225 |
<listitem> |
| 23226 |
<para><command>N</command>etwork <command>map</command>per |
| 23227 |
and port scanner. This command scans a server to |
| 23228 |
locate open ports and the services associated with those |
| 23229 |
ports. It can also report information about packet filters and |
| 23230 |
firewalls. This is an important security tool for locking down |
| 23231 |
a network against hacking attempts.</para> |
| 23232 |
<para><programlisting>#!/bin/bash |
| 23233 |
|
| 23234 |
SERVER=$HOST # localhost.localdomain (127.0.0.1). |
| 23235 |
PORT_NUMBER=25 # SMTP port. |
| 23236 |
|
| 23237 |
nmap $SERVER | grep -w "$PORT_NUMBER" # Is that particular port open? |
| 23238 |
# grep -w matches whole words only, |
| 23239 |
#+ so this wouldn't match port 1025, for example. |
| 23240 |
|
| 23241 |
exit 0 |
| 23242 |
|
| 23243 |
# 25/tcp open smtp</programlisting></para> |
| 23244 |
</listitem> |
| 23245 |
</varlistentry> |
| 23246 |
|
| 23247 |
<varlistentry> |
| 23248 |
<term><anchor id="ifconfigref"><command>ifconfig</command></term> |
| 23249 |
<indexterm> |
| 23250 |
<primary>ifconfig</primary> |
| 23251 |
</indexterm> |
| 23252 |
<indexterm> |
| 23253 |
<primary>command</primary> |
| 23254 |
<secondary>ifconfig</secondary> |
| 23255 |
</indexterm> |
| 23256 |
<listitem> |
| 23257 |
<para>Network <firstterm>interface configuration</firstterm> |
| 23258 |
and tuning utility.</para> |
| 23259 |
|
| 23260 |
<screen><prompt>bash$ </prompt><userinput>ifconfig -a</userinput> |
| 23261 |
<computeroutput>lo Link encap:Local Loopback |
| 23262 |
inet addr:127.0.0.1 Mask:255.0.0.0 |
| 23263 |
UP LOOPBACK RUNNING MTU:16436 Metric:1 |
| 23264 |
RX packets:10 errors:0 dropped:0 overruns:0 frame:0 |
| 23265 |
TX packets:10 errors:0 dropped:0 overruns:0 carrier:0 |
| 23266 |
collisions:0 txqueuelen:0 |
| 23267 |
RX bytes:700 (700.0 b) TX bytes:700 (700.0 b)</computeroutput></screen> |
| 23268 |
|
| 23269 |
<para>The <command>ifconfig</command> command is most often used |
| 23270 |
at bootup to set up the interfaces, or to shut them down |
| 23271 |
when rebooting.</para> |
| 23272 |
|
| 23273 |
<para><programlisting># Code snippets from /etc/rc.d/init.d/network |
| 23274 |
|
| 23275 |
# ... |
| 23276 |
|
| 23277 |
# Check that networking is up. |
| 23278 |
[ ${NETWORKING} = "no" ] && exit 0 |
| 23279 |
|
| 23280 |
[ -x /sbin/ifconfig ] || exit 0 |
| 23281 |
|
| 23282 |
# ... |
| 23283 |
|
| 23284 |
for i in $interfaces ; do |
| 23285 |
if ifconfig $i 2>/dev/null | grep -q "UP" >/dev/null 2>&1 ; then |
| 23286 |
action "Shutting down interface $i: " ./ifdown $i boot |
| 23287 |
fi |
| 23288 |
# The GNU-specific "-q" option to "grep" means "quiet", i.e., |
| 23289 |
#+ producing no output. |
| 23290 |
# Redirecting output to /dev/null is therefore not strictly necessary. |
| 23291 |
|
| 23292 |
# ... |
| 23293 |
|
| 23294 |
echo "Currently active devices:" |
| 23295 |
echo `/sbin/ifconfig | grep ^[a-z] | awk '{print $1}'` |
| 23296 |
# ^^^^^ should be quoted to prevent globbing. |
| 23297 |
# The following also work. |
| 23298 |
# echo $(/sbin/ifconfig | awk '/^[a-z]/ { print $1 })' |
| 23299 |
# echo $(/sbin/ifconfig | sed -e 's/ .*//') |
| 23300 |
# Thanks, S.C., for additional comments.</programlisting></para> |
| 23301 |
|
| 23302 |
<para>See also <xref linkend="online">.</para> |
| 23303 |
|
| 23304 |
</listitem> |
| 23305 |
</varlistentry> |
| 23306 |
|
| 23307 |
<varlistentry> |
| 23308 |
<term><anchor id="netstatref"><command>netstat</command></term> |
| 23309 |
<indexterm> |
| 23310 |
<primary>netstat</primary> |
| 23311 |
</indexterm> |
| 23312 |
<indexterm> |
| 23313 |
<primary>command</primary> |
| 23314 |
<secondary>netstat</secondary> |
| 23315 |
</indexterm> |
| 23316 |
<listitem> |
| 23317 |
|
| 23318 |
<para>Show current network statistics and information, |
| 23319 |
such as routing tables and active connections. This utility |
| 23320 |
accesses information in <filename>/proc/net</filename> |
| 23321 |
(<xref linkend="devproc">). See <xref |
| 23322 |
linkend="constat">.</para> |
| 23323 |
<para><command>netstat -r</command> is equivalent to <link |
| 23324 |
linkend="routeref">route</link>.</para> |
| 23325 |
|
| 23326 |
<screen><prompt>bash$ </prompt><userinput>netstat</userinput> |
| 23327 |
<computeroutput>Active Internet connections (w/o servers) |
| 23328 |
Proto Recv-Q Send-Q Local Address Foreign Address State |
| 23329 |
Active UNIX domain sockets (w/o servers) |
| 23330 |
Proto RefCnt Flags Type State I-Node Path |
| 23331 |
unix 11 [ ] DGRAM 906 /dev/log |
| 23332 |
unix 3 [ ] STREAM CONNECTED 4514 /tmp/.X11-unix/X0 |
| 23333 |
unix 3 [ ] STREAM CONNECTED 4513 |
| 23334 |
. . .</computeroutput></screen> |
| 23335 |
|
| 23336 |
<note><para>A <command>netstat -lptu</command> shows <link |
| 23337 |
linkend="socketref">sockets</link> that are listening |
| 23338 |
to ports, and the associated processes. This can be useful |
| 23339 |
for determining whether a computer has been hacked or |
| 23340 |
compromised.</para></note> |
| 23341 |
|
| 23342 |
</listitem> |
| 23343 |
</varlistentry> |
| 23344 |
|
| 23345 |
<varlistentry> |
| 23346 |
<term><anchor id="iwconfigref"><command>iwconfig</command></term> |
| 23347 |
<indexterm> |
| 23348 |
<primary>iwconfig</primary> |
| 23349 |
</indexterm> |
| 23350 |
<indexterm> |
| 23351 |
<primary>command</primary> |
| 23352 |
<secondary>wireless</secondary> |
| 23353 |
</indexterm> |
| 23354 |
<listitem> |
| 23355 |
<para>This is the command set for configuring a wireless network. |
| 23356 |
It is the wireless equivalent of <command>ifconfig</command>, |
| 23357 |
above.</para> |
| 23358 |
</listitem> |
| 23359 |
</varlistentry> |
| 23360 |
|
| 23361 |
<varlistentry> |
| 23362 |
<term><anchor id="ipref"><command>ip</command></term> |
| 23363 |
<indexterm> |
| 23364 |
<primary>ip</primary> |
| 23365 |
</indexterm> |
| 23366 |
<indexterm> |
| 23367 |
<primary>command</primary> |
| 23368 |
<secondary>routing</secondary> |
| 23369 |
</indexterm> |
| 23370 |
<listitem> |
| 23371 |
|
| 23372 |
<para>General purpose utility for setting up, changing, and |
| 23373 |
analyzing <firstterm>IP</firstterm> (Internet Protocol) |
| 23374 |
networks and attached devices. This command is part of |
| 23375 |
the <firstterm>iproute2</firstterm> package.</para> |
| 23376 |
|
| 23377 |
<para> |
| 23378 |
<screen><prompt>bash$ </prompt><userinput>ip link show</userinput> |
| 23379 |
<computeroutput>1: lo: <LOOPBACK,UP> mtu 16436 qdisc noqueue |
| 23380 |
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 |
| 23381 |
2: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast qlen 1000 |
| 23382 |
link/ether 00:d0:59:ce:af:da brd ff:ff:ff:ff:ff:ff |
| 23383 |
3: sit0: <NOARP> mtu 1480 qdisc noop |
| 23384 |
link/sit 0.0.0.0 brd 0.0.0.0</computeroutput> |
| 23385 |
|
| 23386 |
|
| 23387 |
<prompt>bash$ </prompt><userinput>ip route list</userinput> |
| 23388 |
<computeroutput>169.254.0.0/16 dev lo scope link</computeroutput> |
| 23389 |
</screen> |
| 23390 |
</para> |
| 23391 |
|
| 23392 |
<para>Or, in a script:</para> |
| 23393 |
|
| 23394 |
<para><anchor id="ipscript0"> |
| 23395 |
<programlisting>&ipscript;</programlisting> |
| 23396 |
</para> |
| 23397 |
|
| 23398 |
</listitem> |
| 23399 |
</varlistentry> |
| 23400 |
|
| 23401 |
<varlistentry> |
| 23402 |
<term><anchor id="routeref"><command>route</command></term> |
| 23403 |
<indexterm> |
| 23404 |
<primary>route</primary> |
| 23405 |
</indexterm> |
| 23406 |
<indexterm> |
| 23407 |
<primary>command</primary> |
| 23408 |
<secondary>route</secondary> |
| 23409 |
</indexterm> |
| 23410 |
<listitem> |
| 23411 |
|
| 23412 |
<para>Show info about or make changes to the kernel routing table.</para> |
| 23413 |
|
| 23414 |
<para> |
| 23415 |
<screen><prompt>bash$ </prompt><userinput>route</userinput> |
| 23416 |
<computeroutput>Destination Gateway Genmask Flags MSS Window irtt Iface |
| 23417 |
pm3-67.bozosisp * 255.255.255.255 UH 40 0 0 ppp0 |
| 23418 |
127.0.0.0 * 255.0.0.0 U 40 0 0 lo |
| 23419 |
default pm3-67.bozosisp 0.0.0.0 UG 40 0 0 ppp0</computeroutput> |
| 23420 |
</screen> |
| 23421 |
</para> |
| 23422 |
|
| 23423 |
</listitem> |
| 23424 |
</varlistentry> |
| 23425 |
|
| 23426 |
|
| 23427 |
<varlistentry> |
| 23428 |
<term><anchor |
| 23429 |
id="iptablesref"><command>iptables</command></term> |
| 23430 |
<indexterm> |
| 23431 |
<primary>iptables</primary> |
| 23432 |
</indexterm> |
| 23433 |
<indexterm> |
| 23434 |
<primary>command</primary> |
| 23435 |
<secondary>firewall</secondary> |
| 23436 |
</indexterm> |
| 23437 |
|
| 23438 |
<listitem> |
| 23439 |
|
| 23440 |
<para> The <command>iptables</command> command set is |
| 23441 |
a packet filtering tool used mainly for such security |
| 23442 |
purposes as setting up network firewalls. This |
| 23443 |
is a complex tool, and a detailed explanation of |
| 23444 |
its use is beyond the scope of this document. <ulink |
| 23445 |
url="http://www.frozentux.net/iptables-tutorial/iptables-tutorial.html">Oskar |
| 23446 |
Andreasson's tutorial</ulink> is a reasonable starting |
| 23447 |
point.</para> |
| 23448 |
|
| 23449 |
<para>See also <link linkend="iptables01">shutting down |
| 23450 |
<firstterm>iptables</firstterm></link> and <xref |
| 23451 |
linkend="ipaddresses">.</para> |
| 23452 |
|
| 23453 |
|
| 23454 |
</listitem> |
| 23455 |
</varlistentry> |
| 23456 |
|
| 23457 |
|
| 23458 |
|
| 23459 |
|
| 23460 |
<varlistentry> |
| 23461 |
<term><anchor id="chkconfigref"><command>chkconfig</command></term> |
| 23462 |
<indexterm> |
| 23463 |
<primary>chkconfig</primary> |
| 23464 |
</indexterm> |
| 23465 |
<indexterm> |
| 23466 |
<primary>command</primary> |
| 23467 |
<secondary>network configuration</secondary> |
| 23468 |
</indexterm> |
| 23469 |
<listitem> |
| 23470 |
|
| 23471 |
<para>Check network and system configuration. This command |
| 23472 |
lists and |
| 23473 |
manages the network and system services started at bootup in |
| 23474 |
the <filename class="directory">/etc/rc?.d</filename> |
| 23475 |
directory.</para> |
| 23476 |
|
| 23477 |
<para>Originally a port from IRIX to Red Hat Linux, |
| 23478 |
<command>chkconfig</command> may not be part of the core |
| 23479 |
installation of some Linux flavors.</para> |
| 23480 |
|
| 23481 |
<para> |
| 23482 |
<screen><prompt>bash$ </prompt><userinput>chkconfig --list</userinput> |
| 23483 |
<computeroutput>atd 0:off 1:off 2:off 3:on 4:on 5:on 6:off |
| 23484 |
rwhod 0:off 1:off 2:off 3:off 4:off 5:off 6:off |
| 23485 |
...</computeroutput> |
| 23486 |
</screen> |
| 23487 |
</para> |
| 23488 |
|
| 23489 |
</listitem> |
| 23490 |
</varlistentry> |
| 23491 |
|
| 23492 |
<varlistentry> |
| 23493 |
<term><anchor id="tcpdumpref"><command>tcpdump</command></term> |
| 23494 |
<indexterm> |
| 23495 |
<primary>tcpdump</primary> |
| 23496 |
</indexterm> |
| 23497 |
<indexterm> |
| 23498 |
<primary>command</primary> |
| 23499 |
<secondary>tcp</secondary> |
| 23500 |
</indexterm> |
| 23501 |
<listitem> |
| 23502 |
<para>Network packet <quote>sniffer.</quote> This is a tool for |
| 23503 |
analyzing and troubleshooting traffic on a network by dumping |
| 23504 |
packet headers that match specified criteria.</para> |
| 23505 |
|
| 23506 |
<para>Dump ip packet traffic between hosts |
| 23507 |
<emphasis>bozoville</emphasis> and |
| 23508 |
<emphasis>caduceus</emphasis>:</para> |
| 23509 |
|
| 23510 |
<para> |
| 23511 |
<screen><prompt>bash$ </prompt><userinput>tcpdump ip host bozoville and caduceus</userinput> |
| 23512 |
</screen> |
| 23513 |
</para> |
| 23514 |
|
| 23515 |
<para>Of course, the output of <command>tcpdump</command> can be |
| 23516 |
parsed with certain of the previously discussed <link |
| 23517 |
linkend="tpcommandlisting1">text processing |
| 23518 |
utilities</link>.</para> |
| 23519 |
</listitem> |
| 23520 |
</varlistentry> |
| 23521 |
|
| 23522 |
</variablelist> |
| 23523 |
|
| 23524 |
|
| 23525 |
|
| 23526 |
<variablelist id="filesystemsys"> |
| 23527 |
<title><anchor id="filesystemsys1">Filesystem</title> |
| 23528 |
|
| 23529 |
<varlistentry> |
| 23530 |
<term><anchor id="mountref"><command>mount</command></term> |
| 23531 |
<indexterm> |
| 23532 |
<primary>mount</primary> |
| 23533 |
</indexterm> |
| 23534 |
<indexterm> |
| 23535 |
<primary>command</primary> |
| 23536 |
<secondary>mount</secondary> |
| 23537 |
</indexterm> |
| 23538 |
<listitem> |
| 23539 |
<para>Mount a filesystem, usually on an external device, |
| 23540 |
such as a floppy or CDROM. <anchor id="fstabref">The file |
| 23541 |
<filename>/etc/fstab</filename> provides a handy listing |
| 23542 |
of available filesystems, partitions, and devices, |
| 23543 |
including options, that may be automatically or manually |
| 23544 |
mounted. The file <filename>/etc/mtab</filename> shows |
| 23545 |
the currently mounted filesystems and partitions |
| 23546 |
(including the virtual ones, such as <filename |
| 23547 |
class="directory">/proc</filename>).</para> |
| 23548 |
|
| 23549 |
<para><command>mount -a</command> mounts all filesystems and |
| 23550 |
partitions listed in <filename>/etc/fstab</filename>, |
| 23551 |
except those with a <option>noauto</option> |
| 23552 |
option. At bootup, a startup script in |
| 23553 |
<filename class="directory">/etc/rc.d</filename> |
| 23554 |
(<filename>rc.sysinit</filename> or something similar) |
| 23555 |
invokes this to get everything mounted.</para> |
| 23556 |
|
| 23557 |
|
| 23558 |
<para><programlisting>mount -t iso9660 /dev/cdrom /mnt/cdrom |
| 23559 |
# Mounts CD ROM. ISO 9660 is a standard CD ROM filesystem. |
| 23560 |
mount /mnt/cdrom |
| 23561 |
# Shortcut, if /mnt/cdrom listed in /etc/fstab</programlisting> |
| 23562 |
</para> |
| 23563 |
|
| 23564 |
<para><anchor id="isomountref0"></para> |
| 23565 |
<para>The versatile <firstterm>mount</firstterm> command can even |
| 23566 |
mount an ordinary file on a block device, and the file will |
| 23567 |
act as if it were a filesystem. <firstterm>Mount</firstterm> |
| 23568 |
accomplishes that by associating the file with a <link |
| 23569 |
linkend="loopbackref">loopback device</link>. One application of |
| 23570 |
this is to mount and examine an ISO9660 filesystem image before |
| 23571 |
burning it onto a CDR. |
| 23572 |
|
| 23573 |
<footnote><para>For more detail on burning CDRs, see Alex |
| 23574 |
Withers' article, <ulink |
| 23575 |
url="http://www2.linuxjournal.com/lj-issues/issue66/3335.html">Creating |
| 23576 |
CDs</ulink>, in the October, 1999 issue of <ulink |
| 23577 |
url="http://www.linuxjournal.com"><citetitle |
| 23578 |
pubwork="journal">Linux |
| 23579 |
Journal</citetitle></ulink>.</para></footnote> |
| 23580 |
|
| 23581 |
</para> |
| 23582 |
|
| 23583 |
<example id="isomountref"> |
| 23584 |
<title>Checking a CD image</title> |
| 23585 |
<programlisting># As root... |
| 23586 |
|
| 23587 |
mkdir /mnt/cdtest # Prepare a mount point, if not already there. |
| 23588 |
|
| 23589 |
mount -r -t iso9660 -o loop cd-image.iso /mnt/cdtest # Mount the image. |
| 23590 |
# "-o loop" option equivalent to "losetup /dev/loop0" |
| 23591 |
cd /mnt/cdtest # Now, check the image. |
| 23592 |
ls -alR # List the files in the directory tree there. |
| 23593 |
# And so forth.</programlisting> |
| 23594 |
</example> |
| 23595 |
|
| 23596 |
</listitem> |
| 23597 |
</varlistentry> |
| 23598 |
|
| 23599 |
<varlistentry> |
| 23600 |
<term><anchor id="umountref"><command>umount</command></term> |
| 23601 |
<indexterm> |
| 23602 |
<primary>umount</primary> |
| 23603 |
</indexterm> |
| 23604 |
<indexterm> |
| 23605 |
<primary>command</primary> |
| 23606 |
<secondary>umount</secondary> |
| 23607 |
</indexterm> |
| 23608 |
<listitem> |
| 23609 |
<para>Unmount a currently mounted filesystem. Before physically removing a |
| 23610 |
previously mounted floppy or CDROM disk, the device must be |
| 23611 |
<command>umount</command>ed, else filesystem corruption may result. |
| 23612 |
<programlisting>umount /mnt/cdrom |
| 23613 |
# You may now press the eject button and safely remove the disk.</programlisting></para> |
| 23614 |
|
| 23615 |
<note><para>The <command>automount</command> utility, if |
| 23616 |
properly installed, can mount and unmount floppies or |
| 23617 |
CDROM disks as they are accessed or removed. On |
| 23618 |
<quote>multispindle</quote> laptops with swappable |
| 23619 |
floppy and optical drives, this can cause problems, |
| 23620 |
however.</para></note> |
| 23621 |
|
| 23622 |
</listitem> |
| 23623 |
</varlistentry> |
| 23624 |
|
| 23625 |
|
| 23626 |
<varlistentry> |
| 23627 |
<term><anchor id="gnomemountref"><command>gnome-mount</command></term> |
| 23628 |
<indexterm> |
| 23629 |
<primary>gnome-mount</primary> |
| 23630 |
</indexterm> |
| 23631 |
<indexterm> |
| 23632 |
<primary>command</primary> |
| 23633 |
<secondary>mount</secondary> |
| 23634 |
</indexterm> |
| 23635 |
<listitem> |
| 23636 |
<para>The newer Linux distros have deprecated |
| 23637 |
<command>mount</command> and <command>umount</command>. |
| 23638 |
The successor, for command-line mounting of removable storage |
| 23639 |
devices, is <command>gnome-mount</command>. It can take the |
| 23640 |
<option>-d</option> option to mount a <link |
| 23641 |
linkend="devfileref">device file</link> by its listing in |
| 23642 |
<filename class="directory">/dev</filename>.</para> |
| 23643 |
|
| 23644 |
<para>For example, to mount a USB flash drive:</para> |
| 23645 |
|
| 23646 |
<para> |
| 23647 |
<screen><prompt>bash$ </prompt><userinput>gnome-mount -d /dev/sda1</userinput> |
| 23648 |
<computeroutput>gnome-mount 0.4</computeroutput> |
| 23649 |
|
| 23650 |
|
| 23651 |
<prompt>bash$ </prompt><userinput>df</userinput> |
| 23652 |
<computeroutput>. . . |
| 23653 |
/dev/sda1 63584 12034 51550 19% /media/disk</computeroutput> |
| 23654 |
</screen> |
| 23655 |
</para> |
| 23656 |
|
| 23657 |
|
| 23658 |
</listitem> |
| 23659 |
</varlistentry> |
| 23660 |
|
| 23661 |
<varlistentry> |
| 23662 |
<term><anchor id="syncref"><command>sync</command></term> |
| 23663 |
<indexterm> |
| 23664 |
<primary>sync</primary> |
| 23665 |
</indexterm> |
| 23666 |
<indexterm> |
| 23667 |
<primary>command</primary> |
| 23668 |
<secondary>sync</secondary> |
| 23669 |
</indexterm> |
| 23670 |
<listitem> |
| 23671 |
<para>Forces an immediate write of all updated data from |
| 23672 |
buffers to hard drive (synchronize drive |
| 23673 |
with buffers). While not strictly necessary, a |
| 23674 |
<command>sync</command> assures the sys admin or |
| 23675 |
user that the data just changed will survive a sudden |
| 23676 |
power failure. In the olden days, a <userinput>sync; |
| 23677 |
sync</userinput> (twice, just to make absolutely sure) was a |
| 23678 |
useful precautionary measure before a system reboot.</para> |
| 23679 |
<para>At times, you may wish to force an immediate buffer |
| 23680 |
flush, as when securely deleting a file (see <xref |
| 23681 |
linkend="blotout">) or when the lights begin to |
| 23682 |
flicker.</para> |
| 23683 |
</listitem> |
| 23684 |
</varlistentry> |
| 23685 |
|
| 23686 |
<varlistentry> |
| 23687 |
<term><anchor id="losetupref"><command>losetup</command></term> |
| 23688 |
<indexterm> |
| 23689 |
<primary>losetup</primary> |
| 23690 |
</indexterm> |
| 23691 |
<indexterm> |
| 23692 |
<primary>command</primary> |
| 23693 |
<secondary>losetup</secondary> |
| 23694 |
</indexterm> |
| 23695 |
<listitem> |
| 23696 |
<para>Sets up and configures <link linkend="loopbackref"> |
| 23697 |
loopback devices</link>.</para> |
| 23698 |
|
| 23699 |
<example id="createfs"> |
| 23700 |
<title>Creating a filesystem in a file</title> |
| 23701 |
<programlisting>SIZE=1000000 # 1 meg |
| 23702 |
|
| 23703 |
head -c $SIZE < /dev/zero > file # Set up file of designated size. |
| 23704 |
losetup /dev/loop0 file # Set it up as loopback device. |
| 23705 |
mke2fs /dev/loop0 # Create filesystem. |
| 23706 |
mount -o loop /dev/loop0 /mnt # Mount it. |
| 23707 |
|
| 23708 |
# Thanks, S.C.</programlisting> |
| 23709 |
</example> |
| 23710 |
|
| 23711 |
</listitem> |
| 23712 |
</varlistentry> |
| 23713 |
|
| 23714 |
|
| 23715 |
<varlistentry> |
| 23716 |
<term><anchor id="mkswapref"><command>mkswap</command></term> |
| 23717 |
<indexterm> |
| 23718 |
<primary>mkswap</primary> |
| 23719 |
</indexterm> |
| 23720 |
<indexterm> |
| 23721 |
<primary>command</primary> |
| 23722 |
<secondary>mkswap</secondary> |
| 23723 |
</indexterm> |
| 23724 |
<listitem> |
| 23725 |
<para>Creates a swap partition or file. The swap area must |
| 23726 |
subsequently be enabled with |
| 23727 |
<command>swapon</command>.</para> |
| 23728 |
</listitem> |
| 23729 |
</varlistentry> |
| 23730 |
|
| 23731 |
<varlistentry> |
| 23732 |
<term><anchor id="swaponref"><command>swapon</command></term> |
| 23733 |
<term><anchor id="swapoffref"><command>swapoff</command></term> |
| 23734 |
<indexterm> |
| 23735 |
<primary>swapon</primary> |
| 23736 |
</indexterm> |
| 23737 |
<indexterm> |
| 23738 |
<primary>command</primary> |
| 23739 |
<secondary>swapon</secondary> |
| 23740 |
</indexterm> |
| 23741 |
<indexterm> |
| 23742 |
<primary>swapoff</primary> |
| 23743 |
</indexterm> |
| 23744 |
<indexterm> |
| 23745 |
<primary>command</primary> |
| 23746 |
<secondary>swapoff</secondary> |
| 23747 |
</indexterm> |
| 23748 |
<listitem> |
| 23749 |
<para>Enable / disable swap partitition or file. |
| 23750 |
These commands usually take effect at bootup and |
| 23751 |
shutdown.</para> |
| 23752 |
</listitem> |
| 23753 |
</varlistentry> |
| 23754 |
|
| 23755 |
<varlistentry> |
| 23756 |
<term><anchor id="mke2fsref"><command>mke2fs</command></term> |
| 23757 |
<indexterm> |
| 23758 |
<primary>mke2fs</primary> |
| 23759 |
</indexterm> |
| 23760 |
<indexterm> |
| 23761 |
<primary>command</primary> |
| 23762 |
<secondary>mke2fs</secondary> |
| 23763 |
</indexterm> |
| 23764 |
<listitem> |
| 23765 |
<para>Create a Linux <firstterm>ext2</firstterm> |
| 23766 |
filesystem. This command must be invoked as |
| 23767 |
<firstterm>root</firstterm>.</para> |
| 23768 |
|
| 23769 |
|
| 23770 |
<example id="adddrv"> |
| 23771 |
<title>Adding a new hard drive</title> |
| 23772 |
<programlisting>&adddrv;</programlisting> |
| 23773 |
</example> |
| 23774 |
|
| 23775 |
<para>See also <xref linkend="createfs"> and <xref |
| 23776 |
linkend="ramdisk">.</para> |
| 23777 |
|
| 23778 |
</listitem> |
| 23779 |
</varlistentry> |
| 23780 |
|
| 23781 |
|
| 23782 |
<varlistentry> |
| 23783 |
<term><anchor id="mkdosfsref"><command>mkdosfs</command></term> |
| 23784 |
<indexterm> |
| 23785 |
<primary>mkdosfs</primary> |
| 23786 |
</indexterm> |
| 23787 |
<indexterm> |
| 23788 |
<primary>command</primary> |
| 23789 |
<secondary>mkdosfs</secondary> |
| 23790 |
</indexterm> |
| 23791 |
<listitem> |
| 23792 |
<para>Create a DOS <firstterm>FAT</firstterm> |
| 23793 |
filesystem.</para> |
| 23794 |
</listitem> |
| 23795 |
</varlistentry> |
| 23796 |
|
| 23797 |
|
| 23798 |
<varlistentry> |
| 23799 |
<term><anchor id="tune2fsref"><command>tune2fs</command></term> |
| 23800 |
<indexterm> |
| 23801 |
<primary>tune2fs</primary> |
| 23802 |
</indexterm> |
| 23803 |
<indexterm> |
| 23804 |
<primary>command</primary> |
| 23805 |
<secondary>tune2fs</secondary> |
| 23806 |
</indexterm> |
| 23807 |
<listitem> |
| 23808 |
<para>Tune <firstterm>ext2</firstterm> filesystem. May be |
| 23809 |
used to change filesystem parameters, such as maximum |
| 23810 |
mount count. This must be invoked as |
| 23811 |
<firstterm>root</firstterm>.</para> |
| 23812 |
|
| 23813 |
<warning><para>This is an extremely dangerous command. Use it at |
| 23814 |
your own risk, as you may inadvertently destroy your filesystem. |
| 23815 |
</para></warning> |
| 23816 |
|
| 23817 |
</listitem> |
| 23818 |
</varlistentry> |
| 23819 |
|
| 23820 |
<varlistentry> |
| 23821 |
<term><anchor id="dumpe2fsref"><command>dumpe2fs</command></term> |
| 23822 |
<indexterm> |
| 23823 |
<primary>dumpe2fs</primary> |
| 23824 |
</indexterm> |
| 23825 |
<indexterm> |
| 23826 |
<primary>command</primary> |
| 23827 |
<secondary>dumpe2fs</secondary> |
| 23828 |
</indexterm> |
| 23829 |
<listitem> |
| 23830 |
<para>Dump (list to <filename>stdout</filename>) very verbose |
| 23831 |
filesystem info. This must be invoked as |
| 23832 |
<firstterm>root</firstterm>.</para> |
| 23833 |
|
| 23834 |
<screen><prompt>root# </prompt><command>dumpe2fs /dev/hda7 | grep 'ount count'</command> |
| 23835 |
<computeroutput>dumpe2fs 1.19, 13-Jul-2000 for EXT2 FS 0.5b, 95/08/09 |
| 23836 |
Mount count: 6 |
| 23837 |
Maximum mount count: 20</computeroutput></screen> |
| 23838 |
|
| 23839 |
</listitem> |
| 23840 |
</varlistentry> |
| 23841 |
|
| 23842 |
<varlistentry> |
| 23843 |
<term><anchor id="hdparmref"><command>hdparm</command></term> |
| 23844 |
<indexterm> |
| 23845 |
<primary>hdparm</primary> |
| 23846 |
</indexterm> |
| 23847 |
<indexterm> |
| 23848 |
<primary>command</primary> |
| 23849 |
<secondary>hard disk parameters</secondary> |
| 23850 |
</indexterm> |
| 23851 |
<listitem> |
| 23852 |
<para>List or change hard disk parameters. This command must be |
| 23853 |
invoked as <firstterm>root</firstterm>, and it may be |
| 23854 |
dangerous if misused.</para> |
| 23855 |
</listitem> |
| 23856 |
</varlistentry> |
| 23857 |
|
| 23858 |
<varlistentry> |
| 23859 |
<term><anchor id="fdiskref"><command>fdisk</command></term> |
| 23860 |
<indexterm> |
| 23861 |
<primary>fdisk</primary> |
| 23862 |
</indexterm> |
| 23863 |
<indexterm> |
| 23864 |
<primary>command</primary> |
| 23865 |
<secondary>fdisk</secondary> |
| 23866 |
</indexterm> |
| 23867 |
<listitem> |
| 23868 |
<para>Create or change a partition table on a storage device, |
| 23869 |
usually a hard drive. This command must be invoked as |
| 23870 |
<firstterm>root</firstterm>.</para> |
| 23871 |
<warning><para>Use this command with extreme caution. If something |
| 23872 |
goes wrong, you may destroy an existing |
| 23873 |
filesystem.</para></warning> |
| 23874 |
</listitem> |
| 23875 |
</varlistentry> |
| 23876 |
|
| 23877 |
<varlistentry> |
| 23878 |
<term><anchor id="fsckref"><command>fsck</command></term> |
| 23879 |
<term><anchor id="e2fsckref"><command>e2fsck</command></term> |
| 23880 |
<term><anchor id="debugfsref"><command>debugfs</command></term> |
| 23881 |
<indexterm> |
| 23882 |
<primary>fsck</primary> |
| 23883 |
</indexterm> |
| 23884 |
<indexterm> |
| 23885 |
<primary>command</primary> |
| 23886 |
<secondary>fsck</secondary> |
| 23887 |
</indexterm> |
| 23888 |
<indexterm> |
| 23889 |
<primary>e2fsck</primary> |
| 23890 |
</indexterm> |
| 23891 |
<indexterm> |
| 23892 |
<primary>command</primary> |
| 23893 |
<secondary>e2fsck</secondary> |
| 23894 |
</indexterm> |
| 23895 |
<indexterm> |
| 23896 |
<primary>debugfs</primary> |
| 23897 |
</indexterm> |
| 23898 |
<indexterm> |
| 23899 |
<primary>command</primary> |
| 23900 |
<secondary>debugfs</secondary> |
| 23901 |
</indexterm> |
| 23902 |
<listitem> |
| 23903 |
|
| 23904 |
<para>Filesystem check, repair, and debug command set.</para> |
| 23905 |
|
| 23906 |
<para><command>fsck</command>: a front end for checking a UNIX |
| 23907 |
filesystem (may invoke other utilities). The actual |
| 23908 |
filesystem type generally defaults to |
| 23909 |
<firstterm>ext2</firstterm>.</para> |
| 23910 |
|
| 23911 |
<para><command>e2fsck</command>: ext2 filesystem checker.</para> |
| 23912 |
|
| 23913 |
<para><command>debugfs</command>: ext2 filesystem debugger. |
| 23914 |
One of the uses of this versatile, but dangerous command |
| 23915 |
is to (attempt to) recover deleted files. For advanced users |
| 23916 |
only!</para> |
| 23917 |
|
| 23918 |
<caution><para>All of these should be invoked as |
| 23919 |
<firstterm>root</firstterm>, and they can damage or destroy |
| 23920 |
a filesystem if misused.</para></caution> |
| 23921 |
|
| 23922 |
</listitem> |
| 23923 |
</varlistentry> |
| 23924 |
|
| 23925 |
<varlistentry> |
| 23926 |
<term><anchor id="badblocksref"><command>badblocks</command></term> |
| 23927 |
<indexterm> |
| 23928 |
<primary>badblocks</primary> |
| 23929 |
</indexterm> |
| 23930 |
<indexterm> |
| 23931 |
<primary>command</primary> |
| 23932 |
<secondary>badblocks</secondary> |
| 23933 |
</indexterm> |
| 23934 |
<listitem> |
| 23935 |
<para>Checks for bad blocks (physical media flaws) on a |
| 23936 |
storage device. This command finds use when formatting |
| 23937 |
a newly installed hard drive or testing the integrity |
| 23938 |
of backup media. |
| 23939 |
<footnote><para>The <option>-c</option> option to <link |
| 23940 |
linkend="mke2fsref">mke2fs</link> also invokes a check for bad |
| 23941 |
blocks.</para></footnote> |
| 23942 |
As an example, <command>badblocks /dev/fd0</command> |
| 23943 |
tests a floppy disk.</para> |
| 23944 |
|
| 23945 |
<para>The <command>badblocks</command> command |
| 23946 |
may be invoked destructively (overwrite all data) or |
| 23947 |
in non-destructive read-only mode. If <firstterm>root |
| 23948 |
user</firstterm> owns the device to be tested, as is |
| 23949 |
generally the case, then <firstterm>root</firstterm> |
| 23950 |
must invoke this command.</para> |
| 23951 |
</listitem> |
| 23952 |
</varlistentry> |
| 23953 |
|
| 23954 |
<varlistentry> |
| 23955 |
<term><anchor id="lsusbref"><command>lsusb</command></term> |
| 23956 |
<term><command>usbmodules</command></term> |
| 23957 |
<indexterm> |
| 23958 |
<primary>lsusb</primary> |
| 23959 |
</indexterm> |
| 23960 |
<indexterm> |
| 23961 |
<primary>command</primary> |
| 23962 |
<secondary>usb</secondary> |
| 23963 |
</indexterm> |
| 23964 |
<indexterm> |
| 23965 |
<primary>usbmodules</primary> |
| 23966 |
</indexterm> |
| 23967 |
<indexterm> |
| 23968 |
<primary>command</primary> |
| 23969 |
<secondary>usb</secondary> |
| 23970 |
</indexterm> |
| 23971 |
<listitem> |
| 23972 |
|
| 23973 |
<para>The <command>lsusb</command> command lists all USB |
| 23974 |
(Universal Serial Bus) buses and the devices hooked up to |
| 23975 |
them.</para> |
| 23976 |
|
| 23977 |
<para>The <command>usbmodules</command> command outputs |
| 23978 |
information about the driver modules for connected USB |
| 23979 |
devices.</para> |
| 23980 |
|
| 23981 |
<para> |
| 23982 |
<screen><prompt>bash$ </prompt><userinput>lsusb</userinput> |
| 23983 |
<computeroutput>Bus 001 Device 001: ID 0000:0000 |
| 23984 |
Device Descriptor: |
| 23985 |
bLength 18 |
| 23986 |
bDescriptorType 1 |
| 23987 |
bcdUSB 1.00 |
| 23988 |
bDeviceClass 9 Hub |
| 23989 |
bDeviceSubClass 0 |
| 23990 |
bDeviceProtocol 0 |
| 23991 |
bMaxPacketSize0 8 |
| 23992 |
idVendor 0x0000 |
| 23993 |
idProduct 0x0000 |
| 23994 |
|
| 23995 |
. . .</computeroutput> |
| 23996 |
</screen> |
| 23997 |
</para> |
| 23998 |
|
| 23999 |
</listitem> |
| 24000 |
</varlistentry> |
| 24001 |
|
| 24002 |
<varlistentry> |
| 24003 |
<term><anchor id="lspciref"><command>lspci</command></term> |
| 24004 |
<indexterm> |
| 24005 |
<primary>lspci</primary> |
| 24006 |
</indexterm> |
| 24007 |
<indexterm> |
| 24008 |
<primary>command</primary> |
| 24009 |
<secondary>pci</secondary> |
| 24010 |
</indexterm> |
| 24011 |
<listitem> |
| 24012 |
<para>Lists <firstterm>pci</firstterm> busses present.</para> |
| 24013 |
|
| 24014 |
<para> |
| 24015 |
<screen><prompt>bash$ </prompt><userinput>lspci</userinput> |
| 24016 |
<computeroutput>00:00.0 Host bridge: Intel Corporation 82845 845 |
| 24017 |
(Brookdale) Chipset Host Bridge (rev 04) |
| 24018 |
00:01.0 PCI bridge: Intel Corporation 82845 845 |
| 24019 |
(Brookdale) Chipset AGP Bridge (rev 04) |
| 24020 |
00:1d.0 USB Controller: Intel Corporation 82801CA/CAM USB (Hub #1) (rev 02) |
| 24021 |
00:1d.1 USB Controller: Intel Corporation 82801CA/CAM USB (Hub #2) (rev 02) |
| 24022 |
00:1d.2 USB Controller: Intel Corporation 82801CA/CAM USB (Hub #3) (rev 02) |
| 24023 |
00:1e.0 PCI bridge: Intel Corporation 82801 Mobile PCI Bridge (rev 42) |
| 24024 |
|
| 24025 |
. . .</computeroutput> |
| 24026 |
</screen> |
| 24027 |
</para> |
| 24028 |
|
| 24029 |
</listitem> |
| 24030 |
</varlistentry> |
| 24031 |
|
| 24032 |
|
| 24033 |
|
| 24034 |
<varlistentry> |
| 24035 |
<term><anchor id="mkbootdiskref"><command>mkbootdisk</command></term> |
| 24036 |
<indexterm> |
| 24037 |
<primary>mkbootdisk</primary> |
| 24038 |
</indexterm> |
| 24039 |
<indexterm> |
| 24040 |
<primary>command</primary> |
| 24041 |
<secondary>bootdisk</secondary> |
| 24042 |
</indexterm> |
| 24043 |
<listitem> |
| 24044 |
<para>Creates a boot floppy which can be used to bring up the |
| 24045 |
system if, for example, the MBR (master boot record) becomes |
| 24046 |
corrupted. Of special interest is the <option>--iso</option> |
| 24047 |
option, which uses <command>mkisofs</command> to create a |
| 24048 |
bootable <firstterm>ISO9660</firstterm> filesystem image |
| 24049 |
suitable for burning a bootable CDR.</para> |
| 24050 |
<para>The <command>mkbootdisk</command> command is actually |
| 24051 |
a Bash script, written by Erik Troan, in the <filename |
| 24052 |
class="directory">/sbin</filename> directory.</para> |
| 24053 |
|
| 24054 |
</listitem> |
| 24055 |
</varlistentry> |
| 24056 |
|
| 24057 |
<varlistentry> |
| 24058 |
<term><anchor id="mkisofsref"><command>mkisofs</command></term> |
| 24059 |
<indexterm> |
| 24060 |
<primary>mkisofs</primary> |
| 24061 |
</indexterm> |
| 24062 |
<indexterm> |
| 24063 |
<primary>command</primary> |
| 24064 |
<secondary>ISO9660</secondary> |
| 24065 |
</indexterm> |
| 24066 |
<listitem> |
| 24067 |
<para>Creates an <firstterm>ISO9660</firstterm> filesystem |
| 24068 |
suitable for a CDR image.</para> |
| 24069 |
</listitem> |
| 24070 |
</varlistentry> |
| 24071 |
|
| 24072 |
<varlistentry> |
| 24073 |
<term><anchor id="chrootref"><command>chroot</command></term> |
| 24074 |
<indexterm> |
| 24075 |
<primary>chroot</primary> |
| 24076 |
</indexterm> |
| 24077 |
<indexterm> |
| 24078 |
<primary>command</primary> |
| 24079 |
<secondary>chroot</secondary> |
| 24080 |
</indexterm> |
| 24081 |
<indexterm> |
| 24082 |
<primary>directory</primary> |
| 24083 |
<secondary>root</secondary> |
| 24084 |
<tertiary>change</tertiary> |
| 24085 |
</indexterm> |
| 24086 |
<listitem> |
| 24087 |
<para>CHange ROOT directory. Normally commands are fetched |
| 24088 |
from <link linkend="pathref">$PATH</link>, relative to |
| 24089 |
<filename class="directory">/</filename>, the default |
| 24090 |
<firstterm>root |
| 24091 |
directory</firstterm>. This changes the |
| 24092 |
<firstterm>root</firstterm> directory to a different one |
| 24093 |
(and also changes the working directory to there). This is |
| 24094 |
useful for security purposes, for instance when the system |
| 24095 |
administrator wishes to restrict certain users, such as |
| 24096 |
those <link linkend="telnetref">telnetting</link> in, |
| 24097 |
to a secured portion of the filesystem (this is sometimes |
| 24098 |
referred to as confining a guest user to a <quote>chroot |
| 24099 |
jail</quote>). Note that after a <command>chroot</command>, |
| 24100 |
the execution path for system binaries is no longer |
| 24101 |
valid.</para> |
| 24102 |
|
| 24103 |
<para>A <userinput>chroot /opt</userinput> would cause |
| 24104 |
references to <filename |
| 24105 |
class="directory">/usr/bin</filename> |
| 24106 |
to be translated to <filename |
| 24107 |
class="directory">/opt/usr/bin</filename>. Likewise, |
| 24108 |
<userinput>chroot /aaa/bbb /bin/ls</userinput> would |
| 24109 |
redirect future instances of <command>ls</command> |
| 24110 |
to <filename>/aaa/bbb</filename> as the base directory, |
| 24111 |
rather than <filename class="directory">/</filename> as is |
| 24112 |
normally the case. An <command>alias XX 'chroot /aaa/bbb |
| 24113 |
ls'</command> in a user's <link |
| 24114 |
linkend="sample-bashrc"><filename>~/.bashrc</filename></link> |
| 24115 |
effectively restricts which portion of the filesystem |
| 24116 |
she may run command <quote>XX</quote> on.</para> |
| 24117 |
|
| 24118 |
|
| 24119 |
<para>The <command>chroot</command> command is also handy |
| 24120 |
when running from an emergency boot floppy |
| 24121 |
(<command>chroot</command> to <filename>/dev/fd0</filename>), |
| 24122 |
or as an option to <command>lilo</command> when recovering |
| 24123 |
from a system crash. Other uses include installation from a |
| 24124 |
different filesystem (an <link linkend="rpmref">rpm</link> |
| 24125 |
option) or running a readonly filesystem from a CD ROM. |
| 24126 |
Invoke only as <firstterm>root</firstterm>, and use with |
| 24127 |
care.</para> |
| 24128 |
|
| 24129 |
<caution><para>It might be necessary to copy certain system |
| 24130 |
files to a <firstterm>chrooted</firstterm> directory, |
| 24131 |
since the normal <varname>$PATH</varname> can no longer |
| 24132 |
be relied upon.</para></caution> |
| 24133 |
|
| 24134 |
</listitem> |
| 24135 |
</varlistentry> |
| 24136 |
|
| 24137 |
<varlistentry> |
| 24138 |
<term><anchor id="lockfileref"><command>lockfile</command></term> |
| 24139 |
<indexterm> |
| 24140 |
<primary>lockfile</primary> |
| 24141 |
</indexterm> |
| 24142 |
<indexterm> |
| 24143 |
<primary>command</primary> |
| 24144 |
<secondary>lockfile</secondary> |
| 24145 |
</indexterm> |
| 24146 |
<listitem> |
| 24147 |
<para>This utility is part of the <command>procmail</command> |
| 24148 |
package (<ulink url="http://www.procmail.org">www.procmail.org</ulink>). |
| 24149 |
It creates a <firstterm>lock file</firstterm>, a |
| 24150 |
<firstterm>semaphore</firstterm> that controls access to |
| 24151 |
a file, device, or resource.</para> |
| 24152 |
|
| 24153 |
<sidebar><para><anchor id="semaphoreref"> |
| 24154 |
<userinput>Definition:</userinput> |
| 24155 |
A <firstterm>semaphore</firstterm> is a flag or |
| 24156 |
signal. (The usage originated in railroading, where a |
| 24157 |
colored flag, lantern, or striped movable arm |
| 24158 |
<firstterm>semaphore</firstterm> indicated whether a |
| 24159 |
particular track was in use and therefore unavailable |
| 24160 |
for another train.) A UNIX process can check the |
| 24161 |
appropriate semaphore to determine whether a particular |
| 24162 |
resource is available/accessible.</para></sidebar> |
| 24163 |
|
| 24164 |
<para>The lock file serves as a flag that this particular |
| 24165 |
file, device, or resource is in use by a process (and |
| 24166 |
is therefore <quote>busy</quote>). The presence of a |
| 24167 |
lock file permits only restricted access (or no access) |
| 24168 |
to other processes.</para> |
| 24169 |
|
| 24170 |
<para><programlisting>lockfile /home/bozo/lockfiles/$0.lock |
| 24171 |
# Creates a write-protected lockfile prefixed with the name of the script. |
| 24172 |
|
| 24173 |
lockfile /home/bozo/lockfiles/${0##*/}.lock |
| 24174 |
# A safer version of the above, as pointed out by E. Choroba.</programlisting></para> |
| 24175 |
|
| 24176 |
<para>Lock files are used in such applications as protecting |
| 24177 |
system mail folders from simultaneously being changed |
| 24178 |
by multiple users, indicating that a modem port |
| 24179 |
is being accessed, and showing that an instance of |
| 24180 |
<application>Firefox</application> is using its cache. |
| 24181 |
Scripts may check for the existence of a lock file created |
| 24182 |
by a certain process to check if that process is running. |
| 24183 |
Note that if a script attempts to create a lock file that |
| 24184 |
already exists, the script will likely hang.</para> |
| 24185 |
|
| 24186 |
<para>Normally, applications create and check for lock files |
| 24187 |
in the <filename class="directory">/var/lock</filename> |
| 24188 |
directory. |
| 24189 |
<footnote><para>Since only <firstterm>root</firstterm> |
| 24190 |
has write permission in the <filename |
| 24191 |
class="directory">/var/lock</filename> directory, |
| 24192 |
a user script cannot set a lock file there.</para></footnote> |
| 24193 |
A script can test for the presence of a lock file by |
| 24194 |
something like the following. |
| 24195 |
<programlisting>appname=xyzip |
| 24196 |
# Application "xyzip" created lock file "/var/lock/xyzip.lock". |
| 24197 |
|
| 24198 |
if [ -e "/var/lock/$appname.lock" ] |
| 24199 |
then #+ Prevent other programs & scripts |
| 24200 |
# from accessing files/resources used by xyzip. |
| 24201 |
...</programlisting></para> |
| 24202 |
|
| 24203 |
</listitem> |
| 24204 |
</varlistentry> |
| 24205 |
|
| 24206 |
<varlistentry> |
| 24207 |
<term><anchor id="flockref"><command>flock</command></term> |
| 24208 |
<indexterm> |
| 24209 |
<primary>flock</primary> |
| 24210 |
</indexterm> |
| 24211 |
<indexterm> |
| 24212 |
<primary>command</primary> |
| 24213 |
<secondary>lock file</secondary> |
| 24214 |
</indexterm> |
| 24215 |
<listitem> |
| 24216 |
|
| 24217 |
<para>Much less useful than the <command>lockfile</command> |
| 24218 |
command is <command>flock</command>. It sets an |
| 24219 |
<quote>advisory</quote> lock on a file and then executes |
| 24220 |
a command while the lock is on. This is to prevent |
| 24221 |
any other process from setting a lock on that file until |
| 24222 |
completion of the specified command.</para> |
| 24223 |
|
| 24224 |
<para><programlisting>flock $0 cat $0 > lockfile__$0 |
| 24225 |
# Set a lock on the script the above line appears in, |
| 24226 |
#+ while listing the script to stdout.</programlisting></para> |
| 24227 |
|
| 24228 |
<note><para>Unlike <command>lockfile</command>, |
| 24229 |
<command>flock</command> does <emphasis>not</emphasis> |
| 24230 |
automatically create a lock file.</para></note> |
| 24231 |
|
| 24232 |
</listitem> |
| 24233 |
</varlistentry> |
| 24234 |
|
| 24235 |
<varlistentry> |
| 24236 |
<term><anchor id="mknodref"><command>mknod</command></term> |
| 24237 |
<indexterm> |
| 24238 |
<primary>mknod</primary> |
| 24239 |
</indexterm> |
| 24240 |
<indexterm> |
| 24241 |
<primary>command</primary> |
| 24242 |
<secondary>mknod</secondary> |
| 24243 |
</indexterm> |
| 24244 |
<listitem> |
| 24245 |
<para>Creates block or character <link |
| 24246 |
linkend="devfileref">device files</link> (may be |
| 24247 |
necessary when installing new hardware on the system). The |
| 24248 |
<command>MAKEDEV</command> utility has virtually |
| 24249 |
all of the functionality of <command>mknod</command>, |
| 24250 |
and is easier to use.</para> |
| 24251 |
</listitem> |
| 24252 |
</varlistentry> |
| 24253 |
|
| 24254 |
<varlistentry> |
| 24255 |
<term><anchor id="MAKEDEVref"><command>MAKEDEV</command></term> |
| 24256 |
<indexterm> |
| 24257 |
<primary>MAKEDEV</primary> |
| 24258 |
</indexterm> |
| 24259 |
<indexterm> |
| 24260 |
<primary>command</primary> |
| 24261 |
<secondary>make device file</secondary> |
| 24262 |
</indexterm> |
| 24263 |
<listitem> |
| 24264 |
|
| 24265 |
<para>Utility for creating device files. It must be run as |
| 24266 |
<firstterm>root</firstterm>, and in the <filename |
| 24267 |
class="directory">/dev</filename> directory. It is a sort |
| 24268 |
of advanced version of <command>mknod</command>.</para> |
| 24269 |
|
| 24270 |
</listitem> |
| 24271 |
</varlistentry> |
| 24272 |
|
| 24273 |
<varlistentry> |
| 24274 |
<term><anchor id="tmpwatchref"><command>tmpwatch</command></term> |
| 24275 |
<indexterm> |
| 24276 |
<primary>tmpwatch</primary> |
| 24277 |
</indexterm> |
| 24278 |
<indexterm> |
| 24279 |
<primary>command</primary> |
| 24280 |
<secondary>tmpwatch</secondary> |
| 24281 |
</indexterm> |
| 24282 |
<listitem> |
| 24283 |
<para>Automatically deletes files which have not been accessed |
| 24284 |
within a specified period of time. Usually invoked by |
| 24285 |
<link linkend="cronref">cron</link> to remove stale log |
| 24286 |
files.</para> |
| 24287 |
</listitem> |
| 24288 |
</varlistentry> |
| 24289 |
|
| 24290 |
</variablelist> |
| 24291 |
|
| 24292 |
|
| 24293 |
<variablelist id="periphsys"> |
| 24294 |
<title><anchor id="periphsys1">Backup</title> |
| 24295 |
|
| 24296 |
<varlistentry> |
| 24297 |
<term><anchor id="dumpref"><command>dump</command></term> |
| 24298 |
<term><anchor id="restoreref"><command>restore</command></term> |
| 24299 |
<indexterm> |
| 24300 |
<primary>dump</primary> |
| 24301 |
</indexterm> |
| 24302 |
<indexterm> |
| 24303 |
<primary>command</primary> |
| 24304 |
<secondary>dump</secondary> |
| 24305 |
</indexterm> |
| 24306 |
<indexterm> |
| 24307 |
<primary>restore</primary> |
| 24308 |
</indexterm> |
| 24309 |
<indexterm> |
| 24310 |
<primary>command</primary> |
| 24311 |
<secondary>restore</secondary> |
| 24312 |
</indexterm> |
| 24313 |
<listitem> |
| 24314 |
<para>The <command>dump</command> command is an elaborate |
| 24315 |
filesystem backup utility, generally used on larger |
| 24316 |
installations and networks. |
| 24317 |
<footnote><para>Operators of single-user Linux systems |
| 24318 |
generally prefer something simpler for backups, such |
| 24319 |
as <command>tar</command>.</para></footnote> |
| 24320 |
It reads raw disk partitions and writes a backup file |
| 24321 |
in a binary format. Files to be backed up may be saved |
| 24322 |
to a variety of storage media, including disks and tape |
| 24323 |
drives. The <command>restore</command> command restores |
| 24324 |
backups made with <command>dump</command>.</para> |
| 24325 |
|
| 24326 |
</listitem> |
| 24327 |
</varlistentry> |
| 24328 |
|
| 24329 |
<varlistentry> |
| 24330 |
<term><anchor id="fdformatref"><command>fdformat</command></term> |
| 24331 |
<indexterm> |
| 24332 |
<primary>fdformat</primary> |
| 24333 |
</indexterm> |
| 24334 |
<indexterm> |
| 24335 |
<primary>command</primary> |
| 24336 |
<secondary>floppy</secondary> |
| 24337 |
</indexterm> |
| 24338 |
<listitem> |
| 24339 |
<para>Perform a low-level format on a floppy disk |
| 24340 |
(<filename>/dev/fd0*</filename>).</para> |
| 24341 |
</listitem> |
| 24342 |
</varlistentry> |
| 24343 |
|
| 24344 |
</variablelist> |
| 24345 |
|
| 24346 |
|
| 24347 |
<variablelist id="sysresources"> |
| 24348 |
<title><anchor id="sysresources1">System Resources</title> |
| 24349 |
|
| 24350 |
<varlistentry> |
| 24351 |
<term><anchor id="ulimitref"><command>ulimit</command></term> |
| 24352 |
<indexterm> |
| 24353 |
<primary>ulimit</primary> |
| 24354 |
</indexterm> |
| 24355 |
<indexterm> |
| 24356 |
<primary>command</primary> |
| 24357 |
<secondary>ulimit</secondary> |
| 24358 |
</indexterm> |
| 24359 |
<listitem> |
| 24360 |
|
| 24361 |
<para>Sets an <firstterm>upper limit</firstterm> on use |
| 24362 |
of system resources. Usually invoked with the |
| 24363 |
<option>-f</option> option, which sets a limit on file size |
| 24364 |
(<command>ulimit -f 1000</command> limits files to 1 meg |
| 24365 |
maximum). |
| 24366 |
<footnote><para>As of the <link linkend="bash4ref">version |
| 24367 |
4 update</link> of Bash, the <option>-f</option> |
| 24368 |
and <option>-c</option> options take a block size |
| 24369 |
of 512 when in <link linkend="posix2ref">POSIX</link> |
| 24370 |
mode. Additionally, there are two new options: |
| 24371 |
<option>-b</option> for <link |
| 24372 |
linkend="socketref">socket</link> buffer size, and |
| 24373 |
<option>-T</option> for the limit on the number of |
| 24374 |
<firstterm>threads</firstterm>.</para></footnote> |
| 24375 |
The <option>-t</option> option limits the coredump |
| 24376 |
size (<command>ulimit -c 0</command> eliminates coredumps). |
| 24377 |
Normally, the value of <command>ulimit</command> |
| 24378 |
would be set in <filename>/etc/profile</filename> |
| 24379 |
and/or <filename>~/.bash_profile</filename> (see <xref |
| 24380 |
linkend="files">).</para> |
| 24381 |
|
| 24382 |
<important> |
| 24383 |
|
| 24384 |
<para>Judicious use of <command>ulimit</command> can |
| 24385 |
protect a system against the dreaded <firstterm>fork |
| 24386 |
bomb</firstterm>.</para> |
| 24387 |
|
| 24388 |
<para> |
| 24389 |
<programlisting>#!/bin/bash |
| 24390 |
# This script is for illustrative purposes only. |
| 24391 |
# Run it at your own peril -- it WILL freeze your system. |
| 24392 |
|
| 24393 |
while true # Endless loop. |
| 24394 |
do |
| 24395 |
$0 & # This script invokes itself . . . |
| 24396 |
#+ forks an infinite number of times . . . |
| 24397 |
#+ until the system freezes up because all resources exhausted. |
| 24398 |
done # This is the notorious <quote>sorcerer's appentice</quote> scenario. |
| 24399 |
|
| 24400 |
exit 0 # Will not exit here, because this script will never terminate.</programlisting> |
| 24401 |
</para> |
| 24402 |
|
| 24403 |
<para>A <command>ulimit -Hu XX</command> (where |
| 24404 |
<emphasis>XX</emphasis> is the user process limit) in |
| 24405 |
<filename>/etc/profile</filename> would abort |
| 24406 |
this script when it exceeded the preset limit. |
| 24407 |
</para> |
| 24408 |
|
| 24409 |
</important> |
| 24410 |
|
| 24411 |
</listitem> |
| 24412 |
</varlistentry> |
| 24413 |
|
| 24414 |
<varlistentry> |
| 24415 |
<term><anchor id="quotaref"><command>quota</command></term> |
| 24416 |
<indexterm> |
| 24417 |
<primary>quota</primary> |
| 24418 |
</indexterm> |
| 24419 |
<indexterm> |
| 24420 |
<primary>command</primary> |
| 24421 |
<secondary>quota</secondary> |
| 24422 |
</indexterm> |
| 24423 |
<listitem> |
| 24424 |
<para>Display user or group disk quotas.</para> |
| 24425 |
</listitem> |
| 24426 |
</varlistentry> |
| 24427 |
|
| 24428 |
<varlistentry> |
| 24429 |
<term><anchor id="setquotaref"><command>setquota</command></term> |
| 24430 |
<indexterm> |
| 24431 |
<primary>setquota</primary> |
| 24432 |
</indexterm> |
| 24433 |
<indexterm> |
| 24434 |
<primary>command</primary> |
| 24435 |
<secondary>quota</secondary> |
| 24436 |
</indexterm> |
| 24437 |
<listitem> |
| 24438 |
<para>Set user or group disk quotas from the command-line.</para> |
| 24439 |
</listitem> |
| 24440 |
</varlistentry> |
| 24441 |
|
| 24442 |
<varlistentry> |
| 24443 |
<indexterm> |
| 24444 |
<primary>umask</primary> |
| 24445 |
</indexterm> |
| 24446 |
<indexterm> |
| 24447 |
<primary>command</primary> |
| 24448 |
<secondary>umask</secondary> |
| 24449 |
</indexterm> |
| 24450 |
<term><anchor id="umaskref"><command>umask</command></term> |
| 24451 |
<listitem> |
| 24452 |
<para>User file creation permissions |
| 24453 |
<firstterm>mask</firstterm>. Limit the default file |
| 24454 |
attributes for a particular user. All files created |
| 24455 |
by that user take on the attributes specified by |
| 24456 |
<command>umask</command>. The (octal) value passed to |
| 24457 |
<command>umask</command> defines the file permissions |
| 24458 |
<firstterm>disabled</firstterm>. For example, <command>umask |
| 24459 |
022</command> ensures that new files will have at most |
| 24460 |
755 permissions (777 NAND 022). |
| 24461 |
|
| 24462 |
<footnote><para>NAND is the logical |
| 24463 |
<firstterm>not-and</firstterm> operator. Its effect |
| 24464 |
is somewhat similar to subtraction.</para></footnote> |
| 24465 |
|
| 24466 |
Of course, the user may later change the |
| 24467 |
attributes of particular files with <link |
| 24468 |
linkend="chmodref">chmod</link>. The usual practice |
| 24469 |
is to set the value of <command>umask</command> |
| 24470 |
in <filename>/etc/profile</filename> and/or |
| 24471 |
<filename>~/.bash_profile</filename> (see <xref |
| 24472 |
linkend="files">).</para> |
| 24473 |
|
| 24474 |
<example id="rot13a"> |
| 24475 |
<title>Using <firstterm>umask</firstterm> to hide an output file |
| 24476 |
from prying eyes</title> |
| 24477 |
<programlisting>&rot13a;</programlisting> |
| 24478 |
</example> |
| 24479 |
|
| 24480 |
</listitem> |
| 24481 |
</varlistentry> |
| 24482 |
|
| 24483 |
<varlistentry> |
| 24484 |
<term><anchor id="rdevref"><command>rdev</command></term> |
| 24485 |
<indexterm> |
| 24486 |
<primary>rdev</primary> |
| 24487 |
</indexterm> |
| 24488 |
<indexterm> |
| 24489 |
<primary>command</primary> |
| 24490 |
<secondary>rdev</secondary> |
| 24491 |
</indexterm> |
| 24492 |
<listitem> |
| 24493 |
<para>Get info about or make changes to root device, swap space, or video |
| 24494 |
mode. The functionality of <command>rdev</command> has generally been taken over by |
| 24495 |
<command>lilo</command>, but <command>rdev</command> remains |
| 24496 |
useful for setting up a ram disk. This is a dangerous command, if misused. |
| 24497 |
</para> |
| 24498 |
</listitem> |
| 24499 |
</varlistentry> |
| 24500 |
|
| 24501 |
</variablelist> |
| 24502 |
|
| 24503 |
|
| 24504 |
<variablelist id="modulessys"> |
| 24505 |
<title><anchor id="modulessys1">Modules</title> |
| 24506 |
|
| 24507 |
<varlistentry> |
| 24508 |
<term><anchor id="lsmodref"><command>lsmod</command></term> |
| 24509 |
<indexterm> |
| 24510 |
<primary>lsmod</primary> |
| 24511 |
</indexterm> |
| 24512 |
<indexterm> |
| 24513 |
<primary>command</primary> |
| 24514 |
<secondary>loadable modules</secondary> |
| 24515 |
</indexterm> |
| 24516 |
<listitem> |
| 24517 |
<para>List installed kernel modules.</para> |
| 24518 |
<para> |
| 24519 |
<screen><prompt>bash$ </prompt><userinput>lsmod</userinput> |
| 24520 |
<computeroutput>Module Size Used by |
| 24521 |
autofs 9456 2 (autoclean) |
| 24522 |
opl3 11376 0 |
| 24523 |
serial_cs 5456 0 (unused) |
| 24524 |
sb 34752 0 |
| 24525 |
uart401 6384 0 [sb] |
| 24526 |
sound 58368 0 [opl3 sb uart401] |
| 24527 |
soundlow 464 0 [sound] |
| 24528 |
soundcore 2800 6 [sb sound] |
| 24529 |
ds 6448 2 [serial_cs] |
| 24530 |
i82365 22928 2 |
| 24531 |
pcmcia_core 45984 0 [serial_cs ds i82365]</computeroutput> |
| 24532 |
</screen> |
| 24533 |
</para> |
| 24534 |
<note><para>Doing a <command>cat /proc/modules</command> gives the |
| 24535 |
same information.</para></note> |
| 24536 |
</listitem> |
| 24537 |
</varlistentry> |
| 24538 |
|
| 24539 |
<varlistentry> |
| 24540 |
<term><anchor id="insmodref"><command>insmod</command></term> |
| 24541 |
<indexterm> |
| 24542 |
<primary>insmod</primary> |
| 24543 |
</indexterm> |
| 24544 |
<indexterm> |
| 24545 |
<primary>command</primary> |
| 24546 |
<secondary>loadable modules</secondary> |
| 24547 |
</indexterm> |
| 24548 |
<listitem> |
| 24549 |
<para>Force installation of a kernel module (use |
| 24550 |
<command>modprobe</command> instead, when possible). Must |
| 24551 |
be invoked as <firstterm>root</firstterm>.</para> |
| 24552 |
</listitem> |
| 24553 |
</varlistentry> |
| 24554 |
|
| 24555 |
<varlistentry> |
| 24556 |
<term><anchor id="rmmodref"><command>rmmod</command></term> |
| 24557 |
<indexterm> |
| 24558 |
<primary>rmmod</primary> |
| 24559 |
</indexterm> |
| 24560 |
<indexterm> |
| 24561 |
<primary>command</primary> |
| 24562 |
<secondary>loadable modules</secondary> |
| 24563 |
</indexterm> |
| 24564 |
<listitem> |
| 24565 |
<para>Force unloading of a kernel module. Must be invoked |
| 24566 |
as <firstterm>root</firstterm>.</para> |
| 24567 |
</listitem> |
| 24568 |
</varlistentry> |
| 24569 |
|
| 24570 |
<varlistentry> |
| 24571 |
<term><anchor id="modproberef"><command>modprobe</command></term> |
| 24572 |
<indexterm> |
| 24573 |
<primary>modprobe</primary> |
| 24574 |
</indexterm> |
| 24575 |
<indexterm> |
| 24576 |
<primary>command</primary> |
| 24577 |
<secondary>loadable modules</secondary> |
| 24578 |
</indexterm> |
| 24579 |
<listitem> |
| 24580 |
<para>Module loader that is normally invoked automatically |
| 24581 |
in a startup script. Must be invoked as |
| 24582 |
<firstterm>root</firstterm>.</para> |
| 24583 |
</listitem> |
| 24584 |
</varlistentry> |
| 24585 |
|
| 24586 |
<varlistentry> |
| 24587 |
<term><anchor id="depmodref"><command>depmod</command></term> |
| 24588 |
<indexterm> |
| 24589 |
<primary>depmod</primary> |
| 24590 |
</indexterm> |
| 24591 |
<indexterm> |
| 24592 |
<primary>command</primary> |
| 24593 |
<secondary>loadable modules</secondary> |
| 24594 |
</indexterm> |
| 24595 |
<listitem> |
| 24596 |
<para>Creates module dependency file. Usually invoked from a |
| 24597 |
startup script.</para> |
| 24598 |
</listitem> |
| 24599 |
</varlistentry> |
| 24600 |
|
| 24601 |
<varlistentry> |
| 24602 |
<term><anchor id="modinforef"><command>modinfo</command></term> |
| 24603 |
<indexterm> |
| 24604 |
<primary>modinfo</primary> |
| 24605 |
</indexterm> |
| 24606 |
<indexterm> |
| 24607 |
<primary>command</primary> |
| 24608 |
<secondary>loadable modules</secondary> |
| 24609 |
</indexterm> |
| 24610 |
<listitem> |
| 24611 |
|
| 24612 |
<para>Output information about a loadable module.</para> |
| 24613 |
|
| 24614 |
<screen><prompt>bash$ </prompt><userinput>modinfo hid</userinput> |
| 24615 |
<computeroutput>filename: /lib/modules/2.4.20-6/kernel/drivers/usb/hid.o |
| 24616 |
description: "USB HID support drivers" |
| 24617 |
author: "Andreas Gal, Vojtech Pavlik <[email protected]>" |
| 24618 |
license: "GPL"</computeroutput> |
| 24619 |
</screen> |
| 24620 |
</listitem> |
| 24621 |
</varlistentry> |
| 24622 |
|
| 24623 |
</variablelist> |
| 24624 |
|
| 24625 |
|
| 24626 |
<variablelist id="miscsys"> |
| 24627 |
<title><anchor id="miscsys1">Miscellaneous</title> |
| 24628 |
|
| 24629 |
<varlistentry> |
| 24630 |
<term><anchor id="envvref"><command>env</command></term> |
| 24631 |
<indexterm> |
| 24632 |
<primary>env</primary> |
| 24633 |
</indexterm> |
| 24634 |
<indexterm> |
| 24635 |
<primary>command</primary> |
| 24636 |
<secondary>env</secondary> |
| 24637 |
</indexterm> |
| 24638 |
<listitem> |
| 24639 |
<para> |
| 24640 |
Runs a program or script with certain <link |
| 24641 |
linkend="envref">environmental variables</link> |
| 24642 |
set or changed (without changing the overall system |
| 24643 |
environment). The <option>[varname=xxx]</option> |
| 24644 |
permits changing the environmental variable |
| 24645 |
<varname>varname</varname> for the duration of the |
| 24646 |
script. With no options specified, this command lists all |
| 24647 |
the environmental variable settings. |
| 24648 |
|
| 24649 |
<footnote><para>In Bash and other Bourne shell derivatives, it is |
| 24650 |
possible to set variables in a single command's environment. |
| 24651 |
<programlisting>var1=value1 var2=value2 commandXXX |
| 24652 |
# $var1 and $var2 set in the environment of 'commandXXX' only.</programlisting> |
| 24653 |
</para></footnote> |
| 24654 |
</para> |
| 24655 |
|
| 24656 |
<note> |
| 24657 |
<para><anchor id="envv2ref">The first line of a script (the |
| 24658 |
<quote>sha-bang</quote> line) may use <command>env</command> |
| 24659 |
when the path to the shell or interpreter is unknown.</para> |
| 24660 |
|
| 24661 |
<para><programlisting>#! /usr/bin/env perl |
| 24662 |
|
| 24663 |
print "This Perl script will run,\n"; |
| 24664 |
print "even when I don't know where to find Perl.\n"; |
| 24665 |
|
| 24666 |
# Good for portable cross-platform scripts, |
| 24667 |
# where the Perl binaries may not be in the expected place. |
| 24668 |
# Thanks, S.C.</programlisting></para> |
| 24669 |
|
| 24670 |
<para>Or even ... </para> |
| 24671 |
<para><programlisting>#!/bin/env bash |
| 24672 |
# Queries the $PATH enviromental variable for the location of bash. |
| 24673 |
# Therefore ... |
| 24674 |
# This script will run where Bash is not in its usual place, in /bin. |
| 24675 |
...</programlisting></para> |
| 24676 |
|
| 24677 |
</note> |
| 24678 |
|
| 24679 |
</listitem> |
| 24680 |
</varlistentry> |
| 24681 |
|
| 24682 |
<varlistentry> |
| 24683 |
<term><anchor id="lddref"><command>ldd</command></term> |
| 24684 |
<indexterm> |
| 24685 |
<primary>ldd</primary> |
| 24686 |
</indexterm> |
| 24687 |
<indexterm> |
| 24688 |
<primary>command</primary> |
| 24689 |
<secondary>ldd</secondary> |
| 24690 |
</indexterm> |
| 24691 |
<listitem> |
| 24692 |
<para>Show shared lib dependencies for an executable file.</para> |
| 24693 |
<screen><prompt>bash$ </prompt><userinput>ldd /bin/ls</userinput> |
| 24694 |
<computeroutput>libc.so.6 => /lib/libc.so.6 (0x4000c000) |
| 24695 |
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x80000000)</computeroutput></screen> |
| 24696 |
</listitem> |
| 24697 |
</varlistentry> |
| 24698 |
|
| 24699 |
<varlistentry> |
| 24700 |
<term><anchor id="watchref"><command>watch</command></term> |
| 24701 |
<indexterm> |
| 24702 |
<primary>watch</primary> |
| 24703 |
</indexterm> |
| 24704 |
<indexterm> |
| 24705 |
<primary>command</primary> |
| 24706 |
<secondary>periodic</secondary> |
| 24707 |
</indexterm> |
| 24708 |
<listitem> |
| 24709 |
<para>Run a command repeatedly, at specified time intervals.</para> |
| 24710 |
<para>The default is two-second intervals, but this may be changed |
| 24711 |
with the <option>-n</option> option.</para> |
| 24712 |
<para><programlisting>watch -n 5 tail /var/log/messages |
| 24713 |
# Shows tail end of system log, /var/log/messages, every five seconds.</programlisting></para> |
| 24714 |
|
| 24715 |
<note><para>Unfortunately, <link linkend="piperef">piping</link> |
| 24716 |
the output of <command>watch command</command> to <link |
| 24717 |
linkend="grepref">grep</link> does not work.</para></note> |
| 24718 |
</listitem> |
| 24719 |
</varlistentry> |
| 24720 |
|
| 24721 |
<varlistentry> |
| 24722 |
<term><anchor id="stripref"><command>strip</command></term> |
| 24723 |
<indexterm> |
| 24724 |
<primary>strip</primary> |
| 24725 |
</indexterm> |
| 24726 |
<indexterm> |
| 24727 |
<primary>command</primary> |
| 24728 |
<secondary>symbol</secondary> |
| 24729 |
</indexterm> |
| 24730 |
<listitem> |
| 24731 |
<para>Remove the debugging symbolic references from an executable |
| 24732 |
binary. This decreases its size, but makes debugging it |
| 24733 |
impossible.</para> |
| 24734 |
<para>This command often occurs in a <link |
| 24735 |
linkend="makefileref">Makefile</link>, |
| 24736 |
but rarely in a shell script.</para> |
| 24737 |
</listitem> |
| 24738 |
</varlistentry> |
| 24739 |
|
| 24740 |
<varlistentry> |
| 24741 |
<term><anchor id="nmref"><command>nm</command></term> |
| 24742 |
<indexterm> |
| 24743 |
<primary>nm</primary> |
| 24744 |
</indexterm> |
| 24745 |
<indexterm> |
| 24746 |
<primary>command</primary> |
| 24747 |
<secondary>symbol</secondary> |
| 24748 |
</indexterm> |
| 24749 |
<listitem> |
| 24750 |
<para>List symbols in an unstripped compiled binary.</para> |
| 24751 |
</listitem> |
| 24752 |
</varlistentry> |
| 24753 |
|
| 24754 |
<varlistentry> |
| 24755 |
<term><anchor id="xrandrref"><command>xrandr</command></term> |
| 24756 |
<indexterm> |
| 24757 |
<primary>xrandr</primary> |
| 24758 |
</indexterm> |
| 24759 |
<indexterm> |
| 24760 |
<primary>command</primary> |
| 24761 |
<secondary>xrandr</secondary> |
| 24762 |
</indexterm> |
| 24763 |
<listitem> |
| 24764 |
<para>Command-line tool for manipulating the root window |
| 24765 |
of the screen.</para> |
| 24766 |
|
| 24767 |
<example id="backlight"> |
| 24768 |
<title><firstterm>Backlight</firstterm>: changes |
| 24769 |
the brightness of the (laptop) screen backlight</title> |
| 24770 |
<programlisting>&backlight;</programlisting> |
| 24771 |
</example> |
| 24772 |
|
| 24773 |
</listitem> |
| 24774 |
</varlistentry> |
| 24775 |
|
| 24776 |
<varlistentry> |
| 24777 |
<term><anchor id="rdistref"><command>rdist</command></term> |
| 24778 |
<indexterm> |
| 24779 |
<primary>rdist</primary> |
| 24780 |
</indexterm> |
| 24781 |
<indexterm> |
| 24782 |
<primary>command</primary> |
| 24783 |
<secondary>rdist</secondary> |
| 24784 |
</indexterm> |
| 24785 |
<listitem> |
| 24786 |
<para>Remote distribution client: synchronizes, clones, |
| 24787 |
or backs up a file system on a remote server.</para> |
| 24788 |
</listitem> |
| 24789 |
</varlistentry> |
| 24790 |
|
| 24791 |
</variablelist> |
| 24792 |
|
| 24793 |
|
| 24794 |
|
| 24795 |
<sect1 id="sysscripts"> |
| 24796 |
<title>Analyzing a System Script</title> |
| 24797 |
|
| 24798 |
<para><anchor id="killall2ref"></para> |
| 24799 |
|
| 24800 |
<para>Using our knowledge of administrative commands, let us examine a system |
| 24801 |
script. One of the shortest and simplest to understand scripts is |
| 24802 |
<quote>killall,</quote> |
| 24803 |
<footnote><para>The <firstterm>killall</firstterm> system |
| 24804 |
script should not be confused with the <link |
| 24805 |
linkend="killallref">killall</link> command in <filename |
| 24806 |
class="directory">/usr/bin</filename>.</para></footnote> |
| 24807 |
used to suspend running processes at system shutdown.</para> |
| 24808 |
|
| 24809 |
<example id="ex55"> |
| 24810 |
<title><firstterm>killall</firstterm>, from <filename |
| 24811 |
class="directory">/etc/rc.d/init.d</filename></title> |
| 24812 |
<programlisting>&ex55;</programlisting> |
| 24813 |
</example> |
| 24814 |
|
| 24815 |
<para>That wasn't so bad. Aside from a little fancy footwork with variable |
| 24816 |
matching, there is no new material there.</para> |
| 24817 |
|
| 24818 |
<formalpara><title>Exercise 1</title> |
| 24819 |
<para>In <filename class="directory">/etc/rc.d/init.d</filename>, |
| 24820 |
analyze the <command>halt</command> script. It is a bit longer |
| 24821 |
than <command>killall</command>, but similar in concept. Make |
| 24822 |
a copy of this script somewhere in your home directory and |
| 24823 |
experiment with it (do <emphasis>not</emphasis> run it as |
| 24824 |
<firstterm>root</firstterm>). Do a simulated run |
| 24825 |
with the <option>-vn</option> flags (<userinput>sh |
| 24826 |
-vn scriptname</userinput>). Add extensive |
| 24827 |
comments. Change the commands to <link |
| 24828 |
linkend="echoref">echos</link>.</para></formalpara> |
| 24829 |
|
| 24830 |
<formalpara><title>Exercise 2</title> |
| 24831 |
<para>Look at some of the more complex scripts in |
| 24832 |
<filename class="directory">/etc/rc.d/init.d</filename>. |
| 24833 |
Try to understand at least portions of them. Follow |
| 24834 |
the above procedure to analyze them. For some |
| 24835 |
additional insight, you might also examine the |
| 24836 |
file <filename>sysvinitfiles</filename> in <filename |
| 24837 |
class="directory">/usr/share/doc/initscripts-?.??</filename>, |
| 24838 |
which is part of the <quote>initscripts</quote> |
| 24839 |
documentation.</para></formalpara> |
| 24840 |
|
| 24841 |
</sect1> <!-- Analyzing a System Script --> |
| 24842 |
|
| 24843 |
</chapter> <!-- System and Administrative Commands --> |
| 24844 |
|
| 24845 |
</part> <!-- Part 4 (Beyond the Basics) --> |
| 24846 |
|
| 24847 |
|
| 24848 |
|
| 24849 |
|
| 24850 |
<part label="Part 5" id="part5"> |
| 24851 |
<title>Advanced Topics</title> |
| 24852 |
|
| 24853 |
<partintro> |
| 24854 |
<para>At this point, we are ready to delve into certain of the |
| 24855 |
difficult and unusual aspects of scripting. Along the way, we |
| 24856 |
will attempt to <quote>push the envelope</quote> in various |
| 24857 |
ways and examine <firstterm>boundary conditions</firstterm> |
| 24858 |
(what happens when we move into uncharted territory?).</para> |
| 24859 |
</partintro> |
| 24860 |
|
| 24861 |
<chapter id="regexp"> |
| 24862 |
<title>Regular Expressions</title> |
| 24863 |
|
| 24864 |
<epigraph> |
| 24865 |
<para>. . . the intellectual activity associated with software |
| 24866 |
development is largely one of gaining insight.</para> |
| 24867 |
<para>--Stowe Boyd</para> |
| 24868 |
</epigraph> |
| 24869 |
|
| 24870 |
<para><anchor id="regexref"></para> |
| 24871 |
|
| 24872 |
<para>To fully utilize the power of shell scripting, you need to |
| 24873 |
master Regular Expressions. Certain commands |
| 24874 |
and utilities commonly used in scripts, such |
| 24875 |
as <link linkend="grepref">grep</link>, <link |
| 24876 |
linkend="exprref">expr</link>, <link linkend="sedref">sed</link> |
| 24877 |
and <link linkend="awkref">awk</link>, interpret and use REs. As of |
| 24878 |
<link linkend="bash3ref">version 3</link>, Bash has acquired its |
| 24879 |
own <link linkend="regexmatchref">RE-match operator</link>: |
| 24880 |
<command>=~</command>.</para> |
| 24881 |
|
| 24882 |
|
| 24883 |
|
| 24884 |
<sect1><title>A Brief Introduction to Regular Expressions</title> |
| 24885 |
|
| 24886 |
<para>An expression is a string of characters. Those characters |
| 24887 |
having an interpretation above and beyond their literal |
| 24888 |
meaning are called <firstterm>metacharacters</firstterm>. |
| 24889 |
A quote symbol, for example, may denote speech by a person, |
| 24890 |
<firstterm>ditto</firstterm>, or a meta-meaning |
| 24891 |
|
| 24892 |
<footnote><para><anchor id="metameaningref">A |
| 24893 |
<firstterm>meta-meaning</firstterm> is the meaning of a |
| 24894 |
term or expression on a higher level of abstraction. For |
| 24895 |
example, the <firstterm>literal</firstterm> meaning |
| 24896 |
of <firstterm>regular expression</firstterm> is an |
| 24897 |
ordinary expression that conforms to accepted usage. The |
| 24898 |
<firstterm>meta-meaning</firstterm> is drastically different, |
| 24899 |
as discussed at length in this chapter.</para></footnote> |
| 24900 |
|
| 24901 |
for the symbols that follow. Regular Expressions are sets |
| 24902 |
of characters and/or metacharacters that match (or specify) |
| 24903 |
patterns.</para> |
| 24904 |
|
| 24905 |
<para>A Regular Expression contains one or more of the |
| 24906 |
following:</para> |
| 24907 |
|
| 24908 |
<itemizedlist> |
| 24909 |
|
| 24910 |
<listitem> |
| 24911 |
<para><firstterm>A character set</firstterm>. These are the |
| 24912 |
characters retaining their literal meaning. The |
| 24913 |
simplest type of Regular Expression consists |
| 24914 |
<emphasis>only</emphasis> of a character set, with no |
| 24915 |
metacharacters.</para> |
| 24916 |
</listitem> |
| 24917 |
|
| 24918 |
<listitem> |
| 24919 |
<para><anchor id="anchorref"></para> |
| 24920 |
<para><firstterm>An anchor</firstterm>. These designate |
| 24921 |
(<firstterm>anchor</firstterm>) the position in the line of |
| 24922 |
text that the RE is to match. For example, <token>^</token>, |
| 24923 |
and <token>$</token> are anchors.</para> |
| 24924 |
</listitem> |
| 24925 |
|
| 24926 |
<listitem> |
| 24927 |
<para><firstterm>Modifiers</firstterm>. These expand or narrow |
| 24928 |
(<firstterm>modify</firstterm>) the range of text the RE is |
| 24929 |
to match. Modifiers include the asterisk, brackets, and |
| 24930 |
the backslash.</para> |
| 24931 |
</listitem> |
| 24932 |
|
| 24933 |
</itemizedlist> |
| 24934 |
|
| 24935 |
|
| 24936 |
<para>The main uses for Regular Expressions |
| 24937 |
(<firstterm>RE</firstterm>s) are text searches and string |
| 24938 |
manipulation. An RE <firstterm>matches</firstterm> a single |
| 24939 |
character or a set of characters -- a string or a part of |
| 24940 |
a string.</para> |
| 24941 |
|
| 24942 |
<itemizedlist> |
| 24943 |
<listitem> |
| 24944 |
<indexterm> |
| 24945 |
<primary>*</primary> |
| 24946 |
</indexterm> |
| 24947 |
<indexterm> |
| 24948 |
<primary>special character</primary> |
| 24949 |
<secondary>*</secondary> |
| 24950 |
</indexterm> |
| 24951 |
|
| 24952 |
<para><anchor id="asteriskreg">The asterisk -- |
| 24953 |
<token>*</token> -- matches any number of |
| 24954 |
repeats of the character string or RE preceding it, |
| 24955 |
including <emphasis>zero</emphasis> instances.</para> |
| 24956 |
|
| 24957 |
<para><quote>1133*</quote> matches <replaceable>11 + |
| 24958 |
one or more 3's</replaceable>: |
| 24959 |
<replaceable>113</replaceable>, <replaceable>1133</replaceable>, |
| 24960 |
<replaceable>1133333</replaceable>, and so forth.</para> |
| 24961 |
|
| 24962 |
</listitem> |
| 24963 |
<listitem> |
| 24964 |
<indexterm> |
| 24965 |
<primary>.</primary> |
| 24966 |
</indexterm> |
| 24967 |
<indexterm> |
| 24968 |
<primary>special character</primary> |
| 24969 |
<secondary>.</secondary> |
| 24970 |
</indexterm> |
| 24971 |
|
| 24972 |
<para><anchor id="regexdot">The <firstterm>dot</firstterm> |
| 24973 |
-- <token>.</token> -- matches |
| 24974 |
any one character, except a newline. |
| 24975 |
<footnote><para>Since <link linkend="sedref">sed</link>, <link |
| 24976 |
linkend="awkref">awk</link>, and <link |
| 24977 |
linkend="grepref">grep</link> process single lines, there |
| 24978 |
will usually not be a newline to match. In those cases where |
| 24979 |
there is a newline in a multiple line expression, the dot |
| 24980 |
will match the newline. |
| 24981 |
<programlisting>#!/bin/bash |
| 24982 |
|
| 24983 |
sed -e 'N;s/.*/[&]/' << EOF # Here Document |
| 24984 |
line1 |
| 24985 |
line2 |
| 24986 |
EOF |
| 24987 |
# OUTPUT: |
| 24988 |
# [line1 |
| 24989 |
# line2] |
| 24990 |
|
| 24991 |
|
| 24992 |
|
| 24993 |
echo |
| 24994 |
|
| 24995 |
awk '{ $0=$1 "\n" $2; if (/line.1/) {print}}' << EOF |
| 24996 |
line 1 |
| 24997 |
line 2 |
| 24998 |
EOF |
| 24999 |
# OUTPUT: |
| 25000 |
# line |
| 25001 |
# 1 |
| 25002 |
|
| 25003 |
|
| 25004 |
# Thanks, S.C. |
| 25005 |
|
| 25006 |
exit 0</programlisting></para></footnote> |
| 25007 |
</para> |
| 25008 |
|
| 25009 |
<para><quote>13.</quote> matches <replaceable>13 + at |
| 25010 |
least one of any character (including a |
| 25011 |
space)</replaceable>: <replaceable>1133</replaceable>, |
| 25012 |
<replaceable>11333</replaceable>, but not |
| 25013 |
<replaceable>13</replaceable> (additional character |
| 25014 |
missing).</para> |
| 25015 |
|
| 25016 |
<para>See <xref linkend="cwsolver"> for a demonstration |
| 25017 |
of <firstterm>dot single-character</firstterm> |
| 25018 |
matching.</para> |
| 25019 |
</listitem> |
| 25020 |
|
| 25021 |
|
| 25022 |
<listitem> |
| 25023 |
<indexterm> |
| 25024 |
<primary>^</primary> |
| 25025 |
</indexterm> |
| 25026 |
<indexterm> |
| 25027 |
<primary>special character</primary> |
| 25028 |
<secondary>^</secondary> |
| 25029 |
</indexterm> |
| 25030 |
<para><anchor id="caretref">The caret -- <token>^</token> |
| 25031 |
-- matches the beginning of a line, but sometimes, depending |
| 25032 |
on context, negates the meaning of a set of characters in |
| 25033 |
an RE.</para> |
| 25034 |
</listitem> |
| 25035 |
|
| 25036 |
<listitem> |
| 25037 |
<indexterm> |
| 25038 |
<primary>$</primary> |
| 25039 |
</indexterm> |
| 25040 |
<indexterm> |
| 25041 |
<primary>special character</primary> |
| 25042 |
<secondary>$</secondary> |
| 25043 |
</indexterm> |
| 25044 |
<para><anchor id="dollarsignref"></para> |
| 25045 |
<para>The dollar sign -- <token>$</token> -- at the end of an |
| 25046 |
RE matches the end of a line.</para> |
| 25047 |
<para><quote>XXX$</quote> matches <token>XXX</token> at the |
| 25048 |
end of a line.</para> |
| 25049 |
<para><quote>^$</quote> matches blank lines.</para> |
| 25050 |
</listitem> |
| 25051 |
|
| 25052 |
<listitem> |
| 25053 |
<indexterm> |
| 25054 |
<primary>[...]</primary> |
| 25055 |
</indexterm> |
| 25056 |
<indexterm> |
| 25057 |
<primary>special character</primary> |
| 25058 |
<secondary>[...]</secondary> |
| 25059 |
</indexterm> |
| 25060 |
|
| 25061 |
<para><anchor id="bracketsref"></para> |
| 25062 |
<para>Brackets -- <token>[...]</token> -- enclose a set of characters |
| 25063 |
to match in a single RE.</para> |
| 25064 |
|
| 25065 |
<para><quote>[xyz]</quote> matches any one of the characters |
| 25066 |
<replaceable>x</replaceable>, <replaceable>y</replaceable>, |
| 25067 |
or <replaceable>z</replaceable>.</para> |
| 25068 |
|
| 25069 |
<para><quote>[c-n]</quote> matches any one of the |
| 25070 |
characters in the range <replaceable>c</replaceable> |
| 25071 |
to <replaceable>n</replaceable>.</para> |
| 25072 |
|
| 25073 |
<para><quote>[B-Pk-y]</quote> matches any one of the |
| 25074 |
characters in the ranges <replaceable>B</replaceable> |
| 25075 |
to <replaceable>P</replaceable> |
| 25076 |
and <replaceable>k</replaceable> to |
| 25077 |
<replaceable>y</replaceable>.</para> |
| 25078 |
|
| 25079 |
<para><quote>[a-z0-9]</quote> matches any single lowercase |
| 25080 |
letter or any digit.</para> |
| 25081 |
|
| 25082 |
<para><quote>[^b-d]</quote> matches any character |
| 25083 |
<emphasis>except</emphasis> those in |
| 25084 |
the range <replaceable>b</replaceable> to |
| 25085 |
<replaceable>d</replaceable>. This is an instance of |
| 25086 |
<token>^</token> negating or inverting the meaning |
| 25087 |
of the following RE (taking on a role similar to |
| 25088 |
<token>!</token> in a different context).</para> |
| 25089 |
|
| 25090 |
<para>Combined sequences of bracketed characters match |
| 25091 |
common word patterns. <quote>[Yy][Ee][Ss]</quote> matches |
| 25092 |
<replaceable>yes</replaceable>, <replaceable>Yes</replaceable>, |
| 25093 |
<replaceable>YES</replaceable>, <replaceable>yEs</replaceable>, |
| 25094 |
and so forth. |
| 25095 |
<quote>[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]</quote> |
| 25096 |
matches any Social Security number.</para> |
| 25097 |
|
| 25098 |
</listitem> |
| 25099 |
|
| 25100 |
|
| 25101 |
<listitem> |
| 25102 |
<indexterm> |
| 25103 |
<primary>\</primary> |
| 25104 |
</indexterm> |
| 25105 |
<indexterm> |
| 25106 |
<primary>special character</primary> |
| 25107 |
<secondary>\</secondary> |
| 25108 |
</indexterm> |
| 25109 |
|
| 25110 |
<para><anchor id="regexbs"></para> |
| 25111 |
<para>The backslash -- <token>\</token> -- <link |
| 25112 |
linkend="escp">escapes</link> a special character, which |
| 25113 |
means that character gets interpreted literally (and is |
| 25114 |
therefore no longer <firstterm>special</firstterm>).</para> |
| 25115 |
|
| 25116 |
<para>A <quote>\$</quote> reverts back to its |
| 25117 |
literal meaning of <quote>$</quote>, rather than its |
| 25118 |
RE meaning of end-of-line. Likewise a <quote>\\</quote> |
| 25119 |
has the literal meaning of <quote>\</quote>.</para> |
| 25120 |
</listitem> |
| 25121 |
|
| 25122 |
|
| 25123 |
<listitem> |
| 25124 |
<indexterm> |
| 25125 |
<primary>\< \></primary> |
| 25126 |
</indexterm> |
| 25127 |
<indexterm> |
| 25128 |
<primary>special character</primary> |
| 25129 |
<secondary>\< \></secondary> |
| 25130 |
</indexterm> |
| 25131 |
|
| 25132 |
<para><anchor id="anglebrac"></para> |
| 25133 |
<para><link linkend="escp">Escaped</link> <quote>angle |
| 25134 |
brackets</quote> -- <token>\<...\></token> -- mark word |
| 25135 |
boundaries.</para> |
| 25136 |
|
| 25137 |
<para>The angle brackets must be escaped, since otherwise |
| 25138 |
they have only their literal character meaning.</para> |
| 25139 |
|
| 25140 |
<para><quote>\<the\></quote> matches the word |
| 25141 |
<quote>the,</quote> but not the words <quote>them,</quote> |
| 25142 |
<quote>there,</quote> <quote>other,</quote> etc.</para> |
| 25143 |
|
| 25144 |
<para> |
| 25145 |
<screen> |
| 25146 |
<prompt>bash$ </prompt><userinput>cat textfile</userinput> |
| 25147 |
<computeroutput>This is line 1, of which there is only one instance. |
| 25148 |
This is the only instance of line 2. |
| 25149 |
This is line 3, another line. |
| 25150 |
This is line 4.</computeroutput> |
| 25151 |
|
| 25152 |
|
| 25153 |
<prompt>bash$ </prompt><userinput>grep 'the' textfile</userinput> |
| 25154 |
<computeroutput>This is line 1, of which there is only one instance. |
| 25155 |
This is the only instance of line 2. |
| 25156 |
This is line 3, another line.</computeroutput> |
| 25157 |
|
| 25158 |
|
| 25159 |
<prompt>bash$ </prompt><userinput>grep '\<the\>' textfile</userinput> |
| 25160 |
<computeroutput>This is the only instance of line 2.</computeroutput> |
| 25161 |
</screen> |
| 25162 |
</para> |
| 25163 |
|
| 25164 |
</listitem> |
| 25165 |
|
| 25166 |
</itemizedlist> |
| 25167 |
|
| 25168 |
<sidebar> |
| 25169 |
<para>The only way to be certain that a particular RE works is to |
| 25170 |
test it.</para> |
| 25171 |
|
| 25172 |
<para><programlisting>TEST FILE: tstfile # No match. |
| 25173 |
# No match. |
| 25174 |
Run grep "1133*" on this file. # Match. |
| 25175 |
# No match. |
| 25176 |
# No match. |
| 25177 |
This line contains the number 113. # Match. |
| 25178 |
This line contains the number 13. # No match. |
| 25179 |
This line contains the number 133. # No match. |
| 25180 |
This line contains the number 1133. # Match. |
| 25181 |
This line contains the number 113312. # Match. |
| 25182 |
This line contains the number 1112. # No match. |
| 25183 |
This line contains the number 113312312. # Match. |
| 25184 |
This line contains no numbers at all. # No match.</programlisting></para> |
| 25185 |
|
| 25186 |
<screen><prompt>bash$ </prompt><userinput>grep "1133*" tstfile</userinput> |
| 25187 |
<computeroutput>Run grep "1133*" on this file. # Match. |
| 25188 |
This line contains the number 113. # Match. |
| 25189 |
This line contains the number 1133. # Match. |
| 25190 |
This line contains the number 113312. # Match. |
| 25191 |
This line contains the number 113312312. # Match.</computeroutput> |
| 25192 |
</screen> |
| 25193 |
|
| 25194 |
</sidebar> |
| 25195 |
|
| 25196 |
<itemizedlist> |
| 25197 |
|
| 25198 |
<listitem override="square"> |
| 25199 |
<formalpara> |
| 25200 |
<title><anchor id="extregex">Extended REs</title> |
| 25201 |
|
| 25202 |
<para>Additional metacharacters added to the basic set. Used |
| 25203 |
in <link linkend="egrepref">egrep</link>, |
| 25204 |
<link linkend="awkref">awk</link>, and <link |
| 25205 |
linkend="perlref">Perl</link>.</para> |
| 25206 |
|
| 25207 |
</formalpara> |
| 25208 |
</listitem> |
| 25209 |
|
| 25210 |
<listitem> |
| 25211 |
|
| 25212 |
<indexterm> |
| 25213 |
<primary>?</primary> |
| 25214 |
</indexterm> |
| 25215 |
<indexterm> |
| 25216 |
<primary>special character</primary> |
| 25217 |
<secondary>?</secondary> |
| 25218 |
</indexterm> |
| 25219 |
|
| 25220 |
<para><anchor id="quexregex"></para> |
| 25221 |
<para>The question mark -- <token>?</token> -- matches zero or |
| 25222 |
one of the previous RE. It is generally used for matching |
| 25223 |
single characters.</para> |
| 25224 |
|
| 25225 |
</listitem> |
| 25226 |
|
| 25227 |
<listitem> |
| 25228 |
<indexterm> |
| 25229 |
<primary>+</primary> |
| 25230 |
</indexterm> |
| 25231 |
<indexterm> |
| 25232 |
<primary>special character</primary> |
| 25233 |
<secondary>+</secondary> |
| 25234 |
</indexterm> |
| 25235 |
|
| 25236 |
<para><anchor id="plusref"></para> |
| 25237 |
<para>The plus -- <token>+</token> -- matches one or more of the |
| 25238 |
previous RE. It serves a role similar to the <token>*</token>, but |
| 25239 |
does <emphasis>not</emphasis> match zero occurrences.</para> |
| 25240 |
|
| 25241 |
<para><programlisting># GNU versions of sed and awk can use "+", |
| 25242 |
# but it needs to be escaped. |
| 25243 |
|
| 25244 |
echo a111b | sed -ne '/a1\+b/p' |
| 25245 |
echo a111b | grep 'a1\+b' |
| 25246 |
echo a111b | gawk '/a1+b/' |
| 25247 |
# All of above are equivalent. |
| 25248 |
|
| 25249 |
# Thanks, S.C.</programlisting></para> |
| 25250 |
|
| 25251 |
<para><anchor id="escpcb"></para> |
| 25252 |
</listitem> |
| 25253 |
|
| 25254 |
<listitem> |
| 25255 |
<indexterm> |
| 25256 |
<primary>\{ \}</primary> |
| 25257 |
</indexterm> |
| 25258 |
<indexterm> |
| 25259 |
<primary>special character</primary> |
| 25260 |
<secondary>\{ \}</secondary> |
| 25261 |
</indexterm> |
| 25262 |
|
| 25263 |
<para><link linkend="escp">Escaped</link> <quote>curly |
| 25264 |
brackets</quote> -- <token>\{ \}</token> -- indicate the number |
| 25265 |
of occurrences of a preceding RE to match.</para> |
| 25266 |
|
| 25267 |
|
| 25268 |
<para>It is necessary to escape the curly brackets since |
| 25269 |
they have only their literal character meaning |
| 25270 |
otherwise. This usage is technically not part of the basic |
| 25271 |
RE set.</para> |
| 25272 |
|
| 25273 |
<para><quote>[0-9]\{5\}</quote> matches exactly five digits |
| 25274 |
(characters in the range of 0 to 9).</para> |
| 25275 |
|
| 25276 |
<note> |
| 25277 |
<para>Curly brackets are not available as an RE in the |
| 25278 |
<quote>classic</quote> (non-POSIX compliant) version |
| 25279 |
of <link linkend="awkref">awk</link>. |
| 25280 |
<anchor id="gnugawk">However, the GNU extended version |
| 25281 |
of <firstterm>awk</firstterm>, <command>gawk</command>, |
| 25282 |
has the <option>--re-interval</option> option that permits |
| 25283 |
them (without being escaped).</para> |
| 25284 |
|
| 25285 |
<para> |
| 25286 |
<screen><prompt>bash$ </prompt><userinput>echo 2222 | gawk --re-interval '/2{3}/'</userinput> |
| 25287 |
<computeroutput>2222</computeroutput> |
| 25288 |
</screen> |
| 25289 |
</para> |
| 25290 |
|
| 25291 |
<para><command>Perl</command> and some |
| 25292 |
<command>egrep</command> versions do not require escaping |
| 25293 |
the curly brackets.</para> |
| 25294 |
|
| 25295 |
</note> |
| 25296 |
|
| 25297 |
</listitem> |
| 25298 |
|
| 25299 |
<listitem> |
| 25300 |
<indexterm> |
| 25301 |
<primary>()</primary> |
| 25302 |
</indexterm> |
| 25303 |
<indexterm> |
| 25304 |
<primary>special character</primary> |
| 25305 |
<secondary>()</secondary> |
| 25306 |
</indexterm> |
| 25307 |
|
| 25308 |
<para><anchor id="parengrps"></para> |
| 25309 |
<para>Parentheses -- <command>( )</command> -- enclose a group of |
| 25310 |
REs. They are useful with the following |
| 25311 |
<quote><token>|</token></quote> operator and in <link |
| 25312 |
linkend="exprparen">substring extraction</link> using <link |
| 25313 |
linkend="exprref">expr</link>.</para> |
| 25314 |
</listitem> |
| 25315 |
|
| 25316 |
<listitem> |
| 25317 |
<indexterm> |
| 25318 |
<primary>|</primary> |
| 25319 |
</indexterm> |
| 25320 |
<indexterm> |
| 25321 |
<primary>special character</primary> |
| 25322 |
<secondary>|</secondary> |
| 25323 |
</indexterm> |
| 25324 |
|
| 25325 |
<para>The -- <command>|</command> -- <quote>or</quote> RE operator |
| 25326 |
matches any of a set of alternate characters.</para> |
| 25327 |
<para> |
| 25328 |
<screen><prompt>bash$ </prompt><userinput>egrep 're(a|e)d' misc.txt</userinput> |
| 25329 |
<computeroutput>People who read seem to be better informed than those who do not. |
| 25330 |
The clarinet produces sound by the vibration of its reed.</computeroutput> |
| 25331 |
</screen> |
| 25332 |
</para> |
| 25333 |
</listitem> |
| 25334 |
|
| 25335 |
</itemizedlist> |
| 25336 |
|
| 25337 |
<note><para>Some versions of <command>sed</command>, |
| 25338 |
<command>ed</command>, and <command>ex</command> support |
| 25339 |
escaped versions of the extended Regular Expressions |
| 25340 |
described above, as do the GNU utilities.</para></note> |
| 25341 |
|
| 25342 |
|
| 25343 |
<itemizedlist> |
| 25344 |
|
| 25345 |
<listitem override="square"> |
| 25346 |
|
| 25347 |
<formalpara><title><anchor id="posixref">POSIX Character Classes</title> |
| 25348 |
<para><userinput>[:class:]</userinput></para></formalpara> |
| 25349 |
|
| 25350 |
<indexterm> |
| 25351 |
<primary>[:</primary> |
| 25352 |
</indexterm> |
| 25353 |
<indexterm> |
| 25354 |
<primary>special character</primary> |
| 25355 |
<secondary>:]</secondary> |
| 25356 |
</indexterm> |
| 25357 |
|
| 25358 |
<para>This is an alternate method of specifying a range of |
| 25359 |
characters to match.</para> |
| 25360 |
|
| 25361 |
</listitem> |
| 25362 |
|
| 25363 |
|
| 25364 |
<listitem> |
| 25365 |
|
| 25366 |
<indexterm> |
| 25367 |
<primary>alnum</primary> |
| 25368 |
</indexterm> |
| 25369 |
<indexterm> |
| 25370 |
<primary>character range</primary> |
| 25371 |
<secondary>alphabetic numeric</secondary> |
| 25372 |
</indexterm> |
| 25373 |
|
| 25374 |
<para><userinput>[:alnum:]</userinput> matches alphabetic or |
| 25375 |
numeric characters. This is equivalent to |
| 25376 |
<userinput>A-Za-z0-9</userinput>.</para> |
| 25377 |
|
| 25378 |
</listitem> |
| 25379 |
|
| 25380 |
<listitem> |
| 25381 |
|
| 25382 |
<indexterm> |
| 25383 |
<primary>alpha</primary> |
| 25384 |
</indexterm> |
| 25385 |
<indexterm> |
| 25386 |
<primary>character range</primary> |
| 25387 |
<secondary>alphabetic</secondary> |
| 25388 |
</indexterm> |
| 25389 |
|
| 25390 |
<para><userinput>[:alpha:]</userinput> matches alphabetic |
| 25391 |
characters. This is equivalent to |
| 25392 |
<userinput>A-Za-z</userinput>.</para> |
| 25393 |
|
| 25394 |
</listitem> |
| 25395 |
|
| 25396 |
<listitem> |
| 25397 |
|
| 25398 |
<indexterm> |
| 25399 |
<primary>blank</primary> |
| 25400 |
</indexterm> |
| 25401 |
<indexterm> |
| 25402 |
<primary>character range</primary> |
| 25403 |
<secondary>space tab</secondary> |
| 25404 |
</indexterm> |
| 25405 |
|
| 25406 |
<para><userinput>[:blank:]</userinput> matches a space or a |
| 25407 |
tab.</para> |
| 25408 |
|
| 25409 |
</listitem> |
| 25410 |
|
| 25411 |
<listitem> |
| 25412 |
|
| 25413 |
<indexterm> |
| 25414 |
<primary>cntrl</primary> |
| 25415 |
</indexterm> |
| 25416 |
<indexterm> |
| 25417 |
<primary>character range</primary> |
| 25418 |
<secondary>control</secondary> |
| 25419 |
</indexterm> |
| 25420 |
|
| 25421 |
<para><userinput>[:cntrl:]</userinput> matches control |
| 25422 |
characters.</para> |
| 25423 |
|
| 25424 |
</listitem> |
| 25425 |
|
| 25426 |
<listitem> |
| 25427 |
|
| 25428 |
<indexterm> |
| 25429 |
<primary>digit</primary> |
| 25430 |
</indexterm> |
| 25431 |
<indexterm> |
| 25432 |
<primary>character range</primary> |
| 25433 |
<secondary>decimal digit</secondary> |
| 25434 |
</indexterm> |
| 25435 |
|
| 25436 |
<para><userinput>[:digit:]</userinput> matches (decimal) |
| 25437 |
digits. This is equivalent to |
| 25438 |
<userinput>0-9</userinput>.</para> |
| 25439 |
|
| 25440 |
</listitem> |
| 25441 |
|
| 25442 |
<listitem> |
| 25443 |
|
| 25444 |
<indexterm> |
| 25445 |
<primary>graph</primary> |
| 25446 |
</indexterm> |
| 25447 |
<indexterm> |
| 25448 |
<primary>character range</primary> |
| 25449 |
<secondary>graph</secondary> |
| 25450 |
</indexterm> |
| 25451 |
|
| 25452 |
<para><userinput>[:graph:]</userinput> (graphic printable |
| 25453 |
characters). Matches characters in the range of <link |
| 25454 |
linkend="asciidef">ASCII</link> 33 - 126. This is |
| 25455 |
the same as <userinput>[:print:]</userinput>, below, |
| 25456 |
but excluding the space character.</para> |
| 25457 |
|
| 25458 |
</listitem> |
| 25459 |
|
| 25460 |
<listitem> |
| 25461 |
|
| 25462 |
<indexterm> |
| 25463 |
<primary>lower</primary> |
| 25464 |
</indexterm> |
| 25465 |
<indexterm> |
| 25466 |
<primary>character range</primary> |
| 25467 |
<secondary>lowercase</secondary> |
| 25468 |
</indexterm> |
| 25469 |
|
| 25470 |
<para><userinput>[:lower:]</userinput> matches lowercase |
| 25471 |
alphabetic characters. This is equivalent to |
| 25472 |
<userinput>a-z</userinput>.</para> |
| 25473 |
|
| 25474 |
</listitem> |
| 25475 |
|
| 25476 |
<listitem> |
| 25477 |
|
| 25478 |
<indexterm> |
| 25479 |
<primary>print</primary> |
| 25480 |
</indexterm> |
| 25481 |
<indexterm> |
| 25482 |
<primary>character range</primary> |
| 25483 |
<secondary>printable</secondary> |
| 25484 |
</indexterm> |
| 25485 |
|
| 25486 |
<para><userinput>[:print:]</userinput> (printable |
| 25487 |
characters). Matches characters in the range of ASCII 32 - |
| 25488 |
126. This is the same as <userinput>[:graph:]</userinput>, |
| 25489 |
above, but adding the space character.</para> |
| 25490 |
|
| 25491 |
</listitem> |
| 25492 |
|
| 25493 |
<listitem> |
| 25494 |
|
| 25495 |
<indexterm> |
| 25496 |
<primary>space</primary> |
| 25497 |
</indexterm> |
| 25498 |
<indexterm> |
| 25499 |
<primary>character range</primary> |
| 25500 |
<secondary>whitespace</secondary> |
| 25501 |
</indexterm> |
| 25502 |
|
| 25503 |
<para><anchor id="wsposix"><userinput>[:space:]</userinput> |
| 25504 |
matches whitespace characters (space and horizontal |
| 25505 |
tab).</para> |
| 25506 |
|
| 25507 |
</listitem> |
| 25508 |
|
| 25509 |
<listitem> |
| 25510 |
|
| 25511 |
<indexterm> |
| 25512 |
<primary>upper</primary> |
| 25513 |
</indexterm> |
| 25514 |
<indexterm> |
| 25515 |
<primary>character range</primary> |
| 25516 |
<secondary>uppercase</secondary> |
| 25517 |
</indexterm> |
| 25518 |
|
| 25519 |
<para><userinput>[:upper:]</userinput> matches uppercase |
| 25520 |
alphabetic characters. This is equivalent to |
| 25521 |
<userinput>A-Z</userinput>.</para> |
| 25522 |
|
| 25523 |
</listitem> |
| 25524 |
|
| 25525 |
<listitem> |
| 25526 |
|
| 25527 |
<indexterm> |
| 25528 |
<primary>xdigit</primary> |
| 25529 |
</indexterm> |
| 25530 |
<indexterm> |
| 25531 |
<primary>character range</primary> |
| 25532 |
<secondary>hexadecimal</secondary> |
| 25533 |
</indexterm> |
| 25534 |
|
| 25535 |
<para><userinput>[:xdigit:]</userinput> matches hexadecimal |
| 25536 |
digits. This is equivalent to |
| 25537 |
<userinput>0-9A-Fa-f</userinput>.</para> |
| 25538 |
|
| 25539 |
<important> |
| 25540 |
<para>POSIX character classes generally require quoting |
| 25541 |
or <link linkend="dblbrackets">double brackets</link> |
| 25542 |
([[ ]]).</para> |
| 25543 |
</important> |
| 25544 |
|
| 25545 |
<para> |
| 25546 |
<screen><prompt>bash$ </prompt><userinput>grep [[:digit:]] test.file</userinput> |
| 25547 |
<computeroutput>abc=723</computeroutput> |
| 25548 |
</screen> |
| 25549 |
</para> |
| 25550 |
|
| 25551 |
<para><programlisting># ... |
| 25552 |
if [[ $arow =~ [[:digit:]] ]] # Numerical input? |
| 25553 |
then # POSIX char class |
| 25554 |
if [[ $acol =~ [[:alpha:]] ]] # Number followed by a letter? Illegal! |
| 25555 |
# ... |
| 25556 |
# From ktour.sh example script.</programlisting> |
| 25557 |
</para> |
| 25558 |
|
| 25559 |
<para>These character classes may even be used with <link |
| 25560 |
linkend="globbingref">globbing</link>, to a limited |
| 25561 |
extent.</para> |
| 25562 |
|
| 25563 |
<para> |
| 25564 |
<screen><prompt>bash$ </prompt><userinput>ls -l ?[[:digit:]][[:digit:]]?</userinput> |
| 25565 |
<computeroutput>-rw-rw-r-- 1 bozo bozo 0 Aug 21 14:47 a33b</computeroutput> |
| 25566 |
</screen> |
| 25567 |
</para> |
| 25568 |
|
| 25569 |
<para>POSIX character classes are used in |
| 25570 |
<xref linkend="ex49"> and <xref linkend="lowercase">.</para> |
| 25571 |
|
| 25572 |
|
| 25573 |
</listitem> |
| 25574 |
|
| 25575 |
</itemizedlist> |
| 25576 |
|
| 25577 |
|
| 25578 |
<para><link linkend="sedref">Sed</link>, <link |
| 25579 |
linkend="awkref">awk</link>, and <link |
| 25580 |
linkend="perlref">Perl</link>, used as filters in scripts, take |
| 25581 |
REs as arguments when "sifting" or transforming files or I/O |
| 25582 |
streams. See <xref linkend="behead"> and <xref linkend="tree"> |
| 25583 |
for illustrations of this.</para> |
| 25584 |
|
| 25585 |
<para>The standard reference on this complex topic is Friedl's |
| 25586 |
<citetitle pubwork="book">Mastering Regular |
| 25587 |
Expressions</citetitle>. <citetitle pubwork="book">Sed & |
| 25588 |
Awk</citetitle>, by Dougherty and Robbins, also gives a very |
| 25589 |
lucid treatment of REs. See the <xref linkend="biblio"> for |
| 25590 |
more information on these books.</para> |
| 25591 |
|
| 25592 |
</sect1> <!-- A Brief Introduction to Regular Expressions --> |
| 25593 |
|
| 25594 |
|
| 25595 |
|
| 25596 |
|
| 25597 |
<sect1 id="globbingref"> |
| 25598 |
<title>Globbing</title> |
| 25599 |
|
| 25600 |
<para><anchor id="globbingref2"></para> |
| 25601 |
<para>Bash itself cannot recognize Regular Expressions. Inside |
| 25602 |
scripts, it is commands and utilities -- such as |
| 25603 |
<link linkend="sedref">sed</link> and <link |
| 25604 |
linkend="awkref">awk</link> -- that interpret RE's.</para> |
| 25605 |
|
| 25606 |
|
| 25607 |
<para>Bash <emphasis>does</emphasis> carry out <firstterm>filename |
| 25608 |
expansion</firstterm> |
| 25609 |
|
| 25610 |
<footnote><para><firstterm>Filename expansion</firstterm> |
| 25611 |
means expanding filename patterns or templates |
| 25612 |
containing special characters. For example, |
| 25613 |
<filename>example.???</filename> might expand |
| 25614 |
to <filename>example.001</filename> and/or |
| 25615 |
<filename>example.txt</filename>.</para></footnote> |
| 25616 |
|
| 25617 |
-- a process known as <firstterm>globbing</firstterm> -- but |
| 25618 |
this does <emphasis>not</emphasis> use the standard RE set. |
| 25619 |
Instead, globbing recognizes and expands <firstterm>wild |
| 25620 |
cards</firstterm>. Globbing interprets the standard wild |
| 25621 |
card characters |
| 25622 |
<footnote><para><anchor id="wildcarddef">A <firstterm>wild |
| 25623 |
card</firstterm> character, analogous to a wild card in poker, |
| 25624 |
can represent (almost) any other character.</para></footnote> |
| 25625 |
-- <link linkend="asteriskref">*</link> and |
| 25626 |
<link linkend="wildcardqu">?</link>, character lists in |
| 25627 |
square brackets, and certain other special characters (such |
| 25628 |
as <token>^</token> for negating the sense of a match). |
| 25629 |
<anchor id="wdotfilewc">There are important limitations on wild |
| 25630 |
card characters in globbing, however. Strings containing |
| 25631 |
<replaceable>*</replaceable> will not match filenames that |
| 25632 |
start with a dot, as, for example, <link |
| 25633 |
linkend="sample-bashrc"><filename>.bashrc</filename></link>. |
| 25634 |
|
| 25635 |
<footnote> |
| 25636 |
<para> |
| 25637 |
Filename expansion <emphasis>can</emphasis> |
| 25638 |
match dotfiles, but only if the pattern explicitly includes the dot |
| 25639 |
as a literal character. |
| 25640 |
<programlisting>~/[.]bashrc # Will not expand to ~/.bashrc |
| 25641 |
~/?bashrc # Neither will this. |
| 25642 |
# Wild cards and metacharacters will NOT |
| 25643 |
#+ expand to a dot in globbing. |
| 25644 |
|
| 25645 |
~/.[b]ashrc # Will expand to ~/.bashrc |
| 25646 |
~/.ba?hrc # Likewise. |
| 25647 |
~/.bashr* # Likewise. |
| 25648 |
|
| 25649 |
# Setting the "dotglob" option turns this off. |
| 25650 |
|
| 25651 |
# Thanks, S.C.</programlisting> |
| 25652 |
</para> |
| 25653 |
</footnote> |
| 25654 |
|
| 25655 |
Likewise, the <replaceable>?</replaceable> has a different |
| 25656 |
meaning in globbing than as part of an RE.</para> |
| 25657 |
|
| 25658 |
<para> |
| 25659 |
<screen> |
| 25660 |
<prompt>bash$ </prompt><userinput>ls -l</userinput> |
| 25661 |
<computeroutput>total 2 |
| 25662 |
-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 a.1 |
| 25663 |
-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 b.1 |
| 25664 |
-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 c.1 |
| 25665 |
-rw-rw-r-- 1 bozo bozo 466 Aug 6 17:48 t2.sh |
| 25666 |
-rw-rw-r-- 1 bozo bozo 758 Jul 30 09:02 test1.txt</computeroutput> |
| 25667 |
|
| 25668 |
<prompt>bash$ </prompt><userinput>ls -l t?.sh</userinput> |
| 25669 |
<computeroutput>-rw-rw-r-- 1 bozo bozo 466 Aug 6 17:48 t2.sh</computeroutput> |
| 25670 |
|
| 25671 |
<prompt>bash$ </prompt><userinput>ls -l [ab]*</userinput> |
| 25672 |
<computeroutput>-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 a.1 |
| 25673 |
-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 b.1</computeroutput> |
| 25674 |
|
| 25675 |
<prompt>bash$ </prompt><userinput>ls -l [a-c]*</userinput> |
| 25676 |
<computeroutput>-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 a.1 |
| 25677 |
-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 b.1 |
| 25678 |
-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 c.1</computeroutput> |
| 25679 |
|
| 25680 |
<prompt>bash$ </prompt><userinput>ls -l [^ab]*</userinput> |
| 25681 |
<computeroutput>-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 c.1 |
| 25682 |
-rw-rw-r-- 1 bozo bozo 466 Aug 6 17:48 t2.sh |
| 25683 |
-rw-rw-r-- 1 bozo bozo 758 Jul 30 09:02 test1.txt</computeroutput> |
| 25684 |
|
| 25685 |
<prompt>bash$ </prompt><userinput>ls -l {b*,c*,*est*}</userinput> |
| 25686 |
<computeroutput>-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 b.1 |
| 25687 |
-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 c.1 |
| 25688 |
-rw-rw-r-- 1 bozo bozo 758 Jul 30 09:02 test1.txt</computeroutput> |
| 25689 |
</screen> |
| 25690 |
</para> |
| 25691 |
|
| 25692 |
|
| 25693 |
|
| 25694 |
<para>Bash performs filename expansion on unquoted command-line |
| 25695 |
arguments. The <link linkend="echoref">echo</link> command |
| 25696 |
demonstrates this.</para> |
| 25697 |
|
| 25698 |
<para> |
| 25699 |
<screen> |
| 25700 |
<prompt>bash$ </prompt><userinput>echo *</userinput> |
| 25701 |
<computeroutput>a.1 b.1 c.1 t2.sh test1.txt</computeroutput> |
| 25702 |
|
| 25703 |
<prompt>bash$ </prompt><userinput>echo t*</userinput> |
| 25704 |
<computeroutput>t2.sh test1.txt</computeroutput> |
| 25705 |
|
| 25706 |
<prompt>bash$ </prompt><userinput>echo t?.sh</userinput> |
| 25707 |
<computeroutput>t2.sh</computeroutput> |
| 25708 |
</screen> |
| 25709 |
</para> |
| 25710 |
|
| 25711 |
<note><para>It is possible to modify the way Bash interprets |
| 25712 |
special characters in globbing. A <command>set -f</command> |
| 25713 |
command disables globbing, and the |
| 25714 |
<option>nocaseglob</option> and <option>nullglob</option> |
| 25715 |
options to <link linkend="shoptref">shopt</link> change |
| 25716 |
globbing behavior.</para></note> |
| 25717 |
|
| 25718 |
<para>See also <xref linkend="listglob">.</para> |
| 25719 |
|
| 25720 |
<caution><para><anchor id="handlingfnames">Filenames with |
| 25721 |
embedded <link linkend="whitespaceref">whitespace</link> |
| 25722 |
can cause <firstterm>globbing</firstterm> to choke. |
| 25723 |
<ulink |
| 25724 |
url="http://www.dwheeler.com/essays/filenames-in-shell.html">David |
| 25725 |
Wheeler</ulink> shows how to avoid many such pitfalls.</para> |
| 25726 |
|
| 25727 |
<para><programlisting>IFS="$(printf '\n\t')" # Remove space. |
| 25728 |
|
| 25729 |
# Correct glob use: |
| 25730 |
# Always use for-loop, prefix glob, check if exists file. |
| 25731 |
for file in ./* ; do # Use ./* ... NEVER bare * |
| 25732 |
if [ -e "$file" ] ; then # Check whether file exists. |
| 25733 |
COMMAND ... "$file" ... |
| 25734 |
fi |
| 25735 |
done |
| 25736 |
|
| 25737 |
# This example taken from David Wheeler's site, with permission.</programlisting></para> |
| 25738 |
|
| 25739 |
</caution> |
| 25740 |
|
| 25741 |
</sect1> <!-- Globbing --> |
| 25742 |
|
| 25743 |
|
| 25744 |
</chapter> <!-- Regular Expressions --> |
| 25745 |
|
| 25746 |
|
| 25747 |
|
| 25748 |
<chapter id="here-docs"> |
| 25749 |
<title>Here Documents</title> |
| 25750 |
|
| 25751 |
<epigraph> |
| 25752 |
<para>Here and now, boys.</para> |
| 25753 |
<para>--Aldous Huxley, <firstterm>Island</firstterm></para> |
| 25754 |
</epigraph> |
| 25755 |
|
| 25756 |
<para><anchor id="heredocref"></para> |
| 25757 |
|
| 25758 |
<indexterm> |
| 25759 |
<primary><<</primary> |
| 25760 |
</indexterm> |
| 25761 |
<indexterm> |
| 25762 |
<primary>special character</primary> |
| 25763 |
<secondary><<</secondary> |
| 25764 |
</indexterm> |
| 25765 |
|
| 25766 |
<para>A <firstterm>here document</firstterm> is a special-purpose |
| 25767 |
code block. It uses a form of <link linkend="ioredirref">I/O |
| 25768 |
redirection</link> to feed a command list to |
| 25769 |
an interactive program or a command, such as <link |
| 25770 |
linkend="ftpref">ftp</link>, <link linkend="catref">cat</link>, |
| 25771 |
or the <firstterm>ex</firstterm> text editor.</para> |
| 25772 |
|
| 25773 |
<para><programlisting>COMMAND <<InputComesFromHERE |
| 25774 |
... |
| 25775 |
... |
| 25776 |
... |
| 25777 |
InputComesFromHERE</programlisting></para> |
| 25778 |
|
| 25779 |
|
| 25780 |
<para><anchor id="limitstringref"></para> |
| 25781 |
<para>A <firstterm>limit string</firstterm> delineates (frames) |
| 25782 |
the command list. The special symbol <token><<</token> precedes |
| 25783 |
the limit string. This has the effect of redirecting the output |
| 25784 |
of a command block into the <filename>stdin</filename> of the program |
| 25785 |
or command. It is similar to <userinput>interactive-program < |
| 25786 |
command-file</userinput>, where <filename>command-file</filename> |
| 25787 |
contains |
| 25788 |
|
| 25789 |
<programlisting>command #1 |
| 25790 |
command #2 |
| 25791 |
...</programlisting></para> |
| 25792 |
|
| 25793 |
<para>The <firstterm>here document</firstterm> equivalent looks |
| 25794 |
like this: |
| 25795 |
|
| 25796 |
<programlisting>interactive-program <<LimitString |
| 25797 |
command #1 |
| 25798 |
command #2 |
| 25799 |
... |
| 25800 |
LimitString</programlisting></para> |
| 25801 |
|
| 25802 |
<para>Choose a <firstterm>limit string</firstterm> sufficiently |
| 25803 |
unusual that it will not occur anywhere in the command list and |
| 25804 |
confuse matters.</para> |
| 25805 |
|
| 25806 |
<para>Note that <firstterm>here documents</firstterm> may sometimes |
| 25807 |
be used to good effect with non-interactive utilities and commands, |
| 25808 |
such as, for example, <link linkend="wallref">wall</link>.</para> |
| 25809 |
|
| 25810 |
|
| 25811 |
<example id="ex70"> |
| 25812 |
<title><firstterm>broadcast</firstterm>: Sends message to everyone |
| 25813 |
logged in</title> |
| 25814 |
<programlisting>&ex70;</programlisting> |
| 25815 |
</example> |
| 25816 |
|
| 25817 |
|
| 25818 |
<para><anchor id="vihere"></para> |
| 25819 |
<para>Even such unlikely candidates as the <firstterm>vi</firstterm> |
| 25820 |
text editor lend themselves to <firstterm>here |
| 25821 |
documents</firstterm>.</para> |
| 25822 |
|
| 25823 |
<example id="ex69"> |
| 25824 |
<title><firstterm>dummyfile</firstterm>: Creates a 2-line dummy |
| 25825 |
file</title> |
| 25826 |
<programlisting>&ex69;</programlisting> |
| 25827 |
</example> |
| 25828 |
|
| 25829 |
<para> |
| 25830 |
The above script could just as effectively have been implemented with |
| 25831 |
<command>ex</command>, rather than |
| 25832 |
<command>vi</command>. <anchor id="exscriptref"><firstterm>Here |
| 25833 |
documents</firstterm> containing a list of <command>ex</command> |
| 25834 |
commands are common enough to form their own category, known as |
| 25835 |
<firstterm>ex scripts</firstterm>. |
| 25836 |
|
| 25837 |
<programlisting>#!/bin/bash |
| 25838 |
# Replace all instances of "Smith" with "Jones" |
| 25839 |
#+ in files with a ".txt" filename suffix. |
| 25840 |
|
| 25841 |
ORIGINAL=Smith |
| 25842 |
REPLACEMENT=Jones |
| 25843 |
|
| 25844 |
for word in $(fgrep -l $ORIGINAL *.txt) |
| 25845 |
do |
| 25846 |
# ------------------------------------- |
| 25847 |
ex $word <<EOF |
| 25848 |
:%s/$ORIGINAL/$REPLACEMENT/g |
| 25849 |
:wq |
| 25850 |
EOF |
| 25851 |
# :%s is the "ex" substitution command. |
| 25852 |
# :wq is write-and-quit. |
| 25853 |
# ------------------------------------- |
| 25854 |
done</programlisting> |
| 25855 |
</para> |
| 25856 |
|
| 25857 |
<para><anchor id="catscriptref"></para> |
| 25858 |
<para>Analogous to <quote>ex scripts</quote> are <firstterm>cat |
| 25859 |
scripts</firstterm>.</para> |
| 25860 |
|
| 25861 |
<example id="ex71"> |
| 25862 |
<title>Multi-line message using <firstterm>cat</firstterm></title> |
| 25863 |
<programlisting>&ex71;</programlisting> |
| 25864 |
</example> |
| 25865 |
|
| 25866 |
|
| 25867 |
<para><anchor id="limitstrdash"></para> |
| 25868 |
<para>The <option>-</option> option to mark a here document limit string |
| 25869 |
(<userinput><<-LimitString</userinput>) suppresses leading |
| 25870 |
tabs (but not spaces) in the output. This may be useful in making |
| 25871 |
a script more readable.</para> |
| 25872 |
|
| 25873 |
<example id="ex71a"> |
| 25874 |
<title>Multi-line message, with tabs suppressed</title> |
| 25875 |
<programlisting>&ex71a;</programlisting> |
| 25876 |
</example> |
| 25877 |
|
| 25878 |
|
| 25879 |
<para><anchor id="herepassp"></para> |
| 25880 |
<para>A <firstterm>here document</firstterm> supports parameter and |
| 25881 |
command substitution. It is therefore possible to pass different |
| 25882 |
parameters to the body of the here document, changing its output |
| 25883 |
accordingly.</para> |
| 25884 |
|
| 25885 |
<example id="ex71b"> |
| 25886 |
<title>Here document with replaceable parameters</title> |
| 25887 |
<programlisting>&ex71b;</programlisting> |
| 25888 |
</example> |
| 25889 |
|
| 25890 |
<para><anchor id="hereparamsub"></para> |
| 25891 |
<para>This is a useful script containing a <firstterm>here |
| 25892 |
document</firstterm> with parameter substitution.</para> |
| 25893 |
|
| 25894 |
<example id="ex72"> |
| 25895 |
<title>Upload a file pair to <firstterm>Sunsite</firstterm> incoming |
| 25896 |
directory</title> |
| 25897 |
<programlisting>&ex72;</programlisting> |
| 25898 |
</example> |
| 25899 |
|
| 25900 |
|
| 25901 |
<para><anchor id="hereesc"></para> |
| 25902 |
<para>Quoting or escaping the <quote>limit string</quote> at the |
| 25903 |
head of a here document disables parameter substitution within its |
| 25904 |
body. The reason for this is that <firstterm>quoting/escaping the |
| 25905 |
limit string</firstterm> effectively <link |
| 25906 |
linkend="escp">escapes</link> the <token>$</token>, |
| 25907 |
<token>`</token>, and <token>\</token> <link |
| 25908 |
linkend="scharlist">special characters</link>, and causes them to |
| 25909 |
be interpreted literally. (Thank you, Allen Halsey, for pointing |
| 25910 |
this out.)</para> |
| 25911 |
|
| 25912 |
<example id="ex71c"> |
| 25913 |
<title>Parameter substitution turned off</title> |
| 25914 |
<programlisting>&ex71c;</programlisting> |
| 25915 |
</example> |
| 25916 |
|
| 25917 |
<para><anchor id="herelit"></para> |
| 25918 |
<para>Disabling parameter substitution permits outputting literal text. |
| 25919 |
Generating scripts or even program code is one use for this.</para> |
| 25920 |
|
| 25921 |
<example id="generatescript"> |
| 25922 |
<title>A script that generates another script</title> |
| 25923 |
<programlisting>&generatescript;</programlisting> |
| 25924 |
</example> |
| 25925 |
|
| 25926 |
|
| 25927 |
|
| 25928 |
<para><anchor id="herecs"></para> |
| 25929 |
<para> |
| 25930 |
It is possible to set a variable from the output of a here document. |
| 25931 |
This is actually a devious form of <link |
| 25932 |
linkend="commandsubref">command substitution</link>. |
| 25933 |
<programlisting>variable=$(cat <<SETVAR |
| 25934 |
This variable |
| 25935 |
runs over multiple lines. |
| 25936 |
SETVAR |
| 25937 |
) |
| 25938 |
|
| 25939 |
echo "$variable"</programlisting> |
| 25940 |
</para> |
| 25941 |
|
| 25942 |
|
| 25943 |
<para><anchor id="herefunc"></para> |
| 25944 |
<para>A here document can supply input to a function in the same |
| 25945 |
script.</para> |
| 25946 |
|
| 25947 |
<example id="hf"> |
| 25948 |
<title>Here documents and functions</title> |
| 25949 |
<programlisting>&hf;</programlisting> |
| 25950 |
</example> |
| 25951 |
|
| 25952 |
<para><anchor id="anonheredoc0"></para> |
| 25953 |
<para>It is possible to use <token>:</token> as a dummy command |
| 25954 |
accepting output from a here document. This, in effect, creates an |
| 25955 |
<quote>anonymous</quote> here document.</para> |
| 25956 |
|
| 25957 |
<example id="anonheredoc"> |
| 25958 |
<title><quote>Anonymous</quote> Here Document</title> |
| 25959 |
<programlisting>#!/bin/bash |
| 25960 |
|
| 25961 |
: <<TESTVARIABLES |
| 25962 |
${HOSTNAME?}${USER?}${MAIL?} # Print error message if one of the variables not set. |
| 25963 |
TESTVARIABLES |
| 25964 |
|
| 25965 |
exit $?</programlisting> |
| 25966 |
</example> |
| 25967 |
|
| 25968 |
|
| 25969 |
<para><anchor id="cblock1"></para> |
| 25970 |
<tip><para>A variation of the above technique permits <quote>commenting |
| 25971 |
out</quote> blocks of code.</para></tip> |
| 25972 |
|
| 25973 |
<example id="commentblock"> |
| 25974 |
<title>Commenting out a block of code</title> |
| 25975 |
<programlisting>&commentblock;</programlisting> |
| 25976 |
</example> |
| 25977 |
|
| 25978 |
|
| 25979 |
<para><anchor id="hselfdoc"></para> |
| 25980 |
<tip><para>Yet another twist of this nifty trick makes |
| 25981 |
<quote>self-documenting</quote> scripts possible.</para></tip> |
| 25982 |
|
| 25983 |
<example id="selfdocument"> |
| 25984 |
<title>A self-documenting script</title> |
| 25985 |
<programlisting>&selfdocument;</programlisting> |
| 25986 |
</example> |
| 25987 |
|
| 25988 |
<para>Using a <link linkend="catscriptref">cat script</link> is an |
| 25989 |
alternate way of accomplishing this.</para> |
| 25990 |
|
| 25991 |
<para> |
| 25992 |
<programlisting>DOC_REQUEST=70 |
| 25993 |
|
| 25994 |
if [ "$1" = "-h" -o "$1" = "--help" ] # Request help. |
| 25995 |
then # Use a "cat script" . . . |
| 25996 |
cat <<DOCUMENTATIONXX |
| 25997 |
List the statistics of a specified directory in tabular format. |
| 25998 |
--------------------------------------------------------------- |
| 25999 |
The command-line parameter gives the directory to be listed. |
| 26000 |
If no directory specified or directory specified cannot be read, |
| 26001 |
then list the current working directory. |
| 26002 |
|
| 26003 |
DOCUMENTATIONXX |
| 26004 |
exit $DOC_REQUEST |
| 26005 |
fi</programlisting> |
| 26006 |
</para> |
| 26007 |
|
| 26008 |
|
| 26009 |
<para>See also <xref linkend="isspammer2">, <xref linkend="petals">, |
| 26010 |
<xref linkend="qky">, and <xref linkend="nim"> for more examples |
| 26011 |
of self-documenting scripts.</para> |
| 26012 |
|
| 26013 |
<para><anchor id="heretemp"></para> |
| 26014 |
<note> |
| 26015 |
<para>Here documents create temporary files, but these |
| 26016 |
files are deleted after opening and are not accessible to |
| 26017 |
any other process.</para> |
| 26018 |
<para> |
| 26019 |
<screen><prompt>bash$ </prompt><userinput>bash -c 'lsof -a -p $$ -d0' << EOF</userinput> |
| 26020 |
<prompt>> </prompt><userinput>EOF</userinput> |
| 26021 |
<computeroutput>lsof 1213 bozo 0r REG 3,5 0 30386 /tmp/t1213-0-sh (deleted)</computeroutput> |
| 26022 |
</screen> |
| 26023 |
</para> |
| 26024 |
</note> |
| 26025 |
|
| 26026 |
<caution><para>Some utilities will not work inside a |
| 26027 |
<firstterm>here document</firstterm>.</para></caution> |
| 26028 |
|
| 26029 |
<para><anchor id="indentedls"></para> |
| 26030 |
|
| 26031 |
<warning> |
| 26032 |
|
| 26033 |
<para>The closing <firstterm>limit string</firstterm>, |
| 26034 |
on the final line of a here document, must start in the |
| 26035 |
<emphasis>first</emphasis> character position. There can |
| 26036 |
be <emphasis>no leading whitespace</emphasis>. Trailing |
| 26037 |
whitespace after the limit string likewise causes unexpected |
| 26038 |
behavior. The whitespace prevents the limit string from being |
| 26039 |
recognized. |
| 26040 |
|
| 26041 |
<footnote><para>Except, as Dennis Benzinger points out, |
| 26042 |
if <link linkend="limitstrdash">using |
| 26043 |
<command><<-</command> to suppress |
| 26044 |
tabs</link>.</para></footnote> |
| 26045 |
|
| 26046 |
</para> |
| 26047 |
|
| 26048 |
|
| 26049 |
|
| 26050 |
<para> |
| 26051 |
<programlisting>#!/bin/bash |
| 26052 |
|
| 26053 |
echo "----------------------------------------------------------------------" |
| 26054 |
|
| 26055 |
cat <<LimitString |
| 26056 |
echo "This is line 1 of the message inside the here document." |
| 26057 |
echo "This is line 2 of the message inside the here document." |
| 26058 |
echo "This is the final line of the message inside the here document." |
| 26059 |
LimitString |
| 26060 |
#^^^^Indented limit string. Error! This script will not behave as expected. |
| 26061 |
|
| 26062 |
echo "----------------------------------------------------------------------" |
| 26063 |
|
| 26064 |
# These comments are outside the 'here document', |
| 26065 |
#+ and should not echo. |
| 26066 |
|
| 26067 |
echo "Outside the here document." |
| 26068 |
|
| 26069 |
exit 0 |
| 26070 |
|
| 26071 |
echo "This line had better not echo." # Follows an 'exit' command.</programlisting> |
| 26072 |
</para> |
| 26073 |
</warning> |
| 26074 |
|
| 26075 |
<caution> |
| 26076 |
<para><anchor id="exclls">Some people very cleverly use a |
| 26077 |
single <token>!</token> as a limit string. But, that's not |
| 26078 |
necessarily a good idea.</para> |
| 26079 |
<para><programlisting># This works. |
| 26080 |
cat <<! |
| 26081 |
Hello! |
| 26082 |
! Three more exclamations !!! |
| 26083 |
! |
| 26084 |
|
| 26085 |
|
| 26086 |
# But . . . |
| 26087 |
cat <<! |
| 26088 |
Hello! |
| 26089 |
Single exclamation point follows! |
| 26090 |
! |
| 26091 |
! |
| 26092 |
# Crashes with an error message. |
| 26093 |
|
| 26094 |
|
| 26095 |
# However, the following will work. |
| 26096 |
cat <<EOF |
| 26097 |
Hello! |
| 26098 |
Single exclamation point follows! |
| 26099 |
! |
| 26100 |
EOF |
| 26101 |
# It's safer to use a multi-character limit string.</programlisting></para> |
| 26102 |
</caution> |
| 26103 |
|
| 26104 |
|
| 26105 |
|
| 26106 |
<para>For those tasks too complex for a <firstterm>here |
| 26107 |
document</firstterm>, consider using the |
| 26108 |
<replaceable>expect</replaceable> scripting language, which |
| 26109 |
was specifically designed for feeding input into interactive |
| 26110 |
programs.</para> |
| 26111 |
|
| 26112 |
|
| 26113 |
|
| 26114 |
<sect1><title>Here Strings</title> |
| 26115 |
|
| 26116 |
<para><anchor id="herestringsref"></para> |
| 26117 |
|
| 26118 |
<blockquote> |
| 26119 |
<literallayout> |
| 26120 |
A <firstterm>here string</firstterm> can be considered as a stripped-down form of a <firstterm>here document</firstterm>. |
| 26121 |
It consists of nothing more than <command>COMMAND <<< $WORD</command>, |
| 26122 |
where <varname>$WORD</varname> is expanded and fed to the <filename>stdin</filename> of <command>COMMAND</command>. |
| 26123 |
</literallayout> |
| 26124 |
</blockquote> |
| 26125 |
|
| 26126 |
<para>As a simple example, consider this alternative to the <link |
| 26127 |
linkend="echogrepref">echo-grep</link> construction.</para> |
| 26128 |
|
| 26129 |
<para> |
| 26130 |
<programlisting># Instead of: |
| 26131 |
if echo "$VAR" | grep -q txt # if [[ $VAR = *txt* ]] |
| 26132 |
# etc. |
| 26133 |
|
| 26134 |
# Try: |
| 26135 |
if grep -q "txt" <<< "$VAR" |
| 26136 |
then # ^^^ |
| 26137 |
echo "$VAR contains the substring sequence \"txt\"" |
| 26138 |
fi |
| 26139 |
# Thank you, Sebastian Kaminski, for the suggestion.</programlisting> |
| 26140 |
</para> |
| 26141 |
|
| 26142 |
<para><anchor id="hsread"></para> |
| 26143 |
<para>Or, in combination with <link linkend="readref">read</link>:</para> |
| 26144 |
|
| 26145 |
<para> |
| 26146 |
<programlisting>String="This is a string of words." |
| 26147 |
|
| 26148 |
read -r -a Words <<< "$String" |
| 26149 |
# The -a option to "read" |
| 26150 |
#+ assigns the resulting values to successive members of an array. |
| 26151 |
|
| 26152 |
echo "First word in String is: ${Words[0]}" # This |
| 26153 |
echo "Second word in String is: ${Words[1]}" # is |
| 26154 |
echo "Third word in String is: ${Words[2]}" # a |
| 26155 |
echo "Fourth word in String is: ${Words[3]}" # string |
| 26156 |
echo "Fifth word in String is: ${Words[4]}" # of |
| 26157 |
echo "Sixth word in String is: ${Words[5]}" # words. |
| 26158 |
echo "Seventh word in String is: ${Words[6]}" # (null) |
| 26159 |
# Past end of $String. |
| 26160 |
|
| 26161 |
# Thank you, Francisco Lobo, for the suggestion.</programlisting> |
| 26162 |
</para> |
| 26163 |
|
| 26164 |
<para><anchor id="hsloop">It is, of course, possible to feed |
| 26165 |
the output of a <firstterm>here string</firstterm> |
| 26166 |
into the <filename>stdin</filename> of a <link |
| 26167 |
linkend="loopref00">loop</link>.</para> |
| 26168 |
|
| 26169 |
<para><programlisting># As Seamus points out . . . |
| 26170 |
|
| 26171 |
ArrayVar=( element0 element1 element2 {A..D} ) |
| 26172 |
|
| 26173 |
while read element ; do |
| 26174 |
echo "$element" 1>&2 |
| 26175 |
done <<< $(echo ${ArrayVar[*]}) |
| 26176 |
|
| 26177 |
# element0 element1 element2 A B C D</programlisting></para> |
| 26178 |
|
| 26179 |
<para><anchor id="hspre"></para> |
| 26180 |
<example id="prependex"> |
| 26181 |
<title>Prepending a line to a file</title> |
| 26182 |
<programlisting>&prependex;</programlisting> |
| 26183 |
</example> |
| 26184 |
|
| 26185 |
<example id="mailboxgrep"> |
| 26186 |
<title>Parsing a mailbox</title> |
| 26187 |
<programlisting>&mailboxgrep;</programlisting> |
| 26188 |
</example> |
| 26189 |
|
| 26190 |
<para>Exercise: Find other uses for <firstterm>here |
| 26191 |
strings</firstterm>, such as, for example, <link |
| 26192 |
linkend="goldenratio">feeding input to |
| 26193 |
<firstterm>dc</firstterm></link>.</para> |
| 26194 |
|
| 26195 |
</sect1><!-- Here Strings --> |
| 26196 |
|
| 26197 |
|
| 26198 |
</chapter> <!-- Here Documents --> |
| 26199 |
|
| 26200 |
|
| 26201 |
|
| 26202 |
<chapter id="io-redirection"> |
| 26203 |
<title>I/O Redirection</title> |
| 26204 |
|
| 26205 |
<para><anchor id="ioredirref"></para> |
| 26206 |
|
| 26207 |
<para>There are always three default <firstterm>files</firstterm> |
| 26208 |
<footnote><para>By convention in UNIX and Linux, data streams |
| 26209 |
and peripherals (<link linkend="devfileref">device files</link>) |
| 26210 |
are treated as files, in a fashion analogous to ordinary |
| 26211 |
files.</para></footnote> |
| 26212 |
open, <filename>stdin</filename> (the keyboard), |
| 26213 |
<filename>stdout</filename> (the screen), and |
| 26214 |
<filename>stderr</filename> (error messages output to the |
| 26215 |
screen). These, and any other open files, can be redirected. |
| 26216 |
Redirection simply means capturing output from a file, command, |
| 26217 |
program, script, or even code block within a script (see <xref |
| 26218 |
linkend="ex8"> and <xref linkend="rpmcheck">) and sending it as |
| 26219 |
input to another file, command, program, or script.</para> |
| 26220 |
|
| 26221 |
<para><anchor id="fdref">Each open file gets assigned a file descriptor. |
| 26222 |
|
| 26223 |
<footnote><para><anchor id="fdref1">A <firstterm>file |
| 26224 |
descriptor</firstterm> is simply a number that |
| 26225 |
the operating system assigns to an open file |
| 26226 |
to keep track of it. Consider it a simplified |
| 26227 |
type of file pointer. It is analogous |
| 26228 |
to a <firstterm>file handle</firstterm> in |
| 26229 |
<command>C</command>.</para></footnote> |
| 26230 |
|
| 26231 |
The file descriptors for <filename>stdin</filename>, |
| 26232 |
<filename>stdout</filename>, and <filename>stderr</filename> are |
| 26233 |
0, 1, and 2, respectively. For opening additional files, there |
| 26234 |
remain descriptors 3 to 9. It is sometimes useful to assign one of |
| 26235 |
these additional file descriptors to <filename>stdin</filename>, |
| 26236 |
<filename>stdout</filename>, or <filename>stderr</filename> |
| 26237 |
as a temporary duplicate link. |
| 26238 |
<footnote><para>Using <replaceable>file |
| 26239 |
descriptor 5</replaceable> might cause problems. |
| 26240 |
When Bash creates a child process, as with <link |
| 26241 |
linkend="execref">exec</link>, the child inherits |
| 26242 |
fd 5 (see Chet Ramey's archived e-mail, <ulink |
| 26243 |
url="http://groups.google.com/group/gnu.bash.bug/browse_thread/thread/13955daafded3b5c/18c17050087f9f37"> |
| 26244 |
SUBJECT: RE: File descriptor 5 is held open</ulink>). |
| 26245 |
Best leave this particular fd alone.</para></footnote> |
| 26246 |
This simplifies restoration to normal after complex redirection |
| 26247 |
and reshuffling (see <xref linkend="redir1">).</para> |
| 26248 |
|
| 26249 |
<para><anchor id="ioredirectionref"></para> |
| 26250 |
|
| 26251 |
<programlisting> COMMAND_OUTPUT > |
| 26252 |
# Redirect stdout to a file. |
| 26253 |
# Creates the file if not present, otherwise overwrites it. |
| 26254 |
|
| 26255 |
ls -lR > dir-tree.list |
| 26256 |
# Creates a file containing a listing of the directory tree. |
| 26257 |
|
| 26258 |
: > filename |
| 26259 |
# The > truncates file "filename" to zero length. |
| 26260 |
# If file not present, creates zero-length file (same effect as 'touch'). |
| 26261 |
# The : serves as a dummy placeholder, producing no output. |
| 26262 |
|
| 26263 |
> filename |
| 26264 |
# The > truncates file "filename" to zero length. |
| 26265 |
# If file not present, creates zero-length file (same effect as 'touch'). |
| 26266 |
# (Same result as ": >", above, but this does not work with some shells.) |
| 26267 |
|
| 26268 |
COMMAND_OUTPUT >> |
| 26269 |
# Redirect stdout to a file. |
| 26270 |
# Creates the file if not present, otherwise appends to it. |
| 26271 |
|
| 26272 |
|
| 26273 |
# Single-line redirection commands (affect only the line they are on): |
| 26274 |
# -------------------------------------------------------------------- |
| 26275 |
|
| 26276 |
1>filename |
| 26277 |
# Redirect stdout to file "filename." |
| 26278 |
1>>filename |
| 26279 |
# Redirect and append stdout to file "filename." |
| 26280 |
2>filename |
| 26281 |
# Redirect stderr to file "filename." |
| 26282 |
2>>filename |
| 26283 |
# Redirect and append stderr to file "filename." |
| 26284 |
&>filename |
| 26285 |
# Redirect both stdout and stderr to file "filename." |
| 26286 |
# This operator is now functional, as of Bash 4, final release. |
| 26287 |
|
| 26288 |
M>N |
| 26289 |
# "M" is a file descriptor, which defaults to 1, if not explicitly set. |
| 26290 |
# "N" is a filename. |
| 26291 |
# File descriptor "M" is redirect to file "N." |
| 26292 |
M>&N |
| 26293 |
# "M" is a file descriptor, which defaults to 1, if not set. |
| 26294 |
# "N" is another file descriptor. |
| 26295 |
|
| 26296 |
#============================================================================== |
| 26297 |
|
| 26298 |
# Redirecting stdout, one line at a time. |
| 26299 |
LOGFILE=script.log |
| 26300 |
|
| 26301 |
echo "This statement is sent to the log file, \"$LOGFILE\"." 1>$LOGFILE |
| 26302 |
echo "This statement is appended to \"$LOGFILE\"." 1>>$LOGFILE |
| 26303 |
echo "This statement is also appended to \"$LOGFILE\"." 1>>$LOGFILE |
| 26304 |
echo "This statement is echoed to stdout, and will not appear in \"$LOGFILE\"." |
| 26305 |
# These redirection commands automatically "reset" after each line. |
| 26306 |
|
| 26307 |
|
| 26308 |
|
| 26309 |
# Redirecting stderr, one line at a time. |
| 26310 |
ERRORFILE=script.errors |
| 26311 |
|
| 26312 |
bad_command1 2>$ERRORFILE # Error message sent to $ERRORFILE. |
| 26313 |
bad_command2 2>>$ERRORFILE # Error message appended to $ERRORFILE. |
| 26314 |
bad_command3 # Error message echoed to stderr, |
| 26315 |
#+ and does not appear in $ERRORFILE. |
| 26316 |
# These redirection commands also automatically "reset" after each line. |
| 26317 |
#=======================================================================</programlisting> |
| 26318 |
|
| 26319 |
|
| 26320 |
<para><anchor id="ioredirectionref1"></para> |
| 26321 |
|
| 26322 |
<programlisting> |
| 26323 |
2>&1 |
| 26324 |
# Redirects stderr to stdout. |
| 26325 |
# Error messages get sent to same place as standard output. |
| 26326 |
>>filename 2>&1 |
| 26327 |
bad_command >>filename 2>&1 |
| 26328 |
# Appends both stdout and stderr to the file "filename" ... |
| 26329 |
2>&1 | [command(s)] |
| 26330 |
bad_command 2>&1 | awk '{print $5}' # found |
| 26331 |
# Sends stderr through a pipe. |
| 26332 |
# |& was added to Bash 4 as an abbreviation for 2>&1 |. |
| 26333 |
|
| 26334 |
i>&j |
| 26335 |
# Redirects file descriptor <emphasis>i</emphasis> to <emphasis>j</emphasis>. |
| 26336 |
# All output of file pointed to by <emphasis>i</emphasis> gets sent to file pointed to by <emphasis>j</emphasis>. |
| 26337 |
|
| 26338 |
>&j |
| 26339 |
# Redirects, by default, file descriptor <emphasis>1</emphasis> (stdout) to <emphasis>j</emphasis>. |
| 26340 |
# All stdout gets sent to file pointed to by <emphasis>j</emphasis>.</programlisting> |
| 26341 |
|
| 26342 |
<para><anchor id="ioredirectionref2"></para> |
| 26343 |
|
| 26344 |
<programlisting> |
| 26345 |
0< FILENAME |
| 26346 |
< FILENAME |
| 26347 |
# Accept input from a file. |
| 26348 |
# Companion command to <quote>></quote>, and often used in combination with it. |
| 26349 |
# |
| 26350 |
# grep search-word <filename |
| 26351 |
|
| 26352 |
|
| 26353 |
[j]<>filename |
| 26354 |
# Open file "filename" for reading and writing, |
| 26355 |
#+ and assign file descriptor "j" to it. |
| 26356 |
# If "filename" does not exist, create it. |
| 26357 |
# If file descriptor "j" is not specified, default to fd 0, stdin. |
| 26358 |
# |
| 26359 |
# An application of this is writing at a specified place in a file. |
| 26360 |
echo 1234567890 > File # Write string to "File". |
| 26361 |
exec 3<> File # Open "File" and assign fd 3 to it. |
| 26362 |
read -n 4 <&3 # Read only 4 characters. |
| 26363 |
echo -n . >&3 # Write a decimal point there. |
| 26364 |
exec 3>&- # Close fd 3. |
| 26365 |
cat File # ==> 1234.67890 |
| 26366 |
# Random access, by golly. |
| 26367 |
|
| 26368 |
|
| 26369 |
|
| 26370 |
| |
| 26371 |
# Pipe. |
| 26372 |
# General purpose process and command chaining tool. |
| 26373 |
# Similar to <quote>></quote>, but more general in effect. |
| 26374 |
# Useful for chaining commands, scripts, files, and programs together. |
| 26375 |
cat *.txt | sort | uniq > result-file |
| 26376 |
# Sorts the output of all the .txt files and deletes duplicate lines, |
| 26377 |
# finally saves results to <quote>result-file</quote>.</programlisting> |
| 26378 |
|
| 26379 |
<para>Multiple instances of input and output redirection |
| 26380 |
and/or pipes can be combined in a single command |
| 26381 |
line. |
| 26382 |
|
| 26383 |
<programlisting>command < input-file > output-file |
| 26384 |
# Or the equivalent: |
| 26385 |
< input-file command > output-file # Although this is non-standard. |
| 26386 |
|
| 26387 |
command1 | command2 | command3 > output-file</programlisting> |
| 26388 |
See <xref linkend="derpm"> and <xref linkend="fifo">.</para> |
| 26389 |
|
| 26390 |
|
| 26391 |
<para>Multiple output streams may be redirected to one file. |
| 26392 |
|
| 26393 |
<programlisting>ls -yz >> command.log 2>&1 |
| 26394 |
# Capture result of illegal options "yz" in file "command.log." |
| 26395 |
# Because stderr is redirected to the file, |
| 26396 |
#+ any error messages will also be there. |
| 26397 |
|
| 26398 |
# Note, however, that the following does *not* give the same result. |
| 26399 |
ls -yz 2>&1 >> command.log |
| 26400 |
# Outputs an error message, but does not write to file. |
| 26401 |
# More precisely, the command output (in this case, null) |
| 26402 |
#+ writes to the file, but the error message goes only to stdout. |
| 26403 |
|
| 26404 |
# If redirecting both stdout and stderr, |
| 26405 |
#+ the order of the commands makes a difference. |
| 26406 |
</programlisting></para> |
| 26407 |
|
| 26408 |
|
| 26409 |
<variablelist id="closingfiledescriptors"> |
| 26410 |
<title><anchor id="cfd">Closing File Descriptors</title> |
| 26411 |
|
| 26412 |
<varlistentry> |
| 26413 |
<term><token>n<&-</token></term> |
| 26414 |
<listitem> |
| 26415 |
<para>Close input file descriptor |
| 26416 |
<replaceable>n</replaceable>.</para> |
| 26417 |
</listitem> |
| 26418 |
</varlistentry> |
| 26419 |
|
| 26420 |
<varlistentry> |
| 26421 |
<term><token>0<&-</token></term> |
| 26422 |
<term><token><&-</token></term> |
| 26423 |
<listitem> |
| 26424 |
<para>Close <filename>stdin</filename>.</para> |
| 26425 |
</listitem> |
| 26426 |
</varlistentry> |
| 26427 |
|
| 26428 |
<varlistentry> |
| 26429 |
<term><token>n>&-</token></term> |
| 26430 |
<listitem> |
| 26431 |
<para>Close output file descriptor <replaceable>n</replaceable>.</para> |
| 26432 |
</listitem> |
| 26433 |
</varlistentry> |
| 26434 |
|
| 26435 |
<varlistentry> |
| 26436 |
<term><token>1>&-</token></term> |
| 26437 |
<term><token>>&-</token></term> |
| 26438 |
<listitem> |
| 26439 |
<para>Close <filename>stdout</filename>.</para> |
| 26440 |
</listitem> |
| 26441 |
</varlistentry> |
| 26442 |
|
| 26443 |
</variablelist> |
| 26444 |
|
| 26445 |
|
| 26446 |
<para><anchor id="fdref2"></para> |
| 26447 |
<para>Child processes inherit open file descriptors. This is |
| 26448 |
why pipes work. To prevent an fd from being inherited, close it. |
| 26449 |
<programlisting># Redirecting only stderr to a pipe. |
| 26450 |
|
| 26451 |
exec 3>&1 # Save current "value" of stdout. |
| 26452 |
ls -l 2>&1 >&3 3>&- | grep bad 3>&- # Close fd 3 for 'grep' (but not 'ls'). |
| 26453 |
# ^^^^ ^^^^ |
| 26454 |
exec 3>&- # Now close it for the remainder of the script. |
| 26455 |
|
| 26456 |
# Thanks, S.C.</programlisting> |
| 26457 |
</para> |
| 26458 |
|
| 26459 |
<para>For a more detailed introduction to I/O redirection see |
| 26460 |
<xref linkend="ioredirintro">.</para> |
| 26461 |
|
| 26462 |
|
| 26463 |
|
| 26464 |
|
| 26465 |
<sect1><title>Using <firstterm>exec</firstterm></title> |
| 26466 |
|
| 26467 |
|
| 26468 |
<para><anchor id="usingexecref"></para> |
| 26469 |
|
| 26470 |
<para>An <command>exec <filename</command> command redirects |
| 26471 |
<filename>stdin</filename> to a file. From that point on, all |
| 26472 |
<filename>stdin</filename> comes from that file, rather than |
| 26473 |
its normal source (usually keyboard input). This provides a |
| 26474 |
method of reading a file line by line and possibly parsing |
| 26475 |
each line of input using <link linkend="sedref">sed</link> |
| 26476 |
and/or <link linkend="awkref">awk</link>.</para> |
| 26477 |
|
| 26478 |
<example id="redir1"> |
| 26479 |
<title>Redirecting <filename>stdin</filename> using |
| 26480 |
<firstterm>exec</firstterm></title> |
| 26481 |
<programlisting>&redir1;</programlisting> |
| 26482 |
</example> |
| 26483 |
|
| 26484 |
<para>Similarly, an <command>exec >filename</command> |
| 26485 |
command redirects <filename>stdout</filename> to a designated |
| 26486 |
file. This sends all command output that would normally go |
| 26487 |
to <filename>stdout</filename> to that file.</para> |
| 26488 |
|
| 26489 |
<important> |
| 26490 |
<para> |
| 26491 |
<command>exec N > filename</command> affects the entire |
| 26492 |
script or <firstterm>current shell</firstterm>. Redirection in |
| 26493 |
the <link linkend="processidref">PID</link> of the script or shell |
| 26494 |
from that point on has changed. However . . . |
| 26495 |
</para> |
| 26496 |
<para> |
| 26497 |
<command>N > filename</command> affects only the newly-forked process, |
| 26498 |
not the entire script or shell. |
| 26499 |
</para> |
| 26500 |
<para>Thank you, Ahmed Darwish, for pointing this out.</para> |
| 26501 |
</important> |
| 26502 |
|
| 26503 |
|
| 26504 |
|
| 26505 |
<example id="reassignstdout"> |
| 26506 |
<title>Redirecting <filename>stdout</filename> using |
| 26507 |
<firstterm>exec</firstterm></title> |
| 26508 |
<programlisting>&reassignstdout;</programlisting> |
| 26509 |
</example> |
| 26510 |
|
| 26511 |
<example id="upperconv"> |
| 26512 |
<title>Redirecting both <filename>stdin</filename> and |
| 26513 |
<filename>stdout</filename> in the same script with |
| 26514 |
<firstterm>exec</firstterm></title> |
| 26515 |
<programlisting>&upperconv;</programlisting> |
| 26516 |
</example> |
| 26517 |
|
| 26518 |
<para>I/O redirection is a clever way of avoiding the dreaded <link |
| 26519 |
linkend="parvis">inaccessible variables within a subshell</link> |
| 26520 |
problem.</para> |
| 26521 |
|
| 26522 |
<example id="avoidsubshell"> |
| 26523 |
<title>Avoiding a subshell</title> |
| 26524 |
<programlisting>&avoidsubshell;</programlisting> |
| 26525 |
</example> |
| 26526 |
|
| 26527 |
|
| 26528 |
|
| 26529 |
</sect1><!-- Using exec For Redirection --> |
| 26530 |
|
| 26531 |
|
| 26532 |
|
| 26533 |
<sect1 id="redircb"><title>Redirecting Code Blocks</title> |
| 26534 |
|
| 26535 |
<para><anchor id="redirref">Blocks of code, such as <link |
| 26536 |
linkend="whileloopref">while</link>, <link |
| 26537 |
linkend="untilloopref">until</link>, and <link |
| 26538 |
linkend="forloopref1">for</link> loops, even <link |
| 26539 |
linkend="ifthen">if/then</link> test blocks can also incorporate |
| 26540 |
redirection of <filename>stdin</filename>. Even a function may |
| 26541 |
use this form of redirection (see <xref linkend="realname">). |
| 26542 |
The <token><</token> operator at the end of the code block |
| 26543 |
accomplishes this.</para> |
| 26544 |
|
| 26545 |
<example id="redir2"> |
| 26546 |
<title>Redirected <firstterm>while</firstterm> loop</title> |
| 26547 |
<programlisting>&redir2;</programlisting> |
| 26548 |
</example> |
| 26549 |
|
| 26550 |
<example id="redir2a"> |
| 26551 |
<title>Alternate form of redirected <firstterm>while</firstterm> loop</title> |
| 26552 |
<programlisting>&redir2a;</programlisting> |
| 26553 |
</example> |
| 26554 |
|
| 26555 |
<example id="redir3"> |
| 26556 |
<title>Redirected <firstterm>until</firstterm> loop</title> |
| 26557 |
<programlisting>&redir3;</programlisting> |
| 26558 |
</example> |
| 26559 |
|
| 26560 |
<example id="redir4"> |
| 26561 |
<title>Redirected <firstterm>for</firstterm> loop</title> |
| 26562 |
<programlisting>&redir4;</programlisting> |
| 26563 |
</example> |
| 26564 |
|
| 26565 |
<para>We can modify the previous example to also redirect the output of |
| 26566 |
the loop.</para> |
| 26567 |
|
| 26568 |
<example id="redir4a"> |
| 26569 |
<title>Redirected <firstterm>for</firstterm> loop (both |
| 26570 |
<filename>stdin</filename> and <filename>stdout</filename> |
| 26571 |
redirected)</title> |
| 26572 |
<programlisting>&redir4a;</programlisting> |
| 26573 |
</example> |
| 26574 |
|
| 26575 |
<example id="redir5"> |
| 26576 |
<title>Redirected <firstterm>if/then</firstterm> test</title> |
| 26577 |
<programlisting>&redir5;</programlisting> |
| 26578 |
</example> |
| 26579 |
|
| 26580 |
<example id="namesdata"> |
| 26581 |
<title>Data file <firstterm>names.data</firstterm> for above |
| 26582 |
examples</title> |
| 26583 |
<programlisting>&namesdata;</programlisting> |
| 26584 |
</example> |
| 26585 |
|
| 26586 |
<para>Redirecting the <filename>stdout</filename> of a code |
| 26587 |
block has the effect of saving its output to a file. See <xref |
| 26588 |
linkend="rpmcheck">.</para> |
| 26589 |
|
| 26590 |
<para><link linkend="heredocref">Here documents</link> |
| 26591 |
are a special case of redirected code blocks. That being the case, |
| 26592 |
it should be possible to feed the output of a <firstterm>here |
| 26593 |
document</firstterm> into the <filename>stdin</filename> for a |
| 26594 |
<firstterm>while loop</firstterm>.</para> |
| 26595 |
|
| 26596 |
<para> |
| 26597 |
<programlisting># This example by Albert Siersema |
| 26598 |
# Used with permission (thanks!). |
| 26599 |
|
| 26600 |
function doesOutput() |
| 26601 |
# Could be an external command too, of course. |
| 26602 |
# Here we show you can use a function as well. |
| 26603 |
{ |
| 26604 |
ls -al *.jpg | awk '{print $5,$9}' |
| 26605 |
} |
| 26606 |
|
| 26607 |
|
| 26608 |
nr=0 # We want the while loop to be able to manipulate these and |
| 26609 |
totalSize=0 #+ to be able to see the changes after the 'while' finished. |
| 26610 |
|
| 26611 |
while read fileSize fileName ; do |
| 26612 |
echo "$fileName is $fileSize bytes" |
| 26613 |
let nr++ |
| 26614 |
totalSize=$((totalSize+fileSize)) # Or: "let totalSize+=fileSize" |
| 26615 |
done<<EOF |
| 26616 |
$(doesOutput) |
| 26617 |
EOF |
| 26618 |
|
| 26619 |
echo "$nr files totaling $totalSize bytes"</programlisting> |
| 26620 |
</para> |
| 26621 |
|
| 26622 |
</sect1><!-- Redirecting Code Blocks --> |
| 26623 |
|
| 26624 |
|
| 26625 |
|
| 26626 |
<sect1 id="redirapps"><title>Applications</title> |
| 26627 |
|
| 26628 |
<para>Clever use of I/O redirection permits parsing and stitching |
| 26629 |
together snippets of command output (see <xref |
| 26630 |
linkend="readredir">). This permits |
| 26631 |
generating report and log files.</para> |
| 26632 |
|
| 26633 |
<example id="logevents"> |
| 26634 |
<title>Logging events</title> |
| 26635 |
<programlisting>&logevents;</programlisting> |
| 26636 |
</example> |
| 26637 |
|
| 26638 |
|
| 26639 |
</sect1><!-- Applications --> |
| 26640 |
|
| 26641 |
|
| 26642 |
</chapter> <!-- I/O Redirection --> |
| 26643 |
|
| 26644 |
|
| 26645 |
|
| 26646 |
|
| 26647 |
<chapter id="subshells"> |
| 26648 |
<title>Subshells</title> |
| 26649 |
|
| 26650 |
<para><anchor id="subshellsref"></para> |
| 26651 |
|
| 26652 |
<para>Running a shell script launches a new process, a |
| 26653 |
<firstterm>subshell</firstterm>.</para> |
| 26654 |
|
| 26655 |
<sidebar> |
| 26656 |
<para><userinput>Definition:</userinput> |
| 26657 |
A <firstterm>subshell</firstterm> is a |
| 26658 |
<link linkend="childref2">child process</link> launched by a |
| 26659 |
shell (or <firstterm>shell script</firstterm>).</para> |
| 26660 |
</sidebar> |
| 26661 |
|
| 26662 |
<para>A subshell is a separate instance of the command processor |
| 26663 |
-- the <firstterm>shell</firstterm> that gives you the prompt at |
| 26664 |
the console or in an <firstterm>xterm</firstterm> window. Just |
| 26665 |
as your commands are interpreted at the command-line prompt, |
| 26666 |
similarly does a script <link |
| 26667 |
linkend="batchprocref">batch-process</link> a list of |
| 26668 |
commands. Each shell script running is, in effect, a subprocess |
| 26669 |
(<firstterm>child process</firstterm>) of the <link |
| 26670 |
linkend="forkref">parent</link> shell.</para> |
| 26671 |
|
| 26672 |
|
| 26673 |
<para>A shell script can itself launch subprocesses. These |
| 26674 |
<firstterm>subshells</firstterm> let the script do |
| 26675 |
parallel processing, in effect executing multiple subtasks |
| 26676 |
simultaneously.</para> |
| 26677 |
|
| 26678 |
|
| 26679 |
<para> |
| 26680 |
<programlisting>#!/bin/bash |
| 26681 |
# subshell-test.sh |
| 26682 |
|
| 26683 |
( |
| 26684 |
# Inside parentheses, and therefore a subshell . . . |
| 26685 |
while [ 1 ] # Endless loop. |
| 26686 |
do |
| 26687 |
echo "Subshell running . . ." |
| 26688 |
done |
| 26689 |
) |
| 26690 |
|
| 26691 |
# Script will run forever, |
| 26692 |
#+ or at least until terminated by a Ctl-C. |
| 26693 |
|
| 26694 |
exit $? # End of script (but will never get here). |
| 26695 |
|
| 26696 |
|
| 26697 |
|
| 26698 |
Now, run the script: |
| 26699 |
sh subshell-test.sh |
| 26700 |
|
| 26701 |
And, while the script is running, from a different xterm: |
| 26702 |
ps -ef | grep subshell-test.sh |
| 26703 |
|
| 26704 |
UID PID PPID C STIME TTY TIME CMD |
| 26705 |
500 2698 2502 0 14:26 pts/4 00:00:00 sh subshell-test.sh |
| 26706 |
500 2699 2698 21 14:26 pts/4 00:00:24 sh subshell-test.sh |
| 26707 |
|
| 26708 |
^^^^ |
| 26709 |
|
| 26710 |
Analysis: |
| 26711 |
PID 2698, the script, launched PID 2699, the subshell. |
| 26712 |
|
| 26713 |
Note: The "UID ..." line would be filtered out by the "grep" command, |
| 26714 |
but is shown here for illustrative purposes.</programlisting> |
| 26715 |
</para> |
| 26716 |
|
| 26717 |
|
| 26718 |
<para>In general, an <link linkend="externalref">external |
| 26719 |
command</link> in a script <link linkend="forkref">forks |
| 26720 |
off</link> a subprocess, |
| 26721 |
<footnote><para>An external command invoked with an <link |
| 26722 |
linkend="execref">exec</link> does <emphasis>not</emphasis> |
| 26723 |
(usually) fork off a subprocess / subshell.</para></footnote> |
| 26724 |
whereas a Bash <link linkend="builtinref">builtin</link> |
| 26725 |
does not. For this reason, builtins execute more quickly |
| 26726 |
and use fewer system resources than their external command |
| 26727 |
equivalents.</para> |
| 26728 |
|
| 26729 |
|
| 26730 |
<variablelist id="subshellparens"> |
| 26731 |
<title><anchor id="subshellparens1">Command List within |
| 26732 |
Parentheses</title> |
| 26733 |
|
| 26734 |
<varlistentry> |
| 26735 |
<term>( command1; command2; command3; ... )</term> |
| 26736 |
<listitem> |
| 26737 |
<para>A command list embedded between |
| 26738 |
<replaceable>parentheses</replaceable> runs as a |
| 26739 |
subshell.</para> |
| 26740 |
</listitem> |
| 26741 |
</varlistentry> |
| 26742 |
</variablelist> |
| 26743 |
|
| 26744 |
<para><anchor id="parvis">Variables in a subshell are |
| 26745 |
<emphasis>not</emphasis> visible outside the block of code |
| 26746 |
in the subshell. They are not accessible to the <link |
| 26747 |
linkend="forkref">parent process</link>, to the shell |
| 26748 |
that launched the subshell. These are, in effect, |
| 26749 |
variables <link linkend="localref">local</link> to the |
| 26750 |
<firstterm>child process</firstterm>.</para> |
| 26751 |
|
| 26752 |
<example id="subshell"> |
| 26753 |
<title>Variable scope in a subshell</title> |
| 26754 |
<programlisting>&subshell;</programlisting> |
| 26755 |
</example> |
| 26756 |
<para>See also <link linkend="bashpidref">$BASHPID</link> and |
| 26757 |
<xref linkend="subpit">.</para> |
| 26758 |
|
| 26759 |
<sidebar> |
| 26760 |
<para><anchor id="scoperef"></para> |
| 26761 |
<para><userinput>Definition:</userinput> The |
| 26762 |
<firstterm>scope</firstterm> of a variable is the |
| 26763 |
context in which it has meaning, in which it has a |
| 26764 |
<firstterm>value</firstterm> that can be referenced. For |
| 26765 |
example, the scope of a <link linkend="localref1">local |
| 26766 |
variable</link> lies only within the function, |
| 26767 |
block of code, or subshell within which it is defined, |
| 26768 |
while the scope of a <firstterm>global</firstterm> variable |
| 26769 |
is the entire script in which it appears.</para> |
| 26770 |
</sidebar> |
| 26771 |
|
| 26772 |
|
| 26773 |
|
| 26774 |
|
| 26775 |
<para><anchor id="subshnlevref"></para> |
| 26776 |
<note> |
| 26777 |
<para>While the <link linkend="bashsubshellref">$BASH_SUBSHELL</link> |
| 26778 |
internal variable indicates the nesting level of a |
| 26779 |
subshell, the <link linkend="shlvlref">$SHLVL</link> |
| 26780 |
variable <emphasis>shows no change</emphasis> within |
| 26781 |
a subshell.</para> |
| 26782 |
|
| 26783 |
<para> |
| 26784 |
<programlisting>echo " \$BASH_SUBSHELL outside subshell = $BASH_SUBSHELL" # 0 |
| 26785 |
( echo " \$BASH_SUBSHELL inside subshell = $BASH_SUBSHELL" ) # 1 |
| 26786 |
( ( echo " \$BASH_SUBSHELL inside nested subshell = $BASH_SUBSHELL" ) ) # 2 |
| 26787 |
# ^ ^ *** nested *** ^ ^ |
| 26788 |
|
| 26789 |
echo |
| 26790 |
|
| 26791 |
echo " \$SHLVL outside subshell = $SHLVL" # 3 |
| 26792 |
( echo " \$SHLVL inside subshell = $SHLVL" ) # 3 (No change!)</programlisting> |
| 26793 |
</para> |
| 26794 |
</note> |
| 26795 |
|
| 26796 |
|
| 26797 |
|
| 26798 |
<para>Directory changes made in a subshell do not carry over to the |
| 26799 |
parent shell.</para> |
| 26800 |
|
| 26801 |
<example id="allprofs"> |
| 26802 |
<title>List User Profiles</title> |
| 26803 |
<programlisting>&allprofs;</programlisting> |
| 26804 |
</example> |
| 26805 |
|
| 26806 |
|
| 26807 |
<para>A subshell may be used to set up a <quote>dedicated |
| 26808 |
environment</quote> for a command group. |
| 26809 |
<programlisting>COMMAND1 |
| 26810 |
COMMAND2 |
| 26811 |
COMMAND3 |
| 26812 |
( |
| 26813 |
IFS=: |
| 26814 |
PATH=/bin |
| 26815 |
unset TERMINFO |
| 26816 |
set -C |
| 26817 |
shift 5 |
| 26818 |
COMMAND4 |
| 26819 |
COMMAND5 |
| 26820 |
exit 3 # Only exits the subshell! |
| 26821 |
) |
| 26822 |
# The parent shell has not been affected, and the environment is preserved. |
| 26823 |
COMMAND6 |
| 26824 |
COMMAND7</programlisting> |
| 26825 |
|
| 26826 |
As seen here, the <link linkend="exitref">exit</link> |
| 26827 |
command only terminates the subshell in which it is running, |
| 26828 |
<emphasis>not</emphasis> the parent shell or script.</para> |
| 26829 |
|
| 26830 |
<para>One application of such a <quote>dedicated environment</quote> |
| 26831 |
is testing whether a variable is defined. |
| 26832 |
<programlisting>if (set -u; : $variable) 2> /dev/null |
| 26833 |
then |
| 26834 |
echo "Variable is set." |
| 26835 |
fi # Variable has been set in current script, |
| 26836 |
#+ or is an an internal Bash variable, |
| 26837 |
#+ or is present in environment (has been exported). |
| 26838 |
|
| 26839 |
# Could also be written [[ ${variable-x} != x || ${variable-y} != y ]] |
| 26840 |
# or [[ ${variable-x} != x$variable ]] |
| 26841 |
# or [[ ${variable+x} = x ]] |
| 26842 |
# or [[ ${variable-x} != x ]]</programlisting></para> |
| 26843 |
|
| 26844 |
<para>Another application is checking for a lock file: |
| 26845 |
<programlisting>if (set -C; : > lock_file) 2> /dev/null |
| 26846 |
then |
| 26847 |
: # lock_file didn't exist: no user running the script |
| 26848 |
else |
| 26849 |
echo "Another user is already running that script." |
| 26850 |
exit 65 |
| 26851 |
fi |
| 26852 |
|
| 26853 |
# Code snippet by Stéphane Chazelas, |
| 26854 |
#+ with modifications by Paulo Marcel Coelho Aragao.</programlisting> |
| 26855 |
</para> |
| 26856 |
|
| 26857 |
<para>+</para> |
| 26858 |
|
| 26859 |
<para>Processes may execute in parallel within different |
| 26860 |
subshells. This permits breaking a complex task into subcomponents |
| 26861 |
processed concurrently.</para> |
| 26862 |
|
| 26863 |
<example id="parallel-processes"> |
| 26864 |
<title>Running parallel processes in subshells</title> |
| 26865 |
<programlisting> |
| 26866 |
(cat list1 list2 list3 | sort | uniq > list123) & |
| 26867 |
(cat list4 list5 list6 | sort | uniq > list456) & |
| 26868 |
# Merges and sorts both sets of lists simultaneously. |
| 26869 |
# Running in background ensures parallel execution. |
| 26870 |
# |
| 26871 |
# Same effect as |
| 26872 |
# cat list1 list2 list3 | sort | uniq > list123 & |
| 26873 |
# cat list4 list5 list6 | sort | uniq > list456 & |
| 26874 |
|
| 26875 |
wait # Don't execute the next command until subshells finish. |
| 26876 |
|
| 26877 |
diff list123 list456</programlisting> |
| 26878 |
</example> |
| 26879 |
|
| 26880 |
<para>Redirecting I/O to a subshell uses the <quote>|</quote> pipe |
| 26881 |
operator, as in <userinput>ls -al | (command)</userinput>.</para> |
| 26882 |
|
| 26883 |
|
| 26884 |
<note> |
| 26885 |
<para>A code block between <link |
| 26886 |
linkend="codeblockref">curly brackets</link> does |
| 26887 |
<emphasis>not</emphasis> launch a subshell.</para> |
| 26888 |
<para>{ command1; command2; command3; . . . commandN; }</para> |
| 26889 |
<para><programlisting>var1=23 |
| 26890 |
echo "$var1" # 23 |
| 26891 |
|
| 26892 |
{ var1=76; } |
| 26893 |
echo "$var1" # 76</programlisting></para> |
| 26894 |
</note> |
| 26895 |
|
| 26896 |
|
| 26897 |
|
| 26898 |
</chapter> <!-- Subshells --> |
| 26899 |
|
| 26900 |
|
| 26901 |
|
| 26902 |
<chapter id="restricted-sh"> |
| 26903 |
<title>Restricted Shells</title> |
| 26904 |
|
| 26905 |
<para><anchor id="restrictedshref"></para> |
| 26906 |
|
| 26907 |
<variablelist id="disabledcommref0"> |
| 26908 |
<title><anchor id="disabledcommref">Disabled commands in restricted |
| 26909 |
shells</title> |
| 26910 |
<varlistentry> |
| 26911 |
<term></term> |
| 26912 |
<listitem> |
| 26913 |
<formalpara><title> </title> |
| 26914 |
<para>Running a script or portion of a script in |
| 26915 |
<firstterm>restricted mode</firstterm> disables certain commands |
| 26916 |
that would otherwise be available. This is a security measure |
| 26917 |
intended to limit the privileges of the script user and to |
| 26918 |
minimize possible damage from running the script.</para> |
| 26919 |
</formalpara> |
| 26920 |
</listitem> |
| 26921 |
</varlistentry> |
| 26922 |
|
| 26923 |
</variablelist> |
| 26924 |
|
| 26925 |
<para>The following commands and actions are disabled:</para> |
| 26926 |
|
| 26927 |
<itemizedlist> |
| 26928 |
<listitem> |
| 26929 |
<para>Using <replaceable>cd</replaceable> to change the working |
| 26930 |
directory.</para> |
| 26931 |
</listitem> |
| 26932 |
|
| 26933 |
<listitem> |
| 26934 |
<para>Changing the values of the |
| 26935 |
<replaceable>$PATH</replaceable>, |
| 26936 |
<replaceable>$SHELL</replaceable>, |
| 26937 |
<replaceable>$BASH_ENV</replaceable>, |
| 26938 |
or <replaceable>$ENV</replaceable> <link |
| 26939 |
linkend="envref">environmental variables</link>.</para> |
| 26940 |
</listitem> |
| 26941 |
|
| 26942 |
<listitem> |
| 26943 |
<para>Reading or changing the <replaceable>$SHELLOPTS</replaceable>, |
| 26944 |
shell environmental options.</para> |
| 26945 |
</listitem> |
| 26946 |
|
| 26947 |
<listitem> |
| 26948 |
<para>Output redirection.</para> |
| 26949 |
</listitem> |
| 26950 |
|
| 26951 |
<listitem> |
| 26952 |
<para>Invoking commands containing one or more |
| 26953 |
<token>/</token>'s.</para> |
| 26954 |
</listitem> |
| 26955 |
|
| 26956 |
|
| 26957 |
<listitem> |
| 26958 |
<para>Invoking <link linkend="execref">exec</link> to substitute |
| 26959 |
a different process for the shell.</para> |
| 26960 |
</listitem> |
| 26961 |
|
| 26962 |
<listitem> |
| 26963 |
<para>Various other commands that would enable monkeying |
| 26964 |
with or attempting to subvert the script for an unintended |
| 26965 |
purpose.</para> |
| 26966 |
</listitem> |
| 26967 |
|
| 26968 |
|
| 26969 |
<listitem> |
| 26970 |
<para>Getting out of restricted mode within the script.</para> |
| 26971 |
</listitem> |
| 26972 |
|
| 26973 |
</itemizedlist> |
| 26974 |
|
| 26975 |
|
| 26976 |
<example id="restricted"> |
| 26977 |
<title>Running a script in restricted mode</title> |
| 26978 |
<programlisting>&restricted;</programlisting> |
| 26979 |
</example> |
| 26980 |
|
| 26981 |
</chapter> <!-- Restricted Shells --> |
| 26982 |
|
| 26983 |
|
| 26984 |
|
| 26985 |
|
| 26986 |
<chapter id="process-sub"> |
| 26987 |
<title>Process Substitution</title> |
| 26988 |
|
| 26989 |
<para><anchor id="processsubref"><link |
| 26990 |
linkend="piperef">Piping</link> the <filename>stdout</filename> |
| 26991 |
of a command into the <filename>stdin</filename> of another |
| 26992 |
is a powerful technique. But, what if you need to pipe the |
| 26993 |
<filename>stdout</filename> of <emphasis>multiple</emphasis> |
| 26994 |
commands? This is where <replaceable>process |
| 26995 |
substitution</replaceable> comes in.</para> |
| 26996 |
|
| 26997 |
<para><firstterm>Process substitution</firstterm> feeds the |
| 26998 |
output of a <link linkend="processref">process</link> (or |
| 26999 |
processes) into the <filename>stdin</filename> of another |
| 27000 |
process.</para> |
| 27001 |
|
| 27002 |
<variablelist id="commandsparens"> |
| 27003 |
<title><anchor id="commandsparens1">Template</title> |
| 27004 |
|
| 27005 |
<varlistentry> |
| 27006 |
<term>Command list enclosed within parentheses</term> |
| 27007 |
|
| 27008 |
<listitem> |
| 27009 |
<para><command>>(command_list)</command></para> |
| 27010 |
<para><command><(command_list)</command></para> |
| 27011 |
<para>Process substitution uses |
| 27012 |
<filename>/dev/fd/<n></filename> files to send the |
| 27013 |
results of the process(es) within parentheses to another process. |
| 27014 |
<footnote><para>This has the same effect as a |
| 27015 |
<link linkend="namedpiperef">named pipe</link> (temp |
| 27016 |
file), and, in fact, named pipes were at one time used |
| 27017 |
in process substitution.</para></footnote> |
| 27018 |
</para> |
| 27019 |
|
| 27020 |
<caution><para>There is <emphasis>no</emphasis> space between the |
| 27021 |
the <quote><</quote> or <quote>></quote> and the parentheses. |
| 27022 |
Space there would give an error message.</para></caution> |
| 27023 |
|
| 27024 |
</listitem> |
| 27025 |
|
| 27026 |
</varlistentry> |
| 27027 |
</variablelist> |
| 27028 |
|
| 27029 |
|
| 27030 |
<para> |
| 27031 |
<screen><prompt>bash$ </prompt><userinput>echo >(true)</userinput> |
| 27032 |
<computeroutput>/dev/fd/63</computeroutput> |
| 27033 |
|
| 27034 |
<prompt>bash$ </prompt><userinput>echo <(true)</userinput> |
| 27035 |
<computeroutput>/dev/fd/63</computeroutput> |
| 27036 |
|
| 27037 |
<prompt>bash$ </prompt><userinput>echo >(true) <(true)</userinput> |
| 27038 |
<computeroutput>/dev/fd/63 /dev/fd/62</computeroutput> |
| 27039 |
|
| 27040 |
|
| 27041 |
|
| 27042 |
<prompt>bash$ </prompt><userinput>wc <(cat /usr/share/dict/linux.words)</userinput> |
| 27043 |
<computeroutput> 483523 483523 4992010 /dev/fd/63</computeroutput> |
| 27044 |
|
| 27045 |
<prompt>bash$ </prompt><userinput>grep script /usr/share/dict/linux.words | wc</userinput> |
| 27046 |
<computeroutput> 262 262 3601</computeroutput> |
| 27047 |
|
| 27048 |
<prompt>bash$ </prompt><userinput>wc <(grep script /usr/share/dict/linux.words)</userinput> |
| 27049 |
<computeroutput> 262 262 3601 /dev/fd/63</computeroutput> |
| 27050 |
</screen> |
| 27051 |
</para> |
| 27052 |
|
| 27053 |
<note><para> |
| 27054 |
Bash creates a pipe with two <link linkend="fdref">file |
| 27055 |
descriptors</link>, <filename>--fIn</filename> and |
| 27056 |
<filename>fOut--</filename>. The <filename>stdin</filename> |
| 27057 |
of <link linkend="trueref">true</link> connects |
| 27058 |
to <filename>fOut</filename> (dup2(fOut, 0)), |
| 27059 |
then Bash passes a <filename>/dev/fd/fIn</filename> |
| 27060 |
argument to <command>echo</command>. On systems lacking |
| 27061 |
<filename>/dev/fd/<n></filename> files, Bash may use |
| 27062 |
temporary files. (Thanks, S.C.) |
| 27063 |
</para></note> |
| 27064 |
|
| 27065 |
|
| 27066 |
<para>Process substitution can compare the output of two |
| 27067 |
different commands, or even the output of different options |
| 27068 |
to the same command.</para> |
| 27069 |
|
| 27070 |
<screen><prompt>bash$ </prompt><userinput>comm <(ls -l) <(ls -al)</userinput> |
| 27071 |
<computeroutput>total 12 |
| 27072 |
-rw-rw-r-- 1 bozo bozo 78 Mar 10 12:58 File0 |
| 27073 |
-rw-rw-r-- 1 bozo bozo 42 Mar 10 12:58 File2 |
| 27074 |
-rw-rw-r-- 1 bozo bozo 103 Mar 10 12:58 t2.sh |
| 27075 |
total 20 |
| 27076 |
drwxrwxrwx 2 bozo bozo 4096 Mar 10 18:10 . |
| 27077 |
drwx------ 72 bozo bozo 4096 Mar 10 17:58 .. |
| 27078 |
-rw-rw-r-- 1 bozo bozo 78 Mar 10 12:58 File0 |
| 27079 |
-rw-rw-r-- 1 bozo bozo 42 Mar 10 12:58 File2 |
| 27080 |
-rw-rw-r-- 1 bozo bozo 103 Mar 10 12:58 t2.sh</computeroutput></screen> |
| 27081 |
|
| 27082 |
<para><anchor id="pcc2dir"></para> |
| 27083 |
<para> |
| 27084 |
Process substitution can compare the contents |
| 27085 |
of two directories -- to see which filenames are in one, |
| 27086 |
but not the other.</para> |
| 27087 |
<para> |
| 27088 |
<programlisting>diff <(ls $first_directory) <(ls $second_directory)</programlisting> |
| 27089 |
</para> |
| 27090 |
|
| 27091 |
<para>Some other usages and uses of process substitution:</para> |
| 27092 |
<para><anchor id="psfdstdin"></para> |
| 27093 |
<para><programlisting>read -a list < <( od -Ad -w24 -t u2 /dev/urandom ) |
| 27094 |
# Read a list of random numbers from /dev/urandom, |
| 27095 |
#+ process with "od" |
| 27096 |
#+ and feed into stdin of "read" . . . |
| 27097 |
|
| 27098 |
# From "insertion-sort.bash" example script. |
| 27099 |
# Courtesy of JuanJo Ciarlante.</programlisting></para> |
| 27100 |
|
| 27101 |
|
| 27102 |
<para><anchor id="netcatexample"></para> |
| 27103 |
<para><programlisting>PORT=6881 # bittorrent |
| 27104 |
|
| 27105 |
# Scan the port to make sure nothing nefarious is going on. |
| 27106 |
netcat -l $PORT | tee>(md5sum ->mydata-orig.md5) | |
| 27107 |
gzip | tee>(md5sum - | sed 's/-$/mydata.lz2/'>mydata-gz.md5)>mydata.gz |
| 27108 |
|
| 27109 |
# Check the decompression: |
| 27110 |
gzip -d<mydata.gz | md5sum -c mydata-orig.md5) |
| 27111 |
# The MD5sum of the original checks stdin and detects compression issues. |
| 27112 |
|
| 27113 |
# Bill Davidsen contributed this example |
| 27114 |
#+ (with light edits by the ABS Guide author).</programlisting></para> |
| 27115 |
|
| 27116 |
|
| 27117 |
|
| 27118 |
<para><programlisting>cat <(ls -l) |
| 27119 |
# Same as ls -l | cat |
| 27120 |
|
| 27121 |
sort -k 9 <(ls -l /bin) <(ls -l /usr/bin) <(ls -l /usr/X11R6/bin) |
| 27122 |
# Lists all the files in the 3 main 'bin' directories, and sorts by filename. |
| 27123 |
# Note that three (count 'em) distinct commands are fed to 'sort'. |
| 27124 |
|
| 27125 |
|
| 27126 |
diff <(command1) <(command2) # Gives difference in command output. |
| 27127 |
|
| 27128 |
tar cf >(bzip2 -c > file.tar.bz2) $directory_name |
| 27129 |
# Calls "tar cf /dev/fd/?? $directory_name", and "bzip2 -c > file.tar.bz2". |
| 27130 |
# |
| 27131 |
# Because of the /dev/fd/<n> system feature, |
| 27132 |
# the pipe between both commands does not need to be named. |
| 27133 |
# |
| 27134 |
# This can be emulated. |
| 27135 |
# |
| 27136 |
bzip2 -c < pipe > file.tar.bz2& |
| 27137 |
tar cf pipe $directory_name |
| 27138 |
rm pipe |
| 27139 |
# or |
| 27140 |
exec 3>&1 |
| 27141 |
tar cf /dev/fd/4 $directory_name 4>&1 >&3 3>&- | bzip2 -c > file.tar.bz2 3>&- |
| 27142 |
exec 3>&- |
| 27143 |
|
| 27144 |
|
| 27145 |
# Thanks, Stéphane Chazelas</programlisting></para> |
| 27146 |
|
| 27147 |
<para><anchor id="goodread0">Here is a method of circumventing the |
| 27148 |
problem of an <link linkend=badread0><firstterm>echo</firstterm> |
| 27149 |
piped to a <firstterm>while-read loop</firstterm></link> running |
| 27150 |
in a subshell.</para> |
| 27151 |
|
| 27152 |
<example id="wrps"> |
| 27153 |
<title>Code block redirection without forking</title> |
| 27154 |
<programlisting>&wrps;</programlisting> |
| 27155 |
</example> |
| 27156 |
|
| 27157 |
<para><anchor id="psubpiping">This is a similar example.</para> |
| 27158 |
|
| 27159 |
<example id="psubp"> |
| 27160 |
<title>Redirecting the output of <firstterm>process |
| 27161 |
substitution</firstterm> into a loop.</title> |
| 27162 |
<programlisting>&psubp;</programlisting> |
| 27163 |
</example> |
| 27164 |
|
| 27165 |
<para>A reader sent in the following interesting example of process |
| 27166 |
substitution.</para> |
| 27167 |
|
| 27168 |
<para><programlisting># Script fragment taken from SuSE distribution: |
| 27169 |
|
| 27170 |
# --------------------------------------------------------------# |
| 27171 |
while read des what mask iface; do |
| 27172 |
# Some commands ... |
| 27173 |
done < <(route -n) |
| 27174 |
# ^ ^ First < is redirection, second is process substitution. |
| 27175 |
|
| 27176 |
# To test it, let's make it do something. |
| 27177 |
while read des what mask iface; do |
| 27178 |
echo $des $what $mask $iface |
| 27179 |
done < <(route -n) |
| 27180 |
|
| 27181 |
# Output: |
| 27182 |
# Kernel IP routing table |
| 27183 |
# Destination Gateway Genmask Flags Metric Ref Use Iface |
| 27184 |
# 127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo |
| 27185 |
# --------------------------------------------------------------# |
| 27186 |
|
| 27187 |
# As Stéphane Chazelas points out, |
| 27188 |
#+ an easier-to-understand equivalent is: |
| 27189 |
route -n | |
| 27190 |
while read des what mask iface; do # Variables set from output of pipe. |
| 27191 |
echo $des $what $mask $iface |
| 27192 |
done # This yields the same output as above. |
| 27193 |
# However, as Ulrich Gayer points out . . . |
| 27194 |
#+ this simplified equivalent uses a subshell for the while loop, |
| 27195 |
#+ and therefore the variables disappear when the pipe terminates. |
| 27196 |
|
| 27197 |
# --------------------------------------------------------------# |
| 27198 |
|
| 27199 |
# However, Filip Moritz comments that there is a subtle difference |
| 27200 |
#+ between the above two examples, as the following shows. |
| 27201 |
|
| 27202 |
( |
| 27203 |
route -n | while read x; do ((y++)); done |
| 27204 |
echo $y # $y is still unset |
| 27205 |
|
| 27206 |
while read x; do ((y++)); done < <(route -n) |
| 27207 |
echo $y # $y has the number of lines of output of route -n |
| 27208 |
) |
| 27209 |
|
| 27210 |
More generally spoken |
| 27211 |
( |
| 27212 |
: | x=x |
| 27213 |
# seems to start a subshell like |
| 27214 |
: | ( x=x ) |
| 27215 |
# while |
| 27216 |
x=x < <(:) |
| 27217 |
# does not |
| 27218 |
) |
| 27219 |
|
| 27220 |
# This is useful, when parsing csv and the like. |
| 27221 |
# That is, in effect, what the original SuSE code fragment does.</programlisting></para> |
| 27222 |
|
| 27223 |
</chapter> <!-- Process Substitution --> |
| 27224 |
|
| 27225 |
|
| 27226 |
|
| 27227 |
|
| 27228 |
<chapter id="functions"> |
| 27229 |
<title>Functions</title> |
| 27230 |
|
| 27231 |
<para><anchor id="functionref"></para> |
| 27232 |
|
| 27233 |
<para>Like <quote>real</quote> programming languages, |
| 27234 |
Bash has functions, though in a somewhat limited implementation. |
| 27235 |
A function is a subroutine, a <link linkend="codeblockref">code |
| 27236 |
block</link> that implements a set of operations, a <quote>black |
| 27237 |
box</quote> that performs a specified task. Wherever there is |
| 27238 |
repetitive code, when a task repeats with only slight variations in |
| 27239 |
procedure, then consider using a function.</para> |
| 27240 |
|
| 27241 |
<para><cmdsynopsis> |
| 27242 |
<command>function</command> |
| 27243 |
<arg choice="plain"><replaceable>function_name</replaceable></arg> |
| 27244 |
<arg choice="plain">{</arg><sbr> |
| 27245 |
<arg rep="repeat" choice="plain"><replaceable>command</replaceable></arg><sbr> |
| 27246 |
<arg choice="plain">}</arg><sbr> |
| 27247 |
</cmdsynopsis> |
| 27248 |
or |
| 27249 |
<cmdsynopsis> |
| 27250 |
<arg choice="plain"><replaceable>function_name</replaceable></arg> |
| 27251 |
<arg choice="plain">()</arg> |
| 27252 |
<arg choice="plain">{</arg><sbr> |
| 27253 |
<arg rep="repeat" choice="plain"><replaceable>command</replaceable></arg><sbr> |
| 27254 |
<arg choice="plain">}</arg><sbr> |
| 27255 |
</cmdsynopsis> |
| 27256 |
</para> |
| 27257 |
|
| 27258 |
<para>This second form will cheer the hearts of C programmers |
| 27259 |
(and is more <link |
| 27260 |
linkend="portabilityissues">portable</link>).</para> |
| 27261 |
|
| 27262 |
<para>As in C, the function's opening bracket may optionally appear |
| 27263 |
on the second line.</para> |
| 27264 |
|
| 27265 |
<para><cmdsynopsis> |
| 27266 |
<arg choice="plain"><replaceable>function_name</replaceable></arg> |
| 27267 |
<arg choice="plain">()</arg><sbr> |
| 27268 |
<arg choice="plain">{</arg><sbr> |
| 27269 |
<arg rep="repeat" choice="plain"><replaceable>command</replaceable></arg><sbr> |
| 27270 |
<arg choice="plain">}</arg><sbr> |
| 27271 |
</cmdsynopsis> |
| 27272 |
</para> |
| 27273 |
|
| 27274 |
<note> |
| 27275 |
<para>A function may be <quote>compacted</quote> into a single |
| 27276 |
line.</para> |
| 27277 |
|
| 27278 |
<para><programlisting>fun () { echo "This is a function"; echo; } |
| 27279 |
# ^ ^</programlisting></para> |
| 27280 |
|
| 27281 |
<para>In this case, however, a <firstterm>semicolon</firstterm> |
| 27282 |
must follow the final command in the function.</para> |
| 27283 |
|
| 27284 |
<para><programlisting>fun () { echo "This is a function"; echo } # Error! |
| 27285 |
# ^ |
| 27286 |
|
| 27287 |
fun2 () { echo "Even a single-command function? Yes!"; } |
| 27288 |
# ^</programlisting></para> |
| 27289 |
</note> |
| 27290 |
|
| 27291 |
<para>Functions are called, <firstterm>triggered</firstterm>, simply by |
| 27292 |
invoking their names. <emphasis>A function call is equivalent to |
| 27293 |
a command.</emphasis></para> |
| 27294 |
|
| 27295 |
<example id="ex59"> |
| 27296 |
<title>Simple functions</title> |
| 27297 |
<programlisting>&ex59;</programlisting> |
| 27298 |
</example> |
| 27299 |
|
| 27300 |
<para><anchor id="functdefmust"></para> |
| 27301 |
<para>The function definition must precede the first call to |
| 27302 |
it. There is no method of <quote>declaring</quote> the function, |
| 27303 |
as, for example, in C. |
| 27304 |
<programlisting>f1 |
| 27305 |
# Will give an error message, since function "f1" not yet defined. |
| 27306 |
|
| 27307 |
declare -f f1 # This doesn't help either. |
| 27308 |
f1 # Still an error message. |
| 27309 |
|
| 27310 |
# However... |
| 27311 |
|
| 27312 |
|
| 27313 |
f1 () |
| 27314 |
{ |
| 27315 |
echo "Calling function \"f2\" from within function \"f1\"." |
| 27316 |
f2 |
| 27317 |
} |
| 27318 |
|
| 27319 |
f2 () |
| 27320 |
{ |
| 27321 |
echo "Function \"f2\"." |
| 27322 |
} |
| 27323 |
|
| 27324 |
f1 # Function "f2" is not actually called until this point, |
| 27325 |
#+ although it is referenced before its definition. |
| 27326 |
# This is permissible. |
| 27327 |
|
| 27328 |
# Thanks, S.C.</programlisting> |
| 27329 |
</para> |
| 27330 |
|
| 27331 |
<note><para><anchor id="emptyfunc">Functions may not be empty! |
| 27332 |
<programlisting>#!/bin/bash |
| 27333 |
# empty-function.sh |
| 27334 |
|
| 27335 |
empty () |
| 27336 |
{ |
| 27337 |
} |
| 27338 |
|
| 27339 |
exit 0 # Will not exit here! |
| 27340 |
|
| 27341 |
# $ sh empty-function.sh |
| 27342 |
# empty-function.sh: line 6: syntax error near unexpected token `}' |
| 27343 |
# empty-function.sh: line 6: `}' |
| 27344 |
|
| 27345 |
# $ echo $? |
| 27346 |
# 2 |
| 27347 |
|
| 27348 |
|
| 27349 |
# Note that a function containing only comments is empty. |
| 27350 |
|
| 27351 |
func () |
| 27352 |
{ |
| 27353 |
# Comment 1. |
| 27354 |
# Comment 2. |
| 27355 |
# This is still an empty function. |
| 27356 |
# Thank you, Mark Bova, for pointing this out. |
| 27357 |
} |
| 27358 |
# Results in same error message as above. |
| 27359 |
|
| 27360 |
|
| 27361 |
# However ... |
| 27362 |
|
| 27363 |
not_quite_empty () |
| 27364 |
{ |
| 27365 |
illegal_command |
| 27366 |
} # A script containing this function will *not* bomb |
| 27367 |
#+ as long as the function is not called. |
| 27368 |
|
| 27369 |
not_empty () |
| 27370 |
{ |
| 27371 |
: |
| 27372 |
} # Contains a : (null command), and this is okay. |
| 27373 |
|
| 27374 |
|
| 27375 |
# Thank you, Dominick Geyer and Thiemo Kellner.</programlisting> |
| 27376 |
</para></note> |
| 27377 |
|
| 27378 |
|
| 27379 |
|
| 27380 |
|
| 27381 |
<para>It is even possible to nest a function within another function, |
| 27382 |
although this is not very useful. |
| 27383 |
<programlisting>f1 () |
| 27384 |
{ |
| 27385 |
|
| 27386 |
f2 () # nested |
| 27387 |
{ |
| 27388 |
echo "Function \"f2\", inside \"f1\"." |
| 27389 |
} |
| 27390 |
|
| 27391 |
} |
| 27392 |
|
| 27393 |
f2 # Gives an error message. |
| 27394 |
# Even a preceding "declare -f f2" wouldn't help. |
| 27395 |
|
| 27396 |
echo |
| 27397 |
|
| 27398 |
f1 # Does nothing, since calling "f1" does not automatically call "f2". |
| 27399 |
f2 # Now, it's all right to call "f2", |
| 27400 |
#+ since its definition has been made visible by calling "f1". |
| 27401 |
|
| 27402 |
# Thanks, S.C.</programlisting> |
| 27403 |
</para> |
| 27404 |
|
| 27405 |
<para>Function declarations can appear in unlikely places, even where a |
| 27406 |
command would otherwise go. |
| 27407 |
<programlisting>ls -l | foo() { echo "foo"; } # Permissible, but useless. |
| 27408 |
|
| 27409 |
|
| 27410 |
|
| 27411 |
if [ "$USER" = bozo ] |
| 27412 |
then |
| 27413 |
bozo_greet () # Function definition embedded in an if/then construct. |
| 27414 |
{ |
| 27415 |
echo "Hello, Bozo." |
| 27416 |
} |
| 27417 |
fi |
| 27418 |
|
| 27419 |
bozo_greet # Works only for Bozo, and other users get an error. |
| 27420 |
|
| 27421 |
|
| 27422 |
|
| 27423 |
# Something like this might be useful in some contexts. |
| 27424 |
NO_EXIT=1 # Will enable function definition below. |
| 27425 |
|
| 27426 |
[[ $NO_EXIT -eq 1 ]] && exit() { true; } # Function definition in an "and-list". |
| 27427 |
# If $NO_EXIT is 1, declares "exit ()". |
| 27428 |
# This disables the "exit" builtin by aliasing it to "true". |
| 27429 |
|
| 27430 |
exit # Invokes "exit ()" function, not "exit" builtin. |
| 27431 |
|
| 27432 |
|
| 27433 |
|
| 27434 |
# Or, similarly: |
| 27435 |
filename=file1 |
| 27436 |
|
| 27437 |
[ -f "$filename" ] && |
| 27438 |
foo () { rm -f "$filename"; echo "File "$filename" deleted."; } || |
| 27439 |
foo () { echo "File "$filename" not found."; touch bar; } |
| 27440 |
|
| 27441 |
foo |
| 27442 |
|
| 27443 |
# Thanks, S.C. and Christopher Head</programlisting> |
| 27444 |
</para> |
| 27445 |
|
| 27446 |
|
| 27447 |
<para><anchor id="fstrangeref">Function names can take strange |
| 27448 |
forms. |
| 27449 |
<programlisting> _(){ for i in {1..10}; do echo -n "$FUNCNAME"; done; echo; } |
| 27450 |
# ^^^ No space between function name and parentheses. |
| 27451 |
# This doesn't always work. Why not? |
| 27452 |
|
| 27453 |
# Now, let's invoke the function. |
| 27454 |
_ # __________ |
| 27455 |
# ^^^^^^^^^^ 10 underscores (10 x function name)! |
| 27456 |
# A "naked" underscore is an acceptable function name. |
| 27457 |
|
| 27458 |
|
| 27459 |
# In fact, a colon is likewise an acceptable function name. |
| 27460 |
|
| 27461 |
:(){ echo ":"; }; : |
| 27462 |
|
| 27463 |
# Of what use is this? |
| 27464 |
# It's a devious way to obfuscate the code in a script.</programlisting> |
| 27465 |
See also <xref linkend="gronsfeld"></para> |
| 27466 |
|
| 27467 |
|
| 27468 |
|
| 27469 |
<note><para>What happens when different versions of the same function |
| 27470 |
appear in a script? |
| 27471 |
<programlisting># As Yan Chen points out, |
| 27472 |
# when a function is defined multiple times, |
| 27473 |
# the final version is what is invoked. |
| 27474 |
# This is not, however, particularly useful. |
| 27475 |
|
| 27476 |
func () |
| 27477 |
{ |
| 27478 |
echo "First version of func ()." |
| 27479 |
} |
| 27480 |
|
| 27481 |
func () |
| 27482 |
{ |
| 27483 |
echo "Second version of func ()." |
| 27484 |
} |
| 27485 |
|
| 27486 |
func # Second version of func (). |
| 27487 |
|
| 27488 |
exit $? |
| 27489 |
|
| 27490 |
# It is even possible to use functions to override |
| 27491 |
#+ or preempt system commands. |
| 27492 |
# Of course, this is *not* advisable.</programlisting></para></note> |
| 27493 |
|
| 27494 |
<!-- End of intro section --> |
| 27495 |
|
| 27496 |
<sect1 id="complexfunct"> |
| 27497 |
<title>Complex Functions and Function Complexities</title> |
| 27498 |
|
| 27499 |
<para>Functions may process arguments passed to them and return |
| 27500 |
an <link linkend="exitstatusref">exit status</link> to the script |
| 27501 |
for further processing.</para> |
| 27502 |
|
| 27503 |
<programlisting>function_name $arg1 $arg2</programlisting> |
| 27504 |
|
| 27505 |
<para><anchor id="passedargs"></para> |
| 27506 |
<para>The function refers to the passed arguments by position (as if they were |
| 27507 |
<link linkend="posparamref">positional parameters</link>), |
| 27508 |
that is, <varname>$1</varname>, <varname>$2</varname>, and |
| 27509 |
so forth.</para> |
| 27510 |
|
| 27511 |
<example id="ex60"> |
| 27512 |
<title>Function Taking Parameters</title> |
| 27513 |
<programlisting>&ex60;</programlisting> |
| 27514 |
</example> |
| 27515 |
|
| 27516 |
<para><anchor id="fshiftref"></para> |
| 27517 |
<important><para>The <link linkend="shiftref">shift</link> |
| 27518 |
command works on arguments passed to functions (see <xref |
| 27519 |
linkend="multiplication">).</para></important> |
| 27520 |
|
| 27521 |
<para>But, what about command-line arguments passed to the script? |
| 27522 |
Does a function see them? Well, let's clear up the confusion.</para> |
| 27523 |
|
| 27524 |
<example id="funccmdlinearg"> |
| 27525 |
<title>Functions and command-line args passed to the script</title> |
| 27526 |
<programlisting>&funccmdlinearg;</programlisting> |
| 27527 |
</example> |
| 27528 |
|
| 27529 |
|
| 27530 |
<para>In contrast to certain other programming languages, |
| 27531 |
shell scripts normally pass only value parameters to |
| 27532 |
functions. Variable names (which are actually |
| 27533 |
<firstterm>pointers</firstterm>), if |
| 27534 |
passed as parameters to functions, will be treated as string |
| 27535 |
literals. <emphasis>Functions interpret their arguments |
| 27536 |
literally.</emphasis></para> |
| 27537 |
|
| 27538 |
<para><anchor id="funcpointers"></para> |
| 27539 |
<para><link linkend="ivrref">Indirect variable |
| 27540 |
references</link> (see <xref linkend="ex78">) provide a clumsy |
| 27541 |
sort of mechanism for passing variable pointers to |
| 27542 |
functions.</para> |
| 27543 |
|
| 27544 |
<example id="indfunc"> |
| 27545 |
<title>Passing an indirect reference to a function</title> |
| 27546 |
<programlisting>&indfunc;</programlisting> |
| 27547 |
</example> |
| 27548 |
|
| 27549 |
<para>The next logical question is whether parameters can be |
| 27550 |
dereferenced <emphasis>after</emphasis> being passed to a |
| 27551 |
function.</para> |
| 27552 |
|
| 27553 |
<example id="dereferencecl"> |
| 27554 |
<title>Dereferencing a parameter passed to a function</title> |
| 27555 |
<programlisting>&dereferencecl;</programlisting> |
| 27556 |
</example> |
| 27557 |
|
| 27558 |
<example id="refparams"> |
| 27559 |
<title>Again, dereferencing a parameter passed to a function</title> |
| 27560 |
<programlisting>&refparams;</programlisting> |
| 27561 |
</example> |
| 27562 |
|
| 27563 |
|
| 27564 |
<variablelist id="exitreturn"> |
| 27565 |
<title><anchor id="exitreturn1">Exit and Return</title> |
| 27566 |
|
| 27567 |
<varlistentry> |
| 27568 |
<term><command>exit status</command></term> |
| 27569 |
<listitem> |
| 27570 |
<para>Functions return a value, called an <firstterm>exit |
| 27571 |
status</firstterm>. This is analogous to the <link |
| 27572 |
linkend="exitstatusref">exit status</link> returned by a |
| 27573 |
command. The exit status may be explicitly specified |
| 27574 |
by a <command>return</command> statement, otherwise it |
| 27575 |
is the exit status of the last command in the function |
| 27576 |
(<returnvalue>0</returnvalue> if successful, and a non-zero |
| 27577 |
error code if not). This <link linkend="exitstatusref">exit |
| 27578 |
status</link> may be used in the script by referencing it |
| 27579 |
as <link linkend="xstatvarref">$?</link>. This mechanism |
| 27580 |
effectively permits script functions to have a <quote>return |
| 27581 |
value</quote> similar to C functions.</para> |
| 27582 |
</listitem> |
| 27583 |
</varlistentry> |
| 27584 |
|
| 27585 |
<varlistentry> |
| 27586 |
<term><command>return</command></term> |
| 27587 |
<indexterm> |
| 27588 |
<primary>return</primary> |
| 27589 |
</indexterm> |
| 27590 |
<indexterm> |
| 27591 |
<primary>command</primary> |
| 27592 |
<secondary>return</secondary> |
| 27593 |
</indexterm> |
| 27594 |
<listitem> |
| 27595 |
<para><anchor id="returnref"></para> |
| 27596 |
<para>Terminates a function. A <command>return</command> command |
| 27597 |
<footnote><para>The <command>return</command> command is a |
| 27598 |
Bash <link linkend="builtinref">builtin</link>.</para></footnote> |
| 27599 |
optionally takes an <firstterm>integer</firstterm> |
| 27600 |
argument, which is returned to the calling script as |
| 27601 |
the <quote>exit status</quote> of the function, and |
| 27602 |
this exit status is assigned to the variable <link |
| 27603 |
linkend="xstatvarref">$?</link>.</para> |
| 27604 |
|
| 27605 |
<example id="max"> |
| 27606 |
<title>Maximum of two numbers</title> |
| 27607 |
<programlisting>&max;</programlisting> |
| 27608 |
</example> |
| 27609 |
|
| 27610 |
<tip> |
| 27611 |
<para>For a function to return a string or array, use a |
| 27612 |
dedicated variable. |
| 27613 |
<programlisting>count_lines_in_etc_passwd() |
| 27614 |
{ |
| 27615 |
[[ -r /etc/passwd ]] && REPLY=$(echo $(wc -l < /etc/passwd)) |
| 27616 |
# If /etc/passwd is readable, set REPLY to line count. |
| 27617 |
# Returns both a parameter value and status information. |
| 27618 |
# The 'echo' seems unnecessary, but . . . |
| 27619 |
#+ it removes excess whitespace from the output. |
| 27620 |
} |
| 27621 |
|
| 27622 |
if count_lines_in_etc_passwd |
| 27623 |
then |
| 27624 |
echo "There are $REPLY lines in /etc/passwd." |
| 27625 |
else |
| 27626 |
echo "Cannot count lines in /etc/passwd." |
| 27627 |
fi |
| 27628 |
|
| 27629 |
# Thanks, S.C.</programlisting> |
| 27630 |
</para> |
| 27631 |
</tip> |
| 27632 |
|
| 27633 |
|
| 27634 |
|
| 27635 |
|
| 27636 |
<example id="ex61"> |
| 27637 |
<title>Converting numbers to Roman numerals</title> |
| 27638 |
<programlisting>&ex61;</programlisting> |
| 27639 |
</example> |
| 27640 |
|
| 27641 |
<para>See also <xref linkend="isalpha">.</para> |
| 27642 |
|
| 27643 |
<important> |
| 27644 |
|
| 27645 |
<para>The largest positive integer a function can return is |
| 27646 |
255. The <command>return</command> command is closely tied |
| 27647 |
to the concept of <link linkend="exitstatusref">exit |
| 27648 |
status</link>, which accounts for this particular |
| 27649 |
limitation. Fortunately, there are various <link |
| 27650 |
linkend="rvt">workarounds</link> for those situations |
| 27651 |
requiring a large integer return value from a |
| 27652 |
function.</para> |
| 27653 |
|
| 27654 |
|
| 27655 |
<example id="returntest"> |
| 27656 |
<title>Testing large return values in a function</title> |
| 27657 |
<programlisting>&returntest;</programlisting> |
| 27658 |
</example> |
| 27659 |
|
| 27660 |
|
| 27661 |
<para>A workaround for obtaining large integer <quote>return |
| 27662 |
values</quote> is to simply assign the <quote>return |
| 27663 |
value</quote> to a global variable. |
| 27664 |
|
| 27665 |
<programlisting>Return_Val= # Global variable to hold oversize return value of function. |
| 27666 |
|
| 27667 |
alt_return_test () |
| 27668 |
{ |
| 27669 |
fvar=$1 |
| 27670 |
Return_Val=$fvar |
| 27671 |
return # Returns 0 (success). |
| 27672 |
} |
| 27673 |
|
| 27674 |
alt_return_test 1 |
| 27675 |
echo $? # 0 |
| 27676 |
echo "return value = $Return_Val" # 1 |
| 27677 |
|
| 27678 |
alt_return_test 256 |
| 27679 |
echo "return value = $Return_Val" # 256 |
| 27680 |
|
| 27681 |
alt_return_test 257 |
| 27682 |
echo "return value = $Return_Val" # 257 |
| 27683 |
|
| 27684 |
alt_return_test 25701 |
| 27685 |
echo "return value = $Return_Val" #25701</programlisting> |
| 27686 |
</para> |
| 27687 |
|
| 27688 |
|
| 27689 |
<para><anchor id="captureretval"></para> |
| 27690 |
<para>A more elegant method is to have the function |
| 27691 |
<command>echo</command> its <quote>return |
| 27692 |
value to <filename>stdout</filename>,</quote> and |
| 27693 |
then capture it by <link linkend="commandsubref">command |
| 27694 |
substitution</link>. See the <link linkend="rvt">discussion |
| 27695 |
of this</link> in <xref linkend="assortedtips">.</para> |
| 27696 |
|
| 27697 |
<example id="max2"> |
| 27698 |
<title>Comparing two large integers</title> |
| 27699 |
<programlisting>&max2;</programlisting> |
| 27700 |
</example> |
| 27701 |
|
| 27702 |
<para>Here is another example of capturing a function |
| 27703 |
<quote>return value.</quote> Understanding it requires some |
| 27704 |
knowledge of <link linkend="awkref">awk</link>. |
| 27705 |
|
| 27706 |
<programlisting>month_length () # Takes month number as an argument. |
| 27707 |
{ # Returns number of days in month. |
| 27708 |
monthD="31 28 31 30 31 30 31 31 30 31 30 31" # Declare as local? |
| 27709 |
echo "$monthD" | awk '{ print $'"${1}"' }' # Tricky. |
| 27710 |
# ^^^^^^^^^ |
| 27711 |
# Parameter passed to function ($1 -- month number), then to awk. |
| 27712 |
# Awk sees this as "print $1 . . . print $12" (depending on month number) |
| 27713 |
# Template for passing a parameter to embedded awk script: |
| 27714 |
# $'"${script_parameter}"' |
| 27715 |
|
| 27716 |
# Here's a slightly simpler awk construct: |
| 27717 |
# echo $monthD | awk -v month=$1 '{print $(month)}' |
| 27718 |
# Uses the -v awk option, which assigns a variable value |
| 27719 |
#+ prior to execution of the awk program block. |
| 27720 |
# Thank you, Rich. |
| 27721 |
|
| 27722 |
# Needs error checking for correct parameter range (1-12) |
| 27723 |
#+ and for February in leap year. |
| 27724 |
} |
| 27725 |
|
| 27726 |
# ---------------------------------------------- |
| 27727 |
# Usage example: |
| 27728 |
month=4 # April, for example (4th month). |
| 27729 |
days_in=$(month_length $month) |
| 27730 |
echo $days_in # 30 |
| 27731 |
# ----------------------------------------------</programlisting></para> |
| 27732 |
|
| 27733 |
|
| 27734 |
<para>See also <xref linkend="daysbetween"> |
| 27735 |
and <xref linkend="stddev">.</para> |
| 27736 |
|
| 27737 |
<para><userinput>Exercise:</userinput> Using what we have |
| 27738 |
just learned, extend the previous <link |
| 27739 |
linkend="ex61">Roman numerals example</link> to accept |
| 27740 |
arbitrarily large input.</para> |
| 27741 |
|
| 27742 |
</important> |
| 27743 |
|
| 27744 |
</listitem> |
| 27745 |
</varlistentry> |
| 27746 |
</variablelist> |
| 27747 |
|
| 27748 |
|
| 27749 |
<variablelist id="redstdinfunc"> |
| 27750 |
<title><anchor id="redstdinfunc1">Redirection</title> |
| 27751 |
|
| 27752 |
<varlistentry> |
| 27753 |
<term><replaceable>Redirecting the stdin |
| 27754 |
of a function</replaceable></term> |
| 27755 |
<indexterm> |
| 27756 |
<primary>redirection</primary> |
| 27757 |
<secondary>stdin</secondary> |
| 27758 |
</indexterm> |
| 27759 |
<listitem> |
| 27760 |
|
| 27761 |
<para>A function is essentially a <link |
| 27762 |
linkend="codeblockref">code block</link>, which means its |
| 27763 |
<filename>stdin</filename> can be redirected (as in <xref |
| 27764 |
linkend="ex8">).</para> |
| 27765 |
|
| 27766 |
<example id="realname"> |
| 27767 |
<title>Real name from username</title> |
| 27768 |
<programlisting>&realname;</programlisting> |
| 27769 |
</example> |
| 27770 |
|
| 27771 |
<para>There is an alternate, and perhaps less confusing |
| 27772 |
method of redirecting a function's |
| 27773 |
<filename>stdin</filename>. This involves redirecting the |
| 27774 |
<filename>stdin</filename> to an embedded bracketed code |
| 27775 |
block within the function. |
| 27776 |
|
| 27777 |
<programlisting># Instead of: |
| 27778 |
Function () |
| 27779 |
{ |
| 27780 |
... |
| 27781 |
} < file |
| 27782 |
|
| 27783 |
# Try this: |
| 27784 |
Function () |
| 27785 |
{ |
| 27786 |
{ |
| 27787 |
... |
| 27788 |
} < file |
| 27789 |
} |
| 27790 |
|
| 27791 |
# Similarly, |
| 27792 |
|
| 27793 |
Function () # This works. |
| 27794 |
{ |
| 27795 |
{ |
| 27796 |
echo $* |
| 27797 |
} | tr a b |
| 27798 |
} |
| 27799 |
|
| 27800 |
Function () # This doesn't work. |
| 27801 |
{ |
| 27802 |
echo $* |
| 27803 |
} | tr a b # A nested code block is mandatory here. |
| 27804 |
|
| 27805 |
|
| 27806 |
# Thanks, S.C.</programlisting> |
| 27807 |
</para> |
| 27808 |
|
| 27809 |
<note><para>Emmanuel Rouat's <link |
| 27810 |
linkend="sample-bashrc">sample <filename>bashrc</filename> |
| 27811 |
file</link> contains some instructive examples of |
| 27812 |
functions.</para></note> |
| 27813 |
|
| 27814 |
</listitem> |
| 27815 |
</varlistentry> |
| 27816 |
|
| 27817 |
</variablelist> |
| 27818 |
|
| 27819 |
</sect1> <!-- Complex Functions and Function Complexities --> |
| 27820 |
|
| 27821 |
|
| 27822 |
|
| 27823 |
<sect1 id="localvar"> |
| 27824 |
<title>Local Variables</title> |
| 27825 |
|
| 27826 |
<variablelist id="localref"> |
| 27827 |
<title><anchor id="localref1">What makes a variable |
| 27828 |
<firstterm>local</firstterm>?</title> |
| 27829 |
|
| 27830 |
<varlistentry> |
| 27831 |
<term>local variables</term> |
| 27832 |
<indexterm> |
| 27833 |
<primary>variable</primary> |
| 27834 |
<secondary>local</secondary> |
| 27835 |
</indexterm> |
| 27836 |
<listitem> |
| 27837 |
<para>A variable declared as <firstterm>local</firstterm> |
| 27838 |
is one that is visible only within the <link |
| 27839 |
linkend="codeblockref">block of code</link> in which it |
| 27840 |
appears. It has local <link linkend="scoperef">scope</link>. |
| 27841 |
In a function, a <firstterm>local variable</firstterm> has |
| 27842 |
meaning only within that function block. |
| 27843 |
|
| 27844 |
<footnote> |
| 27845 |
|
| 27846 |
<para>However, as Thomas Braunberger points out, a local |
| 27847 |
variable declared in a function <emphasis>is also visible |
| 27848 |
to functions called by the parent |
| 27849 |
function.</emphasis></para> |
| 27850 |
|
| 27851 |
<para><programlisting>#!/bin/bash |
| 27852 |
|
| 27853 |
function1 () |
| 27854 |
{ |
| 27855 |
local func1var=20 |
| 27856 |
|
| 27857 |
echo "Within function1, \$func1var = $func1var." |
| 27858 |
|
| 27859 |
function2 |
| 27860 |
} |
| 27861 |
|
| 27862 |
function2 () |
| 27863 |
{ |
| 27864 |
echo "Within function2, \$func1var = $func1var." |
| 27865 |
} |
| 27866 |
|
| 27867 |
function1 |
| 27868 |
|
| 27869 |
exit 0 |
| 27870 |
|
| 27871 |
|
| 27872 |
# Output of the script: |
| 27873 |
|
| 27874 |
# Within function1, $func1var = 20. |
| 27875 |
# Within function2, $func1var = 20.</programlisting></para> |
| 27876 |
|
| 27877 |
<para>This is documented in the Bash manual:</para> |
| 27878 |
|
| 27879 |
<para><quote>Local can only be used within a function; |
| 27880 |
it makes the variable name have a visible scope |
| 27881 |
restricted to that function <emphasis>and its |
| 27882 |
children</emphasis>.</quote> [emphasis added] |
| 27883 |
<emphasis>The ABS Guide author considers this behavior |
| 27884 |
to be a bug.</emphasis></para> |
| 27885 |
|
| 27886 |
</footnote> |
| 27887 |
</para> |
| 27888 |
|
| 27889 |
|
| 27890 |
|
| 27891 |
<example id="ex62"> |
| 27892 |
<title>Local variable visibility</title> |
| 27893 |
<programlisting>&ex62;</programlisting> |
| 27894 |
</example> |
| 27895 |
|
| 27896 |
<caution> |
| 27897 |
<para>Before a function is called, <emphasis>all</emphasis> |
| 27898 |
variables declared within the function are invisible outside |
| 27899 |
the body of the function, not just those explicitly declared |
| 27900 |
as <firstterm>local</firstterm>. |
| 27901 |
<programlisting>#!/bin/bash |
| 27902 |
|
| 27903 |
func () |
| 27904 |
{ |
| 27905 |
global_var=37 # Visible only within the function block |
| 27906 |
#+ before the function has been called. |
| 27907 |
} # END OF FUNCTION |
| 27908 |
|
| 27909 |
echo "global_var = $global_var" # global_var = |
| 27910 |
# Function "func" has not yet been called, |
| 27911 |
#+ so $global_var is not visible here. |
| 27912 |
|
| 27913 |
func |
| 27914 |
echo "global_var = $global_var" # global_var = 37 |
| 27915 |
# Has been set by function call.</programlisting> |
| 27916 |
</para> |
| 27917 |
</caution> |
| 27918 |
|
| 27919 |
<note> |
| 27920 |
<para><anchor id="exitvalanomaly01"></para> |
| 27921 |
<para>As Evgeniy Ivanov points out, when declaring and |
| 27922 |
setting a local variable in a single command, apparently the |
| 27923 |
order of operations is to <emphasis>first set the variable, |
| 27924 |
and only afterwards restrict it to local scope</emphasis>. |
| 27925 |
This is reflected in the <link |
| 27926 |
linkend="exitstatusref">return value</link>.</para> |
| 27927 |
|
| 27928 |
<para><programlisting>#!/bin/bash |
| 27929 |
|
| 27930 |
echo "==OUTSIDE Function (global)==" |
| 27931 |
t=$(exit 1) |
| 27932 |
echo $? # 1 |
| 27933 |
# As expected. |
| 27934 |
echo |
| 27935 |
|
| 27936 |
function0 () |
| 27937 |
{ |
| 27938 |
|
| 27939 |
echo "==INSIDE Function==" |
| 27940 |
echo "Global" |
| 27941 |
t0=$(exit 1) |
| 27942 |
echo $? # 1 |
| 27943 |
# As expected. |
| 27944 |
|
| 27945 |
echo |
| 27946 |
echo "Local declared & assigned in same command." |
| 27947 |
local t1=$(exit 1) |
| 27948 |
echo $? # 0 |
| 27949 |
# Unexpected! |
| 27950 |
# Apparently, the variable assignment takes place before |
| 27951 |
#+ the local declaration. |
| 27952 |
#+ The return value is for the latter. |
| 27953 |
|
| 27954 |
echo |
| 27955 |
echo "Local declared, then assigned (separate commands)." |
| 27956 |
local t2 |
| 27957 |
t2=$(exit 1) |
| 27958 |
echo $? # 1 |
| 27959 |
# As expected. |
| 27960 |
|
| 27961 |
} |
| 27962 |
|
| 27963 |
function0</programlisting></para> |
| 27964 |
</note> |
| 27965 |
|
| 27966 |
</listitem> |
| 27967 |
</varlistentry> |
| 27968 |
|
| 27969 |
</variablelist> |
| 27970 |
|
| 27971 |
|
| 27972 |
<sect2 id="locvarrecur"> |
| 27973 |
<title>Local variables and recursion.</title> |
| 27974 |
|
| 27975 |
<para><anchor id="recursionref0"></para> |
| 27976 |
|
| 27977 |
<sidebar> |
| 27978 |
|
| 27979 |
<para><anchor id="recursionref"></para> |
| 27980 |
<para><firstterm>Recursion</firstterm> is an interesting |
| 27981 |
and sometimes useful form of |
| 27982 |
<firstterm>self-reference</firstterm>. <link |
| 27983 |
linkend="mayerref">Herbert Mayer</link> defines it |
| 27984 |
as <quote>. . . expressing an algorithm by using a |
| 27985 |
simpler version of that same algorithm . . .</quote></para> |
| 27986 |
|
| 27987 |
<para>Consider a definition defined in terms of itself, |
| 27988 |
<footnote><para>Otherwise known as |
| 27989 |
<firstterm>redundancy</firstterm>.</para></footnote> |
| 27990 |
an expression implicit in its own expression, |
| 27991 |
<footnote><para>Otherwise known as |
| 27992 |
<firstterm>tautology</firstterm>.</para></footnote> |
| 27993 |
<emphasis>a snake swallowing its own |
| 27994 |
tail</emphasis>, |
| 27995 |
<footnote><para>Otherwise known as a |
| 27996 |
<firstterm>metaphor</firstterm>.</para></footnote> |
| 27997 |
or . . . a function that calls itself. |
| 27998 |
<footnote><para>Otherwise known as a |
| 27999 |
<firstterm>recursive function</firstterm>.</para></footnote> |
| 28000 |
</para> |
| 28001 |
|
| 28002 |
|
| 28003 |
<para><anchor id="recursiondemo0"></para> |
| 28004 |
<example id="recursiondemo"> |
| 28005 |
<title>Demonstration of a simple recursive function</title> |
| 28006 |
<programlisting>&recursiondemo;</programlisting> |
| 28007 |
</example> |
| 28008 |
|
| 28009 |
<para><anchor id="recursiondemo02"></para> |
| 28010 |
<example id="recursiondemo2"> |
| 28011 |
<title>Another simple demonstration</title> |
| 28012 |
<programlisting>&recursiondemo2;</programlisting> |
| 28013 |
</example> |
| 28014 |
|
| 28015 |
</sidebar> |
| 28016 |
|
| 28017 |
<para>Local variables are a useful tool for writing recursive |
| 28018 |
code, but this practice generally involves a great deal of |
| 28019 |
computational overhead and is definitely |
| 28020 |
<emphasis>not</emphasis> recommended in a shell script. |
| 28021 |
|
| 28022 |
<footnote><para>Too many levels of recursion may crash a |
| 28023 |
script with a segfault. |
| 28024 |
<programlisting>#!/bin/bash |
| 28025 |
|
| 28026 |
# Warning: Running this script could possibly lock up your system! |
| 28027 |
# If you're lucky, it will segfault before using up all available memory. |
| 28028 |
|
| 28029 |
recursive_function () |
| 28030 |
{ |
| 28031 |
echo "$1" # Makes the function do something, and hastens the segfault. |
| 28032 |
(( $1 < $2 )) && recursive_function $(( $1 + 1 )) $2; |
| 28033 |
# As long as 1st parameter is less than 2nd, |
| 28034 |
#+ increment 1st and recurse. |
| 28035 |
} |
| 28036 |
|
| 28037 |
recursive_function 1 50000 # Recurse 50,000 levels! |
| 28038 |
# Most likely segfaults (depending on stack size, set by ulimit -m). |
| 28039 |
|
| 28040 |
# Recursion this deep might cause even a C program to segfault, |
| 28041 |
#+ by using up all the memory allotted to the stack. |
| 28042 |
|
| 28043 |
|
| 28044 |
echo "This will probably not print." |
| 28045 |
exit 0 # This script will not exit normally. |
| 28046 |
|
| 28047 |
# Thanks, Stéphane Chazelas.</programlisting> |
| 28048 |
</para></footnote> |
| 28049 |
|
| 28050 |
</para> |
| 28051 |
|
| 28052 |
<para><anchor id="factorialref"></para> |
| 28053 |
<example id="ex63"> |
| 28054 |
<title>Recursion, using a local variable</title> |
| 28055 |
<programlisting>&ex63;</programlisting> |
| 28056 |
</example> |
| 28057 |
|
| 28058 |
<para>Also see <xref linkend="primes"> for an example of |
| 28059 |
recursion in a script. Be aware that recursion is |
| 28060 |
resource-intensive and executes slowly, and is therefore |
| 28061 |
generally not appropriate in a script.</para> |
| 28062 |
|
| 28063 |
</sect2> |
| 28064 |
</sect1> <!-- Local Variables --> |
| 28065 |
|
| 28066 |
|
| 28067 |
|
| 28068 |
<sect1 id="recurnolocvar"> |
| 28069 |
<title>Recursion Without Local Variables</title> |
| 28070 |
|
| 28071 |
<para>A function may recursively call itself even without use of |
| 28072 |
local variables.</para> |
| 28073 |
|
| 28074 |
<para><anchor id="fiboref"></para> |
| 28075 |
<example id="fibo"> |
| 28076 |
<title><firstterm>The Fibonacci Sequence</firstterm></title> |
| 28077 |
<programlisting>&fibo;</programlisting> |
| 28078 |
</example> |
| 28079 |
|
| 28080 |
<para><anchor id="hanoiref"></para> |
| 28081 |
<example id="hanoi"> |
| 28082 |
<title><firstterm>The Towers of Hanoi</firstterm></title> |
| 28083 |
<programlisting>&hanoi;</programlisting> |
| 28084 |
</example> |
| 28085 |
|
| 28086 |
|
| 28087 |
</sect1> <!-- Recursion Without Local Variables --> |
| 28088 |
|
| 28089 |
|
| 28090 |
|
| 28091 |
</chapter> <!-- Functions --> |
| 28092 |
|
| 28093 |
|
| 28094 |
|
| 28095 |
<chapter id="aliases"> |
| 28096 |
<title>Aliases</title> |
| 28097 |
|
| 28098 |
<para><anchor id="aliasref"></para> |
| 28099 |
|
| 28100 |
<indexterm> |
| 28101 |
<primary>alias</primary> |
| 28102 |
</indexterm> |
| 28103 |
|
| 28104 |
<para>A Bash <firstterm>alias</firstterm> is essentially nothing |
| 28105 |
more than a keyboard shortcut, an abbreviation, a means of |
| 28106 |
avoiding typing a long command sequence. If, for example, |
| 28107 |
we include <command>alias lm="ls -l | more"</command> in |
| 28108 |
the <link linkend="sample-bashrc"><filename>~/.bashrc</filename> |
| 28109 |
file</link>, then each <userinput>lm</userinput> |
| 28110 |
<footnote><para> ... as the first word of a command string. |
| 28111 |
Obviously, an alias is only meaningful at the |
| 28112 |
<emphasis>beginning</emphasis> of a command.</para></footnote> |
| 28113 |
typed at the command-line will automatically be replaced by a |
| 28114 |
<command>ls -l | more</command>. This can save a great deal of |
| 28115 |
typing at the command-line and avoid having to remember complex |
| 28116 |
combinations of commands and options. Setting <command>alias |
| 28117 |
rm="rm -i"</command> (interactive mode delete) may save a |
| 28118 |
good deal of grief, since it can prevent inadvertently deleting |
| 28119 |
important files.</para> |
| 28120 |
|
| 28121 |
<para>In a script, aliases have very limited usefulness. It would be |
| 28122 |
nice if aliases could assume some of the functionality of |
| 28123 |
the <command>C</command> preprocessor, such as macro expansion, |
| 28124 |
but unfortunately Bash does not expand arguments within the |
| 28125 |
alias body. |
| 28126 |
<footnote><para>However, aliases do seem to expand positional |
| 28127 |
parameters.</para></footnote> |
| 28128 |
Moreover, a script fails to expand an alias itself |
| 28129 |
within <quote>compound constructs,</quote> such as <link |
| 28130 |
linkend="ifthen">if/then</link> statements, loops, and |
| 28131 |
functions. An added limitation is that an alias will not expand |
| 28132 |
recursively. Almost invariably, whatever we would like an alias |
| 28133 |
to do could be accomplished much more effectively with a <link |
| 28134 |
linkend="functionref">function</link>.</para> |
| 28135 |
|
| 28136 |
<example id="al"> |
| 28137 |
<title>Aliases within a script</title> |
| 28138 |
<programlisting>&al;</programlisting> |
| 28139 |
</example> |
| 28140 |
|
| 28141 |
<para><anchor id="unaliasref"></para> |
| 28142 |
<para>The <command>unalias</command> command removes a previously |
| 28143 |
set <firstterm>alias</firstterm>.</para> |
| 28144 |
|
| 28145 |
<example id="unal"> |
| 28146 |
<title><firstterm>unalias</firstterm>: Setting and unsetting |
| 28147 |
an alias</title> |
| 28148 |
<programlisting>&unal;</programlisting> |
| 28149 |
</example> |
| 28150 |
|
| 28151 |
<screen><prompt>bash$ </prompt><userinput>./unalias.sh</userinput> |
| 28152 |
<computeroutput> |
| 28153 |
total 6 |
| 28154 |
drwxrwxr-x 2 bozo bozo 3072 Feb 6 14:04 . |
| 28155 |
drwxr-xr-x 40 bozo bozo 2048 Feb 6 14:04 .. |
| 28156 |
-rwxr-xr-x 1 bozo bozo 199 Feb 6 14:04 unalias.sh |
| 28157 |
|
| 28158 |
./unalias.sh: llm: command not found |
| 28159 |
</computeroutput></screen> |
| 28160 |
|
| 28161 |
|
| 28162 |
</chapter> <!-- Aliases --> |
| 28163 |
|
| 28164 |
|
| 28165 |
|
| 28166 |
|
| 28167 |
<chapter id="list-cons"> |
| 28168 |
<title>List Constructs</title> |
| 28169 |
|
| 28170 |
<para><anchor id="listconsref"></para> |
| 28171 |
|
| 28172 |
|
| 28173 |
<indexterm> |
| 28174 |
<primary>&&</primary> |
| 28175 |
</indexterm> |
| 28176 |
<indexterm> |
| 28177 |
<primary>special character</primary> |
| 28178 |
<secondary>&&</secondary> |
| 28179 |
</indexterm> |
| 28180 |
<indexterm> |
| 28181 |
<primary>AND</primary> |
| 28182 |
<secondary>list</secondary> |
| 28183 |
</indexterm> |
| 28184 |
<indexterm> |
| 28185 |
<primary>||</primary> |
| 28186 |
</indexterm> |
| 28187 |
<indexterm> |
| 28188 |
<primary>special character</primary> |
| 28189 |
<secondary>||</secondary> |
| 28190 |
</indexterm> |
| 28191 |
<indexterm> |
| 28192 |
<primary>OR</primary> |
| 28193 |
<secondary>list</secondary> |
| 28194 |
</indexterm> |
| 28195 |
|
| 28196 |
<para>The <firstterm>and list</firstterm> and <firstterm>or |
| 28197 |
list</firstterm> constructs provide a means of processing a |
| 28198 |
number of commands consecutively. These can effectively replace |
| 28199 |
complex nested <link linkend="testconstructs1">if/then</link> |
| 28200 |
or even <link linkend="caseesac1">case</link> statements.</para> |
| 28201 |
|
| 28202 |
|
| 28203 |
<variablelist id="lcons"> |
| 28204 |
<title><anchor id="lcons1">Chaining together commands</title> |
| 28205 |
|
| 28206 |
<varlistentry> |
| 28207 |
<term>and list</term> |
| 28208 |
<listitem> |
| 28209 |
<para><programlisting>command-1 && command-2 && command-3 && ... command-n</programlisting> |
| 28210 |
Each command executes in turn, provided that |
| 28211 |
the previous command has given a return value of |
| 28212 |
<replaceable>true</replaceable> (zero). At the first |
| 28213 |
<replaceable>false</replaceable> (non-zero) return, the |
| 28214 |
command chain terminates (the first command returning |
| 28215 |
<replaceable>false</replaceable> is the last one to |
| 28216 |
execute).</para> |
| 28217 |
|
| 28218 |
<para>An interesting use of a two-condition <firstterm>and |
| 28219 |
list</firstterm> from an early version of YongYe's <ulink |
| 28220 |
url="http://bash.deta.in/Tetris_Game.sh">Tetris |
| 28221 |
game script</ulink>:</para> |
| 28222 |
|
| 28223 |
<para> |
| 28224 |
<programlisting>equation() |
| 28225 |
|
| 28226 |
{ # core algorithm used for doubling and halving the coordinates |
| 28227 |
[[ ${cdx} ]] && ((y=cy+(ccy-cdy)${2}2)) |
| 28228 |
eval ${1}+=\"${x} ${y} \" |
| 28229 |
}</programlisting> |
| 28230 |
</para> |
| 28231 |
|
| 28232 |
|
| 28233 |
<example id="ex64"> |
| 28234 |
<title>Using an <firstterm>and list</firstterm> to test |
| 28235 |
for command-line arguments</title> |
| 28236 |
<programlisting>&ex64;</programlisting> |
| 28237 |
</example> |
| 28238 |
|
| 28239 |
<example id="andlist2"> |
| 28240 |
<title>Another command-line arg test using an <firstterm>and |
| 28241 |
list</firstterm></title> |
| 28242 |
<programlisting>&andlist2;</programlisting> |
| 28243 |
</example> |
| 28244 |
|
| 28245 |
<para><anchor id="anddefault"></para> |
| 28246 |
<para> |
| 28247 |
Of course, an <firstterm>and list</firstterm> can also |
| 28248 |
<firstterm>set</firstterm> variables to a default value. |
| 28249 |
<programlisting>arg1=$@ && [ -z "$arg1" ] && arg1=DEFAULT |
| 28250 |
|
| 28251 |
# Set $arg1 to command-line arguments, if any. |
| 28252 |
# But . . . set to DEFAULT if not specified on command-line.</programlisting> |
| 28253 |
</para> |
| 28254 |
|
| 28255 |
</listitem> |
| 28256 |
</varlistentry> |
| 28257 |
|
| 28258 |
<varlistentry> |
| 28259 |
<term><anchor id="orlistref">or list</term> |
| 28260 |
<listitem> |
| 28261 |
<para><programlisting>command-1 || command-2 || command-3 || ... command-n</programlisting> |
| 28262 |
Each command executes in turn for as long as the previous |
| 28263 |
command returns <returnvalue>false</returnvalue>. At |
| 28264 |
the first <returnvalue>true</returnvalue> return, the |
| 28265 |
command chain terminates (the first command returning |
| 28266 |
<returnvalue>true</returnvalue> is the last one to |
| 28267 |
execute). This is obviously the inverse of the <quote>and |
| 28268 |
list</quote>.</para> |
| 28269 |
|
| 28270 |
|
| 28271 |
|
| 28272 |
<example id="ex65"> |
| 28273 |
<title>Using <firstterm>or lists</firstterm> in combination |
| 28274 |
with an <firstterm>and list</firstterm></title> |
| 28275 |
<programlisting>&ex65;</programlisting> |
| 28276 |
</example> |
| 28277 |
|
| 28278 |
<caution><para>If the first command in an <firstterm>or |
| 28279 |
list</firstterm> returns <returnvalue>true</returnvalue>, |
| 28280 |
it <replaceable>will</replaceable> execute.</para></caution> |
| 28281 |
|
| 28282 |
</listitem> |
| 28283 |
</varlistentry> |
| 28284 |
|
| 28285 |
</variablelist> |
| 28286 |
|
| 28287 |
|
| 28288 |
<para><programlisting># ==> The following snippets from the /etc/rc.d/init.d/single |
| 28289 |
#+==> script by Miquel van Smoorenburg |
| 28290 |
#+==> illustrate use of "and" and "or" lists. |
| 28291 |
# ==> "Arrowed" comments added by document author. |
| 28292 |
|
| 28293 |
[ -x /usr/bin/clear ] && /usr/bin/clear |
| 28294 |
# ==> If /usr/bin/clear exists, then invoke it. |
| 28295 |
# ==> Checking for the existence of a command before calling it |
| 28296 |
#+==> avoids error messages and other awkward consequences. |
| 28297 |
|
| 28298 |
# ==> . . . |
| 28299 |
|
| 28300 |
# If they want to run something in single user mode, might as well run it... |
| 28301 |
for i in /etc/rc1.d/S[0-9][0-9]* ; do |
| 28302 |
# Check if the script is there. |
| 28303 |
[ -x "$i" ] || continue |
| 28304 |
# ==> If corresponding file in $PWD *not* found, |
| 28305 |
#+==> then "continue" by jumping to the top of the loop. |
| 28306 |
|
| 28307 |
# Reject backup files and files generated by rpm. |
| 28308 |
case "$1" in |
| 28309 |
*.rpmsave|*.rpmorig|*.rpmnew|*~|*.orig) |
| 28310 |
continue;; |
| 28311 |
esac |
| 28312 |
[ "$i" = "/etc/rc1.d/S00single" ] && continue |
| 28313 |
# ==> Set script name, but don't execute it yet. |
| 28314 |
$i start |
| 28315 |
done |
| 28316 |
|
| 28317 |
# ==> . . .</programlisting></para> |
| 28318 |
|
| 28319 |
|
| 28320 |
<important><para>The <link linkend="exitstatusref">exit |
| 28321 |
status</link> of an <userinput>and list</userinput> or an |
| 28322 |
<userinput>or list</userinput> is the exit status of the last |
| 28323 |
command executed.</para></important> |
| 28324 |
|
| 28325 |
<para>Clever combinations of <firstterm>and</firstterm> and |
| 28326 |
<firstterm>or</firstterm> lists are possible, but the logic may |
| 28327 |
easily become convoluted and require close attention to <link |
| 28328 |
linkend="opprecedence1">operator precedence rules</link>, and |
| 28329 |
possibly extensive debugging.</para> |
| 28330 |
|
| 28331 |
<para><programlisting>false && true || echo false # false |
| 28332 |
|
| 28333 |
# Same result as |
| 28334 |
( false && true ) || echo false # false |
| 28335 |
# But NOT |
| 28336 |
false && ( true || echo false ) # (nothing echoed) |
| 28337 |
|
| 28338 |
# Note left-to-right grouping and evaluation of statements. |
| 28339 |
|
| 28340 |
# It's usually best to avoid such complexities. |
| 28341 |
|
| 28342 |
# Thanks, S.C.</programlisting> |
| 28343 |
</para> |
| 28344 |
|
| 28345 |
<para>See <xref linkend="daysbetween"> and <xref |
| 28346 |
linkend="brokenlink"> for illustrations of using <userinput>and |
| 28347 |
/ or list</userinput> constructs to test variables.</para> |
| 28348 |
|
| 28349 |
|
| 28350 |
</chapter> <!-- List Constructs --> |
| 28351 |
|
| 28352 |
|
| 28353 |
|
| 28354 |
|
| 28355 |
<chapter id="arrays"> |
| 28356 |
<title>Arrays</title> |
| 28357 |
|
| 28358 |
<para><anchor id="arrayref"></para> |
| 28359 |
|
| 28360 |
<para>Newer versions of Bash support one-dimensional arrays. |
| 28361 |
<anchor id="brackarray"> |
| 28362 |
Array elements may be initialized with the |
| 28363 |
<userinput>variable[xx]</userinput> notation. Alternatively, |
| 28364 |
a script may introduce the entire array by an explicit |
| 28365 |
<userinput>declare -a variable</userinput> statement. To |
| 28366 |
dereference (retrieve the contents of) an array element, use |
| 28367 |
<firstterm>curly bracket</firstterm> notation, that is, |
| 28368 |
<userinput>${element[xx]}</userinput>.</para> |
| 28369 |
|
| 28370 |
<para><anchor id="arraynotation"></para> |
| 28371 |
|
| 28372 |
<example id="ex66"> |
| 28373 |
<title>Simple array usage</title> |
| 28374 |
<programlisting>&ex66;</programlisting> |
| 28375 |
</example> |
| 28376 |
|
| 28377 |
<para><anchor id="arrayinit0"></para> |
| 28378 |
<para>As we have seen, a convenient way of initializing an entire array |
| 28379 |
is the <varname>array=( element1 element2 ... elementN )</varname> |
| 28380 |
notation.</para> |
| 28381 |
|
| 28382 |
<para> |
| 28383 |
<programlisting>base64_charset=( {A..Z} {a..z} {0..9} + / = ) |
| 28384 |
# Using extended brace expansion |
| 28385 |
#+ to initialize the elements of the array. |
| 28386 |
# Excerpted from vladz's "base64.sh" script |
| 28387 |
#+ in the "Contributed Scripts" appendix.</programlisting> |
| 28388 |
</para> |
| 28389 |
|
| 28390 |
<para><anchor id="arrayopsvars"></para> |
| 28391 |
|
| 28392 |
<sidebar><para>Bash permits array operations on variables, even if |
| 28393 |
the variables are not explicitly declared as arrays.</para> |
| 28394 |
<para><programlisting>string=abcABC123ABCabc |
| 28395 |
echo ${string[@]} # abcABC123ABCabc |
| 28396 |
echo ${string[*]} # abcABC123ABCabc |
| 28397 |
echo ${string[0]} # abcABC123ABCabc |
| 28398 |
echo ${string[1]} # No output! |
| 28399 |
# Why? |
| 28400 |
echo ${#string[@]} # 1 |
| 28401 |
# One element in the array. |
| 28402 |
# The string itself. |
| 28403 |
|
| 28404 |
# Thank you, Michael Zick, for pointing this out.</programlisting> |
| 28405 |
Once again this demonstrates that <link linkend="bvuntyped">Bash |
| 28406 |
variables are untyped</link>. |
| 28407 |
</para></sidebar> |
| 28408 |
|
| 28409 |
|
| 28410 |
<example id="poem"> |
| 28411 |
<title>Formatting a poem</title> |
| 28412 |
<programlisting>&poem;</programlisting> |
| 28413 |
</example> |
| 28414 |
|
| 28415 |
<para><anchor id="arraysyntax"></para> |
| 28416 |
<para>Array variables have a syntax all their own, and even |
| 28417 |
standard Bash commands and operators have special options adapted |
| 28418 |
for array use.</para> |
| 28419 |
|
| 28420 |
<example id="arrayops"> |
| 28421 |
<title>Various array operations</title> |
| 28422 |
<programlisting>&arrayops;</programlisting> |
| 28423 |
</example> |
| 28424 |
|
| 28425 |
<para><anchor id="arraystringops"></para> |
| 28426 |
<para>Many of the standard <link linkend="stringmanip">string |
| 28427 |
operations</link> work on arrays.</para> |
| 28428 |
|
| 28429 |
<example id="arraystrops"> |
| 28430 |
<title>String operations on arrays</title> |
| 28431 |
<programlisting>&arraystrops;</programlisting> |
| 28432 |
</example> |
| 28433 |
|
| 28434 |
|
| 28435 |
<para><link linkend="commandsubref">Command substitution</link> can |
| 28436 |
construct the individual elements of an array.</para> |
| 28437 |
|
| 28438 |
<example id="scriptarray"> |
| 28439 |
<title>Loading the contents of a script into an array</title> |
| 28440 |
<programlisting>&scriptarray;</programlisting> |
| 28441 |
</example> |
| 28442 |
|
| 28443 |
<para>In an array context, some Bash <link |
| 28444 |
linkend="builtinref">builtins</link> have a slightly |
| 28445 |
altered meaning. <anchor id="arrayunset">For example, <link |
| 28446 |
linkend="unsetref">unset</link> deletes array elements, or even |
| 28447 |
an entire array.</para> |
| 28448 |
|
| 28449 |
<para><anchor id="arrayspecialprops"></para> |
| 28450 |
<example id="ex67"> |
| 28451 |
<title>Some special properties of arrays</title> |
| 28452 |
<programlisting>&ex67;</programlisting> |
| 28453 |
</example> |
| 28454 |
|
| 28455 |
<para><anchor id="arraynumelements"></para> |
| 28456 |
<para>As seen in the previous example, either |
| 28457 |
<command>${array_name[@]}</command> or |
| 28458 |
<command>${array_name[*]}</command> refers to |
| 28459 |
<emphasis>all</emphasis> the elements of the array. |
| 28460 |
Similarly, to get a count of the number of elements in an |
| 28461 |
array, use either <command>${#array_name[@]}</command> |
| 28462 |
or <command>${#array_name[*]}</command>. |
| 28463 |
<command>${#array_name}</command> is the length (number of |
| 28464 |
characters) of <command>${array_name[0]}</command>, the first |
| 28465 |
element of the array.</para> |
| 28466 |
|
| 28467 |
<para><anchor id="emptyarray0"></para> |
| 28468 |
<example id="emptyarray"> |
| 28469 |
<title>Of empty arrays and empty elements</title> |
| 28470 |
<programlisting>&emptyarray;</programlisting> |
| 28471 |
</example> |
| 28472 |
|
| 28473 |
<para>The relationship of <command>${array_name[@]}</command> |
| 28474 |
and <command>${array_name[*]}</command> is analogous to that |
| 28475 |
between <link linkend="appref">$@ and $*</link>. This powerful |
| 28476 |
array notation has a number of uses.</para> |
| 28477 |
|
| 28478 |
<para><anchor id="copyarray0"></para> |
| 28479 |
<para> |
| 28480 |
<programlisting># Copying an array. |
| 28481 |
array2=( "${array1[@]}" ) |
| 28482 |
# or |
| 28483 |
array2="${array1[@]}" |
| 28484 |
# |
| 28485 |
# However, this fails with "sparse" arrays, |
| 28486 |
#+ arrays with holes (missing elements) in them, |
| 28487 |
#+ as Jochen DeSmet points out. |
| 28488 |
# ------------------------------------------ |
| 28489 |
array1[0]=0 |
| 28490 |
# array1[1] not assigned |
| 28491 |
array1[2]=2 |
| 28492 |
array2=( "${array1[@]}" ) # Copy it? |
| 28493 |
|
| 28494 |
echo ${array2[0]} # 0 |
| 28495 |
echo ${array2[2]} # (null), should be 2 |
| 28496 |
# ------------------------------------------ |
| 28497 |
|
| 28498 |
|
| 28499 |
|
| 28500 |
# Adding an element to an array. |
| 28501 |
array=( "${array[@]}" "new element" ) |
| 28502 |
# or |
| 28503 |
array[${#array[*]}]="new element" |
| 28504 |
|
| 28505 |
# Thanks, S.C.</programlisting> |
| 28506 |
</para> |
| 28507 |
|
| 28508 |
|
| 28509 |
<para><anchor id="arrayinitcs"></para> |
| 28510 |
<tip> |
| 28511 |
<para>The <command>array=( element1 element2 ... elementN )</command> |
| 28512 |
initialization operation, with the help of <link |
| 28513 |
linkend="commandsubref">command substitution</link>, makes it |
| 28514 |
possible to load the contents of a text file into an array.</para> |
| 28515 |
|
| 28516 |
<para> |
| 28517 |
<programlisting>#!/bin/bash |
| 28518 |
|
| 28519 |
filename=sample_file |
| 28520 |
|
| 28521 |
# cat sample_file |
| 28522 |
# |
| 28523 |
# 1 a b c |
| 28524 |
# 2 d e fg |
| 28525 |
|
| 28526 |
|
| 28527 |
declare -a array1 |
| 28528 |
|
| 28529 |
array1=( `cat "$filename"`) # Loads contents |
| 28530 |
# List file to stdout #+ of $filename into array1. |
| 28531 |
# |
| 28532 |
# array1=( `cat "$filename" | tr '\n' ' '`) |
| 28533 |
# change linefeeds in file to spaces. |
| 28534 |
# Not necessary because Bash does word splitting, |
| 28535 |
#+ changing linefeeds to spaces. |
| 28536 |
|
| 28537 |
echo ${array1[@]} # List the array. |
| 28538 |
# 1 a b c 2 d e fg |
| 28539 |
# |
| 28540 |
# Each whitespace-separated "word" in the file |
| 28541 |
#+ has been assigned to an element of the array. |
| 28542 |
|
| 28543 |
element_count=${#array1[*]} |
| 28544 |
echo $element_count # 8</programlisting> |
| 28545 |
</para> |
| 28546 |
</tip> |
| 28547 |
|
| 28548 |
<para>Clever scripting makes it possible to add array operations.</para> |
| 28549 |
|
| 28550 |
<para><anchor id="arrayassign0"></para> |
| 28551 |
<example id="arrayassign"> |
| 28552 |
<title>Initializing arrays</title> |
| 28553 |
<programlisting>&arrayassign;</programlisting> |
| 28554 |
</example> |
| 28555 |
|
| 28556 |
<note><para>Adding a superfluous <command>declare -a</command> |
| 28557 |
statement to an array declaration may speed up execution of |
| 28558 |
subsequent operations on the array.</para></note> |
| 28559 |
|
| 28560 |
<para><anchor id="arrayappend0"></para> |
| 28561 |
<example id="copyarray"> |
| 28562 |
<title>Copying and concatenating arrays</title> |
| 28563 |
<programlisting>©array;</programlisting> |
| 28564 |
</example> |
| 28565 |
|
| 28566 |
<example id="arrayappend"> |
| 28567 |
<title>More on concatenating arrays</title> |
| 28568 |
<programlisting>&arrayappend;</programlisting> |
| 28569 |
</example> |
| 28570 |
|
| 28571 |
|
| 28572 |
<para>--</para> |
| 28573 |
|
| 28574 |
<para>Arrays permit deploying old familiar algorithms as shell scripts. |
| 28575 |
Whether this is necessarily a good idea is left for the reader to |
| 28576 |
decide.</para> |
| 28577 |
|
| 28578 |
<para><anchor id="bubblesort"></para> |
| 28579 |
|
| 28580 |
<example id="bubble"> |
| 28581 |
<title>The Bubble Sort</title> |
| 28582 |
<programlisting>&bubble;</programlisting> |
| 28583 |
</example> |
| 28584 |
|
| 28585 |
<para>--</para> |
| 28586 |
|
| 28587 |
<para><anchor id="arraynest"></para> |
| 28588 |
<para>Is it possible to nest arrays within arrays?</para> |
| 28589 |
<para><programlisting>#!/bin/bash |
| 28590 |
# "Nested" array. |
| 28591 |
|
| 28592 |
# Michael Zick provided this example, |
| 28593 |
#+ with corrections and clarifications by William Park. |
| 28594 |
|
| 28595 |
AnArray=( $(ls --inode --ignore-backups --almost-all \ |
| 28596 |
--directory --full-time --color=none --time=status \ |
| 28597 |
--sort=time -l ${PWD} ) ) # Commands and options. |
| 28598 |
|
| 28599 |
# Spaces are significant . . . and don't quote anything in the above. |
| 28600 |
|
| 28601 |
SubArray=( ${AnArray[@]:11:1} ${AnArray[@]:6:5} ) |
| 28602 |
# This array has six elements: |
| 28603 |
#+ SubArray=( [0]=${AnArray[11]} [1]=${AnArray[6]} [2]=${AnArray[7]} |
| 28604 |
# [3]=${AnArray[8]} [4]=${AnArray[9]} [5]=${AnArray[10]} ) |
| 28605 |
# |
| 28606 |
# Arrays in Bash are (circularly) linked lists |
| 28607 |
#+ of type string (char *). |
| 28608 |
# So, this isn't actually a nested array, |
| 28609 |
#+ but it's functionally similar. |
| 28610 |
|
| 28611 |
echo "Current directory and date of last status change:" |
| 28612 |
echo "${SubArray[@]}" |
| 28613 |
|
| 28614 |
exit 0</programlisting></para> |
| 28615 |
|
| 28616 |
<para>--</para> |
| 28617 |
|
| 28618 |
<para>Embedded arrays in combination with <link |
| 28619 |
linkend="varrefnew">indirect references</link> create some fascinating |
| 28620 |
possibilities</para> |
| 28621 |
|
| 28622 |
<para><anchor id="arrayindir"></para> |
| 28623 |
<example id="embarr"> |
| 28624 |
<title>Embedded arrays and indirect references</title> |
| 28625 |
<programlisting>&embarr;</programlisting> |
| 28626 |
</example> |
| 28627 |
|
| 28628 |
<para>--</para> |
| 28629 |
|
| 28630 |
<para><anchor id="primes0"></para> |
| 28631 |
<para>Arrays enable implementing a shell script version of the |
| 28632 |
<firstterm>Sieve of Eratosthenes</firstterm>. Of course, a |
| 28633 |
resource-intensive application of this nature should really be |
| 28634 |
written in a compiled language, such as C. It runs excruciatingly |
| 28635 |
slowly as a script.</para> |
| 28636 |
|
| 28637 |
<example id="ex68"> |
| 28638 |
<title>The Sieve of Eratosthenes</title> |
| 28639 |
<programlisting>&ex68;</programlisting> |
| 28640 |
</example> |
| 28641 |
|
| 28642 |
<example id="ex68a"> |
| 28643 |
<title>The Sieve of Eratosthenes, Optimized</title> |
| 28644 |
<programlisting>&ex68a;</programlisting> |
| 28645 |
</example> |
| 28646 |
|
| 28647 |
<para>Compare these array-based prime number generators with |
| 28648 |
alternatives that do not use arrays, <xref linkend="primes">, |
| 28649 |
and <xref linkend="primes2">.</para> |
| 28650 |
|
| 28651 |
<para>--</para> |
| 28652 |
|
| 28653 |
<para>Arrays lend themselves, to some extent, to emulating data |
| 28654 |
structures for which Bash has no native support.</para> |
| 28655 |
|
| 28656 |
<para><anchor id="stackex0"></para> |
| 28657 |
<example id="stackex"> |
| 28658 |
<title>Emulating a push-down stack</title> |
| 28659 |
<programlisting>&stackex;</programlisting> |
| 28660 |
</example> |
| 28661 |
|
| 28662 |
<para>--</para> |
| 28663 |
|
| 28664 |
<para>Fancy manipulation of array <quote>subscripts</quote> may require |
| 28665 |
intermediate variables. For projects involving this, again consider |
| 28666 |
using a more powerful programming language, such as Perl or C.</para> |
| 28667 |
|
| 28668 |
<example id="qfunction"> |
| 28669 |
<title>Complex array application: |
| 28670 |
<emphasis>Exploring a weird mathematical series</emphasis></title> |
| 28671 |
<programlisting>&qfunction;</programlisting> |
| 28672 |
</example> |
| 28673 |
|
| 28674 |
<para>--</para> |
| 28675 |
|
| 28676 |
<para><anchor id="arraymultidim"></para> |
| 28677 |
<para>Bash supports only one-dimensional arrays, though a little |
| 28678 |
trickery permits simulating multi-dimensional ones.</para> |
| 28679 |
|
| 28680 |
<example id="twodim"> |
| 28681 |
<title>Simulating a two-dimensional array, then tilting it</title> |
| 28682 |
<programlisting>&twodim;</programlisting> |
| 28683 |
</example> |
| 28684 |
|
| 28685 |
<para>A two-dimensional array is essentially equivalent to a |
| 28686 |
one-dimensional one, but with additional addressing modes |
| 28687 |
for referencing and manipulating the individual elements by |
| 28688 |
<firstterm>row</firstterm> and <firstterm>column</firstterm> |
| 28689 |
position.</para> |
| 28690 |
|
| 28691 |
<para>For an even more elaborate example of simulating a |
| 28692 |
two-dimensional array, see <xref linkend="lifeslow">.</para> |
| 28693 |
|
| 28694 |
<para>--</para> |
| 28695 |
|
| 28696 |
<para>For more interesting scripts using arrays, see: |
| 28697 |
<itemizedlist> |
| 28698 |
<listitem><para><xref linkend="agram2"></para></listitem> |
| 28699 |
<listitem><para><xref linkend="primes2"></para></listitem> |
| 28700 |
<listitem><para><xref linkend="hashex2"></para></listitem> |
| 28701 |
<listitem><para><xref linkend="homework"></para></listitem> |
| 28702 |
<listitem><para><xref linkend="qky"></para></listitem> |
| 28703 |
<listitem><para><xref linkend="nim"></para></listitem> |
| 28704 |
</itemizedlist> |
| 28705 |
</para> |
| 28706 |
|
| 28707 |
</chapter> <!-- Arrays --> |
| 28708 |
|
| 28709 |
|
| 28710 |
<chapter id="ivr"> |
| 28711 |
<title>Indirect References</title> |
| 28712 |
|
| 28713 |
<para><anchor id="ivrref"></para> |
| 28714 |
|
| 28715 |
<para>We have seen that <link linkend="varsubn">referencing |
| 28716 |
a variable</link>, <varname>$var</varname>, fetches its |
| 28717 |
<firstterm>value</firstterm>. <anchor id="evalindref">But, |
| 28718 |
what about the <emphasis>value of a value</emphasis>? What |
| 28719 |
about <varname>$$var</varname>?</para> |
| 28720 |
|
| 28721 |
<para>The actual notation is |
| 28722 |
<replaceable>\$$var</replaceable>, usually preceded by |
| 28723 |
an <link linkend="evalref">eval</link> (and sometimes an |
| 28724 |
<link linkend="echoref">echo</link>). This is called an |
| 28725 |
<firstterm>indirect reference</firstterm>.</para> |
| 28726 |
|
| 28727 |
|
| 28728 |
<example id="indref"> |
| 28729 |
<title>Indirect Variable References</title> |
| 28730 |
<programlisting>&indref;</programlisting> |
| 28731 |
</example> |
| 28732 |
|
| 28733 |
<para><anchor id="irrref"></para> |
| 28734 |
|
| 28735 |
<sidebar><para>Indirect referencing in Bash |
| 28736 |
is a multi-step process. First, take the name of a variable: |
| 28737 |
<varname>varname</varname>. Then, reference it: |
| 28738 |
<varname>$varname</varname>. Then, reference the reference: |
| 28739 |
<varname>$$varname</varname>. Then, <firstterm>escape</firstterm> |
| 28740 |
the first <token>$</token>: <varname>\$$varname</varname>. |
| 28741 |
Finally, force a reevaluation of the expression and assign it: |
| 28742 |
<command>eval newvar=\$$varname</command>.</para></sidebar> |
| 28743 |
|
| 28744 |
|
| 28745 |
<para>Of what practical use is indirect referencing of |
| 28746 |
variables? It gives Bash a little of the functionality |
| 28747 |
of <link linkend="pointerref">pointers</link> |
| 28748 |
in <firstterm>C</firstterm>, for instance, in <link |
| 28749 |
linkend="resistor">table lookup</link>. And, it also has some |
| 28750 |
other very interesting applications. . . .</para> |
| 28751 |
|
| 28752 |
<para>Nils Radtke shows how to build <quote>dynamic</quote> |
| 28753 |
variable names and evaluate their contents. This can be useful |
| 28754 |
when <link linkend="sourceref">sourcing</link> configuration |
| 28755 |
files.</para> |
| 28756 |
|
| 28757 |
<para><programlisting>#!/bin/bash |
| 28758 |
|
| 28759 |
|
| 28760 |
# --------------------------------------------- |
| 28761 |
# This could be "sourced" from a separate file. |
| 28762 |
isdnMyProviderRemoteNet=172.16.0.100 |
| 28763 |
isdnYourProviderRemoteNet=10.0.0.10 |
| 28764 |
isdnOnlineService="MyProvider" |
| 28765 |
# --------------------------------------------- |
| 28766 |
|
| 28767 |
|
| 28768 |
remoteNet=$(eval "echo \$$(echo isdn${isdnOnlineService}RemoteNet)") |
| 28769 |
remoteNet=$(eval "echo \$$(echo isdnMyProviderRemoteNet)") |
| 28770 |
remoteNet=$(eval "echo \$isdnMyProviderRemoteNet") |
| 28771 |
remoteNet=$(eval "echo $isdnMyProviderRemoteNet") |
| 28772 |
|
| 28773 |
echo "$remoteNet" # 172.16.0.100 |
| 28774 |
|
| 28775 |
# ================================================================ |
| 28776 |
|
| 28777 |
# And, it gets even better. |
| 28778 |
|
| 28779 |
# Consider the following snippet given a variable named getSparc, |
| 28780 |
#+ but no such variable getIa64: |
| 28781 |
|
| 28782 |
chkMirrorArchs () { |
| 28783 |
arch="$1"; |
| 28784 |
if [ "$(eval "echo \${$(echo get$(echo -ne $arch | |
| 28785 |
sed 's/^\(.\).*/\1/g' | tr 'a-z' 'A-Z'; echo $arch | |
| 28786 |
sed 's/^.\(.*\)/\1/g')):-false}")" = true ] |
| 28787 |
then |
| 28788 |
return 0; |
| 28789 |
else |
| 28790 |
return 1; |
| 28791 |
fi; |
| 28792 |
} |
| 28793 |
|
| 28794 |
getSparc="true" |
| 28795 |
unset getIa64 |
| 28796 |
chkMirrorArchs sparc |
| 28797 |
echo $? # 0 |
| 28798 |
# True |
| 28799 |
|
| 28800 |
chkMirrorArchs Ia64 |
| 28801 |
echo $? # 1 |
| 28802 |
# False |
| 28803 |
|
| 28804 |
# Notes: |
| 28805 |
# ----- |
| 28806 |
# Even the to-be-substituted variable name part is built explicitly. |
| 28807 |
# The parameters to the chkMirrorArchs calls are all lower case. |
| 28808 |
# The variable name is composed of two parts: "get" and "Sparc" . . .</programlisting> |
| 28809 |
</para> |
| 28810 |
|
| 28811 |
|
| 28812 |
|
| 28813 |
<example id="coltotaler2"> |
| 28814 |
<title>Passing an indirect reference to <firstterm>awk</firstterm></title> |
| 28815 |
<programlisting>&coltotaler2;</programlisting> |
| 28816 |
</example> |
| 28817 |
|
| 28818 |
<caution><para>This method of indirect referencing is a bit tricky. |
| 28819 |
If the second order variable changes its value, then the first |
| 28820 |
order variable must be properly dereferenced (as in the above |
| 28821 |
example). <anchor id="ivr2">Fortunately, the |
| 28822 |
<replaceable>${!variable}</replaceable> notation introduced |
| 28823 |
with <link linkend="bash2ref">version 2</link> of Bash |
| 28824 |
(see <xref linkend="ex78"> and <xref linkend="hashex2">) makes |
| 28825 |
indirect referencing more intuitive.</para></caution> |
| 28826 |
|
| 28827 |
|
| 28828 |
<sidebar> |
| 28829 |
<para>Bash does not support pointer arithmetic, and this severely |
| 28830 |
limits the usefulness of indirect referencing. In fact, indirect |
| 28831 |
referencing in a scripting language is, at best, something of |
| 28832 |
an afterthought.</para> |
| 28833 |
</sidebar> |
| 28834 |
|
| 28835 |
</chapter> <!-- Indirect References to Variables --> |
| 28836 |
|
| 28837 |
|
| 28838 |
|
| 28839 |
|
| 28840 |
<chapter id="devproc"> |
| 28841 |
<title><filename class="directory">/dev</filename> and <filename |
| 28842 |
class="directory">/proc</filename></title> |
| 28843 |
|
| 28844 |
<para><anchor id="devprocref"></para> |
| 28845 |
|
| 28846 |
<para>A Linux or UNIX filesystem typically has the |
| 28847 |
<filename class="directory">/dev</filename> and |
| 28848 |
<filename class="directory">/proc</filename> special-purpose |
| 28849 |
directories.</para> |
| 28850 |
|
| 28851 |
|
| 28852 |
|
| 28853 |
<sect1 id="devref1"> |
| 28854 |
<title><filename class="directory">/dev</filename></title> |
| 28855 |
|
| 28856 |
<para><anchor id="devfileref">The <filename |
| 28857 |
class="directory">/dev</filename> directory contains entries for |
| 28858 |
the <firstterm>physical devices</firstterm> that may or may not |
| 28859 |
be present in the hardware. |
| 28860 |
|
| 28861 |
<footnote> |
| 28862 |
<para>The entries in <filename class="directory">/dev</filename> |
| 28863 |
provide mount points for physical and virtual devices. These |
| 28864 |
entries use very little drive space.</para> |
| 28865 |
<para>Some devices, such as <filename>/dev/null</filename>, |
| 28866 |
<filename>/dev/zero</filename>, |
| 28867 |
and <filename>/dev/urandom</filename> are virtual. They |
| 28868 |
are not actual physical devices and exist only in |
| 28869 |
software.</para> |
| 28870 |
</footnote> |
| 28871 |
|
| 28872 |
Appropriately enough, these are called <firstterm>device |
| 28873 |
files</firstterm>. |
| 28874 |
As an example, the hard drive partitions containing |
| 28875 |
the mounted filesystem(s) have entries in <filename |
| 28876 |
class="directory">/dev</filename>, as <link |
| 28877 |
linkend="dfref">df</link> shows.</para> |
| 28878 |
|
| 28879 |
<para><screen><prompt>bash$ </prompt><userinput>df</userinput> |
| 28880 |
<computeroutput>Filesystem 1k-blocks Used Available Use% |
| 28881 |
Mounted on |
| 28882 |
/dev/hda6 495876 222748 247527 48% / |
| 28883 |
/dev/hda1 50755 3887 44248 9% /boot |
| 28884 |
/dev/hda8 367013 13262 334803 4% /home |
| 28885 |
/dev/hda5 1714416 1123624 503704 70% /usr</computeroutput> |
| 28886 |
</screen> |
| 28887 |
</para> |
| 28888 |
|
| 28889 |
<para><anchor id="loopbackref">Among other things, the <filename |
| 28890 |
class="directory">/dev</filename> directory |
| 28891 |
contains <firstterm>loopback</firstterm> devices, such as |
| 28892 |
<filename>/dev/loop0</filename>. A loopback device is a gimmick |
| 28893 |
that allows an ordinary file to be accessed as if it were a |
| 28894 |
block device. |
| 28895 |
|
| 28896 |
<footnote><para><anchor id="blockdevref">A <firstterm>block |
| 28897 |
device</firstterm> reads and/or writes data in chunks, |
| 28898 |
or <firstterm>blocks</firstterm>, in contrast to a <anchor |
| 28899 |
id="chardevref"><firstterm>character device</firstterm>, |
| 28900 |
which acesses data in <firstterm>character</firstterm> |
| 28901 |
units. Examples of block devices are hard drives, CDROM |
| 28902 |
drives, and flash drives. Examples of character devices are |
| 28903 |
keyboards, modems, sound cards.</para></footnote> |
| 28904 |
|
| 28905 |
This permits mounting an entire filesystem within a |
| 28906 |
single large file. See <xref linkend="createfs"> and <xref |
| 28907 |
linkend="isomountref">.</para> |
| 28908 |
|
| 28909 |
<para>A few of the pseudo-devices in <filename |
| 28910 |
class="directory">/dev</filename> |
| 28911 |
have other specialized uses, such as <link |
| 28912 |
linkend="zerosref"><filename>/dev/null</filename></link>, <link |
| 28913 |
linkend="zerosref1"><filename>/dev/zero</filename></link>, <link |
| 28914 |
linkend="urandomref"><filename>/dev/urandom</filename></link>, |
| 28915 |
<filename>/dev/sda1</filename> (hard drive partition), |
| 28916 |
<filename>/dev/udp</filename> (<firstterm>User |
| 28917 |
Datagram Packet</firstterm> port), and <link |
| 28918 |
linkend="devtcp"><filename>/dev/tcp</filename></link>.</para> |
| 28919 |
|
| 28920 |
<para>For instance:</para> |
| 28921 |
|
| 28922 |
<para>To manually <link linkend="mountref">mount</link> |
| 28923 |
a USB flash drive, append the following line to |
| 28924 |
<link linkend="fstabref"><filename>/etc/fstab</filename></link>. |
| 28925 |
|
| 28926 |
<footnote> |
| 28927 |
<para>Of course, the mount point |
| 28928 |
<filename>/mnt/flashdrive</filename> must exist. If not, |
| 28929 |
then, as <firstterm>root</firstterm>, <command>mkdir |
| 28930 |
/mnt/flashdrive</command>.</para> |
| 28931 |
<para>To actually mount the drive, use the following command: |
| 28932 |
<command>mount /mnt/flashdrive</command></para> |
| 28933 |
<para>Newer Linux distros automount flash drives in the |
| 28934 |
<filename class="directory">/media</filename> |
| 28935 |
directory without user intervention.</para> |
| 28936 |
</footnote> |
| 28937 |
|
| 28938 |
<programlisting>/dev/sda1 /mnt/flashdrive auto noauto,user,noatime 0 0</programlisting> |
| 28939 |
|
| 28940 |
(See also <xref linkend="usbinst">.)</para> |
| 28941 |
|
| 28942 |
<para>Checking whether a disk is in the CD-burner |
| 28943 |
(soft-linked to <filename>/dev/hdc</filename>): |
| 28944 |
|
| 28945 |
<programlisting>head -1 /dev/hdc |
| 28946 |
|
| 28947 |
|
| 28948 |
# head: cannot open '/dev/hdc' for reading: No medium found |
| 28949 |
# (No disc in the drive.) |
| 28950 |
|
| 28951 |
# head: error reading '/dev/hdc': Input/output error |
| 28952 |
# (There is a disk in the drive, but it can't be read; |
| 28953 |
#+ possibly it's an unrecorded CDR blank.) |
| 28954 |
|
| 28955 |
# Stream of characters and assorted gibberish |
| 28956 |
# (There is a pre-recorded disk in the drive, |
| 28957 |
#+ and this is raw output -- a stream of ASCII and binary data.) |
| 28958 |
# Here we see the wisdom of using 'head' to limit the output |
| 28959 |
#+ to manageable proportions, rather than 'cat' or something similar. |
| 28960 |
|
| 28961 |
|
| 28962 |
# Now, it's just a matter of checking/parsing the output and taking |
| 28963 |
#+ appropriate action.</programlisting></para> |
| 28964 |
|
| 28965 |
|
| 28966 |
<para><anchor id="socketref"></para> |
| 28967 |
|
| 28968 |
<para>When executing a command on a |
| 28969 |
<filename>/dev/tcp/$host/$port</filename> pseudo-device file, Bash |
| 28970 |
opens a TCP connection to the associated |
| 28971 |
<firstterm>socket</firstterm>.</para> |
| 28972 |
|
| 28973 |
|
| 28974 |
<sidebar><para>A <firstterm>socket</firstterm> is a |
| 28975 |
communications node associated with a specific I/O |
| 28976 |
port. (This is analogous to a <firstterm>hardware |
| 28977 |
socket</firstterm>, or <firstterm>receptacle</firstterm>, |
| 28978 |
for a connecting cable.) It permits data transfer between |
| 28979 |
hardware devices on the same machine, between machines |
| 28980 |
on the same network, between machines across different |
| 28981 |
networks, and, of course, between machines at different |
| 28982 |
locations on the Internet.</para></sidebar> |
| 28983 |
|
| 28984 |
|
| 28985 |
<para><anchor id="npref">The following examples assume an active Internet |
| 28986 |
connection.</para> |
| 28987 |
|
| 28988 |
<para>Getting the time from <filename>nist.gov</filename>:</para> |
| 28989 |
<screen><prompt>bash$ </prompt><userinput>cat </dev/tcp/time.nist.gov/13</userinput> |
| 28990 |
<computeroutput>53082 04-03-18 04:26:54 68 0 0 502.3 UTC(NIST) *</computeroutput> |
| 28991 |
</screen> |
| 28992 |
|
| 28993 |
<para>[Mark contributed this example.]</para> |
| 28994 |
|
| 28995 |
<para>Generalizing the above into a script:</para> |
| 28996 |
<para><programlisting>#!/bin/bash |
| 28997 |
# This script must run with root permissions. |
| 28998 |
|
| 28999 |
URL="time.nist.gov/13" |
| 29000 |
|
| 29001 |
Time=$(cat </dev/tcp/"$URL") |
| 29002 |
UTC=$(echo "$Time" | awk '{print$3}') # Third field is UTC (GMT) time. |
| 29003 |
# Exercise: modify this for different time zones. |
| 29004 |
|
| 29005 |
echo "UTC Time = "$UTC""</programlisting></para> |
| 29006 |
|
| 29007 |
<para><anchor id="nw001">Downloading a URL:</para> |
| 29008 |
<screen><prompt>bash$ </prompt><userinput>exec 5<>/dev/tcp/www.net.cn/80</userinput> |
| 29009 |
<prompt>bash$ </prompt><userinput>echo -e "GET / HTTP/1.0\n" >&5</userinput> |
| 29010 |
<prompt>bash$ </prompt><userinput>cat <&5</userinput> |
| 29011 |
</screen> |
| 29012 |
|
| 29013 |
<para>[Thanks, Mark and Mihai Maties.]</para> |
| 29014 |
|
| 29015 |
|
| 29016 |
|
| 29017 |
|
| 29018 |
<example id="devtcp"> |
| 29019 |
<title>Using <filename>/dev/tcp</filename> for |
| 29020 |
troubleshooting</title> |
| 29021 |
<programlisting>&devtcp;</programlisting> |
| 29022 |
</example> |
| 29023 |
|
| 29024 |
<example id="musicscr"> |
| 29025 |
<title>Playing music</title> |
| 29026 |
<programlisting>&musicscr;</programlisting> |
| 29027 |
</example> |
| 29028 |
|
| 29029 |
|
| 29030 |
</sect1> <!-- /dev --> |
| 29031 |
|
| 29032 |
|
| 29033 |
|
| 29034 |
<sect1 id="procref1"> |
| 29035 |
<title><filename class="directory">/proc</filename></title> |
| 29036 |
|
| 29037 |
|
| 29038 |
<para><anchor id="procref2"></para> |
| 29039 |
<para>The <filename class="directory">/proc</filename> directory |
| 29040 |
is actually a pseudo-filesystem. The files in <filename |
| 29041 |
class="directory">/proc</filename> mirror currently running |
| 29042 |
system and kernel <link linkend="processref">processes</link> |
| 29043 |
and contain information and statistics about them.</para> |
| 29044 |
|
| 29045 |
<para> |
| 29046 |
<screen><prompt>bash$ </prompt><userinput>cat /proc/devices</userinput> |
| 29047 |
<computeroutput>Character devices: |
| 29048 |
1 mem |
| 29049 |
2 pty |
| 29050 |
3 ttyp |
| 29051 |
4 ttyS |
| 29052 |
5 cua |
| 29053 |
7 vcs |
| 29054 |
10 misc |
| 29055 |
14 sound |
| 29056 |
29 fb |
| 29057 |
36 netlink |
| 29058 |
128 ptm |
| 29059 |
136 pts |
| 29060 |
162 raw |
| 29061 |
254 pcmcia |
| 29062 |
|
| 29063 |
Block devices: |
| 29064 |
1 ramdisk |
| 29065 |
2 fd |
| 29066 |
3 ide0 |
| 29067 |
9 md</computeroutput> |
| 29068 |
|
| 29069 |
|
| 29070 |
|
| 29071 |
<prompt>bash$ </prompt><userinput>cat /proc/interrupts</userinput> |
| 29072 |
<computeroutput> CPU0 |
| 29073 |
0: 84505 XT-PIC timer |
| 29074 |
1: 3375 XT-PIC keyboard |
| 29075 |
2: 0 XT-PIC cascade |
| 29076 |
5: 1 XT-PIC soundblaster |
| 29077 |
8: 1 XT-PIC rtc |
| 29078 |
12: 4231 XT-PIC PS/2 Mouse |
| 29079 |
14: 109373 XT-PIC ide0 |
| 29080 |
NMI: 0 |
| 29081 |
ERR: 0</computeroutput> |
| 29082 |
|
| 29083 |
|
| 29084 |
<prompt>bash$ </prompt><userinput>cat /proc/partitions</userinput> |
| 29085 |
<computeroutput>major minor #blocks name rio rmerge rsect ruse wio wmerge wsect wuse running use aveq |
| 29086 |
|
| 29087 |
3 0 3007872 hda 4472 22260 114520 94240 3551 18703 50384 549710 0 111550 644030 |
| 29088 |
3 1 52416 hda1 27 395 844 960 4 2 14 180 0 800 1140 |
| 29089 |
3 2 1 hda2 0 0 0 0 0 0 0 0 0 0 0 |
| 29090 |
3 4 165280 hda4 10 0 20 210 0 0 0 0 0 210 210 |
| 29091 |
...</computeroutput> |
| 29092 |
|
| 29093 |
|
| 29094 |
|
| 29095 |
<prompt>bash$ </prompt><userinput>cat /proc/loadavg</userinput> |
| 29096 |
<computeroutput>0.13 0.42 0.27 2/44 1119</computeroutput> |
| 29097 |
|
| 29098 |
|
| 29099 |
|
| 29100 |
<prompt>bash$ </prompt><userinput>cat /proc/apm</userinput> |
| 29101 |
<computeroutput>1.16 1.2 0x03 0x01 0xff 0x80 -1% -1 ?</computeroutput> |
| 29102 |
|
| 29103 |
|
| 29104 |
|
| 29105 |
<prompt>bash$ </prompt><userinput>cat /proc/acpi/battery/BAT0/info</userinput> |
| 29106 |
<computeroutput>present: yes |
| 29107 |
design capacity: 43200 mWh |
| 29108 |
last full capacity: 36640 mWh |
| 29109 |
battery technology: rechargeable |
| 29110 |
design voltage: 10800 mV |
| 29111 |
design capacity warning: 1832 mWh |
| 29112 |
design capacity low: 200 mWh |
| 29113 |
capacity granularity 1: 1 mWh |
| 29114 |
capacity granularity 2: 1 mWh |
| 29115 |
model number: IBM-02K6897 |
| 29116 |
serial number: 1133 |
| 29117 |
battery type: LION |
| 29118 |
OEM info: Panasonic</computeroutput> |
| 29119 |
|
| 29120 |
|
| 29121 |
|
| 29122 |
<prompt>bash$ </prompt><userinput>fgrep Mem /proc/meminfo</userinput> |
| 29123 |
<computeroutput>MemTotal: 515216 kB |
| 29124 |
MemFree: 266248 kB</computeroutput> |
| 29125 |
</screen> |
| 29126 |
</para> |
| 29127 |
|
| 29128 |
<para>Shell scripts may extract data from certain of the files in |
| 29129 |
<filename class="directory">/proc</filename>. |
| 29130 |
|
| 29131 |
<footnote><para>Certain system commands, such as |
| 29132 |
<link linkend="procinforef">procinfo</link>, |
| 29133 |
<link linkend="freeref">free</link>, |
| 29134 |
<link linkend="vmstatref">vmstat</link>, |
| 29135 |
<link linkend="lsdevref">lsdev</link>, |
| 29136 |
and <link linkend="uptimeref">uptime</link> |
| 29137 |
do this as well.</para></footnote></para> |
| 29138 |
|
| 29139 |
<para><programlisting>FS=iso # ISO filesystem support in kernel? |
| 29140 |
|
| 29141 |
grep $FS /proc/filesystems # iso9660</programlisting></para> |
| 29142 |
|
| 29143 |
|
| 29144 |
<para><programlisting>kernel_version=$( awk '{ print $3 }' /proc/version )</programlisting></para> |
| 29145 |
|
| 29146 |
<para><programlisting>CPU=$( awk '/model name/ {print $5}' < /proc/cpuinfo ) |
| 29147 |
|
| 29148 |
if [ "$CPU" = "Pentium(R)" ] |
| 29149 |
then |
| 29150 |
run_some_commands |
| 29151 |
... |
| 29152 |
else |
| 29153 |
run_other_commands |
| 29154 |
... |
| 29155 |
fi |
| 29156 |
|
| 29157 |
|
| 29158 |
|
| 29159 |
cpu_speed=$( fgrep "cpu MHz" /proc/cpuinfo | awk '{print $4}' ) |
| 29160 |
# Current operating speed (in MHz) of the cpu on your machine. |
| 29161 |
# On a laptop this may vary, depending on use of battery |
| 29162 |
#+ or AC power.</programlisting></para> |
| 29163 |
|
| 29164 |
|
| 29165 |
<para><programlisting>#!/bin/bash |
| 29166 |
# get-commandline.sh |
| 29167 |
# Get the command-line parameters of a process. |
| 29168 |
|
| 29169 |
OPTION=cmdline |
| 29170 |
|
| 29171 |
# Identify PID. |
| 29172 |
pid=$( echo $(pidof "$1") | awk '{ print $1 }' ) |
| 29173 |
# Get only first ^^^^^^^^^^^^^^^^^^ of multiple instances. |
| 29174 |
|
| 29175 |
echo |
| 29176 |
echo "Process ID of (first instance of) "$1" = $pid" |
| 29177 |
echo -n "Command-line arguments: " |
| 29178 |
cat /proc/"$pid"/"$OPTION" | xargs -0 echo |
| 29179 |
# Formats output: ^^^^^^^^^^^^^^^ |
| 29180 |
# (Thanks, Han Holl, for the fixup!) |
| 29181 |
|
| 29182 |
echo; echo |
| 29183 |
|
| 29184 |
|
| 29185 |
# For example: |
| 29186 |
# sh get-commandline.sh xterm</programlisting></para> |
| 29187 |
|
| 29188 |
<para>+</para> |
| 29189 |
|
| 29190 |
<para><programlisting>devfile="/proc/bus/usb/devices" |
| 29191 |
text="Spd" |
| 29192 |
USB1="Spd=12" |
| 29193 |
USB2="Spd=480" |
| 29194 |
|
| 29195 |
|
| 29196 |
bus_speed=$(fgrep -m 1 "$text" $devfile | awk '{print $9}') |
| 29197 |
# ^^^^ Stop after first match. |
| 29198 |
|
| 29199 |
if [ "$bus_speed" = "$USB1" ] |
| 29200 |
then |
| 29201 |
echo "USB 1.1 port found." |
| 29202 |
# Do something appropriate for USB 1.1. |
| 29203 |
fi</programlisting></para> |
| 29204 |
|
| 29205 |
|
| 29206 |
<note> |
| 29207 |
<para>It is even possible to control certain peripherals with commands |
| 29208 |
sent to the <filename class="directory">/proc</filename> directory. |
| 29209 |
|
| 29210 |
<screen> |
| 29211 |
<prompt>root# </prompt><command>echo on > /proc/acpi/ibm/light</command> |
| 29212 |
</screen> |
| 29213 |
|
| 29214 |
This turns on the <emphasis>Thinklight</emphasis> in certain models |
| 29215 |
of IBM/Lenovo Thinkpads. (May not work on all Linux distros.)</para> |
| 29216 |
|
| 29217 |
<para>Of course, caution is advised when writing to <filename |
| 29218 |
class="directory">/proc</filename>.</para> |
| 29219 |
|
| 29220 |
</note> |
| 29221 |
|
| 29222 |
|
| 29223 |
<para><anchor id="procrunning"></para> |
| 29224 |
<para>The <filename class="directory">/proc</filename> directory |
| 29225 |
contains subdirectories with unusual numerical |
| 29226 |
names. Every one of these names maps to the <link |
| 29227 |
linkend="ppidref">process ID</link> of a currently running |
| 29228 |
process. Within each of these subdirectories, there are |
| 29229 |
a number of files that hold useful information about the |
| 29230 |
corresponding process. The <filename>stat</filename> and |
| 29231 |
<filename>status</filename> files keep running statistics |
| 29232 |
on the process, the <filename>cmdline</filename> file holds |
| 29233 |
the command-line arguments the process was invoked with, and |
| 29234 |
the <filename>exe</filename> file is a symbolic link to the |
| 29235 |
complete path name of the invoking process. There are a few |
| 29236 |
more such files, but these seem to be the most interesting |
| 29237 |
from a scripting standpoint.</para> |
| 29238 |
|
| 29239 |
<example id="pidid"> |
| 29240 |
<title>Finding the process associated with a PID</title> |
| 29241 |
<programlisting>&pidid;</programlisting> |
| 29242 |
</example> |
| 29243 |
|
| 29244 |
<example id="constat"> |
| 29245 |
<title>On-line connect status</title> |
| 29246 |
<programlisting>&constat;</programlisting> |
| 29247 |
</example> |
| 29248 |
|
| 29249 |
|
| 29250 |
<para><anchor id="procwarning"></para> |
| 29251 |
<warning><para>In general, it is dangerous to |
| 29252 |
<emphasis>write</emphasis> to the files in <filename |
| 29253 |
class="directory">/proc</filename>, as this can corrupt the |
| 29254 |
filesystem or crash the machine.</para></warning> |
| 29255 |
|
| 29256 |
</sect1> <!-- /proc --> |
| 29257 |
|
| 29258 |
</chapter> <!-- /dev and /proc --> |
| 29259 |
|
| 29260 |
<chapter id="networkprogramming"> |
| 29261 |
<title>Network Programming</title> |
| 29262 |
|
| 29263 |
<epigraph> |
| 29264 |
<para>The Net's a cross between an elephant and a white |
| 29265 |
elephant sale: it never forgets, and it's always crap.</para> |
| 29266 |
<para>--Nemo</para> |
| 29267 |
</epigraph> |
| 29268 |
|
| 29269 |
<para>A Linux system has quite a number of tools for accessing, |
| 29270 |
manipulating, and troubleshooting network connections. We can |
| 29271 |
incorporate some of these tools into scripts -- scripts that |
| 29272 |
expand our knowledge of networking, useful scripts that can |
| 29273 |
facilitate the administration of a network.</para> |
| 29274 |
|
| 29275 |
<para><anchor id="cgiscript">Here is a simple CGI |
| 29276 |
script that demonstrates connecting to a remote server.</para> |
| 29277 |
|
| 29278 |
<example id="testcgi"> |
| 29279 |
<title>Print the server environment</title> |
| 29280 |
<programlisting>&testcgi;</programlisting> |
| 29281 |
</example> |
| 29282 |
|
| 29283 |
<para>For security purposes, it may be helpful to identify the IP |
| 29284 |
addresses a computer is accessing.</para> |
| 29285 |
|
| 29286 |
<para><anchor id="iptables02"></para> |
| 29287 |
|
| 29288 |
<example id="ipaddresses"> |
| 29289 |
<title>IP addresses</title> |
| 29290 |
<programlisting>&ipaddresses;</programlisting> |
| 29291 |
</example> |
| 29292 |
|
| 29293 |
<para>More examples of network programming: |
| 29294 |
<orderedlist> |
| 29295 |
<listitem><para><link linkend="npref">Getting the time from |
| 29296 |
<firstterm>nist.gov</firstterm></link></para></listitem> |
| 29297 |
<listitem><para><link linkend="nw001">Downloading a |
| 29298 |
URL</link></para></listitem> |
| 29299 |
<listitem><para><link linkend="ipscript0">A GRE |
| 29300 |
tunnel</link></para></listitem> |
| 29301 |
<listitem><para><link linkend="ping0">Checking |
| 29302 |
if an Internet server is up</link></para></listitem> |
| 29303 |
<listitem><para><xref linkend="isspammer"></para></listitem> |
| 29304 |
<listitem><para><xref linkend="isspammer2"></para></listitem> |
| 29305 |
<listitem><para><xref linkend="whx"></para></listitem> |
| 29306 |
<listitem><para><xref linkend="devtcp"></para></listitem> |
| 29307 |
</orderedlist> |
| 29308 |
</para> |
| 29309 |
|
| 29310 |
<para>See also the <link linkend="networksys1">networking commands</link> |
| 29311 |
in the <link linkend="system">System and |
| 29312 |
Administrative Commands</link> chapter and the <link |
| 29313 |
linkend="communications">communications commands</link> in |
| 29314 |
the <link linkend="external">External Filters, Programs and |
| 29315 |
Commands</link> chapter.</para> |
| 29316 |
|
| 29317 |
|
| 29318 |
</chapter> <!-- Network Programing --> |
| 29319 |
|
| 29320 |
|
| 29321 |
<chapter id="zeros"> |
| 29322 |
<title>Of Zeros and Nulls</title> |
| 29323 |
|
| 29324 |
<epigraph> |
| 29325 |
<para>Faultily faultless, icily regular, splendidly null</para> |
| 29326 |
<para>Dead perfection; no more.</para> |
| 29327 |
<para>--Alfred Lord Tennyson</para> |
| 29328 |
</epigraph> |
| 29329 |
|
| 29330 |
<para><anchor id="zerosref"></para> |
| 29331 |
|
| 29332 |
<variablelist id="zeronull"> |
| 29333 |
<title><anchor id="zeronull1"><filename>/dev/zero</filename> |
| 29334 |
... <filename>/dev/null</filename></title> |
| 29335 |
<varlistentry> |
| 29336 |
<term><anchor id="devnullref">Uses of |
| 29337 |
<filename>/dev/null</filename></term> |
| 29338 |
<listitem> |
| 29339 |
<para>Think of <filename>/dev/null</filename> as a <firstterm>black |
| 29340 |
hole</firstterm>. It is essentially the equivalent of |
| 29341 |
a write-only file. Everything written to it disappears. |
| 29342 |
Attempts to read or output from it result in nothing. All |
| 29343 |
the same, <filename>/dev/null</filename> can be quite |
| 29344 |
useful from both the command-line and in scripts.</para> |
| 29345 |
|
| 29346 |
<para>Suppressing <filename>stdout</filename>. |
| 29347 |
<programlisting>cat $filename >/dev/null |
| 29348 |
# Contents of the file will not list to stdout.</programlisting> |
| 29349 |
</para> |
| 29350 |
|
| 29351 |
<para>Suppressing <filename>stderr</filename> |
| 29352 |
(from <xref linkend="ex57">). |
| 29353 |
<programlisting>rm $badname 2>/dev/null |
| 29354 |
# So error messages [stderr] deep-sixed.</programlisting> |
| 29355 |
</para> |
| 29356 |
|
| 29357 |
<para>Suppressing output from <emphasis>both</emphasis> |
| 29358 |
<filename>stdout</filename> and <filename>stderr</filename>. |
| 29359 |
<programlisting>cat $filename 2>/dev/null >/dev/null |
| 29360 |
# If "$filename" does not exist, there will be no error message output. |
| 29361 |
# If "$filename" does exist, the contents of the file will not list to stdout. |
| 29362 |
# Therefore, no output at all will result from the above line of code. |
| 29363 |
# |
| 29364 |
# This can be useful in situations where the return code from a command |
| 29365 |
#+ needs to be tested, but no output is desired. |
| 29366 |
# |
| 29367 |
# cat $filename &>/dev/null |
| 29368 |
# also works, as Baris Cicek points out.</programlisting> |
| 29369 |
</para> |
| 29370 |
|
| 29371 |
<para>Deleting contents of a file, but preserving the file itself, with |
| 29372 |
all attendant permissions (from <xref linkend="ex1"> and <xref linkend="ex2">): |
| 29373 |
<programlisting>cat /dev/null > /var/log/messages |
| 29374 |
# : > /var/log/messages has same effect, but does not spawn a new process. |
| 29375 |
|
| 29376 |
cat /dev/null > /var/log/wtmp</programlisting> |
| 29377 |
</para> |
| 29378 |
|
| 29379 |
<para>Automatically emptying the contents of a logfile |
| 29380 |
(especially good for dealing with those nasty |
| 29381 |
<quote>cookies</quote> sent by commercial Web sites):</para> |
| 29382 |
|
| 29383 |
<example id="cookies"> |
| 29384 |
<title>Hiding the cookie jar</title> |
| 29385 |
<programlisting># Obsolete Netscape browser. |
| 29386 |
# Same principle applies to newer browsers. |
| 29387 |
|
| 29388 |
if [ -f ~/.netscape/cookies ] # Remove, if exists. |
| 29389 |
then |
| 29390 |
rm -f ~/.netscape/cookies |
| 29391 |
fi |
| 29392 |
|
| 29393 |
ln -s /dev/null ~/.netscape/cookies |
| 29394 |
# All cookies now get sent to a black hole, rather than saved to disk.</programlisting> |
| 29395 |
</example> |
| 29396 |
|
| 29397 |
</listitem> |
| 29398 |
</varlistentry> |
| 29399 |
|
| 29400 |
<varlistentry> |
| 29401 |
<term><anchor id="zerosref1">Uses of <filename>/dev/zero</filename></term> |
| 29402 |
<listitem> |
| 29403 |
<para>Like <filename>/dev/null</filename>, |
| 29404 |
<filename>/dev/zero</filename> is a pseudo-device file, but |
| 29405 |
it actually produces a stream of nulls |
| 29406 |
(<emphasis>binary</emphasis> zeros, not the <link |
| 29407 |
linkend="asciidef">ASCII</link> kind). Output written |
| 29408 |
to <filename>/dev/zero</filename> disappears, and it is |
| 29409 |
fairly difficult to actually read the nulls emitted there, |
| 29410 |
though it can be done with <link linkend="odref">od</link> |
| 29411 |
or a hex editor. <anchor id="swapfileref">The chief use of |
| 29412 |
<filename>/dev/zero</filename> is creating an initialized |
| 29413 |
dummy file of predetermined length intended as a temporary |
| 29414 |
swap file.</para> |
| 29415 |
|
| 29416 |
<example id="ex73"> |
| 29417 |
<title>Setting up a swapfile using <filename>/dev/zero</filename></title> |
| 29418 |
<programlisting>&ex73;</programlisting> |
| 29419 |
</example> |
| 29420 |
|
| 29421 |
<para>Another application of <filename>/dev/zero</filename> |
| 29422 |
is to <quote>zero out</quote> a file of a designated |
| 29423 |
size for a special purpose, such as mounting a filesystem |
| 29424 |
on a <link linkend="loopbackref">loopback device</link> |
| 29425 |
(see <xref linkend="createfs">) or <quote>securely</quote> |
| 29426 |
deleting a file (see <xref linkend="blotout">).</para> |
| 29427 |
|
| 29428 |
<example id="ramdisk"> |
| 29429 |
<title>Creating a ramdisk</title> |
| 29430 |
<programlisting>&ramdisk;</programlisting> |
| 29431 |
</example> |
| 29432 |
|
| 29433 |
<para>In addition to all the above, |
| 29434 |
<filename>/dev/zero</filename> is needed by ELF |
| 29435 |
(<firstterm>Executable and Linking Format</firstterm>) |
| 29436 |
UNIX/Linux binaries.</para> |
| 29437 |
|
| 29438 |
</listitem> |
| 29439 |
</varlistentry> |
| 29440 |
|
| 29441 |
</variablelist> |
| 29442 |
|
| 29443 |
</chapter> <!-- Zeros and Nulls --> |
| 29444 |
|
| 29445 |
|
| 29446 |
|
| 29447 |
<chapter id="debugging"> |
| 29448 |
<title>Debugging</title> |
| 29449 |
|
| 29450 |
<epigraph> |
| 29451 |
<para>Debugging is twice as hard as writing the code in the first |
| 29452 |
place. Therefore, if you write the code as cleverly as possible, |
| 29453 |
you are, by definition, not smart enough to debug it.</para> |
| 29454 |
<para>--Brian Kernighan</para> |
| 29455 |
</epigraph> |
| 29456 |
|
| 29457 |
<para>The Bash shell contains no built-in debugger, and only bare-bones |
| 29458 |
debugging-specific commands and constructs. Syntax errors or |
| 29459 |
outright typos in the script generate cryptic error messages that |
| 29460 |
are often of no help in debugging a non-functional script.</para> |
| 29461 |
|
| 29462 |
<example id="ex74"> |
| 29463 |
<title>A buggy script</title> |
| 29464 |
<programlisting>&ex74;</programlisting> |
| 29465 |
</example> |
| 29466 |
|
| 29467 |
<para>Output from script: |
| 29468 |
<screen><computeroutput>./ex74.sh: [37: command not found</computeroutput></screen> |
| 29469 |
What's wrong with the above script? Hint: after the |
| 29470 |
<firstterm>if</firstterm>.</para> |
| 29471 |
|
| 29472 |
<example id="missingkeyword"> |
| 29473 |
<title>Missing <link linkend="keywordref">keyword</link></title> |
| 29474 |
<programlisting>&missingkeyword;</programlisting> |
| 29475 |
</example> |
| 29476 |
|
| 29477 |
<para>Output from script: |
| 29478 |
<screen> |
| 29479 |
<computeroutput>missing-keyword.sh: line 10: syntax error: unexpected end of file</computeroutput> |
| 29480 |
</screen> |
| 29481 |
Note that the error message does <emphasis>not</emphasis> necessarily |
| 29482 |
reference the line in which the error occurs, but the line where the |
| 29483 |
Bash interpreter finally becomes aware of the error. |
| 29484 |
</para> |
| 29485 |
|
| 29486 |
<para>Error messages may disregard comment lines in a script when |
| 29487 |
reporting the line number of a syntax error.</para> |
| 29488 |
|
| 29489 |
<para>What if the script executes, but does not work as expected? This is the |
| 29490 |
all too familiar logic error.</para> |
| 29491 |
|
| 29492 |
<example id="ex75"> |
| 29493 |
<title><firstterm>test24</firstterm>: another buggy script</title> |
| 29494 |
<programlisting>&ex75;</programlisting> |
| 29495 |
</example> |
| 29496 |
|
| 29497 |
<para>Try to find out what's wrong with <xref linkend="ex75"> |
| 29498 |
by uncommenting the <userinput>echo "$badname"</userinput> line. Echo |
| 29499 |
statements are useful for seeing whether what you expect is |
| 29500 |
actually what you get.</para> |
| 29501 |
|
| 29502 |
<para>In this particular case, <userinput>rm "$badname"</userinput> |
| 29503 |
will not give the desired results because |
| 29504 |
<varname>$badname</varname> should not be quoted. Placing it |
| 29505 |
in quotes ensures that <command>rm</command> has only one |
| 29506 |
argument (it will match only one filename). A partial fix |
| 29507 |
is to remove to quotes from <varname>$badname</varname> and |
| 29508 |
to reset <varname>$IFS</varname> to contain only a newline, |
| 29509 |
<userinput>IFS=$'\n'</userinput>. However, there are simpler |
| 29510 |
ways of going about it. |
| 29511 |
<programlisting># Correct methods of deleting filenames containing spaces. |
| 29512 |
rm *\ * |
| 29513 |
rm *" "* |
| 29514 |
rm *' '* |
| 29515 |
# Thank you. S.C.</programlisting> |
| 29516 |
|
| 29517 |
</para> |
| 29518 |
|
| 29519 |
<para>Summarizing the symptoms of a buggy script, |
| 29520 |
<orderedlist> |
| 29521 |
<listitem> |
| 29522 |
<para>It bombs with a <quote><errorname>syntax error</errorname></quote> message, or</para> |
| 29523 |
</listitem> |
| 29524 |
<listitem> |
| 29525 |
<para>It runs, but does not work as expected |
| 29526 |
(<errorname>logic error</errorname>).</para> |
| 29527 |
</listitem> |
| 29528 |
<listitem> |
| 29529 |
<para>It runs, works as expected, but has nasty side effects |
| 29530 |
(<errorname>logic bomb</errorname>).</para> |
| 29531 |
</listitem> |
| 29532 |
</orderedlist> |
| 29533 |
</para> |
| 29534 |
|
| 29535 |
<para><anchor id="debugtools"></para> |
| 29536 |
<para>Tools for debugging non-working scripts include |
| 29537 |
<orderedlist> |
| 29538 |
<listitem> |
| 29539 |
|
| 29540 |
<para>Inserting <link linkend="echoref">echo</link> |
| 29541 |
statements at critical points in the script to trace the |
| 29542 |
variables, and otherwise give a snapshot of what is going |
| 29543 |
on.</para> |
| 29544 |
|
| 29545 |
<tip> |
| 29546 |
<para>Even better is an <command>echo</command> that echoes |
| 29547 |
only when <firstterm>debug</firstterm> is on.</para> |
| 29548 |
|
| 29549 |
<para><programlisting>### debecho (debug-echo), by Stefano Falsetto ### |
| 29550 |
### Will echo passed parameters only if DEBUG is set to a value. ### |
| 29551 |
debecho () { |
| 29552 |
if [ ! -z "$DEBUG" ]; then |
| 29553 |
echo "$1" >&2 |
| 29554 |
# ^^^ to stderr |
| 29555 |
fi |
| 29556 |
} |
| 29557 |
|
| 29558 |
DEBUG=on |
| 29559 |
Whatever=whatnot |
| 29560 |
debecho $Whatever # whatnot |
| 29561 |
|
| 29562 |
DEBUG= |
| 29563 |
Whatever=notwhat |
| 29564 |
debecho $Whatever # (Will not echo.)</programlisting></para> |
| 29565 |
|
| 29566 |
</tip> |
| 29567 |
|
| 29568 |
</listitem> |
| 29569 |
<listitem> |
| 29570 |
<para>Using the <link linkend="teeref">tee</link> filter |
| 29571 |
to check processes or data flows at critical points.</para> |
| 29572 |
</listitem> |
| 29573 |
<listitem> |
| 29574 |
<para>Setting option flags <option>-n -v -x</option></para> |
| 29575 |
|
| 29576 |
<para><userinput>sh -n scriptname</userinput> checks for |
| 29577 |
syntax errors without actually running the script. This is |
| 29578 |
the equivalent of inserting <userinput>set -n</userinput> or |
| 29579 |
<userinput>set -o noexec</userinput> into the script. Note |
| 29580 |
that certain types of syntax errors can slip past this |
| 29581 |
check.</para> |
| 29582 |
|
| 29583 |
<para><userinput>sh -v scriptname</userinput> echoes each |
| 29584 |
command before executing it. This is the equivalent of |
| 29585 |
inserting <userinput>set -v</userinput> or <userinput>set |
| 29586 |
-o verbose</userinput> in the script.</para> |
| 29587 |
|
| 29588 |
<para>The <option>-n</option> and <option>-v</option> |
| 29589 |
flags work well together. <userinput>sh -nv |
| 29590 |
scriptname</userinput> gives a verbose syntax check.</para> |
| 29591 |
|
| 29592 |
<para><userinput>sh -x scriptname</userinput> echoes the result each |
| 29593 |
command, but in an abbreviated manner. This is the equivalent of |
| 29594 |
inserting <userinput>set -x</userinput> or |
| 29595 |
<userinput>set -o xtrace</userinput> in the script.</para> |
| 29596 |
|
| 29597 |
<para><anchor id="undvarerr"></para> |
| 29598 |
<para>Inserting <userinput>set -u</userinput> or |
| 29599 |
<userinput>set -o nounset</userinput> in the script runs it, but |
| 29600 |
gives an <errorname>unbound variable</errorname> error message |
| 29601 |
and aborts the script. |
| 29602 |
<programlisting>set -u # Or set -o nounset |
| 29603 |
|
| 29604 |
# Setting a variable to null will not trigger the error/abort. |
| 29605 |
# unset_var= |
| 29606 |
|
| 29607 |
echo $unset_var # Unset (and undeclared) variable. |
| 29608 |
|
| 29609 |
echo "Should not echo!" |
| 29610 |
|
| 29611 |
# sh t2.sh |
| 29612 |
# t2.sh: line 6: unset_var: unbound variable</programlisting></para> |
| 29613 |
</listitem> |
| 29614 |
|
| 29615 |
<listitem> |
| 29616 |
|
| 29617 |
<para>Using an <quote>assert</quote> function to test a |
| 29618 |
variable or condition at critical points in a script. (This is |
| 29619 |
an idea borrowed from C.)</para> |
| 29620 |
|
| 29621 |
<example id="assert"> |
| 29622 |
<title>Testing a condition with an |
| 29623 |
<firstterm>assert</firstterm></title> |
| 29624 |
<programlisting>&assert;</programlisting> |
| 29625 |
</example> |
| 29626 |
|
| 29627 |
</listitem> |
| 29628 |
|
| 29629 |
<listitem> |
| 29630 |
|
| 29631 |
<para>Using the <link linkend="linenoref">$LINENO</link> |
| 29632 |
variable and the <link linkend="callerref">caller</link> |
| 29633 |
builtin.</para> |
| 29634 |
|
| 29635 |
</listitem> |
| 29636 |
|
| 29637 |
|
| 29638 |
<listitem> |
| 29639 |
|
| 29640 |
<para><anchor id="debugtrap">Trapping at exit.</para> |
| 29641 |
|
| 29642 |
<para>The <link linkend="exitref">exit</link> command in a script |
| 29643 |
triggers a signal <returnvalue>0</returnvalue>, terminating |
| 29644 |
the process, that is, the script itself. |
| 29645 |
|
| 29646 |
<footnote><para>By convention, <replaceable>signal |
| 29647 |
0</replaceable> is assigned to <link |
| 29648 |
linkend="exitcommandref">exit</link>. </para></footnote> |
| 29649 |
|
| 29650 |
It is often useful to trap the |
| 29651 |
<firstterm>exit</firstterm>, forcing a <quote>printout</quote> |
| 29652 |
of variables, for example. The <firstterm>trap</firstterm> |
| 29653 |
must be the first command in the script.</para> |
| 29654 |
|
| 29655 |
</listitem> |
| 29656 |
</orderedlist> |
| 29657 |
</para> |
| 29658 |
|
| 29659 |
<variablelist id="trapref"> |
| 29660 |
<title><anchor id="trapref1">Trapping signals</title> |
| 29661 |
|
| 29662 |
<varlistentry> |
| 29663 |
<term><command>trap</command></term> |
| 29664 |
<listitem> |
| 29665 |
<para>Specifies an action on receipt of a |
| 29666 |
signal; also useful for debugging.</para> |
| 29667 |
|
| 29668 |
<para><anchor id="signald"></para> |
| 29669 |
<sidebar> |
| 29670 |
<para>A <firstterm>signal</firstterm> is a message |
| 29671 |
sent to a process, either by the kernel or another |
| 29672 |
process, telling it to take some specified action |
| 29673 |
(usually to terminate). For example, hitting a |
| 29674 |
<link linkend="ctlcref">Control-C</link> |
| 29675 |
sends a user interrupt, an INT signal, to a running |
| 29676 |
program.</para></sidebar> |
| 29677 |
|
| 29678 |
<para><emphasis>A simple instance:</emphasis> |
| 29679 |
<programlisting>trap '' 2 |
| 29680 |
# Ignore interrupt 2 (Control-C), with no action specified. |
| 29681 |
|
| 29682 |
trap 'echo "Control-C disabled."' 2 |
| 29683 |
# Message when Control-C pressed.</programlisting> |
| 29684 |
</para> |
| 29685 |
|
| 29686 |
</listitem> |
| 29687 |
</varlistentry> |
| 29688 |
</variablelist> |
| 29689 |
|
| 29690 |
<example id="ex76"> |
| 29691 |
<title>Trapping at exit</title> |
| 29692 |
<programlisting>&ex76;</programlisting> |
| 29693 |
</example> |
| 29694 |
|
| 29695 |
<example id="online"> |
| 29696 |
<title>Cleaning up after <keycap>Control-C</keycap></title> |
| 29697 |
<programlisting>&online;</programlisting> |
| 29698 |
</example> |
| 29699 |
|
| 29700 |
<example id="progressbar2"> |
| 29701 |
<title>A Simple Implementation of a Progress Bar</title> |
| 29702 |
<programlisting>&progressbar2;</programlisting> |
| 29703 |
</example> |
| 29704 |
|
| 29705 |
<note> |
| 29706 |
<para>The <option>DEBUG</option> argument to |
| 29707 |
<command>trap</command> causes a specified action to execute |
| 29708 |
after every command in a script. This permits tracing variables, |
| 29709 |
for example. |
| 29710 |
|
| 29711 |
<example id="vartrace"> |
| 29712 |
<title>Tracing a variable</title> |
| 29713 |
<programlisting>&vartrace;</programlisting> |
| 29714 |
</example> |
| 29715 |
|
| 29716 |
</para> |
| 29717 |
</note> |
| 29718 |
|
| 29719 |
|
| 29720 |
<para>Of course, the <command>trap</command> command has other uses |
| 29721 |
aside from debugging, such as disabling certain keystrokes within a |
| 29722 |
script (see <xref linkend="stopwatch">).</para> |
| 29723 |
|
| 29724 |
<example id="multipleproc"> |
| 29725 |
<title>Running multiple processes (on an SMP box)</title> |
| 29726 |
<programlisting>&multipleproc;</programlisting> |
| 29727 |
</example> |
| 29728 |
|
| 29729 |
|
| 29730 |
<note><para><userinput>trap '' SIGNAL</userinput> (two adjacent |
| 29731 |
apostrophes) disables SIGNAL for the remainder of the |
| 29732 |
script. <userinput>trap SIGNAL</userinput> restores |
| 29733 |
the functioning of SIGNAL once more. This is useful to |
| 29734 |
protect a critical portion of a script from an undesirable |
| 29735 |
interrupt.</para></note> |
| 29736 |
|
| 29737 |
<para><programlisting> |
| 29738 |
trap '' 2 # Signal 2 is Control-C, now disabled. |
| 29739 |
command |
| 29740 |
command |
| 29741 |
command |
| 29742 |
trap 2 # Reenables Control-C |
| 29743 |
</programlisting></para> |
| 29744 |
|
| 29745 |
<sidebar> |
| 29746 |
<para><link linkend="bash3ref">Version 3</link> of Bash adds the |
| 29747 |
following <link linkend="internalvariables1">internal |
| 29748 |
variables</link> for use by the debugger. |
| 29749 |
|
| 29750 |
<orderedlist> |
| 29751 |
|
| 29752 |
<listitem> |
| 29753 |
<para><varname>$BASH_ARGC</varname></para> |
| 29754 |
<para>Number of command-line arguments passed to script, |
| 29755 |
similar to <link |
| 29756 |
linkend="clacountref"><varname>$#</varname></link>.</para> |
| 29757 |
</listitem> |
| 29758 |
|
| 29759 |
<listitem> |
| 29760 |
<para><varname>$BASH_ARGV</varname></para> |
| 29761 |
<para>Final command-line parameter passed to script, equivalent |
| 29762 |
<link |
| 29763 |
linkend="lastargref"><varname>${!#}</varname></link>.</para> |
| 29764 |
</listitem> |
| 29765 |
|
| 29766 |
<listitem> |
| 29767 |
<para><varname>$BASH_COMMAND</varname></para> |
| 29768 |
<para>Command currently executing.</para> |
| 29769 |
</listitem> |
| 29770 |
|
| 29771 |
<listitem> |
| 29772 |
<para><varname>$BASH_EXECUTION_STRING</varname></para> |
| 29773 |
<para>The <firstterm>option string</firstterm> following the |
| 29774 |
<option>-c</option> <link linkend="clopts">option</link> |
| 29775 |
to Bash.</para> |
| 29776 |
</listitem> |
| 29777 |
|
| 29778 |
<listitem> |
| 29779 |
<para><varname>$BASH_LINENO</varname></para> |
| 29780 |
<para>In a <link linkend="functionref">function</link>, |
| 29781 |
indicates the line number of the function call.</para> |
| 29782 |
</listitem> |
| 29783 |
|
| 29784 |
<listitem> |
| 29785 |
<para><varname>$BASH_REMATCH</varname></para> |
| 29786 |
<para>Array variable associated with <command>=~</command> |
| 29787 |
<link linkend="regexmatchref">conditional regex |
| 29788 |
matching</link>.</para> |
| 29789 |
</listitem> |
| 29790 |
|
| 29791 |
<listitem> |
| 29792 |
<para><anchor id="bashsourceref"></para> |
| 29793 |
<para><varname>$BASH_SOURCE</varname></para> |
| 29794 |
<para>This is the name of the script, usually the same as |
| 29795 |
<link linkend="arg0">$0</link>.</para> |
| 29796 |
</listitem> |
| 29797 |
|
| 29798 |
<listitem> |
| 29799 |
<para> |
| 29800 |
<link |
| 29801 |
linkend="bashsubshellref"><varname>$BASH_SUBSHELL</varname></link></para> |
| 29802 |
</listitem> |
| 29803 |
|
| 29804 |
</orderedlist></para> |
| 29805 |
</sidebar> |
| 29806 |
|
| 29807 |
</chapter> <!-- Debugging --> |
| 29808 |
|
| 29809 |
|
| 29810 |
|
| 29811 |
<chapter id="options"> |
| 29812 |
<title>Options</title> |
| 29813 |
|
| 29814 |
<para><anchor id="optionsref"></para> |
| 29815 |
|
| 29816 |
<para>Options are settings that change shell and/or script |
| 29817 |
behavior.</para> |
| 29818 |
|
| 29819 |
<para>The <link linkend="setref">set</link> command |
| 29820 |
enables options within a script. At the point in the script |
| 29821 |
where you want the options to take effect, use <command>set |
| 29822 |
-o option-name</command> or, in short form, <command>set |
| 29823 |
-option-abbrev</command>. These two forms are equivalent.</para> |
| 29824 |
|
| 29825 |
<para><programlisting> |
| 29826 |
#!/bin/bash |
| 29827 |
|
| 29828 |
set -o verbose |
| 29829 |
# Echoes all commands before executing. |
| 29830 |
</programlisting></para> |
| 29831 |
|
| 29832 |
<para><programlisting> |
| 29833 |
#!/bin/bash |
| 29834 |
|
| 29835 |
set -v |
| 29836 |
# Exact same effect as above. |
| 29837 |
</programlisting></para> |
| 29838 |
|
| 29839 |
<note><para>To <firstterm>disable</firstterm> an option within a script, |
| 29840 |
use <command>set +o option-name</command> or <command>set |
| 29841 |
+option-abbrev</command>.</para></note> |
| 29842 |
|
| 29843 |
<para><programlisting> |
| 29844 |
#!/bin/bash |
| 29845 |
|
| 29846 |
set -o verbose |
| 29847 |
# Command echoing on. |
| 29848 |
command |
| 29849 |
... |
| 29850 |
command |
| 29851 |
|
| 29852 |
set +o verbose |
| 29853 |
# Command echoing off. |
| 29854 |
command |
| 29855 |
# Not echoed. |
| 29856 |
|
| 29857 |
|
| 29858 |
set -v |
| 29859 |
# Command echoing on. |
| 29860 |
command |
| 29861 |
... |
| 29862 |
command |
| 29863 |
|
| 29864 |
set +v |
| 29865 |
# Command echoing off. |
| 29866 |
command |
| 29867 |
|
| 29868 |
exit 0 |
| 29869 |
</programlisting></para> |
| 29870 |
|
| 29871 |
|
| 29872 |
<para>An alternate method of enabling options in a script is |
| 29873 |
to specify them immediately following the |
| 29874 |
<replaceable>#!</replaceable> script header.</para> |
| 29875 |
|
| 29876 |
<para><programlisting> |
| 29877 |
#!/bin/bash -x |
| 29878 |
# |
| 29879 |
# Body of script follows. |
| 29880 |
</programlisting></para> |
| 29881 |
|
| 29882 |
|
| 29883 |
<para><anchor id="invocationoptionsref"></para> |
| 29884 |
<para>It is also possible to enable script options from the command |
| 29885 |
line. Some options that will not work with |
| 29886 |
<command>set</command> are available this way. Among these |
| 29887 |
are <replaceable>-i</replaceable>, force script to run |
| 29888 |
interactive.</para> |
| 29889 |
|
| 29890 |
<para><userinput>bash -v script-name</userinput></para> |
| 29891 |
|
| 29892 |
<para><userinput>bash -o verbose script-name</userinput></para> |
| 29893 |
|
| 29894 |
|
| 29895 |
<para>The following is a listing of some useful options. They may be |
| 29896 |
specified in either abbreviated form (preceded by a single dash) |
| 29897 |
or by complete name (preceded by a <emphasis>double</emphasis> |
| 29898 |
dash or by <option>-o</option>).</para> |
| 29899 |
|
| 29900 |
<para><anchor id="optionstable"></para> |
| 29901 |
|
| 29902 |
<table> |
| 29903 |
<title>Bash options</title> |
| 29904 |
<tgroup cols="3"> |
| 29905 |
<thead> |
| 29906 |
<row> |
| 29907 |
<entry>Abbreviation</entry> |
| 29908 |
<entry>Name</entry> |
| 29909 |
<entry>Effect</entry> |
| 29910 |
</row> |
| 29911 |
</thead> |
| 29912 |
<tbody> |
| 29913 |
<row> |
| 29914 |
<entry><option>-B</option></entry> |
| 29915 |
<entry>brace expansion</entry> |
| 29916 |
<entry><emphasis>Enable</emphasis> |
| 29917 |
<link linkend="braceexpref">brace |
| 29918 |
expansion</link> (default setting = |
| 29919 |
<emphasis>on</emphasis>)</entry> |
| 29920 |
</row> |
| 29921 |
<row> |
| 29922 |
<entry><option>+B</option></entry> |
| 29923 |
<entry>brace expansion</entry> |
| 29924 |
<entry><emphasis>Disable</emphasis> |
| 29925 |
brace expansion</entry> |
| 29926 |
</row> |
| 29927 |
<row> |
| 29928 |
<entry><anchor id="noclobberref"><option>-C</option></entry> |
| 29929 |
<entry>noclobber</entry> |
| 29930 |
<entry>Prevent overwriting of files by redirection (may be |
| 29931 |
overridden by <token>>|</token>)</entry> |
| 29932 |
</row> |
| 29933 |
<row> |
| 29934 |
<entry><option>-D</option></entry> |
| 29935 |
<entry>(none)</entry> |
| 29936 |
<entry>List double-quoted strings prefixed by <token>$</token>, |
| 29937 |
but do not execute commands in script</entry> |
| 29938 |
</row> |
| 29939 |
<row> |
| 29940 |
<entry><option>-a</option></entry> |
| 29941 |
<entry>allexport</entry> |
| 29942 |
<entry>Export all defined variables</entry> |
| 29943 |
</row> |
| 29944 |
<row> |
| 29945 |
<entry><option>-b</option></entry> |
| 29946 |
<entry>notify</entry> |
| 29947 |
<entry>Notify when jobs running in background terminate (not of |
| 29948 |
much use in a script)</entry> |
| 29949 |
</row> |
| 29950 |
<row> |
| 29951 |
<entry><option>-c ...</option></entry> |
| 29952 |
<entry>(none)</entry> |
| 29953 |
<entry>Read commands from <command>...</command></entry> |
| 29954 |
</row> |
| 29955 |
<row> |
| 29956 |
<entry><option>checkjobs</option></entry> |
| 29957 |
<entry></entry> |
| 29958 |
<entry>Informs user of any open <link |
| 29959 |
linkend="jobsref">jobs</link> upon shell exit. |
| 29960 |
Introduced in <link linkend="bash4ref">version 4</link> |
| 29961 |
of Bash, and still <quote>experimental.</quote> |
| 29962 |
<emphasis>Usage:</emphasis> shopt -s checkjobs |
| 29963 |
(<emphasis>Caution:</emphasis> may hang!)</entry> |
| 29964 |
</row> |
| 29965 |
<row> |
| 29966 |
<entry><option>-e</option></entry> |
| 29967 |
<entry>errexit</entry> |
| 29968 |
<entry>Abort script at first error, when a command |
| 29969 |
exits with non-zero status (except in <link |
| 29970 |
linkend="untilloopref">until</link> or <link |
| 29971 |
linkend="whileloopref">while loops</link>, <link |
| 29972 |
linkend="testconstructs1">if-tests</link>, <link |
| 29973 |
linkend="lcons1">list constructs</link>)</entry> |
| 29974 |
</row> |
| 29975 |
<row> |
| 29976 |
<entry><option>-f</option></entry> |
| 29977 |
<entry>noglob</entry> |
| 29978 |
<entry>Filename expansion (globbing) disabled</entry> |
| 29979 |
</row> |
| 29980 |
<row> |
| 29981 |
<entry><option>globstar</option></entry> |
| 29982 |
<entry><link |
| 29983 |
linkend="globstarref"><firstterm>globbing</firstterm> |
| 29984 |
star-match</link></entry> |
| 29985 |
<entry>Enables the <token>**</token> <link |
| 29986 |
linkend="globbingref">globbing</link> operator |
| 29987 |
(<link linkend="bash4ref">version 4+</link> of Bash). |
| 29988 |
<emphasis>Usage:</emphasis> shopt -s globstar</entry> |
| 29989 |
</row> |
| 29990 |
<row> |
| 29991 |
<entry><option>-i</option></entry> |
| 29992 |
<entry>interactive</entry> |
| 29993 |
<entry>Script runs in <firstterm>interactive</firstterm> mode</entry> |
| 29994 |
</row> |
| 29995 |
<row> |
| 29996 |
<entry><option>-n</option></entry> |
| 29997 |
<entry>noexec</entry> |
| 29998 |
<entry>Read commands in script, but do not execute them (syntax check)</entry> |
| 29999 |
</row> |
| 30000 |
<row> |
| 30001 |
<entry><option>-o Option-Name</option></entry> |
| 30002 |
<entry>(none)</entry> |
| 30003 |
<entry>Invoke the <firstterm>Option-Name</firstterm> |
| 30004 |
option</entry> |
| 30005 |
</row> |
| 30006 |
<row> |
| 30007 |
<entry><option>-o posix</option></entry> |
| 30008 |
<entry>POSIX</entry> |
| 30009 |
<entry>Change the behavior of Bash, or invoked script, to |
| 30010 |
conform to <link linkend="posix2ref">POSIX</link> |
| 30011 |
standard.</entry> |
| 30012 |
</row> |
| 30013 |
<row> |
| 30014 |
<entry><option>-o pipefail</option></entry> |
| 30015 |
<entry>pipe failure</entry> |
| 30016 |
<entry>Causes a pipeline to return the <link |
| 30017 |
linkend="exitstatusref">exit status</link> of |
| 30018 |
the last command in the pipe that returned a non-zero |
| 30019 |
return value.</entry> |
| 30020 |
</row> |
| 30021 |
<row> |
| 30022 |
<entry><option>-p</option></entry> |
| 30023 |
<entry>privileged</entry> |
| 30024 |
<entry>Script runs as <quote>suid</quote> (caution!)</entry> |
| 30025 |
</row> |
| 30026 |
<row> |
| 30027 |
<entry><option>-r</option></entry> |
| 30028 |
<entry>restricted</entry> |
| 30029 |
<entry>Script runs in <firstterm>restricted</firstterm> |
| 30030 |
mode (see <xref linkend="restricted-sh">).</entry> |
| 30031 |
</row> |
| 30032 |
<row> |
| 30033 |
<entry><option>-s</option></entry> |
| 30034 |
<entry>stdin</entry> |
| 30035 |
<entry>Read commands from <filename>stdin</filename></entry> |
| 30036 |
</row> |
| 30037 |
<row> |
| 30038 |
<entry><option>-t</option></entry> |
| 30039 |
<entry>(none)</entry> |
| 30040 |
<entry>Exit after first command</entry> |
| 30041 |
</row> |
| 30042 |
<row> |
| 30043 |
<entry><option>-u</option></entry> |
| 30044 |
<entry>nounset</entry> |
| 30045 |
<entry>Attempt to use undefined variable |
| 30046 |
outputs error message, and forces an exit</entry> |
| 30047 |
</row> |
| 30048 |
<row> |
| 30049 |
<entry><option>-v</option></entry> |
| 30050 |
<entry>verbose</entry> |
| 30051 |
<entry>Print each command to <filename>stdout</filename> before executing it</entry> |
| 30052 |
</row> |
| 30053 |
<row> |
| 30054 |
<entry><option>-x</option></entry> |
| 30055 |
<entry>xtrace</entry> |
| 30056 |
<entry>Similar to <option>-v</option>, but expands commands</entry> |
| 30057 |
</row> |
| 30058 |
<row> |
| 30059 |
<entry><option>-</option></entry> |
| 30060 |
<entry>(none)</entry> |
| 30061 |
<entry>End of options flag. All other arguments |
| 30062 |
are <link linkend="posparamref">positional |
| 30063 |
parameters</link>.</entry> |
| 30064 |
</row> |
| 30065 |
<row> |
| 30066 |
<entry><option>--</option></entry> |
| 30067 |
<entry>(none)</entry> |
| 30068 |
<entry>Unset positional parameters. |
| 30069 |
If arguments given (<parameter>-- arg1 arg2</parameter>), |
| 30070 |
positional parameters set to arguments.</entry> |
| 30071 |
</row> |
| 30072 |
</tbody> |
| 30073 |
</tgroup> |
| 30074 |
</table> |
| 30075 |
</chapter> <!-- Options --> |
| 30076 |
|
| 30077 |
|
| 30078 |
|
| 30079 |
<chapter id="gotchas"> |
| 30080 |
<title>Gotchas</title> |
| 30081 |
|
| 30082 |
<epigraph> |
| 30083 |
<para>Turandot: <foreignphrase>Gli enigmi sono tre, la morte |
| 30084 |
una!</foreignphrase></para> |
| 30085 |
<para>Caleph: <foreignphrase>No, no! Gli enigmi sono tre, una la |
| 30086 |
vita!</foreignphrase></para> |
| 30087 |
<para>--Puccini</para> |
| 30088 |
</epigraph> |
| 30089 |
|
| 30090 |
<para><anchor id="bash3gotcha"></para> |
| 30091 |
|
| 30092 |
<para>Here are some (non-recommended!) scripting practices that |
| 30093 |
will bring excitement into an otherwise dull life.</para> |
| 30094 |
|
| 30095 |
|
| 30096 |
<itemizedlist> |
| 30097 |
|
| 30098 |
<listitem> |
| 30099 |
<para><anchor id="inappvn"></para> |
| 30100 |
|
| 30101 |
<para>Assigning reserved words or characters to variable names.</para> |
| 30102 |
|
| 30103 |
<para> |
| 30104 |
<programlisting>case=value0 # Causes problems. |
| 30105 |
23skidoo=value1 # Also problems. |
| 30106 |
# Variable names starting with a digit are reserved by the shell. |
| 30107 |
# Try _23skidoo=value1. Starting variables with an underscore is okay. |
| 30108 |
|
| 30109 |
# However . . . using just an underscore will not work. |
| 30110 |
_=25 |
| 30111 |
echo $_ # $_ is a special variable set to last arg of last command. |
| 30112 |
# But . . . _ is a valid function name! |
| 30113 |
|
| 30114 |
xyz((!*=value2 # Causes severe problems. |
| 30115 |
# As of version 3 of Bash, periods are not allowed within variable names.</programlisting> |
| 30116 |
</para> |
| 30117 |
</listitem> |
| 30118 |
|
| 30119 |
<listitem> |
| 30120 |
<para>Using a hyphen or other reserved characters in a variable name (or |
| 30121 |
function name).</para> |
| 30122 |
<para> |
| 30123 |
<programlisting>var-1=23 |
| 30124 |
# Use 'var_1' instead. |
| 30125 |
|
| 30126 |
function-whatever () # Error |
| 30127 |
# Use 'function_whatever ()' instead. |
| 30128 |
|
| 30129 |
|
| 30130 |
# As of version 3 of Bash, periods are not allowed within function names. |
| 30131 |
function.whatever () # Error |
| 30132 |
# Use 'functionWhatever ()' instead.</programlisting> |
| 30133 |
</para> |
| 30134 |
</listitem> |
| 30135 |
|
| 30136 |
<listitem> |
| 30137 |
<para>Using the same name for a variable and a function. This can make a |
| 30138 |
script difficult to understand.</para> |
| 30139 |
<para> |
| 30140 |
<programlisting>do_something () |
| 30141 |
{ |
| 30142 |
echo "This function does something with \"$1\"." |
| 30143 |
} |
| 30144 |
|
| 30145 |
do_something=do_something |
| 30146 |
|
| 30147 |
do_something do_something |
| 30148 |
|
| 30149 |
# All this is legal, but highly confusing.</programlisting> |
| 30150 |
</para> |
| 30151 |
</listitem> |
| 30152 |
|
| 30153 |
|
| 30154 |
|
| 30155 |
<listitem> |
| 30156 |
<para><anchor id="wsbad">Using <link |
| 30157 |
linkend="whitespaceref">whitespace</link> inappropriately. |
| 30158 |
In contrast to other programming languages, Bash can be quite |
| 30159 |
finicky about whitespace.</para> |
| 30160 |
|
| 30161 |
<para> |
| 30162 |
<programlisting>var1 = 23 # 'var1=23' is correct. |
| 30163 |
# On line above, Bash attempts to execute command "var1" |
| 30164 |
# with the arguments "=" and "23". |
| 30165 |
|
| 30166 |
let c = $a - $b # Instead: let c=$a-$b or let "c = $a - $b" |
| 30167 |
|
| 30168 |
if [ $a -le 5] # if [ $a -le 5 ] is correct. |
| 30169 |
# ^^ if [ "$a" -le 5 ] is even better. |
| 30170 |
# [[ $a -le 5 ]] also works.</programlisting> |
| 30171 |
</para> |
| 30172 |
</listitem> |
| 30173 |
|
| 30174 |
|
| 30175 |
<listitem> |
| 30176 |
<para><anchor id="omitsemicolon"></para> |
| 30177 |
<para>Not terminating with a <link |
| 30178 |
linkend="semicolonref">semicolon</link> the final command |
| 30179 |
in a <link linkend="codeblockref">code block within curly |
| 30180 |
brackets</link>.</para> |
| 30181 |
<para> |
| 30182 |
<programlisting>{ ls -l; df; echo "Done." } |
| 30183 |
# bash: syntax error: unexpected end of file |
| 30184 |
|
| 30185 |
{ ls -l; df; echo "Done."; } |
| 30186 |
# ^ ### Final command needs semicolon.</programlisting> |
| 30187 |
</para> |
| 30188 |
</listitem> |
| 30189 |
|
| 30190 |
|
| 30191 |
<listitem> |
| 30192 |
<para><anchor id="uninitvar"></para> |
| 30193 |
<para> |
| 30194 |
Assuming uninitialized variables (variables before a value is |
| 30195 |
assigned to them) are <quote>zeroed out</quote>. An |
| 30196 |
uninitialized variable has a value of <firstterm>null</firstterm>, |
| 30197 |
<emphasis>not</emphasis> zero.</para> |
| 30198 |
|
| 30199 |
<para><anchor id="bash4.2-uninitialized"></para> |
| 30200 |
<para> |
| 30201 |
<programlisting>#!/bin/bash |
| 30202 |
|
| 30203 |
echo "uninitialized_var = $uninitialized_var" |
| 30204 |
# uninitialized_var = |
| 30205 |
|
| 30206 |
# However . . . |
| 30207 |
# if $BASH_VERSION ≥ 4.2; then |
| 30208 |
|
| 30209 |
if [[ ! -v uninitialized_var ]] |
| 30210 |
then |
| 30211 |
uninitialized_var=0 # Initialize it to zero! |
| 30212 |
fi |
| 30213 |
|
| 30214 |
|
| 30215 |
</programlisting> |
| 30216 |
|
| 30217 |
</para> |
| 30218 |
</listitem> |
| 30219 |
|
| 30220 |
|
| 30221 |
<listitem> |
| 30222 |
<para><anchor id="eqdif"></para> |
| 30223 |
<para>Mixing up <firstterm>=</firstterm> and |
| 30224 |
<firstterm>-eq</firstterm> in a test. Remember, |
| 30225 |
<firstterm>=</firstterm> is for comparing literal variables |
| 30226 |
and <firstterm>-eq</firstterm> for integers.</para> |
| 30227 |
|
| 30228 |
<para> |
| 30229 |
<programlisting>if [ "$a" = 273 ] # Is $a an integer or string? |
| 30230 |
if [ "$a" -eq 273 ] # If $a is an integer. |
| 30231 |
|
| 30232 |
# Sometimes you can interchange -eq and = without adverse consequences. |
| 30233 |
# However . . . |
| 30234 |
|
| 30235 |
|
| 30236 |
a=273.0 # Not an integer. |
| 30237 |
|
| 30238 |
if [ "$a" = 273 ] |
| 30239 |
then |
| 30240 |
echo "Comparison works." |
| 30241 |
else |
| 30242 |
echo "Comparison does not work." |
| 30243 |
fi # Comparison does not work. |
| 30244 |
|
| 30245 |
# Same with a=" 273" and a="0273". |
| 30246 |
|
| 30247 |
|
| 30248 |
# Likewise, problems trying to use "-eq" with non-integer values. |
| 30249 |
|
| 30250 |
if [ "$a" -eq 273.0 ] |
| 30251 |
then |
| 30252 |
echo "a = $a" |
| 30253 |
fi # Aborts with an error message. |
| 30254 |
# test.sh: [: 273.0: integer expression expected</programlisting> |
| 30255 |
</para> |
| 30256 |
</listitem> |
| 30257 |
|
| 30258 |
|
| 30259 |
<listitem> |
| 30260 |
<para><anchor id="numstrcompne"></para> |
| 30261 |
<para>Misusing <link linkend="scomparison1">string comparison</link> |
| 30262 |
operators.</para> |
| 30263 |
|
| 30264 |
<example id="badop"> |
| 30265 |
<title>Numerical and string comparison are not equivalent</title> |
| 30266 |
<programlisting>&badop;</programlisting> |
| 30267 |
</example> |
| 30268 |
</listitem> |
| 30269 |
|
| 30270 |
<listitem> |
| 30271 |
<para><anchor id="letbad"></para> |
| 30272 |
<para>Attempting to use <link linkend="letref">let</link> |
| 30273 |
to set string variables.</para> |
| 30274 |
<para><programlisting>let "a = hello, you" |
| 30275 |
echo "$a" # 0</programlisting></para> |
| 30276 |
</listitem> |
| 30277 |
|
| 30278 |
|
| 30279 |
<listitem> |
| 30280 |
<para><anchor id="failquote"></para> |
| 30281 |
<para>Sometimes variables within <quote>test</quote> brackets |
| 30282 |
([ ]) need to be quoted (double quotes). Failure to do so may |
| 30283 |
cause unexpected behavior. See <xref linkend="strtest">, <xref |
| 30284 |
linkend="redir2">, and <xref linkend="arglist">.</para> |
| 30285 |
</listitem> |
| 30286 |
|
| 30287 |
<listitem> |
| 30288 |
<para><anchor id="failnotquote"></para> |
| 30289 |
<para>Quoting a variable containing whitespace <link |
| 30290 |
linkend="wsquo">prevents splitting</link>. Sometimes |
| 30291 |
this produces <link linkend="varsplitting">unintended |
| 30292 |
consequences</link>.</para> |
| 30293 |
</listitem> |
| 30294 |
|
| 30295 |
|
| 30296 |
<listitem> |
| 30297 |
<para><anchor id="execperm"></para> |
| 30298 |
<para>Commands issued from a script may fail to execute because |
| 30299 |
the script owner lacks execute permission for them. If a user |
| 30300 |
cannot invoke a command from the command-line, then putting it |
| 30301 |
into a script will likewise fail. Try changing the attributes of |
| 30302 |
the command in question, perhaps even setting the suid bit |
| 30303 |
(as <firstterm>root</firstterm>, of course).</para> |
| 30304 |
</listitem> |
| 30305 |
|
| 30306 |
|
| 30307 |
|
| 30308 |
<listitem> |
| 30309 |
<para><anchor id="dashnredr"></para> |
| 30310 |
<para>Attempting to use <command>-</command> as a redirection |
| 30311 |
operator (which it is not) will usually result in an unpleasant |
| 30312 |
surprise.</para> |
| 30313 |
<para> |
| 30314 |
<programlisting>command1 2> - | command2 |
| 30315 |
# Trying to redirect error output of command1 into a pipe . . . |
| 30316 |
# . . . will not work. |
| 30317 |
|
| 30318 |
command1 2>& - | command2 # Also futile. |
| 30319 |
|
| 30320 |
Thanks, S.C.</programlisting></para> |
| 30321 |
</listitem> |
| 30322 |
|
| 30323 |
|
| 30324 |
|
| 30325 |
|
| 30326 |
<listitem> |
| 30327 |
<para><anchor id="lateverf"></para> |
| 30328 |
<para>Using Bash <link linkend="bash2ref">version 2+</link> |
| 30329 |
functionality may cause a bailout with error messages. Older |
| 30330 |
Linux machines may have version 1.XX of Bash as the default |
| 30331 |
installation.</para> |
| 30332 |
<para> |
| 30333 |
<programlisting>#!/bin/bash |
| 30334 |
|
| 30335 |
minimum_version=2 |
| 30336 |
# Since Chet Ramey is constantly adding features to Bash, |
| 30337 |
# you may set $minimum_version to 2.XX, 3.XX, or whatever is appropriate. |
| 30338 |
E_BAD_VERSION=80 |
| 30339 |
|
| 30340 |
if [ "$BASH_VERSION" \< "$minimum_version" ] |
| 30341 |
then |
| 30342 |
echo "This script works only with Bash, version $minimum or greater." |
| 30343 |
echo "Upgrade strongly recommended." |
| 30344 |
exit $E_BAD_VERSION |
| 30345 |
fi |
| 30346 |
|
| 30347 |
...</programlisting></para> |
| 30348 |
</listitem> |
| 30349 |
|
| 30350 |
|
| 30351 |
|
| 30352 |
<listitem> |
| 30353 |
<para>Using Bash-specific functionality in a <link |
| 30354 |
linkend="bashdef">Bourne shell</link> script |
| 30355 |
(<userinput>#!/bin/sh</userinput>) on a non-Linux machine |
| 30356 |
<link linkend="binsh">may cause unexpected behavior</link>. |
| 30357 |
A Linux system usually aliases <command>sh</command> to |
| 30358 |
<command>bash</command>, but this does not necessarily hold true |
| 30359 |
for a generic UNIX machine.</para> </listitem> |
| 30360 |
|
| 30361 |
<listitem> |
| 30362 |
<para><anchor id="undocf"></para> |
| 30363 |
<para>Using undocumented features in Bash turns out to be a |
| 30364 |
dangerous practice. In previous releases of this |
| 30365 |
book there were several scripts that depended on the |
| 30366 |
<quote>feature</quote> that, although the maximum value |
| 30367 |
of an <link linkend="exitstatusref">exit</link> or <link |
| 30368 |
linkend="returnref">return</link> value was 255, that limit |
| 30369 |
did not apply to <emphasis>negative</emphasis> integers. |
| 30370 |
Unfortunately, in version 2.05b and later, that loophole |
| 30371 |
disappeared. See <xref linkend="returntest">.</para> |
| 30372 |
</listitem> |
| 30373 |
|
| 30374 |
|
| 30375 |
<listitem> |
| 30376 |
<para><anchor id="gotchaexitvalanamalies"></para> |
| 30377 |
<para>In certain contexts, a misleading <link |
| 30378 |
linkend="exitstatusref">exit status</link> |
| 30379 |
may be returned. This may occur when <link |
| 30380 |
linkend="exitvalanomaly01">setting a local variable within a |
| 30381 |
function</link> or when <link linkend="exitvalanomaly02">assigning |
| 30382 |
an arithmetic value to a variable</link>.</para> |
| 30383 |
</listitem> |
| 30384 |
|
| 30385 |
<listitem> |
| 30386 |
<para><anchor id="arxs1">The <link linkend="arxs">exit |
| 30387 |
status of an arithmetic expression</link> is |
| 30388 |
<emphasis>not</emphasis> equivalent to an <firstterm>error |
| 30389 |
code</firstterm>.</para> |
| 30390 |
<para><programlisting>var=1 && ((--var)) && echo $var |
| 30391 |
# ^^^^^^^^^ Here the and-list terminates with exit status 1. |
| 30392 |
# $var doesn't echo! |
| 30393 |
echo $? # 1</programlisting></para> |
| 30394 |
</listitem> |
| 30395 |
|
| 30396 |
<listitem> |
| 30397 |
<para><anchor id="dosnewlines"></para> |
| 30398 |
<para> |
| 30399 |
A script with DOS-type newlines (<replaceable>\r\n</replaceable>) |
| 30400 |
will fail to execute, since <userinput>#!/bin/bash\r\n</userinput> |
| 30401 |
is <emphasis>not</emphasis> recognized, <emphasis>not</emphasis> |
| 30402 |
the same as the expected <userinput>#!/bin/bash\n</userinput>. The |
| 30403 |
fix is to convert the script to UNIX-style newlines.</para> |
| 30404 |
<para> |
| 30405 |
<programlisting>#!/bin/bash |
| 30406 |
|
| 30407 |
echo "Here" |
| 30408 |
|
| 30409 |
unix2dos $0 # Script changes itself to DOS format. |
| 30410 |
chmod 755 $0 # Change back to execute permission. |
| 30411 |
# The 'unix2dos' command removes execute permission. |
| 30412 |
|
| 30413 |
./$0 # Script tries to run itself again. |
| 30414 |
# But it won't work as a DOS file. |
| 30415 |
|
| 30416 |
echo "There" |
| 30417 |
|
| 30418 |
exit 0</programlisting> |
| 30419 |
</para> |
| 30420 |
|
| 30421 |
</listitem> |
| 30422 |
|
| 30423 |
|
| 30424 |
|
| 30425 |
<listitem> |
| 30426 |
<para><anchor id="binsh"></para> |
| 30427 |
<para>A shell script headed by <userinput>#!/bin/sh</userinput> |
| 30428 |
will not run in full Bash-compatibility mode. Some Bash-specific |
| 30429 |
functions might be disabled. Scripts that need complete |
| 30430 |
access to all the Bash-specific extensions should start with |
| 30431 |
<userinput>#!/bin/bash</userinput>.</para> |
| 30432 |
</listitem> |
| 30433 |
|
| 30434 |
<listitem> |
| 30435 |
<para><link linkend="indentedls">Putting whitespace in front of |
| 30436 |
the terminating limit string</link> of a <link |
| 30437 |
linkend="heredocref">here document</link> will cause unexpected |
| 30438 |
behavior in a script.</para> |
| 30439 |
</listitem> |
| 30440 |
|
| 30441 |
<listitem> |
| 30442 |
<para><anchor id="rvtcaution2">Putting more than one |
| 30443 |
<firstterm>echo</firstterm> statement in a function <link |
| 30444 |
linkend="rvt">whose output is captured</link>. |
| 30445 |
<programlisting>add2 () |
| 30446 |
{ |
| 30447 |
echo "Whatever ... " # Delete this line! |
| 30448 |
let "retval = $1 + $2" |
| 30449 |
echo $retval |
| 30450 |
} |
| 30451 |
|
| 30452 |
num1=12 |
| 30453 |
num2=43 |
| 30454 |
echo "Sum of $num1 and $num2 = $(add2 $num1 $num2)" |
| 30455 |
|
| 30456 |
# Sum of 12 and 43 = Whatever ... |
| 30457 |
# 55 |
| 30458 |
|
| 30459 |
# The "echoes" concatenate.</programlisting> |
| 30460 |
This <link |
| 30461 |
linkend="rvtcaution">will not work</link>.</para> |
| 30462 |
</listitem> |
| 30463 |
|
| 30464 |
|
| 30465 |
<listitem> |
| 30466 |
<para><anchor id="parchildprobref"></para> |
| 30467 |
|
| 30468 |
<para>A script may not <command>export</command> variables back |
| 30469 |
to its <link linkend="forkref">parent process</link>, the shell, |
| 30470 |
or to the environment. Just as we learned in biology, a child |
| 30471 |
process can inherit from a parent, but not vice versa.</para> |
| 30472 |
<para> |
| 30473 |
<programlisting>WHATEVER=/home/bozo |
| 30474 |
export WHATEVER |
| 30475 |
exit 0</programlisting> |
| 30476 |
<screen><prompt>bash$ </prompt><command>echo $WHATEVER</command> |
| 30477 |
<computeroutput> |
| 30478 |
|
| 30479 |
</computeroutput> |
| 30480 |
<prompt>bash$ </prompt></screen> |
| 30481 |
</para> |
| 30482 |
<para> |
| 30483 |
Sure enough, back at the command prompt, $WHATEVER remains unset. |
| 30484 |
</para> |
| 30485 |
|
| 30486 |
</listitem> |
| 30487 |
|
| 30488 |
|
| 30489 |
|
| 30490 |
<listitem> |
| 30491 |
<para><anchor id="varsubsh"></para> |
| 30492 |
<para>Setting and manipulating variables in a <link |
| 30493 |
linkend="subshellsref">subshell</link>, then attempting |
| 30494 |
to use those same variables outside the scope of the subshell will |
| 30495 |
result an unpleasant surprise.</para> |
| 30496 |
|
| 30497 |
<example id="subpit"> |
| 30498 |
<title>Subshell Pitfalls</title> |
| 30499 |
<programlisting>&subpit;</programlisting> |
| 30500 |
</example> |
| 30501 |
</listitem> |
| 30502 |
|
| 30503 |
|
| 30504 |
<listitem> |
| 30505 |
<para><anchor id="badread0"></para> |
| 30506 |
<para><link linkend="piperef">Piping</link> |
| 30507 |
<command>echo</command> output to a <link |
| 30508 |
linkend="readref">read</link> may produce unexpected |
| 30509 |
results. In this scenario, the <command>read</command> |
| 30510 |
acts as if it were running in a subshell. Instead, use |
| 30511 |
the <link linkend="setref">set</link> command (as in <xref |
| 30512 |
linkend="setpos">).</para> |
| 30513 |
|
| 30514 |
<example id="badread"> |
| 30515 |
<title>Piping the output of <firstterm>echo</firstterm> to a |
| 30516 |
<firstterm>read</firstterm></title> |
| 30517 |
<programlisting>&badread;</programlisting> |
| 30518 |
</example> |
| 30519 |
|
| 30520 |
<para><anchor id="pipeloop"></para> |
| 30521 |
<para>In fact, as Anthony Richardson points out, piping to |
| 30522 |
<emphasis>any</emphasis> loop can cause a similar problem.</para> |
| 30523 |
|
| 30524 |
<para> |
| 30525 |
<programlisting># Loop piping troubles. |
| 30526 |
# This example by Anthony Richardson, |
| 30527 |
#+ with addendum by Wilbert Berendsen. |
| 30528 |
|
| 30529 |
|
| 30530 |
foundone=false |
| 30531 |
find $HOME -type f -atime +30 -size 100k | |
| 30532 |
while true |
| 30533 |
do |
| 30534 |
read f |
| 30535 |
echo "$f is over 100KB and has not been accessed in over 30 days" |
| 30536 |
echo "Consider moving the file to archives." |
| 30537 |
foundone=true |
| 30538 |
# ------------------------------------ |
| 30539 |
echo "Subshell level = $BASH_SUBSHELL" |
| 30540 |
# Subshell level = 1 |
| 30541 |
# Yes, we're inside a subshell. |
| 30542 |
# ------------------------------------ |
| 30543 |
done |
| 30544 |
|
| 30545 |
# foundone will always be false here since it is |
| 30546 |
#+ set to true inside a subshell |
| 30547 |
if [ $foundone = false ] |
| 30548 |
then |
| 30549 |
echo "No files need archiving." |
| 30550 |
fi |
| 30551 |
|
| 30552 |
# =====================Now, here is the correct way:================= |
| 30553 |
|
| 30554 |
foundone=false |
| 30555 |
for f in $(find $HOME -type f -atime +30 -size 100k) # No pipe here. |
| 30556 |
do |
| 30557 |
echo "$f is over 100KB and has not been accessed in over 30 days" |
| 30558 |
echo "Consider moving the file to archives." |
| 30559 |
foundone=true |
| 30560 |
done |
| 30561 |
|
| 30562 |
if [ $foundone = false ] |
| 30563 |
then |
| 30564 |
echo "No files need archiving." |
| 30565 |
fi |
| 30566 |
|
| 30567 |
# ==================And here is another alternative================== |
| 30568 |
|
| 30569 |
# Places the part of the script that reads the variables |
| 30570 |
#+ within a code block, so they share the same subshell. |
| 30571 |
# Thank you, W.B. |
| 30572 |
|
| 30573 |
find $HOME -type f -atime +30 -size 100k | { |
| 30574 |
foundone=false |
| 30575 |
while read f |
| 30576 |
do |
| 30577 |
echo "$f is over 100KB and has not been accessed in over 30 days" |
| 30578 |
echo "Consider moving the file to archives." |
| 30579 |
foundone=true |
| 30580 |
done |
| 30581 |
|
| 30582 |
if ! $foundone |
| 30583 |
then |
| 30584 |
echo "No files need archiving." |
| 30585 |
fi |
| 30586 |
}</programlisting> |
| 30587 |
</para> |
| 30588 |
|
| 30589 |
<para><anchor id="ptailgrep"></para> |
| 30590 |
<para> |
| 30591 |
A lookalike problem occurs when trying to write the |
| 30592 |
<filename>stdout</filename> of a <command>tail -f</command> |
| 30593 |
piped to <link linkend="grepref">grep</link>. |
| 30594 |
<programlisting>tail -f /var/log/messages | grep "$ERROR_MSG" >> error.log |
| 30595 |
# The "error.log" file will not have anything written to it. |
| 30596 |
# As Samuli Kaipiainen points out, this results from grep |
| 30597 |
#+ buffering its output. |
| 30598 |
# The fix is to add the "--line-buffered" parameter to grep.</programlisting> |
| 30599 |
</para> |
| 30600 |
</listitem> |
| 30601 |
|
| 30602 |
|
| 30603 |
|
| 30604 |
<listitem> |
| 30605 |
<para><anchor id="suidscr"></para> |
| 30606 |
<para>Using <quote>suid</quote> commands within scripts is risky, |
| 30607 |
as it may compromise system security. |
| 30608 |
<footnote><para>Setting the <link linkend="suidref">suid</link> |
| 30609 |
permission on the script itself has no effect in Linux |
| 30610 |
and most other UNIX flavors.</para></footnote> |
| 30611 |
</para> |
| 30612 |
</listitem> |
| 30613 |
|
| 30614 |
|
| 30615 |
<listitem> |
| 30616 |
<para><anchor id="cgiref"></para> |
| 30617 |
<para>Using shell scripts for CGI programming may be problematic. Shell |
| 30618 |
script variables are not <quote>typesafe,</quote> and this can cause |
| 30619 |
undesirable behavior as far as CGI is concerned. Moreover, it is |
| 30620 |
difficult to <quote>cracker-proof</quote> shell scripts.</para> |
| 30621 |
</listitem> |
| 30622 |
|
| 30623 |
|
| 30624 |
<listitem> |
| 30625 |
<para>Bash does not handle the <link linkend="doubleslashref">double slash |
| 30626 |
(<token>//</token>) string</link> correctly.</para> |
| 30627 |
</listitem> |
| 30628 |
|
| 30629 |
|
| 30630 |
<listitem> |
| 30631 |
<para><anchor id="gnuref"></para> |
| 30632 |
<para>Bash scripts written for Linux or BSD systems may need |
| 30633 |
fixups to run on a commercial UNIX machine. Such |
| 30634 |
scripts often employ the GNU set of commands and filters, |
| 30635 |
which have greater functionality than their generic UNIX |
| 30636 |
counterparts. This is particularly true of such text processing |
| 30637 |
utilites as <link linkend="trref">tr</link>.</para> |
| 30638 |
</listitem> |
| 30639 |
|
| 30640 |
<listitem> |
| 30641 |
<para><anchor id="updatebreaks"></para> |
| 30642 |
<para>Sadly, updates to Bash itself have broken older scripts |
| 30643 |
that <link linkend="paragraphspace">used to work perfectly |
| 30644 |
fine</link>. Let us recall <link linkend="undocf">how |
| 30645 |
risky it is to use undocumented Bash features</link>.</para> |
| 30646 |
</listitem> |
| 30647 |
|
| 30648 |
</itemizedlist> |
| 30649 |
|
| 30650 |
<epigraph> |
| 30651 |
<para>Danger is near thee --</para> |
| 30652 |
<para>Beware, beware, beware, beware.</para> |
| 30653 |
<para>Many brave hearts are asleep in the deep.</para> |
| 30654 |
<para>So beware --</para> |
| 30655 |
<para>Beware.</para> |
| 30656 |
<para>--A.J. Lamb and H.W. Petrie</para> |
| 30657 |
</epigraph> |
| 30658 |
|
| 30659 |
</chapter> <!-- Gotchas --> |
| 30660 |
|
| 30661 |
|
| 30662 |
|
| 30663 |
<chapter id="scrstyle"> |
| 30664 |
<title>Scripting With Style</title> |
| 30665 |
|
| 30666 |
<para>Get into the habit of writing shell scripts in a structured and |
| 30667 |
systematic manner. Even on-the-fly and <quote>written on the |
| 30668 |
back of an envelope</quote> scripts will benefit if you take a |
| 30669 |
few minutes to plan and organize your thoughts before sitting |
| 30670 |
down and coding.</para> |
| 30671 |
|
| 30672 |
<para>Herewith are a few stylistic guidelines. This is not |
| 30673 |
(necessarily) intended as an <emphasis>Official Shell Scripting |
| 30674 |
Stylesheet</emphasis>.</para> |
| 30675 |
|
| 30676 |
|
| 30677 |
|
| 30678 |
<sect1 id="unofficialst"> |
| 30679 |
<title>Unofficial Shell Scripting Stylesheet</title> |
| 30680 |
|
| 30681 |
<itemizedlist> |
| 30682 |
|
| 30683 |
<listitem> |
| 30684 |
|
| 30685 |
<para>Comment your code. This makes it easier for others to |
| 30686 |
understand (and appreciate), and easier for you to maintain. |
| 30687 |
<programlisting>PASS="$PASS${MATRIX:$(($RANDOM%${#MATRIX})):1}" |
| 30688 |
# It made perfect sense when you wrote it last year, |
| 30689 |
#+ but now it's a complete mystery. |
| 30690 |
# (From Antek Sawicki's "pw.sh" script.)</programlisting> |
| 30691 |
</para> |
| 30692 |
|
| 30693 |
<para>Add descriptive headers to your scripts and functions. |
| 30694 |
<programlisting>#!/bin/bash |
| 30695 |
|
| 30696 |
#************************************************# |
| 30697 |
# xyz.sh # |
| 30698 |
# written by Bozo Bozeman # |
| 30699 |
# July 05, 2001 # |
| 30700 |
# # |
| 30701 |
# Clean up project files. # |
| 30702 |
#************************************************# |
| 30703 |
|
| 30704 |
E_BADDIR=85 # No such directory. |
| 30705 |
projectdir=/home/bozo/projects # Directory to clean up. |
| 30706 |
|
| 30707 |
# --------------------------------------------------------- # |
| 30708 |
# cleanup_pfiles () # |
| 30709 |
# Removes all files in designated directory. # |
| 30710 |
# Parameter: $target_directory # |
| 30711 |
# Returns: 0 on success, $E_BADDIR if something went wrong. # |
| 30712 |
# --------------------------------------------------------- # |
| 30713 |
cleanup_pfiles () |
| 30714 |
{ |
| 30715 |
if [ ! -d "$1" ] # Test if target directory exists. |
| 30716 |
then |
| 30717 |
echo "$1 is not a directory." |
| 30718 |
return $E_BADDIR |
| 30719 |
fi |
| 30720 |
|
| 30721 |
rm -f "$1"/* |
| 30722 |
return 0 # Success. |
| 30723 |
} |
| 30724 |
|
| 30725 |
cleanup_pfiles $projectdir |
| 30726 |
|
| 30727 |
exit $?</programlisting></para> |
| 30728 |
|
| 30729 |
</listitem> |
| 30730 |
|
| 30731 |
|
| 30732 |
<listitem> |
| 30733 |
<para>Avoid using <quote>magic numbers,</quote> |
| 30734 |
|
| 30735 |
<footnote><para>In this context, <quote>magic |
| 30736 |
numbers</quote> have an entirely different meaning than |
| 30737 |
the <link linkend="magnumref">magic numbers</link> used |
| 30738 |
to designate file types.</para></footnote> |
| 30739 |
|
| 30740 |
that is, <quote>hard-wired</quote> literal constants. Use |
| 30741 |
meaningful variable names instead. This makes the script |
| 30742 |
easier to understand and permits making changes and updates |
| 30743 |
without breaking the application. |
| 30744 |
|
| 30745 |
<programlisting>if [ -f /var/log/messages ] |
| 30746 |
then |
| 30747 |
... |
| 30748 |
fi |
| 30749 |
# A year later, you decide to change the script to check /var/log/syslog. |
| 30750 |
# It is now necessary to manually change the script, instance by instance, |
| 30751 |
#+ and hope nothing breaks. |
| 30752 |
|
| 30753 |
# A better way: |
| 30754 |
LOGFILE=/var/log/messages # Only line that needs to be changed. |
| 30755 |
if [ -f "$LOGFILE" ] |
| 30756 |
then |
| 30757 |
... |
| 30758 |
fi</programlisting> |
| 30759 |
</para> |
| 30760 |
</listitem> |
| 30761 |
|
| 30762 |
|
| 30763 |
<listitem> |
| 30764 |
|
| 30765 |
<para>Choose descriptive names for variables and functions. |
| 30766 |
<programlisting>fl=`ls -al $dirname` # Cryptic. |
| 30767 |
file_listing=`ls -al $dirname` # Better. |
| 30768 |
|
| 30769 |
|
| 30770 |
MAXVAL=10 # All caps used for a script constant. |
| 30771 |
while [ "$index" -le "$MAXVAL" ] |
| 30772 |
... |
| 30773 |
|
| 30774 |
|
| 30775 |
E_NOTFOUND=95 # Uppercase for an errorcode, |
| 30776 |
#+ and name prefixed with E_. |
| 30777 |
if [ ! -e "$filename" ] |
| 30778 |
then |
| 30779 |
echo "File $filename not found." |
| 30780 |
exit $E_NOTFOUND |
| 30781 |
fi |
| 30782 |
|
| 30783 |
|
| 30784 |
MAIL_DIRECTORY=/var/spool/mail/bozo # Uppercase for an environmental |
| 30785 |
export MAIL_DIRECTORY #+ variable. |
| 30786 |
|
| 30787 |
|
| 30788 |
GetAnswer () # Mixed case works well for a |
| 30789 |
{ #+ function name, especially |
| 30790 |
prompt=$1 #+ when it improves legibility. |
| 30791 |
echo -n $prompt |
| 30792 |
read answer |
| 30793 |
return $answer |
| 30794 |
} |
| 30795 |
|
| 30796 |
GetAnswer "What is your favorite number? " |
| 30797 |
favorite_number=$? |
| 30798 |
echo $favorite_number |
| 30799 |
|
| 30800 |
|
| 30801 |
_uservariable=23 # Permissible, but not recommended. |
| 30802 |
# It's better for user-defined variables not to start with an underscore. |
| 30803 |
# Leave that for system variables.</programlisting> |
| 30804 |
</para> |
| 30805 |
</listitem> |
| 30806 |
|
| 30807 |
|
| 30808 |
<listitem> |
| 30809 |
|
| 30810 |
<para>Use <link linkend="exitcommandref">exit codes</link> |
| 30811 |
in a systematic and meaningful way. |
| 30812 |
|
| 30813 |
<programlisting>E_WRONG_ARGS=95 |
| 30814 |
... |
| 30815 |
... |
| 30816 |
exit $E_WRONG_ARGS</programlisting> |
| 30817 |
See also <xref linkend="exitcodes">.</para> |
| 30818 |
|
| 30819 |
|
| 30820 |
<para><emphasis>Ender</emphasis> suggests using the <link |
| 30821 |
linkend="sysexitsref">exit codes |
| 30822 |
in <filename>/usr/include/sysexits.h</filename></link> in shell |
| 30823 |
scripts, though these are primarily intended for C and C++ |
| 30824 |
programming.</para> |
| 30825 |
|
| 30826 |
</listitem> |
| 30827 |
|
| 30828 |
|
| 30829 |
|
| 30830 |
<listitem> |
| 30831 |
|
| 30832 |
|
| 30833 |
<para>Use standardized parameter flags for script invocation. |
| 30834 |
<emphasis>Ender</emphasis> proposes the following set |
| 30835 |
of flags.</para> |
| 30836 |
|
| 30837 |
<para> |
| 30838 |
<programlisting>-a All: Return all information (including hidden file info). |
| 30839 |
-b Brief: Short version, usually for other scripts. |
| 30840 |
-c Copy, concatenate, etc. |
| 30841 |
-d Daily: Use information from the whole day, and not merely |
| 30842 |
information for a specific instance/user. |
| 30843 |
-e Extended/Elaborate: (often does not include hidden file info). |
| 30844 |
-h Help: Verbose usage w/descs, aux info, discussion, help. |
| 30845 |
See also -V. |
| 30846 |
-l Log output of script. |
| 30847 |
-m Manual: Launch man-page for base command. |
| 30848 |
-n Numbers: Numerical data only. |
| 30849 |
-r Recursive: All files in a directory (and/or all sub-dirs). |
| 30850 |
-s Setup & File Maintenance: Config files for this script. |
| 30851 |
-u Usage: List of invocation flags for the script. |
| 30852 |
-v Verbose: Human readable output, more or less formatted. |
| 30853 |
-V Version / License / Copy(right|left) / Contribs (email too).</programlisting> |
| 30854 |
</para> |
| 30855 |
|
| 30856 |
<para>See also <xref linkend="standard-options">.</para> |
| 30857 |
|
| 30858 |
</listitem> |
| 30859 |
|
| 30860 |
|
| 30861 |
<listitem> |
| 30862 |
<para>Break complex scripts into simpler modules. Use functions |
| 30863 |
where appropriate. See <xref linkend="cards">.</para> |
| 30864 |
</listitem> |
| 30865 |
|
| 30866 |
<listitem> |
| 30867 |
<para>Don't use a complex construct where a simpler one will do. |
| 30868 |
|
| 30869 |
<programlisting>COMMAND |
| 30870 |
if [ $? -eq 0 ] |
| 30871 |
... |
| 30872 |
# Redundant and non-intuitive. |
| 30873 |
|
| 30874 |
if COMMAND |
| 30875 |
... |
| 30876 |
# More concise (if perhaps not quite as legible).</programlisting> |
| 30877 |
</para> |
| 30878 |
</listitem> |
| 30879 |
|
| 30880 |
|
| 30881 |
</itemizedlist> |
| 30882 |
|
| 30883 |
|
| 30884 |
<epigraph> |
| 30885 |
<para>... reading the UNIX source code to the Bourne shell (/bin/sh). I |
| 30886 |
was shocked at how much simple algorithms could be made cryptic, and |
| 30887 |
therefore useless, by a poor choice of code style. I asked myself, |
| 30888 |
<quote>Could someone be proud of this code?</quote></para> |
| 30889 |
<para>--Landon Noll</para> |
| 30890 |
</epigraph> |
| 30891 |
|
| 30892 |
</sect1> <!-- Unofficial Shell Scripting Stylesheet --> |
| 30893 |
|
| 30894 |
|
| 30895 |
|
| 30896 |
</chapter> <!-- Scripting With Style --> |
| 30897 |
|
| 30898 |
|
| 30899 |
|
| 30900 |
<chapter id="miscellany"> |
| 30901 |
<title>Miscellany</title> |
| 30902 |
|
| 30903 |
<epigraph> |
| 30904 |
<para>Nobody really knows what the Bourne shell's grammar is. Even |
| 30905 |
examination of the source code is little help.</para> |
| 30906 |
<para>--Tom Duff</para> |
| 30907 |
</epigraph> |
| 30908 |
|
| 30909 |
|
| 30910 |
<sect1 id="intandnonint"> |
| 30911 |
<title>Interactive and non-interactive shells and scripts</title> |
| 30912 |
|
| 30913 |
<para>An <firstterm>interactive</firstterm> shell reads |
| 30914 |
commands from user input on a <filename>tty</filename>. Among |
| 30915 |
other things, such a shell reads startup files on activation, |
| 30916 |
displays a prompt, and enables job control by default. The |
| 30917 |
user can <firstterm>interact</firstterm> with the shell.</para> |
| 30918 |
|
| 30919 |
<para>A shell running a script is always a non-interactive |
| 30920 |
shell. All the same, the script can still access its |
| 30921 |
<filename>tty</filename>. It is even possible to emulate an |
| 30922 |
interactive shell in a script. |
| 30923 |
|
| 30924 |
<programlisting>#!/bin/bash |
| 30925 |
MY_PROMPT='$ ' |
| 30926 |
while : |
| 30927 |
do |
| 30928 |
echo -n "$MY_PROMPT" |
| 30929 |
read line |
| 30930 |
eval "$line" |
| 30931 |
done |
| 30932 |
|
| 30933 |
exit 0 |
| 30934 |
|
| 30935 |
# This example script, and much of the above explanation supplied by |
| 30936 |
# Stéphane Chazelas (thanks again).</programlisting></para> |
| 30937 |
|
| 30938 |
<para>Let us consider an <firstterm>interactive</firstterm> |
| 30939 |
script to be one that requires input from the user, usually |
| 30940 |
with <link linkend="readref">read</link> statements (see <xref |
| 30941 |
linkend="ex36">). <quote>Real life</quote> is actually a |
| 30942 |
bit messier than that. For now, assume an interactive script |
| 30943 |
is bound to a tty, a script that a user has invoked from the |
| 30944 |
console or an <firstterm>xterm</firstterm>.</para> |
| 30945 |
|
| 30946 |
<para>Init and startup scripts are necessarily non-interactive, |
| 30947 |
since they must run without human intervention. Many |
| 30948 |
administrative and system maintenance scripts are likewise |
| 30949 |
non-interactive. Unvarying repetitive tasks cry out for |
| 30950 |
automation by non-interactive scripts.</para> |
| 30951 |
|
| 30952 |
<para>Non-interactive scripts can run in the background, but |
| 30953 |
interactive ones hang, waiting for input that never comes. |
| 30954 |
Handle that difficulty by having an <command>expect</command> |
| 30955 |
script or embedded <link linkend="heredocref">here |
| 30956 |
document</link> feed input to an interactive script running |
| 30957 |
as a background job. In the simplest case, redirect a |
| 30958 |
file to supply input to a <command>read</command> statement |
| 30959 |
(<command>read variable <file</command>). These particular |
| 30960 |
workarounds make possible general purpose scripts that run |
| 30961 |
in either interactive or non-interactive modes.</para> |
| 30962 |
|
| 30963 |
<para>If a script needs to test whether it is running in an |
| 30964 |
interactive shell, it is simply a matter of finding |
| 30965 |
whether the <firstterm>prompt</firstterm> variable, <link |
| 30966 |
linkend="ps1ref">$PS1</link> is set. (If the user is being |
| 30967 |
prompted for input, then the script needs to display a |
| 30968 |
prompt.)</para> |
| 30969 |
|
| 30970 |
<para><programlisting>if [ -z $PS1 ] # no prompt? |
| 30971 |
### if [ -v PS1 ] # On Bash 4.2+ ... |
| 30972 |
then |
| 30973 |
# non-interactive |
| 30974 |
... |
| 30975 |
else |
| 30976 |
# interactive |
| 30977 |
... |
| 30978 |
fi</programlisting></para> |
| 30979 |
|
| 30980 |
<para><anchor id="iitest">Alternatively, the script can test |
| 30981 |
for the presence of option <quote>i</quote> in the <link |
| 30982 |
linkend="flpref">$-</link> flag.</para> |
| 30983 |
|
| 30984 |
<para><programlisting>case $- in |
| 30985 |
*i*) # interactive shell |
| 30986 |
;; |
| 30987 |
*) # non-interactive shell |
| 30988 |
;; |
| 30989 |
# (Courtesy of "UNIX F.A.Q.," 1993)</programlisting></para> |
| 30990 |
|
| 30991 |
<para><anchor id="ii2test">However, John Lange describes |
| 30992 |
an alternative method, using the <link |
| 30993 |
linkend="termtest"><token>-t</token> |
| 30994 |
<firstterm>test</firstterm> operator</link>.</para> |
| 30995 |
|
| 30996 |
<para><programlisting># Test for a terminal! |
| 30997 |
|
| 30998 |
fd=0 # stdin |
| 30999 |
|
| 31000 |
# As we recall, the -t test option checks whether the stdin, [ -t 0 ], |
| 31001 |
#+ or stdout, [ -t 1 ], in a given script is running in a terminal. |
| 31002 |
if [ -t "$fd" ] |
| 31003 |
then |
| 31004 |
echo interactive |
| 31005 |
else |
| 31006 |
echo non-interactive |
| 31007 |
fi |
| 31008 |
|
| 31009 |
|
| 31010 |
# But, as John points out: |
| 31011 |
# if [ -t 0 ] works ... when you're logged in locally |
| 31012 |
# but fails when you invoke the command remotely via ssh. |
| 31013 |
# So for a true test you also have to test for a socket. |
| 31014 |
|
| 31015 |
if [[ -t "$fd" || -p /dev/stdin ]] |
| 31016 |
then |
| 31017 |
echo interactive |
| 31018 |
else |
| 31019 |
echo non-interactive |
| 31020 |
fi</programlisting></para> |
| 31021 |
|
| 31022 |
<note><para>Scripts may be forced to run in interactive |
| 31023 |
mode with the <token>-i</token> option or with a |
| 31024 |
<userinput>#!/bin/bash -i</userinput> header. Be aware that |
| 31025 |
this can cause erratic script behavior or show error messages |
| 31026 |
even when no error is present.</para></note> |
| 31027 |
|
| 31028 |
|
| 31029 |
</sect1> <!-- Interactive and non-interactive scripts --> |
| 31030 |
|
| 31031 |
|
| 31032 |
|
| 31033 |
<sect1 id="wrapper"> |
| 31034 |
<title>Shell Wrappers</title> |
| 31035 |
|
| 31036 |
<para><anchor id="shwrapper"></para> |
| 31037 |
|
| 31038 |
<para>A <firstterm>wrapper</firstterm> is a shell script that embeds |
| 31039 |
a system command or utility, that accepts and passes a set of |
| 31040 |
parameters to that command. |
| 31041 |
|
| 31042 |
<footnote><para>Quite a number of Linux utilities are, in fact, |
| 31043 |
shell wrappers. Some examples are |
| 31044 |
<filename>/usr/bin/pdf2ps</filename>, |
| 31045 |
<filename>/usr/bin/batch</filename>, and |
| 31046 |
<filename>/usr/bin/xmkmf</filename>.</para></footnote> |
| 31047 |
|
| 31048 |
Wrapping a script around a complex command-line |
| 31049 |
simplifies invoking it. This is expecially useful |
| 31050 |
with <link linkend="sedref">sed</link> and <link |
| 31051 |
linkend="awkref">awk</link>.</para> |
| 31052 |
|
| 31053 |
<para>A |
| 31054 |
<command> |
| 31055 |
<indexterm> |
| 31056 |
<primary>sed</primary> |
| 31057 |
</indexterm> <indexterm> |
| 31058 |
<primary>script</primary> <secondary>sed</secondary> |
| 31059 |
</indexterm> sed</command> or |
| 31060 |
<command> |
| 31061 |
<indexterm> |
| 31062 |
<primary>awk</primary> |
| 31063 |
</indexterm> <indexterm> |
| 31064 |
<primary>script</primary> <secondary>awk</secondary> |
| 31065 |
</indexterm> |
| 31066 |
awk</command> script would normally be invoked |
| 31067 |
from the command-line by a <userinput>sed -e |
| 31068 |
<replaceable>'commands'</replaceable></userinput> |
| 31069 |
or <userinput>awk |
| 31070 |
<replaceable>'commands'</replaceable></userinput>. Embedding |
| 31071 |
such a script in a Bash script permits calling it more simply, |
| 31072 |
and makes it <firstterm>reusable</firstterm>. This also |
| 31073 |
enables combining the functionality of <firstterm>sed</firstterm> |
| 31074 |
and <firstterm>awk</firstterm>, for example <link |
| 31075 |
linkend="piperef">piping</link> the output of a set of |
| 31076 |
<firstterm>sed</firstterm> commands to |
| 31077 |
<firstterm>awk</firstterm>. As a saved executable file, |
| 31078 |
you can then repeatedly invoke it in its original form or |
| 31079 |
modified, without the inconvenience of retyping it on the |
| 31080 |
command-line.</para> |
| 31081 |
|
| 31082 |
<example id="ex3"> |
| 31083 |
<title><firstterm>shell wrapper</firstterm></title> |
| 31084 |
<programlisting>&ex3;</programlisting> |
| 31085 |
</example> |
| 31086 |
|
| 31087 |
<example id="ex4"> |
| 31088 |
<title> A slightly more complex <firstterm>shell |
| 31089 |
wrapper</firstterm></title> |
| 31090 |
<programlisting>&ex4;</programlisting> |
| 31091 |
</example> |
| 31092 |
|
| 31093 |
<example id="loggingwrapper"> |
| 31094 |
<title> A generic <firstterm>shell wrapper</firstterm> that |
| 31095 |
writes to a logfile</title> |
| 31096 |
<programlisting>&loggingwrapper;</programlisting> |
| 31097 |
</example> |
| 31098 |
|
| 31099 |
<example id="prasc"> |
| 31100 |
<title> A <firstterm>shell wrapper</firstterm> around an awk |
| 31101 |
script</title> |
| 31102 |
<programlisting>&prasc;</programlisting> |
| 31103 |
</example> |
| 31104 |
|
| 31105 |
<example id="coltotaler"> |
| 31106 |
<title> A <firstterm>shell wrapper</firstterm> around another |
| 31107 |
awk script</title> |
| 31108 |
<programlisting>&coltotaler;</programlisting> |
| 31109 |
</example> |
| 31110 |
|
| 31111 |
|
| 31112 |
<para><anchor id="perlref">For those scripts needing a single |
| 31113 |
do-it-all tool, a Swiss army knife, there is |
| 31114 |
<firstterm>Perl</firstterm>. Perl combines the |
| 31115 |
capabilities of <link linkend="sedref">sed</link> and <link |
| 31116 |
linkend="awkref">awk</link>, and throws in a large subset of |
| 31117 |
<command>C</command>, to boot. It is modular and contains support |
| 31118 |
for everything ranging from object-oriented programming up to and |
| 31119 |
including the kitchen sink. Short Perl scripts lend themselves to |
| 31120 |
embedding within shell scripts, and there may be some substance |
| 31121 |
to the claim that Perl can totally replace shell scripting |
| 31122 |
(though the author of the <emphasis>ABS Guide</emphasis> remains |
| 31123 |
skeptical).</para> |
| 31124 |
|
| 31125 |
<para><anchor id="perlemb"></para> |
| 31126 |
<example id="ex56"> |
| 31127 |
<title>Perl embedded in a <firstterm>Bash</firstterm> script</title> |
| 31128 |
<programlisting>&ex56;</programlisting> |
| 31129 |
</example> |
| 31130 |
|
| 31131 |
<para>It is even possible to combine a Bash script and Perl script |
| 31132 |
within the same file. Depending on how the script is invoked, either |
| 31133 |
the Bash part or the Perl part will execute.</para> |
| 31134 |
|
| 31135 |
<para><anchor id="bashandperl0"></para> |
| 31136 |
<example id="bashandperl"> |
| 31137 |
<title>Bash and Perl scripts combined</title> |
| 31138 |
<programlisting>&bashandperl;</programlisting> |
| 31139 |
</example> |
| 31140 |
|
| 31141 |
<para> |
| 31142 |
<screen><prompt>bash$ </prompt><userinput>bash bashandperl.sh</userinput> |
| 31143 |
<computeroutput>Greetings from the Bash part of the script.</computeroutput> |
| 31144 |
|
| 31145 |
|
| 31146 |
<prompt>bash$ </prompt><userinput>perl -x bashandperl.sh</userinput> |
| 31147 |
<computeroutput>Greetings from the Perl part of the script.</computeroutput> |
| 31148 |
</screen> |
| 31149 |
</para> |
| 31150 |
|
| 31151 |
<para>It is, of course, possible to embed even more exotic scripting |
| 31152 |
languages within shell wrappers. <firstterm>Python</firstterm>, |
| 31153 |
for example ...</para> |
| 31154 |
|
| 31155 |
<para><anchor id="pythonemb"></para> |
| 31156 |
<example id="ex56py"> |
| 31157 |
<title>Python embedded in a <firstterm>Bash</firstterm> script</title> |
| 31158 |
<programlisting>&ex56py;</programlisting> |
| 31159 |
</example> |
| 31160 |
|
| 31161 |
<para>Wrapping a script around <firstterm>mplayer</firstterm> |
| 31162 |
and the Google's translation server, you can create something |
| 31163 |
that talks back to you.</para> |
| 31164 |
|
| 31165 |
<para><anchor id="speech00"></para> |
| 31166 |
<example id="speech0"> |
| 31167 |
<title>A script that speaks</title> |
| 31168 |
<programlisting>&speech0;</programlisting> |
| 31169 |
</example> |
| 31170 |
|
| 31171 |
<para>One interesting example of a complex shell wrapper is Martin |
| 31172 |
Matusiak's <ulink |
| 31173 |
url="http://sourceforge.net/projects/undvd/"><firstterm>undvd</firstterm> |
| 31174 |
script</ulink>, which provides an easy-to-use |
| 31175 |
command-line interface to the complex <ulink |
| 31176 |
url="http://www.mplayerhq.hu/DOCS/HTML/en/mencoder.html">mencoder</ulink> |
| 31177 |
utility. Another example is Itzchak Rehberg's <ulink |
| 31178 |
url="http://projects.izzysoft.de/trac/ext3undel">Ext3Undel</ulink>, |
| 31179 |
a set of scripts to recover deleted file on an |
| 31180 |
<firstterm>ext3</firstterm> filesystem.</para> |
| 31181 |
|
| 31182 |
|
| 31183 |
</sect1> <!-- Shell wrappers --> |
| 31184 |
|
| 31185 |
|
| 31186 |
|
| 31187 |
<sect1 id="testsandcomparisons"> |
| 31188 |
<title>Tests and Comparisons: Alternatives</title> |
| 31189 |
|
| 31190 |
<para>For tests, the <link linkend="dblbrackets">[[ ]]</link> |
| 31191 |
construct may be more appropriate than <userinput>[ |
| 31192 |
]</userinput>. Likewise, <link linkend="icomparison1">arithmetic |
| 31193 |
comparisons</link> might benefit from the <link |
| 31194 |
linkend="dblparens">(( ))</link> construct. |
| 31195 |
|
| 31196 |
<programlisting>a=8 |
| 31197 |
|
| 31198 |
# All of the comparisons below are equivalent. |
| 31199 |
test "$a" -lt 16 && echo "yes, $a < 16" # "and list" |
| 31200 |
/bin/test "$a" -lt 16 && echo "yes, $a < 16" |
| 31201 |
[ "$a" -lt 16 ] && echo "yes, $a < 16" |
| 31202 |
[[ $a -lt 16 ]] && echo "yes, $a < 16" # Quoting variables within |
| 31203 |
(( a < 16 )) && echo "yes, $a < 16" # [[ ]] and (( )) not necessary. |
| 31204 |
|
| 31205 |
city="New York" |
| 31206 |
# Again, all of the comparisons below are equivalent. |
| 31207 |
test "$city" \< Paris && echo "Yes, Paris is greater than $city" |
| 31208 |
# Greater ASCII order. |
| 31209 |
/bin/test "$city" \< Paris && echo "Yes, Paris is greater than $city" |
| 31210 |
[ "$city" \< Paris ] && echo "Yes, Paris is greater than $city" |
| 31211 |
[[ $city < Paris ]] && echo "Yes, Paris is greater than $city" |
| 31212 |
# Need not quote $city. |
| 31213 |
|
| 31214 |
# Thank you, S.C.</programlisting></para> |
| 31215 |
|
| 31216 |
</sect1> <!-- Tests and Comparisons: Alternatives --> |
| 31217 |
|
| 31218 |
|
| 31219 |
|
| 31220 |
<sect1 id="recursionsct"> |
| 31221 |
<title>Recursion: a script calling itself</title> |
| 31222 |
|
| 31223 |
|
| 31224 |
<para><anchor id="scriptrecursion"></para> |
| 31225 |
<para>Can a script <link linkend="recursionref">recursively</link> |
| 31226 |
call itself? Indeed.</para> |
| 31227 |
|
| 31228 |
<example id="recurse"> |
| 31229 |
<title>A (useless) script that recursively calls itself</title> |
| 31230 |
<programlisting>&recurse;</programlisting> |
| 31231 |
</example> |
| 31232 |
|
| 31233 |
<example id="pbook"> |
| 31234 |
<title>A (useful) script that recursively calls itself</title> |
| 31235 |
<programlisting>&pbook;</programlisting> |
| 31236 |
</example> |
| 31237 |
|
| 31238 |
<example id="usrmnt"> |
| 31239 |
<title>Another (useful) script that recursively calls itself</title> |
| 31240 |
<programlisting>&usrmnt;</programlisting> |
| 31241 |
</example> |
| 31242 |
|
| 31243 |
<caution><para>Too many levels of recursion can exhaust the |
| 31244 |
script's stack space, causing a segfault.</para></caution> |
| 31245 |
|
| 31246 |
</sect1> <!-- Recursion --> |
| 31247 |
|
| 31248 |
|
| 31249 |
|
| 31250 |
<sect1 id="colorizing"> |
| 31251 |
<title><quote>Colorizing</quote> Scripts</title> |
| 31252 |
|
| 31253 |
<para><anchor id="colorizingref"></para> |
| 31254 |
|
| 31255 |
<para>The ANSI |
| 31256 |
<footnote><para><acronym>ANSI</acronym> is, of course, the |
| 31257 |
acronym for the American National Standards |
| 31258 |
Institute. This august body establishes and maintains |
| 31259 |
various technical and industrial standards.</para></footnote> |
| 31260 |
escape sequences set screen attributes, such as bold |
| 31261 |
text, and color of foreground and background. <link |
| 31262 |
linkend="dosbatch1">DOS batch files</link> commonly used |
| 31263 |
ANSI escape codes for <emphasis>color</emphasis> output, |
| 31264 |
and so can Bash scripts.</para> |
| 31265 |
|
| 31266 |
<example id="ex30a"> |
| 31267 |
<title>A <quote>colorized</quote> address database</title> |
| 31268 |
<programlisting>&ex30a;</programlisting> |
| 31269 |
</example> |
| 31270 |
|
| 31271 |
<example id="draw-box"> |
| 31272 |
<title>Drawing a box</title> |
| 31273 |
<programlisting>&drawbox;</programlisting> |
| 31274 |
</example> |
| 31275 |
|
| 31276 |
|
| 31277 |
<para>The simplest, and perhaps most useful ANSI escape sequence is |
| 31278 |
bold text, <command>\033[1m ... \033[0m</command>. The |
| 31279 |
<token>\033</token> represents an <link |
| 31280 |
linkend="escp">escape</link>, the <quote>[1</quote> turns on the |
| 31281 |
bold attribute, while the <quote>[0</quote> switches it off. The |
| 31282 |
<quote>m</quote> terminates each term of the escape sequence. |
| 31283 |
<screen> |
| 31284 |
<prompt>bash$ </prompt><userinput>echo -e "\033[1mThis is bold text.\033[0m"</userinput> |
| 31285 |
</screen> |
| 31286 |
</para> |
| 31287 |
|
| 31288 |
<para>A similar escape sequence switches on the underline |
| 31289 |
attribute (on an <firstterm>rxvt</firstterm> and an |
| 31290 |
<firstterm>aterm</firstterm>). |
| 31291 |
<screen> |
| 31292 |
<prompt>bash$ </prompt><userinput>echo -e "\033[4mThis is underlined text.\033[0m"</userinput> |
| 31293 |
</screen> |
| 31294 |
</para> |
| 31295 |
|
| 31296 |
<note><para>With an <command>echo</command>, the |
| 31297 |
<option>-e</option> option enables the escape |
| 31298 |
sequences.</para></note> |
| 31299 |
|
| 31300 |
<para>Other escape sequences change the text and/or background |
| 31301 |
color.</para> |
| 31302 |
|
| 31303 |
<para> |
| 31304 |
<screen> |
| 31305 |
<prompt>bash$ </prompt><userinput>echo -e '\E[34;47mThis prints in blue.'; tput sgr0</userinput> |
| 31306 |
|
| 31307 |
|
| 31308 |
<prompt>bash$ </prompt><userinput>echo -e '\E[33;44m'"yellow text on blue background"; tput sgr0</userinput> |
| 31309 |
|
| 31310 |
|
| 31311 |
<prompt>bash$ </prompt><userinput>echo -e '\E[1;33;44m'"BOLD yellow text on blue background"; tput sgr0</userinput> |
| 31312 |
</screen> |
| 31313 |
</para> |
| 31314 |
|
| 31315 |
<note><para>It's usually advisable to set the |
| 31316 |
<firstterm>bold</firstterm> attribute for light-colored foreground |
| 31317 |
text.</para></note> |
| 31318 |
|
| 31319 |
<para>The <command>tput sgr0</command> restores the |
| 31320 |
terminal settings to normal. Omitting this lets all |
| 31321 |
subsequent output from that particular terminal remain |
| 31322 |
blue.</para> |
| 31323 |
|
| 31324 |
<note><para>Since <command>tput sgr0</command> fails to restore |
| 31325 |
terminal settings under certain circumstances, |
| 31326 |
<command>echo -ne \E[0m</command> may be a better choice.</para></note> |
| 31327 |
|
| 31328 |
<para><anchor id="coloriztempl"></para> |
| 31329 |
<sidebar> |
| 31330 |
<para>Use the following template for writing colored text on a colored |
| 31331 |
background.</para> |
| 31332 |
|
| 31333 |
<para> |
| 31334 |
<userinput>echo -e '\E[COLOR1;COLOR2mSome text goes here.'</userinput> |
| 31335 |
</para> |
| 31336 |
|
| 31337 |
<para>The <quote>\E[</quote> begins the escape sequence. |
| 31338 |
The semicolon-separated numbers <quote>COLOR1</quote> and |
| 31339 |
<quote>COLOR2</quote> specify a foreground and a background |
| 31340 |
color, according to the table below. (The order of the |
| 31341 |
numbers does not matter, since the foreground and background |
| 31342 |
numbers fall in non-overlapping ranges.) The <quote>m</quote> |
| 31343 |
terminates the escape sequence, and the text begins immediately |
| 31344 |
after that.</para> |
| 31345 |
|
| 31346 |
<para>Note also that <link linkend="snglquo">single quotes</link> |
| 31347 |
enclose the remainder of the command sequence following the |
| 31348 |
<command>echo -e</command>.</para> |
| 31349 |
</sidebar> |
| 31350 |
|
| 31351 |
|
| 31352 |
<para>The numbers in the following table work for an |
| 31353 |
<firstterm>rxvt</firstterm> terminal. Results may vary for other |
| 31354 |
terminal emulators.</para> |
| 31355 |
|
| 31356 |
<para><anchor id="coloriztable"></para> |
| 31357 |
<table> |
| 31358 |
<title>Numbers representing colors in Escape Sequences</title> |
| 31359 |
<tgroup cols="3"> |
| 31360 |
<thead> |
| 31361 |
<row> |
| 31362 |
<entry>Color</entry> |
| 31363 |
<entry>Foreground</entry> |
| 31364 |
<entry>Background</entry> |
| 31365 |
</row> |
| 31366 |
</thead> |
| 31367 |
<tbody> |
| 31368 |
<row> |
| 31369 |
<entry><option>black</option></entry> |
| 31370 |
<entry>30</entry> |
| 31371 |
<entry>40</entry> |
| 31372 |
</row> |
| 31373 |
<row> |
| 31374 |
<entry><option>red</option></entry> |
| 31375 |
<entry>31</entry> |
| 31376 |
<entry>41</entry> |
| 31377 |
</row> |
| 31378 |
<row> |
| 31379 |
<entry><option>green</option></entry> |
| 31380 |
<entry>32</entry> |
| 31381 |
<entry>42</entry> |
| 31382 |
</row> |
| 31383 |
<row> |
| 31384 |
<entry><option>yellow</option></entry> |
| 31385 |
<entry>33</entry> |
| 31386 |
<entry>43</entry> |
| 31387 |
</row> |
| 31388 |
<row> |
| 31389 |
<entry><option>blue</option></entry> |
| 31390 |
<entry>34</entry> |
| 31391 |
<entry>44</entry> |
| 31392 |
</row> |
| 31393 |
<row> |
| 31394 |
<entry><option>magenta</option></entry> |
| 31395 |
<entry>35</entry> |
| 31396 |
<entry>45</entry> |
| 31397 |
</row> |
| 31398 |
<row> |
| 31399 |
<entry><option>cyan</option></entry> |
| 31400 |
<entry>36</entry> |
| 31401 |
<entry>46</entry> |
| 31402 |
</row> |
| 31403 |
<row> |
| 31404 |
<entry><option>white</option></entry> |
| 31405 |
<entry>37</entry> |
| 31406 |
<entry>47</entry> |
| 31407 |
</row> |
| 31408 |
</tbody> |
| 31409 |
</tgroup> |
| 31410 |
</table> |
| 31411 |
|
| 31412 |
<example id="colorecho"> |
| 31413 |
<title>Echoing colored text</title> |
| 31414 |
<programlisting>&colorecho;</programlisting> |
| 31415 |
</example> |
| 31416 |
|
| 31417 |
<para><anchor id="horseraceref"></para> |
| 31418 |
<example id="horserace"> |
| 31419 |
<title>A <quote>horserace</quote> game</title> |
| 31420 |
<programlisting>&horserace;</programlisting> |
| 31421 |
</example> |
| 31422 |
|
| 31423 |
<para>See also <xref linkend="hashexample">, <xref |
| 31424 |
linkend="homework">, <xref linkend="showallc">, and <xref |
| 31425 |
linkend="petals">.</para> |
| 31426 |
|
| 31427 |
<caution><para>There is, however, a major problem with all |
| 31428 |
this. <emphasis>ANSI escape sequences are emphatically |
| 31429 |
<link |
| 31430 |
linkend="portabilityissues">non-portable</link>.</emphasis> |
| 31431 |
What works fine on some terminal emulators (or the |
| 31432 |
console) may work differently, or not at all, on others. |
| 31433 |
A <quote>colorized</quote> script that looks stunning on the |
| 31434 |
script author's machine may produce unreadable output on |
| 31435 |
someone else's. This somewhat compromises the usefulness of |
| 31436 |
colorizing scripts, and possibly relegates this technique |
| 31437 |
to the status of a gimmick. Colorized scripts are probably |
| 31438 |
inappropriate in a commercial setting, i.e., your supervisor |
| 31439 |
might disapprove.</para></caution> |
| 31440 |
|
| 31441 |
<para>Alister's <ulink url="http://code.google.com/p/ansi-color/"> |
| 31442 |
ansi-color</ulink> utility (based on <ulink |
| 31443 |
url="http://bash.deta.in/color-1.1.tar.gz">Moshe |
| 31444 |
Jacobson's color utility</ulink> considerably simplifies using |
| 31445 |
ANSI escape sequences. It substitutes a clean and logical |
| 31446 |
syntax for the clumsy constructs just discussed.</para> |
| 31447 |
|
| 31448 |
<para>Henry/teikedvl has likewise created a utility (<ulink |
| 31449 |
url="http://scriptechocolor.sourceforge.net/">http://scriptechocolor.sourceforge.net/</ulink>) to simplify creation of colorized scripts.</para> |
| 31450 |
|
| 31451 |
|
| 31452 |
</sect1> <!-- "Colorizing" scripts --> |
| 31453 |
|
| 31454 |
|
| 31455 |
|
| 31456 |
<sect1 id="optimizations"> |
| 31457 |
<title>Optimizations</title> |
| 31458 |
|
| 31459 |
<para>Most shell scripts are quick 'n dirty solutions to non-complex |
| 31460 |
problems. As such, optimizing them for speed is not much of an |
| 31461 |
issue. Consider the case, though, where a script carries out |
| 31462 |
an important task, does it well, but runs too slowly. Rewriting |
| 31463 |
it in a compiled language may not be a palatable option. The |
| 31464 |
simplest fix would be to rewrite the parts of the script |
| 31465 |
that slow it down. Is it possible to apply principles of code |
| 31466 |
optimization even to a lowly shell script?</para> |
| 31467 |
|
| 31468 |
<para>Check the loops in the script. Time consumed by repetitive |
| 31469 |
operations adds up quickly. If at all possible, remove |
| 31470 |
time-consuming operations from within loops.</para> |
| 31471 |
|
| 31472 |
<para>Use <link linkend="builtinref">builtin</link> commands in |
| 31473 |
preference to system commands. Builtins execute faster and |
| 31474 |
usually do not launch a subshell when invoked.</para> |
| 31475 |
|
| 31476 |
<para><anchor id="catabuse"></para> |
| 31477 |
<para>Avoid unnecessary commands, particularly in a <link |
| 31478 |
linkend="piperef">pipe</link>. |
| 31479 |
<programlisting>cat "$file" | grep "$word" |
| 31480 |
|
| 31481 |
grep "$word" "$file" |
| 31482 |
|
| 31483 |
# The above command-lines have an identical effect, |
| 31484 |
#+ but the second runs faster since it launches one fewer subprocess.</programlisting> |
| 31485 |
The <link linkend="catref">cat</link> command seems especially |
| 31486 |
prone to overuse in scripts.</para> |
| 31487 |
|
| 31488 |
<para><anchor id="lcall"></para> |
| 31489 |
<sidebar><para>Disabling certain Bash options can speed up scripts.</para> |
| 31490 |
<para>As Erik Brandsberg points out:</para> |
| 31491 |
<para>If you don't need <link |
| 31492 |
linkend="unicoderef">Unicode</link> support, you can |
| 31493 |
get potentially a 2x or more improvement in speed by |
| 31494 |
simply setting the <userinput>LC_ALL</userinput> variable. |
| 31495 |
|
| 31496 |
<programlisting> export LC_ALL=C |
| 31497 |
|
| 31498 |
[specifies the locale as ANSI C, |
| 31499 |
thereby disabling Unicode support] |
| 31500 |
|
| 31501 |
[In an example script ...] |
| 31502 |
|
| 31503 |
Without [Unicode support]: |
| 31504 |
erik@erik-desktop:~/capture$ time ./cap-ngrep.sh |
| 31505 |
live2.pcap > out.txt |
| 31506 |
|
| 31507 |
real 0m20.483s |
| 31508 |
user 1m34.470s |
| 31509 |
sys 0m12.869s |
| 31510 |
|
| 31511 |
With [Unicode support]: |
| 31512 |
erik@erik-desktop:~/capture$ time ./cap-ngrep.sh |
| 31513 |
live2.pcap > out.txt |
| 31514 |
|
| 31515 |
real 0m50.232s |
| 31516 |
user 3m51.118s |
| 31517 |
sys 0m11.221s |
| 31518 |
|
| 31519 |
A large part of the overhead that is optimized is, I believe, |
| 31520 |
regex match using [[ string =~ REGEX ]], |
| 31521 |
but it may help with other portions of the code as well. |
| 31522 |
I hadn't [seen it] mentioned that this optimization helped |
| 31523 |
with Bash, but I had seen it helped with "grep," |
| 31524 |
so why not try?</programlisting></para></sidebar> |
| 31525 |
|
| 31526 |
<para><anchor id="optimes"></para> |
| 31527 |
<note><para>Certain operators, notably <link |
| 31528 |
linkend="exprref">expr</link>, are very inefficient |
| 31529 |
and might be replaced by <link linkend="dblparens">double |
| 31530 |
parentheses</link> arithmetic expansion. |
| 31531 |
See <xref linkend="testexectime">.</para> |
| 31532 |
|
| 31533 |
|
| 31534 |
<para><programlisting>Math tests |
| 31535 |
|
| 31536 |
math via $(( )) |
| 31537 |
real 0m0.294s |
| 31538 |
user 0m0.288s |
| 31539 |
sys 0m0.008s |
| 31540 |
|
| 31541 |
math via expr: |
| 31542 |
real 1m17.879s # Much slower! |
| 31543 |
user 0m3.600s |
| 31544 |
sys 0m8.765s |
| 31545 |
|
| 31546 |
math via let: |
| 31547 |
real 0m0.364s |
| 31548 |
user 0m0.372s |
| 31549 |
sys 0m0.000s</programlisting></para> |
| 31550 |
|
| 31551 |
|
| 31552 |
<para><link linkend="ifthen">Condition testing</link> |
| 31553 |
constructs in scripts deserve close scrutiny. Substitute |
| 31554 |
<link linkend="caseesac1">case</link> for <link |
| 31555 |
linkend="ifthen">if-then</link> constructs and combine tests |
| 31556 |
when possible, to minimize script execution time. Again, |
| 31557 |
refer to <xref linkend="testexectime">.</para> |
| 31558 |
|
| 31559 |
|
| 31560 |
<para><programlisting>Test using "case" construct: |
| 31561 |
real 0m0.329s |
| 31562 |
user 0m0.320s |
| 31563 |
sys 0m0.000s |
| 31564 |
|
| 31565 |
|
| 31566 |
Test with if [], no quotes: |
| 31567 |
real 0m0.438s |
| 31568 |
user 0m0.432s |
| 31569 |
sys 0m0.008s |
| 31570 |
|
| 31571 |
|
| 31572 |
Test with if [], quotes: |
| 31573 |
real 0m0.476s |
| 31574 |
user 0m0.452s |
| 31575 |
sys 0m0.024s |
| 31576 |
|
| 31577 |
|
| 31578 |
Test with if [], using -eq: |
| 31579 |
real 0m0.457s |
| 31580 |
user 0m0.456s |
| 31581 |
sys 0m0.000s</programlisting></para></note> |
| 31582 |
|
| 31583 |
|
| 31584 |
<para><anchor id="assocarrtst"></para> |
| 31585 |
<note><para>Erik Brandsberg recommends using <link |
| 31586 |
linkend="assocarr">associative arrays</link> in preference to |
| 31587 |
conventional numeric-indexed arrays in most cases. When |
| 31588 |
overwriting values in a numeric array, there is a significant |
| 31589 |
performance penalty vs. associative arrays. Running a test |
| 31590 |
script confirms this. See <xref linkend="assocarrtest">.</para> |
| 31591 |
|
| 31592 |
|
| 31593 |
<para><programlisting>Assignment tests |
| 31594 |
|
| 31595 |
Assigning a simple variable |
| 31596 |
real 0m0.418s |
| 31597 |
user 0m0.416s |
| 31598 |
sys 0m0.004s |
| 31599 |
|
| 31600 |
Assigning a numeric index array entry |
| 31601 |
real 0m0.582s |
| 31602 |
user 0m0.564s |
| 31603 |
sys 0m0.016s |
| 31604 |
|
| 31605 |
Overwriting a numeric index array entry |
| 31606 |
real 0m21.931s |
| 31607 |
user 0m21.913s |
| 31608 |
sys 0m0.016s |
| 31609 |
|
| 31610 |
Linear reading of numeric index array |
| 31611 |
real 0m0.422s |
| 31612 |
user 0m0.416s |
| 31613 |
sys 0m0.004s |
| 31614 |
|
| 31615 |
Assigning an associative array entry |
| 31616 |
real 0m1.800s |
| 31617 |
user 0m1.796s |
| 31618 |
sys 0m0.004s |
| 31619 |
|
| 31620 |
Overwriting an associative array entry |
| 31621 |
real 0m1.798s |
| 31622 |
user 0m1.784s |
| 31623 |
sys 0m0.012s |
| 31624 |
|
| 31625 |
Linear reading an associative array entry |
| 31626 |
real 0m0.420s |
| 31627 |
user 0m0.420s |
| 31628 |
sys 0m0.000s |
| 31629 |
|
| 31630 |
Assigning a random number to a simple variable |
| 31631 |
real 0m0.402s |
| 31632 |
user 0m0.388s |
| 31633 |
sys 0m0.016s |
| 31634 |
|
| 31635 |
Assigning a sparse numeric index array entry randomly into 64k cells |
| 31636 |
real 0m12.678s |
| 31637 |
user 0m12.649s |
| 31638 |
sys 0m0.028s |
| 31639 |
|
| 31640 |
Reading sparse numeric index array entry |
| 31641 |
real 0m0.087s |
| 31642 |
user 0m0.084s |
| 31643 |
sys 0m0.000s |
| 31644 |
|
| 31645 |
Assigning a sparse associative array entry randomly into 64k cells |
| 31646 |
real 0m0.698s |
| 31647 |
user 0m0.696s |
| 31648 |
sys 0m0.004s |
| 31649 |
|
| 31650 |
Reading sparse associative index array entry |
| 31651 |
real 0m0.083s |
| 31652 |
user 0m0.084s |
| 31653 |
sys 0m0.000s</programlisting></para> |
| 31654 |
</note> |
| 31655 |
|
| 31656 |
|
| 31657 |
<para>Use the <link linkend="timref">time</link> and <link |
| 31658 |
linkend="timesref">times</link> tools to profile |
| 31659 |
computation-intensive commands. Consider rewriting time-critical |
| 31660 |
code sections in C, or even in assembler.</para> |
| 31661 |
|
| 31662 |
<para>Try to minimize file I/O. Bash is not particularly |
| 31663 |
efficient at handling files, so consider using |
| 31664 |
more appropriate tools for this within the script, |
| 31665 |
such as <link linkend="awkref">awk</link> or <link |
| 31666 |
linkend="perlref">Perl</link>.</para> |
| 31667 |
|
| 31668 |
<para>Write your scripts in a modular and coherent form, |
| 31669 |
<footnote><para>This usually means liberal use of |
| 31670 |
<link linkend="functionref">functions</link>.</para></footnote> |
| 31671 |
so they can be reorganized and tightened up as necessary. Some |
| 31672 |
of the optimization techniques applicable to high-level |
| 31673 |
languages may work for scripts, but others, such as |
| 31674 |
<firstterm>loop unrolling</firstterm>, are mostly |
| 31675 |
irrelevant. Above all, use common sense.</para> |
| 31676 |
|
| 31677 |
<para>For an excellent demonstration of how optimization can |
| 31678 |
dramatically reduce the execution time of a script, see <xref |
| 31679 |
linkend="monthlypmt">.</para> |
| 31680 |
|
| 31681 |
</sect1> <!-- Optimizations --> |
| 31682 |
|
| 31683 |
|
| 31684 |
|
| 31685 |
<sect1 id="assortedtips"> |
| 31686 |
<title>Assorted Tips</title> |
| 31687 |
|
| 31688 |
|
| 31689 |
<sect2> |
| 31690 |
<title>Ideas for more powerful scripts</title> |
| 31691 |
|
| 31692 |
<itemizedlist> |
| 31693 |
|
| 31694 |
<listitem> |
| 31695 |
|
| 31696 |
<para><anchor id="pseudocoderef"></para> |
| 31697 |
<para>You have a problem that you want to solve by writing a Bash |
| 31698 |
script. Unfortunately, you don't know quite where to start. |
| 31699 |
One method is to plunge right in and code those parts |
| 31700 |
of the script that come easily, and write the hard parts as |
| 31701 |
<firstterm>pseudo-code</firstterm>.</para> |
| 31702 |
|
| 31703 |
<para><programlisting>#!/bin/bash |
| 31704 |
|
| 31705 |
ARGCOUNT=1 # Need name as argument. |
| 31706 |
E_WRONGARGS=65 |
| 31707 |
|
| 31708 |
if [ number-of-arguments is-not-equal-to "$ARGCOUNT" ] |
| 31709 |
# ^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ |
| 31710 |
# Can't figure out how to code this . . . |
| 31711 |
#+ . . . so write it in pseudo-code. |
| 31712 |
|
| 31713 |
then |
| 31714 |
echo "Usage: name-of-script name" |
| 31715 |
# ^^^^^^^^^^^^^^ More pseudo-code. |
| 31716 |
exit $E_WRONGARGS |
| 31717 |
fi |
| 31718 |
|
| 31719 |
. . . |
| 31720 |
|
| 31721 |
exit 0 |
| 31722 |
|
| 31723 |
|
| 31724 |
# Later on, substitute working code for the pseudo-code. |
| 31725 |
|
| 31726 |
# Line 6 becomes: |
| 31727 |
if [ $# -ne "$ARGCOUNT" ] |
| 31728 |
|
| 31729 |
# Line 12 becomes: |
| 31730 |
echo "Usage: `basename $0` name"</programlisting></para> |
| 31731 |
|
| 31732 |
<para>For an example of using pseudo-code, see the <link |
| 31733 |
linkend="newtonsqrt">Square Root</link> exercise.</para> |
| 31734 |
|
| 31735 |
</listitem> |
| 31736 |
|
| 31737 |
|
| 31738 |
<listitem> |
| 31739 |
|
| 31740 |
<para><anchor id="trackingscr"></para> |
| 31741 |
<para>To keep a record of which user scripts have run |
| 31742 |
during a particular session or over a number of sessions, |
| 31743 |
add the following lines to each script you want to keep track |
| 31744 |
of. This will keep a continuing file record of the script |
| 31745 |
names and invocation times. </para> |
| 31746 |
|
| 31747 |
<para> |
| 31748 |
<programlisting># Append (>>) following to end of each script tracked. |
| 31749 |
|
| 31750 |
whoami>> $SAVE_FILE # User invoking the script. |
| 31751 |
echo $0>> $SAVE_FILE # Script name. |
| 31752 |
date>> $SAVE_FILE # Date and time. |
| 31753 |
echo>> $SAVE_FILE # Blank line as separator. |
| 31754 |
|
| 31755 |
# Of course, SAVE_FILE defined and exported as environmental variable in ~/.bashrc |
| 31756 |
#+ (something like ~/.scripts-run)</programlisting> |
| 31757 |
</para> |
| 31758 |
</listitem> |
| 31759 |
|
| 31760 |
<listitem> |
| 31761 |
<para><anchor id="prependref"></para> |
| 31762 |
<para>The <token>>></token> operator |
| 31763 |
<firstterm>appends</firstterm> lines to a file. |
| 31764 |
What if you wish to <firstterm>prepend</firstterm> a |
| 31765 |
line to an existing file, that is, to paste it in at the |
| 31766 |
beginning?</para> |
| 31767 |
|
| 31768 |
<para> |
| 31769 |
<programlisting>file=data.txt |
| 31770 |
title="***This is the title line of data text file***" |
| 31771 |
|
| 31772 |
echo $title | cat - $file >$file.new |
| 31773 |
# "cat -" concatenates stdout to $file. |
| 31774 |
# End result is |
| 31775 |
#+ to write a new file with $title appended at *beginning*.</programlisting> |
| 31776 |
</para> |
| 31777 |
|
| 31778 |
<para>This is a simplified variant of the <xref |
| 31779 |
linkend="prependex"> script given earlier. And, of course, |
| 31780 |
<link linkend="sedref">sed</link> can also do this.</para> |
| 31781 |
|
| 31782 |
|
| 31783 |
</listitem> |
| 31784 |
|
| 31785 |
<listitem> |
| 31786 |
<para><anchor id="scriptasemb"></para> |
| 31787 |
<para>A shell script may act as an embedded command inside |
| 31788 |
another shell script, a <firstterm>Tcl</firstterm> or |
| 31789 |
<firstterm>wish</firstterm> script, or even a <link |
| 31790 |
linkend="makefileref">Makefile</link>. It can be invoked |
| 31791 |
as an external shell command in a C program using the |
| 31792 |
<replaceable>system()</replaceable> call, i.e., |
| 31793 |
<replaceable>system("script_name");</replaceable>.</para> |
| 31794 |
</listitem> |
| 31795 |
|
| 31796 |
<listitem> |
| 31797 |
<para><anchor id="setvaremb"></para> |
| 31798 |
<para>Setting a variable to the contents of an embedded |
| 31799 |
<firstterm>sed</firstterm> or <firstterm>awk</firstterm> |
| 31800 |
script increases the readability of the surrounding <link |
| 31801 |
linkend="shwrapper">shell wrapper</link>. See <xref |
| 31802 |
linkend="mailformat"> and <xref linkend="coltotaler3">.</para> |
| 31803 |
</listitem> |
| 31804 |
|
| 31805 |
<listitem> |
| 31806 |
<para><anchor id="libroutines"></para> |
| 31807 |
<para>Put together files containing your favorite and most useful |
| 31808 |
definitions and functions. As necessary, |
| 31809 |
<quote>include</quote> one or more of these |
| 31810 |
<quote>library files</quote> in scripts with either the |
| 31811 |
<link linkend="dotref">dot</link> (<command>.</command>) |
| 31812 |
or <link linkend="sourceref">source</link> command.</para> |
| 31813 |
|
| 31814 |
<para> |
| 31815 |
<programlisting># SCRIPT LIBRARY |
| 31816 |
# ------ ------- |
| 31817 |
|
| 31818 |
# Note: |
| 31819 |
# No "#!" here. |
| 31820 |
# No "live code" either. |
| 31821 |
|
| 31822 |
|
| 31823 |
# Useful variable definitions |
| 31824 |
|
| 31825 |
ROOT_UID=0 # Root has $UID 0. |
| 31826 |
E_NOTROOT=101 # Not root user error. |
| 31827 |
MAXRETVAL=255 # Maximum (positive) return value of a function. |
| 31828 |
SUCCESS=0 |
| 31829 |
FAILURE=-1 |
| 31830 |
|
| 31831 |
|
| 31832 |
|
| 31833 |
# Functions |
| 31834 |
|
| 31835 |
Usage () # "Usage:" message. |
| 31836 |
{ |
| 31837 |
if [ -z "$1" ] # No arg passed. |
| 31838 |
then |
| 31839 |
msg=filename |
| 31840 |
else |
| 31841 |
msg=$@ |
| 31842 |
fi |
| 31843 |
|
| 31844 |
echo "Usage: `basename $0` "$msg"" |
| 31845 |
} |
| 31846 |
|
| 31847 |
|
| 31848 |
Check_if_root () # Check if root running script. |
| 31849 |
{ # From "ex39.sh" example. |
| 31850 |
if [ "$UID" -ne "$ROOT_UID" ] |
| 31851 |
then |
| 31852 |
echo "Must be root to run this script." |
| 31853 |
exit $E_NOTROOT |
| 31854 |
fi |
| 31855 |
} |
| 31856 |
|
| 31857 |
|
| 31858 |
CreateTempfileName () # Creates a "unique" temp filename. |
| 31859 |
{ # From "ex51.sh" example. |
| 31860 |
prefix=temp |
| 31861 |
suffix=`eval date +%s` |
| 31862 |
Tempfilename=$prefix.$suffix |
| 31863 |
} |
| 31864 |
|
| 31865 |
|
| 31866 |
isalpha2 () # Tests whether *entire string* is alphabetic. |
| 31867 |
{ # From "isalpha.sh" example. |
| 31868 |
[ $# -eq 1 ] || return $FAILURE |
| 31869 |
|
| 31870 |
case $1 in |
| 31871 |
*[!a-zA-Z]*|"") return $FAILURE;; |
| 31872 |
*) return $SUCCESS;; |
| 31873 |
esac # Thanks, S.C. |
| 31874 |
} |
| 31875 |
|
| 31876 |
|
| 31877 |
abs () # Absolute value. |
| 31878 |
{ # Caution: Max return value = 255. |
| 31879 |
E_ARGERR=-999999 |
| 31880 |
|
| 31881 |
if [ -z "$1" ] # Need arg passed. |
| 31882 |
then |
| 31883 |
return $E_ARGERR # Obvious error value returned. |
| 31884 |
fi |
| 31885 |
|
| 31886 |
if [ "$1" -ge 0 ] # If non-negative, |
| 31887 |
then # |
| 31888 |
absval=$1 # stays as-is. |
| 31889 |
else # Otherwise, |
| 31890 |
let "absval = (( 0 - $1 ))" # change sign. |
| 31891 |
fi |
| 31892 |
|
| 31893 |
return $absval |
| 31894 |
} |
| 31895 |
|
| 31896 |
|
| 31897 |
tolower () # Converts string(s) passed as argument(s) |
| 31898 |
{ #+ to lowercase. |
| 31899 |
|
| 31900 |
if [ -z "$1" ] # If no argument(s) passed, |
| 31901 |
then #+ send error message |
| 31902 |
echo "(null)" #+ (C-style void-pointer error message) |
| 31903 |
return #+ and return from function. |
| 31904 |
fi |
| 31905 |
|
| 31906 |
echo "$@" | tr A-Z a-z |
| 31907 |
# Translate all passed arguments ($@). |
| 31908 |
|
| 31909 |
return |
| 31910 |
|
| 31911 |
# Use command substitution to set a variable to function output. |
| 31912 |
# For example: |
| 31913 |
# oldvar="A seT of miXed-caSe LEtTerS" |
| 31914 |
# newvar=`tolower "$oldvar"` |
| 31915 |
# echo "$newvar" # a set of mixed-case letters |
| 31916 |
# |
| 31917 |
# Exercise: Rewrite this function to change lowercase passed argument(s) |
| 31918 |
# to uppercase ... toupper() [easy]. |
| 31919 |
}</programlisting> |
| 31920 |
</para> |
| 31921 |
|
| 31922 |
</listitem> |
| 31923 |
|
| 31924 |
<listitem> |
| 31925 |
<para><anchor id="commenth"></para> |
| 31926 |
<para>Use special-purpose comment headers to increase clarity |
| 31927 |
and legibility in scripts.</para> |
| 31928 |
<para><programlisting>## Caution. |
| 31929 |
rm -rf *.zzy ## The "-rf" options to "rm" are very dangerous, |
| 31930 |
##+ especially with wild cards. |
| 31931 |
|
| 31932 |
#+ Line continuation. |
| 31933 |
# This is line 1 |
| 31934 |
#+ of a multi-line comment, |
| 31935 |
#+ and this is the final line. |
| 31936 |
|
| 31937 |
#* Note. |
| 31938 |
|
| 31939 |
#o List item. |
| 31940 |
|
| 31941 |
#> Another point of view. |
| 31942 |
while [ "$var1" != "end" ] #> while test "$var1" != "end"</programlisting></para> |
| 31943 |
|
| 31944 |
</listitem> |
| 31945 |
|
| 31946 |
<listitem> |
| 31947 |
<para><anchor id="progbar"></para> |
| 31948 |
<para>Dotan Barak contributes template code for a |
| 31949 |
<firstterm>progress bar</firstterm> in a script.</para> |
| 31950 |
|
| 31951 |
<example id="progressbar"> |
| 31952 |
<title>A Progress Bar</title> |
| 31953 |
<programlisting>&progressbar;</programlisting> |
| 31954 |
</example> |
| 31955 |
</listitem> |
| 31956 |
|
| 31957 |
<listitem> |
| 31958 |
<para><anchor id="comoutbl"></para> |
| 31959 |
<para>A particularly clever use of <link |
| 31960 |
linkend="testconstructs1">if-test</link> constructs |
| 31961 |
is for comment blocks.</para> |
| 31962 |
|
| 31963 |
<para> |
| 31964 |
<programlisting>#!/bin/bash |
| 31965 |
|
| 31966 |
COMMENT_BLOCK= |
| 31967 |
# Try setting the above variable to some value |
| 31968 |
#+ for an unpleasant surprise. |
| 31969 |
|
| 31970 |
if [ $COMMENT_BLOCK ]; then |
| 31971 |
|
| 31972 |
Comment block -- |
| 31973 |
================================= |
| 31974 |
This is a comment line. |
| 31975 |
This is another comment line. |
| 31976 |
This is yet another comment line. |
| 31977 |
================================= |
| 31978 |
|
| 31979 |
echo "This will not echo." |
| 31980 |
|
| 31981 |
Comment blocks are error-free! Whee! |
| 31982 |
|
| 31983 |
fi |
| 31984 |
|
| 31985 |
echo "No more comments, please." |
| 31986 |
|
| 31987 |
exit 0</programlisting> |
| 31988 |
</para> |
| 31989 |
|
| 31990 |
<para>Compare this with <link linkend="cblock1">using |
| 31991 |
here documents to comment out code blocks</link>.</para> |
| 31992 |
|
| 31993 |
</listitem> |
| 31994 |
|
| 31995 |
<listitem> |
| 31996 |
<para><anchor id="intparam"></para> |
| 31997 |
<para>Using the <link linkend="xstatvarref">$? exit status |
| 31998 |
variable</link>, a script may test if a parameter contains |
| 31999 |
only digits, so it can be treated as an integer.</para> |
| 32000 |
|
| 32001 |
<para> |
| 32002 |
<programlisting>#!/bin/bash |
| 32003 |
|
| 32004 |
SUCCESS=0 |
| 32005 |
E_BADINPUT=85 |
| 32006 |
|
| 32007 |
test "$1" -ne 0 -o "$1" -eq 0 2>/dev/null |
| 32008 |
# An integer is either equal to 0 or not equal to 0. |
| 32009 |
# 2>/dev/null suppresses error message. |
| 32010 |
|
| 32011 |
if [ $? -ne "$SUCCESS" ] |
| 32012 |
then |
| 32013 |
echo "Usage: `basename $0` integer-input" |
| 32014 |
exit $E_BADINPUT |
| 32015 |
fi |
| 32016 |
|
| 32017 |
let "sum = $1 + 25" # Would give error if $1 not integer. |
| 32018 |
echo "Sum = $sum" |
| 32019 |
|
| 32020 |
# Any variable, not just a command-line parameter, can be tested this way. |
| 32021 |
|
| 32022 |
exit 0</programlisting> |
| 32023 |
</para> |
| 32024 |
</listitem> |
| 32025 |
|
| 32026 |
|
| 32027 |
<listitem> |
| 32028 |
|
| 32029 |
<para><anchor id="rvt">The 0 - 255 range for function return |
| 32030 |
values is a severe limitation. Global variables and |
| 32031 |
other workarounds are often problematic. An alternative |
| 32032 |
method for a function to communicate a value back to |
| 32033 |
the main body of the script is to have the function |
| 32034 |
write to <filename>stdout</filename> (usually with |
| 32035 |
<link linkend="echoref">echo</link>) the <quote>return |
| 32036 |
value,</quote> and assign this to a variable. This is |
| 32037 |
actually a variant of <link linkend="commandsubref">command |
| 32038 |
substitution.</link></para> |
| 32039 |
|
| 32040 |
<example id="multiplication"> |
| 32041 |
<title>Return value trickery</title> |
| 32042 |
<programlisting>&multiplication;</programlisting> |
| 32043 |
</example> |
| 32044 |
|
| 32045 |
<para>The same technique also works for alphanumeric |
| 32046 |
strings. This means that a function can <quote>return</quote> |
| 32047 |
a non-numeric value.</para> |
| 32048 |
|
| 32049 |
<para> |
| 32050 |
<programlisting>capitalize_ichar () # Capitalizes initial character |
| 32051 |
{ #+ of argument string(s) passed. |
| 32052 |
|
| 32053 |
string0="$@" # Accepts multiple arguments. |
| 32054 |
|
| 32055 |
firstchar=${string0:0:1} # First character. |
| 32056 |
string1=${string0:1} # Rest of string(s). |
| 32057 |
|
| 32058 |
FirstChar=`echo "$firstchar" | tr a-z A-Z` |
| 32059 |
# Capitalize first character. |
| 32060 |
|
| 32061 |
echo "$FirstChar$string1" # Output to stdout. |
| 32062 |
|
| 32063 |
} |
| 32064 |
|
| 32065 |
newstring=`capitalize_ichar "every sentence should start with a capital letter."` |
| 32066 |
echo "$newstring" # Every sentence should start with a capital letter.</programlisting> |
| 32067 |
</para> |
| 32068 |
|
| 32069 |
<para>It is even possible for a function to <quote>return</quote> |
| 32070 |
multiple values with this method.</para> |
| 32071 |
|
| 32072 |
<example id="sumproduct"> |
| 32073 |
<title>Even more return value trickery</title> |
| 32074 |
<programlisting>&sumproduct;</programlisting> |
| 32075 |
</example> |
| 32076 |
|
| 32077 |
<caution><para><anchor id="rvtcaution">There can be only |
| 32078 |
<command>one</command> <firstterm>echo</firstterm> statement |
| 32079 |
in the function for this to work. If you alter the previous |
| 32080 |
example:</para> |
| 32081 |
|
| 32082 |
<para><programlisting>sum_and_product () |
| 32083 |
{ |
| 32084 |
echo "This is the sum_and_product function." # This messes things up! |
| 32085 |
echo $(( $1 + $2 )) $(( $1 * $2 )) |
| 32086 |
} |
| 32087 |
... |
| 32088 |
retval=`sum_and_product $first $second` # Assigns output of function. |
| 32089 |
# Now, this will not work correctly.</programlisting></para></caution> |
| 32090 |
|
| 32091 |
|
| 32092 |
</listitem> |
| 32093 |
|
| 32094 |
<listitem> |
| 32095 |
<para><anchor id="passarray"></para> |
| 32096 |
<para>Next in our bag of tricks are techniques for passing |
| 32097 |
an <link linkend="arrayref">array</link> to a |
| 32098 |
<link linkend="functionref">function</link>, then |
| 32099 |
<quote>returning</quote> an array back to the main body of |
| 32100 |
the script.</para> |
| 32101 |
|
| 32102 |
<para>Passing an array involves loading the space-separated |
| 32103 |
elements of the array into a variable with <link |
| 32104 |
linkend="commandsubref">command substitution</link>. <anchor |
| 32105 |
id="retarray">Getting an array back as the <quote>return |
| 32106 |
value</quote> from a function uses the previously mentioned |
| 32107 |
strategem of <link linkend="echoref">echoing</link> the |
| 32108 |
array in the function, then invoking command substitution |
| 32109 |
and the <command>( ... )</command> operator to assign it to |
| 32110 |
an array.</para> |
| 32111 |
|
| 32112 |
<example id="arrfunc"> |
| 32113 |
<title>Passing and returning arrays</title> |
| 32114 |
<programlisting>&arrfunc;</programlisting> |
| 32115 |
</example> |
| 32116 |
|
| 32117 |
<para>For a more elaborate example of passing arrays to |
| 32118 |
functions, see <xref linkend="lifeslow">.</para> |
| 32119 |
|
| 32120 |
</listitem> |
| 32121 |
|
| 32122 |
<listitem> |
| 32123 |
<para><anchor id="cstyle"></para> |
| 32124 |
<para>Using the <link linkend="dblparens">double-parentheses |
| 32125 |
construct</link>, it is possible to use C-style syntax |
| 32126 |
for setting and incrementing/decrementing variables |
| 32127 |
and in <link linkend="forloopref1">for</link> and <link |
| 32128 |
linkend="whileloopref">while</link> loops. See <xref |
| 32129 |
linkend="forloopc"> and <xref linkend="whloopc">.</para> |
| 32130 |
</listitem> |
| 32131 |
|
| 32132 |
<listitem> |
| 32133 |
<para><anchor id="setpum"></para> |
| 32134 |
<para>Setting the <link linkend="pathref">path</link> and <link |
| 32135 |
linkend="umaskref">umask</link> at the beginning of a script makes |
| 32136 |
it more <link linkend="portabilityissues">portable</link> |
| 32137 |
-- more likely to run on a <quote>foreign</quote> machine |
| 32138 |
whose user may have bollixed up the <varname>$PATH</varname> |
| 32139 |
and <command>umask</command>. |
| 32140 |
<programlisting>#!/bin/bash |
| 32141 |
PATH=/bin:/usr/bin:/usr/local/bin ; export PATH |
| 32142 |
umask 022 # Files that the script creates will have 755 permission. |
| 32143 |
|
| 32144 |
# Thanks to Ian D. Allen, for this tip.</programlisting></para> |
| 32145 |
</listitem> |
| 32146 |
|
| 32147 |
<listitem> |
| 32148 |
<para><anchor id="filteroutp"></para> |
| 32149 |
<para>A useful scripting technique is to |
| 32150 |
<emphasis>repeatedly</emphasis> feed the output of a filter |
| 32151 |
(by piping) back to the <emphasis>same filter</emphasis>, but |
| 32152 |
with a different set of arguments and/or options. Especially |
| 32153 |
suitable for this are <link linkend="trref">tr</link> and |
| 32154 |
<link linkend="grepref">grep</link>.</para> |
| 32155 |
|
| 32156 |
<para> |
| 32157 |
<programlisting># From "wstrings.sh" example. |
| 32158 |
|
| 32159 |
wlist=`strings "$1" | tr A-Z a-z | tr '[:space:]' Z | \ |
| 32160 |
tr -cs '[:alpha:]' Z | tr -s '\173-\377' Z | tr Z ' '`</programlisting> |
| 32161 |
</para> |
| 32162 |
|
| 32163 |
<example id="agram"> |
| 32164 |
<title>Fun with anagrams</title> |
| 32165 |
<programlisting>&agram;</programlisting> |
| 32166 |
</example> |
| 32167 |
|
| 32168 |
<para>See also <xref linkend="constat">, <xref |
| 32169 |
linkend="cryptoquote">, and <xref linkend="soundex">.</para> |
| 32170 |
|
| 32171 |
</listitem> |
| 32172 |
|
| 32173 |
<listitem> |
| 32174 |
<para><anchor id="commblahd"></para> |
| 32175 |
<para>Use <quote><link linkend="anonheredoc0">anonymous here |
| 32176 |
documents</link></quote> to comment out blocks of code, |
| 32177 |
to save having to individually comment out each line with |
| 32178 |
a <token>#</token>. See <xref linkend="commentblock">.</para> |
| 32179 |
</listitem> |
| 32180 |
|
| 32181 |
<listitem> |
| 32182 |
<para><anchor id="whatisref3"></para> |
| 32183 |
<para>Running a script on a machine that relies on a command |
| 32184 |
that might not be installed is dangerous. Use <link |
| 32185 |
linkend="whatisref">whatis</link> to avoid potential problems |
| 32186 |
with this.</para> |
| 32187 |
|
| 32188 |
<para> |
| 32189 |
<programlisting>CMD=command1 # First choice. |
| 32190 |
PlanB=command2 # Fallback option. |
| 32191 |
|
| 32192 |
command_test=$(whatis "$CMD" | grep 'nothing appropriate') |
| 32193 |
# If 'command1' not found on system , 'whatis' will return |
| 32194 |
#+ "command1: nothing appropriate." |
| 32195 |
# |
| 32196 |
# A safer alternative is: |
| 32197 |
# command_test=$(whereis "$CMD" | grep \/) |
| 32198 |
# But then the sense of the following test would have to be reversed, |
| 32199 |
#+ since the $command_test variable holds content only if |
| 32200 |
#+ the $CMD exists on the system. |
| 32201 |
# (Thanks, bojster.) |
| 32202 |
|
| 32203 |
|
| 32204 |
if [[ -z "$command_test" ]] # Check whether command present. |
| 32205 |
then |
| 32206 |
$CMD option1 option2 # Run command1 with options. |
| 32207 |
else # Otherwise, |
| 32208 |
$PlanB #+ run command2. |
| 32209 |
fi</programlisting> |
| 32210 |
</para> |
| 32211 |
</listitem> |
| 32212 |
|
| 32213 |
<listitem> |
| 32214 |
|
| 32215 |
<para><anchor id="ifgrepfix"></para> |
| 32216 |
<para>An <link linkend="ifgrepref">if-grep test</link> may not |
| 32217 |
return expected results in an error case, when text is output to |
| 32218 |
<filename>stderr</filename>, rather that |
| 32219 |
<filename>stdout</filename>. |
| 32220 |
<programlisting>if ls -l nonexistent_filename | grep -q 'No such file or directory' |
| 32221 |
then echo "File \"nonexistent_filename\" does not exist." |
| 32222 |
fi</programlisting></para> |
| 32223 |
|
| 32224 |
<para><link linkend="ioredirref">Redirecting</link> |
| 32225 |
<filename>stderr</filename> to <filename>stdout</filename> fixes |
| 32226 |
this. |
| 32227 |
<programlisting>if ls -l nonexistent_filename 2>&1 | grep -q 'No such file or directory' |
| 32228 |
# ^^^^ |
| 32229 |
then echo "File \"nonexistent_filename\" does not exist." |
| 32230 |
fi |
| 32231 |
|
| 32232 |
# Thanks, Chris Martin, for pointing this out.</programlisting></para> |
| 32233 |
|
| 32234 |
</listitem> |
| 32235 |
|
| 32236 |
|
| 32237 |
<listitem> |
| 32238 |
|
| 32239 |
<para><anchor id="subshtmp"> |
| 32240 |
If you absolutely must access a subshell variable outside the |
| 32241 |
subshell, here's a way to do it. |
| 32242 |
<programlisting>TMPFILE=tmpfile # Create a temp file to store the variable. |
| 32243 |
|
| 32244 |
( # Inside the subshell ... |
| 32245 |
inner_variable=Inner |
| 32246 |
echo $inner_variable |
| 32247 |
echo $inner_variable >>$TMPFILE # Append to temp file. |
| 32248 |
) |
| 32249 |
|
| 32250 |
# Outside the subshell ... |
| 32251 |
|
| 32252 |
echo; echo "-----"; echo |
| 32253 |
echo $inner_variable # Null, as expected. |
| 32254 |
echo "-----"; echo |
| 32255 |
|
| 32256 |
# Now ... |
| 32257 |
read inner_variable <$TMPFILE # Read back shell variable. |
| 32258 |
rm -f "$TMPFILE" # Get rid of temp file. |
| 32259 |
echo "$inner_variable" # It's an ugly kludge, but it works.</programlisting> |
| 32260 |
</para> |
| 32261 |
|
| 32262 |
</listitem> |
| 32263 |
|
| 32264 |
|
| 32265 |
<listitem> |
| 32266 |
<para><anchor id="runpartsref2"></para> |
| 32267 |
<para>The <link linkend="runpartsref">run-parts</link> |
| 32268 |
command is handy for running a set of command |
| 32269 |
scripts in a particular sequence, especially in |
| 32270 |
combination with <link linkend="cronref">cron</link> or |
| 32271 |
<link linkend="atref">at</link>.</para> |
| 32272 |
</listitem> |
| 32273 |
|
| 32274 |
|
| 32275 |
<listitem> |
| 32276 |
<para><anchor id="rcsref"></para> |
| 32277 |
<para>For doing multiple revisions on a complex script, use the |
| 32278 |
<firstterm>rcs</firstterm> Revision Control System package.</para> |
| 32279 |
|
| 32280 |
<para> Among other benefits of this is automatically updated ID |
| 32281 |
header tags. The <command>co</command> command in |
| 32282 |
<firstterm>rcs</firstterm> does a parameter replacement of |
| 32283 |
certain reserved key words, for example, replacing |
| 32284 |
<parameter># $Id$</parameter> in a script with something like: |
| 32285 |
<programlisting># $Id: hello-world.sh,v 1.1 2004/10/16 02:43:05 bozo Exp $</programlisting></para> |
| 32286 |
|
| 32287 |
</listitem> |
| 32288 |
|
| 32289 |
|
| 32290 |
</itemizedlist> |
| 32291 |
|
| 32292 |
</sect2> |
| 32293 |
|
| 32294 |
|
| 32295 |
<sect2> |
| 32296 |
<title>Widgets</title> |
| 32297 |
|
| 32298 |
<para><anchor id="widgetref"></para> |
| 32299 |
<para>It would be nice to be able to invoke X-Windows widgets |
| 32300 |
from a shell script. There happen to exist several packages |
| 32301 |
that purport to do so, namely <firstterm>Xscript</firstterm>, |
| 32302 |
<firstterm>Xmenu</firstterm>, and <firstterm>widtools</firstterm>. |
| 32303 |
The first two of these no longer seem |
| 32304 |
to be maintained. Fortunately, it is still |
| 32305 |
possible to obtain <firstterm>widtools</firstterm> <ulink |
| 32306 |
url="http://www.batse.msfc.nasa.gov/~mallozzi/home/software/xforms/src/widtools-2.0.tgz">here</ulink>. |
| 32307 |
</para> |
| 32308 |
|
| 32309 |
<caution><para>The <firstterm>widtools</firstterm> (widget tools) |
| 32310 |
package requires the <firstterm>XForms</firstterm> library to |
| 32311 |
be installed. Additionally, the <link |
| 32312 |
linkend="makefileref">Makefile</link> needs some judicious |
| 32313 |
editing before the package will build on a typical Linux |
| 32314 |
system. Finally, three of the six widgets offered do not work |
| 32315 |
(and, in fact, segfault).</para></caution> |
| 32316 |
|
| 32317 |
|
| 32318 |
<para><anchor id="dialogref"></para> |
| 32319 |
|
| 32320 |
<para>The <firstterm>dialog</firstterm> family of tools offers a method |
| 32321 |
of calling <quote>dialog</quote> widgets from a shell script. The |
| 32322 |
original <firstterm>dialog</firstterm> utility works in a text |
| 32323 |
console, but its successors, <firstterm>gdialog</firstterm>, |
| 32324 |
<firstterm>Xdialog</firstterm>, and <firstterm>kdialog</firstterm> |
| 32325 |
use X-Windows-based widget sets.</para> |
| 32326 |
|
| 32327 |
<example id="dialog"> |
| 32328 |
<title>Widgets invoked from a shell script</title> |
| 32329 |
<programlisting>&dialog;</programlisting> |
| 32330 |
</example> |
| 32331 |
|
| 32332 |
<para><anchor id="xmessageref2"> |
| 32333 |
The <link linkend="xmessageref">xmessage</link> command is |
| 32334 |
a simple method of popping up a message/query window. For |
| 32335 |
example: |
| 32336 |
<programlisting>xmessage Fatal error in script! -button exit</programlisting> |
| 32337 |
</para> |
| 32338 |
|
| 32339 |
<para><anchor id="zenityref2"> |
| 32340 |
The latest entry in the widget sweepstakes is |
| 32341 |
<link linkend="zenityref">zenity</link>. |
| 32342 |
This utility pops up |
| 32343 |
<firstterm>GTK+</firstterm> dialog widgets-and-windows, |
| 32344 |
and it works very nicely within a script. |
| 32345 |
<programlisting>get_info () |
| 32346 |
{ |
| 32347 |
zenity --entry # Pops up query window . . . |
| 32348 |
#+ and prints user entry to stdout. |
| 32349 |
|
| 32350 |
# Also try the --calendar and --scale options. |
| 32351 |
} |
| 32352 |
|
| 32353 |
answer=$( get_info ) # Capture stdout in $answer variable. |
| 32354 |
|
| 32355 |
echo "User entered: "$answer""</programlisting> |
| 32356 |
</para> |
| 32357 |
|
| 32358 |
<para>For other methods of scripting with widgets, try |
| 32359 |
<firstterm>Tk</firstterm> or <firstterm>wish</firstterm> |
| 32360 |
(<firstterm>Tcl</firstterm> derivatives), |
| 32361 |
<firstterm>PerlTk</firstterm> (<firstterm>Perl</firstterm> |
| 32362 |
with <firstterm>Tk</firstterm> extensions), |
| 32363 |
<firstterm>tksh</firstterm> (<firstterm>ksh</firstterm> |
| 32364 |
with <firstterm>Tk</firstterm> extensions), |
| 32365 |
<firstterm>XForms4Perl</firstterm> |
| 32366 |
(<firstterm>Perl</firstterm> with |
| 32367 |
<firstterm>XForms</firstterm> extensions), |
| 32368 |
<firstterm>Gtk-Perl</firstterm> (<firstterm>Perl</firstterm> |
| 32369 |
with <firstterm>Gtk</firstterm> extensions), or |
| 32370 |
<firstterm>PyQt</firstterm> (<firstterm>Python</firstterm> |
| 32371 |
with <firstterm>Qt</firstterm> extensions).</para> |
| 32372 |
|
| 32373 |
</sect2> |
| 32374 |
|
| 32375 |
|
| 32376 |
|
| 32377 |
</sect1> <!-- Assorted Tips --> |
| 32378 |
|
| 32379 |
|
| 32380 |
|
| 32381 |
<sect1 id="securityissues"> |
| 32382 |
<title>Security Issues</title> |
| 32383 |
|
| 32384 |
<sect2 id="infectedscripts"> |
| 32385 |
<title>Infected Shell Scripts</title> |
| 32386 |
|
| 32387 |
<para><anchor id="infectedscripts1"></para> |
| 32388 |
<para>A brief warning about script security is indicated. |
| 32389 |
A shell script may contain a <firstterm>worm</firstterm>, |
| 32390 |
<firstterm>trojan</firstterm>, or even a |
| 32391 |
<firstterm>virus</firstterm>. For that reason, never run |
| 32392 |
as <firstterm>root</firstterm> a script (or permit it to |
| 32393 |
be inserted into the system startup scripts in <filename |
| 32394 |
class="directory">/etc/rc.d</filename>) unless you have obtained |
| 32395 |
said script from a trusted source or you have carefully analyzed |
| 32396 |
it to make certain it does nothing harmful.</para> |
| 32397 |
|
| 32398 |
<para>Various researchers at Bell Labs and other sites, including M. |
| 32399 |
Douglas McIlroy, Tom Duff, and Fred Cohen have investigated the |
| 32400 |
implications of shell script viruses. They conclude that it is |
| 32401 |
all too easy for even a novice, a <quote>script kiddie,</quote> |
| 32402 |
to write one. |
| 32403 |
<footnote><para>See Marius van Oers' article, <ulink |
| 32404 |
url="http://www.virusbtn.com/magazine/archives/200204/malshell.xml">Unix |
| 32405 |
Shell Scripting Malware</ulink>, and also the |
| 32406 |
<link linkend="denningref"><emphasis>Denning</emphasis> |
| 32407 |
reference</link> in the |
| 32408 |
<firstterm>bibliography</firstterm>.</para></footnote> |
| 32409 |
</para> |
| 32410 |
|
| 32411 |
<para>Here is yet another reason to learn scripting. Being able to |
| 32412 |
look at and understand scripts may protect your system from |
| 32413 |
being compromised by a rogue script.</para> |
| 32414 |
|
| 32415 |
</sect2> <!-- Infected Shell Scripts --> |
| 32416 |
|
| 32417 |
<sect2 id="hidingsource"> |
| 32418 |
<title>Hiding Shell Script Source</title> |
| 32419 |
|
| 32420 |
<para>For security purposes, it may be necessary to render a script |
| 32421 |
unreadable. If only there were a utility to create a stripped |
| 32422 |
binary executable from a script. Francisco Rosales' <ulink |
| 32423 |
url="http://www.datsi.fi.upm.es/~frosal/sources/">shc -- |
| 32424 |
generic shell script compiler</ulink> does exactly that.</para> |
| 32425 |
|
| 32426 |
<para>Unfortunately, according to <ulink |
| 32427 |
url="http://www.linuxjournal.com/article/8256">an article</ulink> in |
| 32428 |
the October, 2005 <emphasis>Linux Journal</emphasis>, |
| 32429 |
the binary can, in at least some cases, be decrypted to recover |
| 32430 |
the original script source. Still, this could be a useful |
| 32431 |
method of keeping scripts secure from all but the most skilled |
| 32432 |
hackers.</para> |
| 32433 |
|
| 32434 |
</sect2> <!-- Hiding Shell Script Source --> |
| 32435 |
|
| 32436 |
|
| 32437 |
<sect2 id="securitytips"> |
| 32438 |
<title>Writing Secure Shell Scripts</title> |
| 32439 |
|
| 32440 |
<para><anchor id="securitytips1"></para> |
| 32441 |
<para><emphasis>Dan Stromberg</emphasis> suggests the following |
| 32442 |
guidelines for writing (relatively) secure shell scripts.</para> |
| 32443 |
|
| 32444 |
<para> |
| 32445 |
<itemizedlist> |
| 32446 |
|
| 32447 |
<listitem> |
| 32448 |
<para>Don't put secret data in <link |
| 32449 |
linkend="envref">environment variables</link>.</para> |
| 32450 |
</listitem> |
| 32451 |
|
| 32452 |
<listitem> |
| 32453 |
<para>Don't pass secret data in an external |
| 32454 |
command's arguments (pass them in via a <link |
| 32455 |
linkend="piperef">pipe</link> or <link |
| 32456 |
linkend="ioredirref">redirection</link> instead).</para> |
| 32457 |
</listitem> |
| 32458 |
|
| 32459 |
<listitem> |
| 32460 |
<para>Set your <link linkend="pathref">$PATH</link> |
| 32461 |
carefully. Don't just trust whatever path you |
| 32462 |
inherit from the caller if your script is running as |
| 32463 |
<firstterm>root</firstterm>. In fact, whenever you use |
| 32464 |
an environment variable inherited from the caller, think |
| 32465 |
about what could happen if the caller put something |
| 32466 |
misleading in the variable, e.g., if the caller set |
| 32467 |
<link linkend="homedirref">$HOME</link> to <filename |
| 32468 |
class="directory">/etc</filename>.</para> |
| 32469 |
</listitem> |
| 32470 |
|
| 32471 |
</itemizedlist> |
| 32472 |
</para> |
| 32473 |
|
| 32474 |
</sect2> <!-- Security Tips --> |
| 32475 |
|
| 32476 |
</sect1> <!-- Security --> |
| 32477 |
|
| 32478 |
|
| 32479 |
|
| 32480 |
<sect1 id="portabilityissues"> |
| 32481 |
<title>Portability Issues</title> |
| 32482 |
|
| 32483 |
<epigraph> |
| 32484 |
<para>It is easier to port a shell than a shell script.</para> |
| 32485 |
<para>--Larry Wall</para> |
| 32486 |
</epigraph> |
| 32487 |
|
| 32488 |
<para>This book deals specifically with Bash scripting on |
| 32489 |
a GNU/Linux system. All the same, users of <command>sh</command> |
| 32490 |
and <command>ksh</command> will find much of value here.</para> |
| 32491 |
|
| 32492 |
<para><anchor id="posix3ref">As it happens, many of the various |
| 32493 |
shells and scripting languages seem to be converging toward the |
| 32494 |
<link linkend="posix2ref">POSIX</link> 1003.2 standard. Invoking |
| 32495 |
Bash with the <option>--posix</option> option or inserting |
| 32496 |
a <command>set -o posix</command> at the head of a script |
| 32497 |
causes Bash to conform very closely to this standard. Another |
| 32498 |
alternative is to use a <firstterm>#!/bin/sh</firstterm> <link |
| 32499 |
linkend="shabangref">sha-bang header</link> in the script, |
| 32500 |
rather than <firstterm>#!/bin/bash</firstterm>. |
| 32501 |
|
| 32502 |
<footnote><para>Or, better yet, <link |
| 32503 |
linkend="envv2ref">#!/bin/env sh</link>.</para></footnote> |
| 32504 |
|
| 32505 |
Note that <filename>/bin/sh</filename> is a <link |
| 32506 |
linkend="linkref">link</link> to <filename>/bin/bash</filename> |
| 32507 |
in Linux and certain other flavors of UNIX, and a script invoked |
| 32508 |
this way disables extended Bash functionality.</para> |
| 32509 |
|
| 32510 |
<para>Most Bash scripts will run as-is under |
| 32511 |
<command>ksh</command>, and vice-versa, since Chet Ramey has |
| 32512 |
been busily porting <command>ksh</command> features to the |
| 32513 |
latest versions of Bash.</para> |
| 32514 |
|
| 32515 |
<para>On a commercial UNIX machine, scripts using GNU-specific |
| 32516 |
features of standard commands may not work. This has become less |
| 32517 |
of a problem in the last few years, as the GNU utilities have |
| 32518 |
pretty much displaced their proprietary |
| 32519 |
counterparts even on <quote>big-iron</quote> UNIX. |
| 32520 |
<ulink url="http://linux.oreillynet.com/pub/a/linux/2002/02/28/caldera.html">Caldera's |
| 32521 |
release of the source</ulink> to many of the original UNIX |
| 32522 |
utilities has accelerated the trend.</para> |
| 32523 |
|
| 32524 |
<para><anchor id="bashcompat"></para> |
| 32525 |
<para>Bash has certain features that the traditional <link |
| 32526 |
linkend="bashdef">Bourne shell</link> lacks. Among these are: |
| 32527 |
|
| 32528 |
<itemizedlist> |
| 32529 |
|
| 32530 |
<listitem> |
| 32531 |
<para>Certain extended <link |
| 32532 |
linkend="invocationoptionsref">invocation options</link></para> |
| 32533 |
</listitem> |
| 32534 |
|
| 32535 |
<listitem> |
| 32536 |
<para><link linkend="commandsubref">Command substitution</link> using |
| 32537 |
<command>$( )</command> notation</para> |
| 32538 |
</listitem> |
| 32539 |
|
| 32540 |
<listitem> |
| 32541 |
<para><link linkend="braceexpref3">Brace expansion</link></para> |
| 32542 |
</listitem> |
| 32543 |
|
| 32544 |
<listitem> |
| 32545 |
<para>Certain <link linkend="arrayref">array</link> operations, |
| 32546 |
and <link linkend="assocarr">associative arrays</link></para> |
| 32547 |
</listitem> |
| 32548 |
|
| 32549 |
<listitem> |
| 32550 |
<para>The <link linkend="dblbrackets">double brackets</link> |
| 32551 |
extended test construct</para> |
| 32552 |
</listitem> |
| 32553 |
|
| 32554 |
<listitem> |
| 32555 |
<para>The <link linkend="dblparensref">double-parentheses</link> |
| 32556 |
arithmetic-evaluation construct</para> |
| 32557 |
</listitem> |
| 32558 |
|
| 32559 |
<listitem> |
| 32560 |
<para>Certain <link linkend="stringmanip">string manipulation</link> |
| 32561 |
operations</para> |
| 32562 |
</listitem> |
| 32563 |
|
| 32564 |
<listitem> |
| 32565 |
<para><link linkend="processsubref">Process substitution</link></para> |
| 32566 |
</listitem> |
| 32567 |
|
| 32568 |
<listitem> |
| 32569 |
<para>A Regular Expression <link linkend="regexmatchref">matching |
| 32570 |
operator</link></para> |
| 32571 |
</listitem> |
| 32572 |
|
| 32573 |
<listitem> |
| 32574 |
<para>Bash-specific <link linkend="builtinref">builtins</link></para> |
| 32575 |
</listitem> |
| 32576 |
|
| 32577 |
<listitem> |
| 32578 |
<para><link linkend="coprocref">Coprocesses</link></para> |
| 32579 |
</listitem> |
| 32580 |
|
| 32581 |
</itemizedlist> |
| 32582 |
</para> |
| 32583 |
|
| 32584 |
<para>See the <ulink url="ftp://ftp.cwru.edu/pub/bash/FAQ">Bash |
| 32585 |
F.A.Q.</ulink> for a complete listing.</para> |
| 32586 |
|
| 32587 |
<sect2> <!-- A Test Suite --> |
| 32588 |
<title>A Test Suite</title> |
| 32589 |
|
| 32590 |
<para><anchor id="testsuite0">Let us illustrate some of the |
| 32591 |
incompatibilities between Bash and the classic |
| 32592 |
Bourne shell. Download and install the <ulink |
| 32593 |
url="http://freshmeat.net/projects/bournesh"><quote>Heirloom |
| 32594 |
Bourne Shell</quote></ulink> and run the following |
| 32595 |
script, first using Bash, then the classic |
| 32596 |
<firstterm>sh</firstterm>.</para> |
| 32597 |
|
| 32598 |
<example id="testsuite"> |
| 32599 |
<title>Test Suite</title> |
| 32600 |
<programlisting>&testsuite;</programlisting> |
| 32601 |
</example> |
| 32602 |
|
| 32603 |
</sect2> <!-- End: A Test Suite --> |
| 32604 |
|
| 32605 |
</sect1> <!-- Portability Issues --> |
| 32606 |
|
| 32607 |
|
| 32608 |
|
| 32609 |
<sect1 id="winscript"> |
| 32610 |
<title>Shell Scripting Under Windows</title> |
| 32611 |
|
| 32612 |
<para>Even users running <emphasis>that other</emphasis> OS can |
| 32613 |
run UNIX-like shell scripts, and therefore benefit |
| 32614 |
from many of the lessons of this book. The <ulink |
| 32615 |
url="http://sourceware.cygnus.com/cygwin/"> |
| 32616 |
Cygwin</ulink> package from Cygnus and the <ulink |
| 32617 |
url="http://www.mkssoftware.com/">MKS utilities</ulink> from |
| 32618 |
Mortice Kern Associates add shell scripting capabilities to |
| 32619 |
Windows.</para> |
| 32620 |
|
| 32621 |
<para>Another alternative is <ulink |
| 32622 |
url="http://www2.research.att.com/~gsf/download/uwin/uwin.html"> |
| 32623 |
UWIN</ulink>, written by David Korn of AT&T, of <link |
| 32624 |
linkend="kornshellref">Korn Shell</link> fame.</para> |
| 32625 |
|
| 32626 |
<para>In 2006, Microsoft released the <trademark |
| 32627 |
class=registered>Windows Powershell</trademark>, |
| 32628 |
which contains limited Bash-like command-line scripting |
| 32629 |
capabilities.</para> |
| 32630 |
|
| 32631 |
</sect1> <!-- Shell Scripting Under Windows --> |
| 32632 |
|
| 32633 |
|
| 32634 |
|
| 32635 |
</chapter> <!-- Miscellany --> |
| 32636 |
|
| 32637 |
|
| 32638 |
|
| 32639 |
<chapter id="bash2"> |
| 32640 |
<title>Bash, versions 2, 3, and 4</title> |
| 32641 |
|
| 32642 |
<sect1 id="bashver2"> |
| 32643 |
<title>Bash, version 2</title> |
| 32644 |
|
| 32645 |
<para><anchor id="bash2ref"></para> |
| 32646 |
|
| 32647 |
<para> |
| 32648 |
The current version of <firstterm>Bash</firstterm>, the one |
| 32649 |
you have running on your machine, is most likely version 2.xx.yy, |
| 32650 |
3.xx.yy, or 4.xx.yy. |
| 32651 |
<screen><prompt>bash$ </prompt><userinput>echo $BASH_VERSION</userinput> |
| 32652 |
<computeroutput>3.2.25(1)-release</computeroutput> |
| 32653 |
</screen> |
| 32654 |
</para> |
| 32655 |
|
| 32656 |
<para>The version 2 update of the classic Bash scripting language |
| 32657 |
added array variables, string and parameter expansion, and |
| 32658 |
a better method of indirect variable references, among other |
| 32659 |
features.</para> |
| 32660 |
|
| 32661 |
<example id="ex77"> |
| 32662 |
<title>String expansion</title> |
| 32663 |
<programlisting>&ex77;</programlisting> |
| 32664 |
</example> |
| 32665 |
|
| 32666 |
<para><anchor id="varrefnew"></para> |
| 32667 |
<example id="ex78"> |
| 32668 |
<title>Indirect variable references - the new way</title> |
| 32669 |
<programlisting>&ex78;</programlisting> |
| 32670 |
</example> |
| 32671 |
|
| 32672 |
<example id="resistor"> |
| 32673 |
<title>Simple database application, using indirect variable |
| 32674 |
referencing</title> |
| 32675 |
<programlisting>&resistor;</programlisting> |
| 32676 |
</example> |
| 32677 |
|
| 32678 |
<example id="cards"> |
| 32679 |
<title>Using arrays and other miscellaneous trickery |
| 32680 |
to deal four random hands from a deck of cards</title> |
| 32681 |
<programlisting>&cards;</programlisting> |
| 32682 |
</example> |
| 32683 |
|
| 32684 |
</sect1> <!-- Bash, Version 2 --> |
| 32685 |
|
| 32686 |
|
| 32687 |
|
| 32688 |
<sect1 id="bashver3"> |
| 32689 |
<title>Bash, version 3</title> |
| 32690 |
|
| 32691 |
<para><anchor id="bash3ref"></para> |
| 32692 |
|
| 32693 |
<para>On July 27, 2004, Chet Ramey released version 3 of Bash. |
| 32694 |
This update fixed quite a number of bugs and added new |
| 32695 |
features.</para> |
| 32696 |
|
| 32697 |
<para>Some of the more important added features: |
| 32698 |
|
| 32699 |
<itemizedlist> |
| 32700 |
<listitem> |
| 32701 |
<para><anchor id="braceexpref3"></para> |
| 32702 |
<para>A new, more generalized <command>{a..z}</command> <link |
| 32703 |
linkend="braceexpref">brace expansion</link> operator.</para> |
| 32704 |
|
| 32705 |
<para><programlisting>#!/bin/bash |
| 32706 |
|
| 32707 |
for i in {1..10} |
| 32708 |
# Simpler and more straightforward than |
| 32709 |
#+ for i in $(seq 10) |
| 32710 |
do |
| 32711 |
echo -n "$i " |
| 32712 |
done |
| 32713 |
|
| 32714 |
echo |
| 32715 |
|
| 32716 |
# 1 2 3 4 5 6 7 8 9 10 |
| 32717 |
|
| 32718 |
|
| 32719 |
|
| 32720 |
# Or just . . . |
| 32721 |
|
| 32722 |
echo {a..z} # a b c d e f g h i j k l m n o p q r s t u v w x y z |
| 32723 |
echo {e..m} # e f g h i j k l m |
| 32724 |
echo {z..a} # z y x w v u t s r q p o n m l k j i h g f e d c b a |
| 32725 |
# Works backwards, too. |
| 32726 |
echo {25..30} # 25 26 27 28 29 30 |
| 32727 |
echo {3..-2} # 3 2 1 0 -1 -2 |
| 32728 |
echo {X..d} # X Y Z [ ] ^ _ ` a b c d |
| 32729 |
# Shows (some of) the ASCII characters between Z and a, |
| 32730 |
#+ but don't rely on this type of behavior because . . . |
| 32731 |
echo {]..a} # {]..a} |
| 32732 |
# Why? |
| 32733 |
|
| 32734 |
|
| 32735 |
# You can tack on prefixes and suffixes. |
| 32736 |
echo "Number #"{1..4}, "..." |
| 32737 |
# Number #1, Number #2, Number #3, Number #4, ... |
| 32738 |
|
| 32739 |
|
| 32740 |
# You can concatenate brace-expansion sets. |
| 32741 |
echo {1..3}{x..z}" +" "..." |
| 32742 |
# 1x + 1y + 1z + 2x + 2y + 2z + 3x + 3y + 3z + ... |
| 32743 |
# Generates an algebraic expression. |
| 32744 |
# This could be used to find permutations. |
| 32745 |
|
| 32746 |
# You can nest brace-expansion sets. |
| 32747 |
echo {{a..c},{1..3}} |
| 32748 |
# a b c 1 2 3 |
| 32749 |
# The "comma operator" splices together strings. |
| 32750 |
|
| 32751 |
# ########## ######### ############ ########### ######### ############### |
| 32752 |
# Unfortunately, brace expansion does not lend itself to parameterization. |
| 32753 |
var1=1 |
| 32754 |
var2=5 |
| 32755 |
echo {$var1..$var2} # {1..5} |
| 32756 |
|
| 32757 |
|
| 32758 |
# Yet, as Emiliano G. points out, using "eval" overcomes this limitation. |
| 32759 |
|
| 32760 |
start=0 |
| 32761 |
end=10 |
| 32762 |
for index in $(eval echo {$start..$end}) |
| 32763 |
do |
| 32764 |
echo -n "$index " # 0 1 2 3 4 5 6 7 8 9 10 |
| 32765 |
done |
| 32766 |
|
| 32767 |
echo</programlisting></para> |
| 32768 |
|
| 32769 |
</listitem> |
| 32770 |
|
| 32771 |
|
| 32772 |
<listitem> |
| 32773 |
<para>The <command>${!array[@]}</command> operator, which |
| 32774 |
expands to all the indices of a given <link |
| 32775 |
linkend="arrayref">array</link>.</para> |
| 32776 |
|
| 32777 |
<para><programlisting>#!/bin/bash |
| 32778 |
|
| 32779 |
Array=(element-zero element-one element-two element-three) |
| 32780 |
|
| 32781 |
echo ${Array[0]} # element-zero |
| 32782 |
# First element of array. |
| 32783 |
|
| 32784 |
echo ${!Array[@]} # 0 1 2 3 |
| 32785 |
# All the indices of Array. |
| 32786 |
|
| 32787 |
for i in ${!Array[@]} |
| 32788 |
do |
| 32789 |
echo ${Array[i]} # element-zero |
| 32790 |
# element-one |
| 32791 |
# element-two |
| 32792 |
# element-three |
| 32793 |
# |
| 32794 |
# All the elements in Array. |
| 32795 |
done</programlisting></para> |
| 32796 |
|
| 32797 |
</listitem> |
| 32798 |
|
| 32799 |
|
| 32800 |
<listitem> |
| 32801 |
<para><anchor id="regexmatchref"></para> |
| 32802 |
<para>The <command>=~</command> <link linkend="regexref">Regular |
| 32803 |
Expression</link> matching operator within a <link |
| 32804 |
linkend="dblbrackets">double brackets</link> test expression. |
| 32805 |
(Perl has a similar operator.)</para> |
| 32806 |
|
| 32807 |
<para><programlisting>#!/bin/bash |
| 32808 |
|
| 32809 |
variable="This is a fine mess." |
| 32810 |
|
| 32811 |
echo "$variable" |
| 32812 |
|
| 32813 |
# Regex matching with =~ operator within [[ double brackets ]]. |
| 32814 |
if [[ "$variable" =~ T.........fin*es* ]] |
| 32815 |
# NOTE: As of version 3.2 of Bash, expression to match no longer quoted. |
| 32816 |
then |
| 32817 |
echo "match found" |
| 32818 |
# match found |
| 32819 |
fi</programlisting></para> |
| 32820 |
|
| 32821 |
<para>Or, more usefully:</para> |
| 32822 |
|
| 32823 |
<para><programlisting>#!/bin/bash |
| 32824 |
|
| 32825 |
input=$1 |
| 32826 |
|
| 32827 |
|
| 32828 |
if [[ "$input" =~ "[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]" ]] |
| 32829 |
# ^ NOTE: Quoting not necessary, as of version 3.2 of Bash. |
| 32830 |
# NNN-NN-NNNN (where each N is a digit). |
| 32831 |
then |
| 32832 |
echo "Social Security number." |
| 32833 |
# Process SSN. |
| 32834 |
else |
| 32835 |
echo "Not a Social Security number!" |
| 32836 |
# Or, ask for corrected input. |
| 32837 |
fi</programlisting></para> |
| 32838 |
|
| 32839 |
<para>For additional examples of using the |
| 32840 |
<command>=~</command> operator, see <xref linkend="whx">, |
| 32841 |
<xref linkend="mailboxgrep">, <xref |
| 32842 |
linkend="findsplit">, and <xref linkend="tohtml">.</para> |
| 32843 |
|
| 32844 |
|
| 32845 |
</listitem> |
| 32846 |
|
| 32847 |
<listitem> |
| 32848 |
<para><anchor id="pipefailref"></para> |
| 32849 |
<para>The new <option>set -o pipefail</option> option is |
| 32850 |
useful for debugging <link linkend="piperef">pipes</link>. If |
| 32851 |
this option is set, then the <link |
| 32852 |
linkend="exitstatusref">exit status</link> of a pipe |
| 32853 |
is the exit status of the last command in the pipe to |
| 32854 |
<emphasis>fail</emphasis> (return a non-zero value), rather |
| 32855 |
than the actual final command in the pipe.</para> |
| 32856 |
<para>See <xref linkend="fc4upd">.</para> |
| 32857 |
</listitem> |
| 32858 |
|
| 32859 |
|
| 32860 |
</itemizedlist> |
| 32861 |
</para> |
| 32862 |
|
| 32863 |
<caution> |
| 32864 |
<para>The update to version 3 of Bash breaks a few scripts |
| 32865 |
that worked under earlier versions. <emphasis>Test critical legacy |
| 32866 |
scripts to make sure they still work!</emphasis></para> |
| 32867 |
<para>As it happens, a couple of the scripts in the |
| 32868 |
<emphasis>Advanced Bash Scripting Guide</emphasis> had to be |
| 32869 |
fixed up (see <xref linkend="tout">, for instance).</para> |
| 32870 |
</caution> |
| 32871 |
|
| 32872 |
|
| 32873 |
<sect2> <!-- Bash, Version 3.1 --> |
| 32874 |
<title>Bash, version 3.1</title> |
| 32875 |
|
| 32876 |
<para>The version 3.1 update of Bash introduces a number of bugfixes |
| 32877 |
and a few minor changes.</para> |
| 32878 |
|
| 32879 |
<itemizedlist> |
| 32880 |
|
| 32881 |
<listitem> |
| 32882 |
<para>The <token>+=</token> operator is now permitted in |
| 32883 |
in places where previously only the <token>=</token> |
| 32884 |
assignment operator was recognized.</para> |
| 32885 |
|
| 32886 |
<para><anchor id="pluseqstr"></para> |
| 32887 |
|
| 32888 |
<para><programlisting>a=1 |
| 32889 |
echo $a # 1 |
| 32890 |
|
| 32891 |
a+=5 # Won't work under versions of Bash earlier than 3.1. |
| 32892 |
echo $a # 15 |
| 32893 |
|
| 32894 |
a+=Hello |
| 32895 |
echo $a # 15Hello</programlisting> |
| 32896 |
</para> |
| 32897 |
|
| 32898 |
<para>Here, <token>+=</token> functions as a <firstterm>string |
| 32899 |
concatenation</firstterm> operator. Note that its behavior |
| 32900 |
in this particular context is different than within a |
| 32901 |
<link linkend="letref">let</link> construct.</para> |
| 32902 |
|
| 32903 |
<para><programlisting>a=1 |
| 32904 |
echo $a # 1 |
| 32905 |
|
| 32906 |
let a+=5 # Integer arithmetic, rather than string concatenation. |
| 32907 |
echo $a # 6 |
| 32908 |
|
| 32909 |
let a+=Hello # Doesn't "add" anything to a. |
| 32910 |
echo $a # 6</programlisting> |
| 32911 |
</para> |
| 32912 |
|
| 32913 |
<para><anchor id="pathappend">Jeffrey Haemer points out |
| 32914 |
that this concatenation operator can be quite |
| 32915 |
useful. In this instance, we append a directory to the |
| 32916 |
<varname>$PATH</varname>.</para> |
| 32917 |
|
| 32918 |
<para> |
| 32919 |
|
| 32920 |
<screen> |
| 32921 |
<prompt>bash$ </prompt><userinput>echo $PATH</userinput> |
| 32922 |
<computeroutput>/usr/bin:/bin:/usr/local/bin:/usr/X11R6/bin/:/usr/games</computeroutput> |
| 32923 |
|
| 32924 |
|
| 32925 |
<prompt>bash$ </prompt><userinput>PATH+=:/opt/bin</userinput> |
| 32926 |
|
| 32927 |
<prompt>bash$ </prompt><userinput>echo $PATH</userinput> |
| 32928 |
<computeroutput>/usr/bin:/bin:/usr/local/bin:/usr/X11R6/bin/:/usr/games:/opt/bin</computeroutput> |
| 32929 |
</screen> |
| 32930 |
|
| 32931 |
</para> |
| 32932 |
|
| 32933 |
</listitem> |
| 32934 |
|
| 32935 |
</itemizedlist> |
| 32936 |
|
| 32937 |
|
| 32938 |
</sect2> <!-- Bash, Version 3.1 --> |
| 32939 |
|
| 32940 |
|
| 32941 |
|
| 32942 |
<sect2> <!-- Bash, Version 3.2 --> |
| 32943 |
<title>Bash, version 3.2</title> |
| 32944 |
|
| 32945 |
<para>This is pretty much a bugfix update.</para> |
| 32946 |
|
| 32947 |
<itemizedlist> |
| 32948 |
|
| 32949 |
<listitem> |
| 32950 |
<para>In <link linkend="psglob"><firstterm>global</firstterm> |
| 32951 |
parameter substitutions</link>, the pattern no longer anchors |
| 32952 |
at the start of the string.</para> |
| 32953 |
</listitem> |
| 32954 |
|
| 32955 |
<listitem> |
| 32956 |
<para>The <option>--wordexp</option> option disables |
| 32957 |
<link linkend="processsubref">process substitution</link>.</para> |
| 32958 |
</listitem> |
| 32959 |
|
| 32960 |
<listitem> |
| 32961 |
<para>The <command>=~</command> <link |
| 32962 |
linkend="regexmatchref">Regular Expression |
| 32963 |
match operator</link> no longer requires |
| 32964 |
<link linkend="quotingref">quoting</link> of the |
| 32965 |
<firstterm>pattern</firstterm> within <link |
| 32966 |
linkend="dblbrackets">[[ ... ]]</link>.</para> |
| 32967 |
|
| 32968 |
<caution> |
| 32969 |
<para>In fact, quoting in this context is |
| 32970 |
<emphasis>not</emphasis> advisable as it may |
| 32971 |
cause <firstterm>regex</firstterm> evaluation to fail. |
| 32972 |
Chet Ramey states in the <link linkend="bashfaq">Bash |
| 32973 |
FAQ</link> that quoting explicitly disables regex evaluation. |
| 32974 |
See also the <ulink |
| 32975 |
url="https://bugs.launchpad.net/ubuntu-website/+bug/109931"> |
| 32976 |
Ubuntu Bug List</ulink> and <ulink |
| 32977 |
url="http://en.wikinerds.org/index.php/Bash_syntax_and_semantics"> |
| 32978 |
Wikinerds on Bash syntax</ulink>.</para> |
| 32979 |
|
| 32980 |
<para>Setting <emphasis>shopt -s compat31</emphasis> |
| 32981 |
in a script causes reversion to the original |
| 32982 |
behavior.</para> |
| 32983 |
</caution> |
| 32984 |
|
| 32985 |
</listitem> |
| 32986 |
|
| 32987 |
</itemizedlist> |
| 32988 |
|
| 32989 |
</sect2> <!-- Bash, Version 3.2 --> |
| 32990 |
|
| 32991 |
|
| 32992 |
</sect1> <!-- Bash, Version 3 --> |
| 32993 |
|
| 32994 |
|
| 32995 |
|
| 32996 |
<sect1 id="bashver4"> |
| 32997 |
<title>Bash, version 4</title> |
| 32998 |
|
| 32999 |
<para><anchor id="bash4ref"></para> |
| 33000 |
<para>Chet Ramey announced Version 4 of Bash on the 20th |
| 33001 |
of February, 2009. This release has a number of significant |
| 33002 |
new features, as well as some important bugfixes.</para> |
| 33003 |
|
| 33004 |
<para>Among the new goodies:</para> |
| 33005 |
|
| 33006 |
<itemizedlist> |
| 33007 |
|
| 33008 |
<listitem> |
| 33009 |
<para><anchor id="assocarr">Associative arrays. |
| 33010 |
<footnote><para>To be more specific, Bash 4+ has |
| 33011 |
<emphasis>limited</emphasis> support for associative |
| 33012 |
arrays. It's a bare-bones implementation, |
| 33013 |
and it lacks the much of the functionality of such |
| 33014 |
arrays in other programming languages. Note, however, |
| 33015 |
that <link linkend="assocarrtst">associative arrays in |
| 33016 |
Bash seem to execute faster and more efficiently than |
| 33017 |
numerically-indexed arrays</link>.</para></footnote> |
| 33018 |
</para> |
| 33019 |
|
| 33020 |
<sidebar> |
| 33021 |
<para>An <firstterm>associative</firstterm> array can |
| 33022 |
be thought of as a set of two linked arrays -- one holding |
| 33023 |
the <firstterm>data</firstterm>, and the other the |
| 33024 |
<firstterm>keys</firstterm> that index the individual elements |
| 33025 |
of the <firstterm>data</firstterm> array.</para> |
| 33026 |
</sidebar> |
| 33027 |
|
| 33028 |
<example id="fetchaddress"> |
| 33029 |
<title>A simple address database</title> |
| 33030 |
<programlisting>&fetchaddress;</programlisting> |
| 33031 |
</example> |
| 33032 |
|
| 33033 |
<example id="fetchaddress2"> |
| 33034 |
<title>A somewhat more elaborate address database</title> |
| 33035 |
<programlisting>&fetchaddress2;</programlisting> |
| 33036 |
</example> |
| 33037 |
|
| 33038 |
<para>See <xref linkend="samorse"> for an interesting |
| 33039 |
usage of an <firstterm>associative array</firstterm>.</para> |
| 33040 |
|
| 33041 |
<caution><para>Elements of the <firstterm>index</firstterm> array |
| 33042 |
may include embedded <link linkend="whitespaceref">space |
| 33043 |
characters</link>, or even leading and/or trailing space |
| 33044 |
characters. However, index array elements containing |
| 33045 |
<emphasis>only</emphasis> <firstterm>whitespace</firstterm> |
| 33046 |
are <emphasis>not</emphasis> permitted.</para> |
| 33047 |
<para><programlisting>address[ ]="Blank" # Error!</programlisting> |
| 33048 |
</para></caution> |
| 33049 |
|
| 33050 |
</listitem> |
| 33051 |
|
| 33052 |
|
| 33053 |
<listitem> |
| 33054 |
<para><anchor id="ncterm">Enhancements to the |
| 33055 |
<link linkend="caseesac1">case</link> construct: |
| 33056 |
the <replaceable>;;&</replaceable> and |
| 33057 |
<replaceable>;&</replaceable> terminators.</para> |
| 33058 |
|
| 33059 |
<example id="case4"> |
| 33060 |
<title>Testing characters</title> |
| 33061 |
<programlisting>&case4;</programlisting> |
| 33062 |
</example> |
| 33063 |
</listitem> |
| 33064 |
|
| 33065 |
<listitem> |
| 33066 |
<para><anchor id="coprocref">The new <command>coproc</command> |
| 33067 |
builtin enables two parallel <link |
| 33068 |
linkend="processref">processes</link> to communicate and |
| 33069 |
interact. As Chet Ramey states in the |
| 33070 |
<link linkend="bashfaq">Bash FAQ</link> |
| 33071 |
<footnote><para>Copyright 1995-2009 by Chester Ramey.</para></footnote> |
| 33072 |
, ver. 4.01: |
| 33073 |
</para> |
| 33074 |
|
| 33075 |
<blockquote> |
| 33076 |
<literallayout> |
| 33077 |
There is a new 'coproc' reserved word that specifies a coprocess: |
| 33078 |
an asynchronous command run with two pipes connected to the creating |
| 33079 |
shell. Coprocs can be named. The input and output file descriptors |
| 33080 |
and the PID of the coprocess are available to the calling shell in |
| 33081 |
variables with coproc-specific names. |
| 33082 |
|
| 33083 |
George Dimitriu explains, |
| 33084 |
"... coproc ... is a feature used in Bash process substitution, |
| 33085 |
which now is made publicly available." |
| 33086 |
This means it can be explicitly invoked in a script, rather than |
| 33087 |
just being a behind-the-scenes mechanism used by Bash. |
| 33088 |
</literallayout> |
| 33089 |
</blockquote> |
| 33090 |
|
| 33091 |
<para>Coprocesses use <firstterm>file descriptors</firstterm>. |
| 33092 |
<link linkend="fdref2">File descriptors enable processes and |
| 33093 |
pipes to communicate</link>.</para> |
| 33094 |
|
| 33095 |
<para><programlisting>#!/bin/bash4 |
| 33096 |
# A coprocess communicates with a while-read loop. |
| 33097 |
|
| 33098 |
|
| 33099 |
coproc { cat mx_data.txt; sleep 2; } |
| 33100 |
# ^^^^^^^ |
| 33101 |
# Try running this without "sleep 2" and see what happens. |
| 33102 |
|
| 33103 |
while read -u ${COPROC[0]} line # ${COPROC[0]} is the |
| 33104 |
do #+ file descriptor of the coprocess. |
| 33105 |
echo "$line" | sed -e 's/line/NOT-ORIGINAL-TEXT/' |
| 33106 |
done |
| 33107 |
|
| 33108 |
kill $COPROC_PID # No longer need the coprocess, |
| 33109 |
#+ so kill its PID.</programlisting></para> |
| 33110 |
|
| 33111 |
<para>But, be careful!</para> |
| 33112 |
|
| 33113 |
<para><programlisting>#!/bin/bash4 |
| 33114 |
|
| 33115 |
echo; echo |
| 33116 |
a=aaa |
| 33117 |
b=bbb |
| 33118 |
c=ccc |
| 33119 |
|
| 33120 |
coproc echo "one two three" |
| 33121 |
while read -u ${COPROC[0]} a b c; # Note that this loop |
| 33122 |
do #+ runs in a subshell. |
| 33123 |
echo "Inside while-read loop: "; |
| 33124 |
echo "a = $a"; echo "b = $b"; echo "c = $c" |
| 33125 |
echo "coproc file descriptor: ${COPROC[0]}" |
| 33126 |
done |
| 33127 |
|
| 33128 |
# a = one |
| 33129 |
# b = two |
| 33130 |
# c = three |
| 33131 |
# So far, so good, but ... |
| 33132 |
|
| 33133 |
echo "-----------------" |
| 33134 |
echo "Outside while-read loop: " |
| 33135 |
echo "a = $a" # a = |
| 33136 |
echo "b = $b" # b = |
| 33137 |
echo "c = $c" # c = |
| 33138 |
echo "coproc file descriptor: ${COPROC[0]}" |
| 33139 |
echo |
| 33140 |
# The coproc is still running, but ... |
| 33141 |
#+ it still doesn't enable the parent process |
| 33142 |
#+ to "inherit" variables from the child process, the while-read loop. |
| 33143 |
|
| 33144 |
# Compare this to the "badread.sh" script.</programlisting></para> |
| 33145 |
|
| 33146 |
<caution> |
| 33147 |
<para>The coprocess is <firstterm>asynchronous</firstterm>, |
| 33148 |
and this might cause a problem. It may terminate before another |
| 33149 |
process has finished communicating with it.</para> |
| 33150 |
|
| 33151 |
<programlisting>#!/bin/bash4 |
| 33152 |
|
| 33153 |
coproc cpname { for i in {0..10}; do echo "index = $i"; done; } |
| 33154 |
# ^^^^^^ This is a *named* coprocess. |
| 33155 |
read -u ${cpname[0]} |
| 33156 |
echo $REPLY # index = 0 |
| 33157 |
echo ${COPROC[0]} #+ No output ... the coprocess timed out |
| 33158 |
# after the first loop iteration. |
| 33159 |
|
| 33160 |
|
| 33161 |
|
| 33162 |
# However, George Dimitriu has a partial fix. |
| 33163 |
|
| 33164 |
coproc cpname { for i in {0..10}; do echo "index = $i"; done; sleep 1; |
| 33165 |
echo hi > myo; cat - >> myo; } |
| 33166 |
# ^^^^^ This is a *named* coprocess. |
| 33167 |
|
| 33168 |
echo "I am main"$'\04' >&${cpname[1]} |
| 33169 |
myfd=${cpname[0]} |
| 33170 |
echo myfd=$myfd |
| 33171 |
|
| 33172 |
### while read -u $myfd |
| 33173 |
### do |
| 33174 |
### echo $REPLY; |
| 33175 |
### done |
| 33176 |
|
| 33177 |
echo $cpname_PID |
| 33178 |
|
| 33179 |
# Run this with and without the commented-out while-loop, and it is |
| 33180 |
#+ apparent that each process, the executing shell and the coprocess, |
| 33181 |
#+ waits for the other to finish writing in its own write-enabled pipe.</programlisting> |
| 33182 |
</caution> |
| 33183 |
|
| 33184 |
</listitem> |
| 33185 |
|
| 33186 |
<listitem> |
| 33187 |
|
| 33188 |
<para><anchor id="mapfileref">The new <command>mapfile</command> |
| 33189 |
builtin makes it possible to load an array with the contents |
| 33190 |
of a text file without using a loop or <link |
| 33191 |
linkend="arrayinitcs">command substitution</link>.</para> |
| 33192 |
|
| 33193 |
<para><programlisting>#!/bin/bash4 |
| 33194 |
|
| 33195 |
mapfile Arr1 < $0 |
| 33196 |
# Same result as Arr1=( $(cat $0) ) |
| 33197 |
echo "${Arr1[@]}" # Copies this entire script out to stdout. |
| 33198 |
|
| 33199 |
echo "--"; echo |
| 33200 |
|
| 33201 |
# But, not the same as read -a !!! |
| 33202 |
read -a Arr2 < $0 |
| 33203 |
echo "${Arr2[@]}" # Reads only first line of script into the array. |
| 33204 |
|
| 33205 |
exit</programlisting></para> |
| 33206 |
|
| 33207 |
</listitem> |
| 33208 |
|
| 33209 |
<listitem> |
| 33210 |
<para>The <link linkend="readref">read</link> builtin got |
| 33211 |
a minor facelift. The <option>-t</option> |
| 33212 |
<link linkend="readtimed">timeout</link> option now accepts |
| 33213 |
(decimal) fractional values |
| 33214 |
<footnote><para>This only works with <link |
| 33215 |
linkend="piperef">pipes</link> and certain other |
| 33216 |
<firstterm>special</firstterm> files.</para></footnote> |
| 33217 |
and the <option>-i</option> option |
| 33218 |
permits preloading the edit buffer. |
| 33219 |
<footnote><para>But only in conjunction with |
| 33220 |
<link linkend="readlineref">readline</link>, i.e., |
| 33221 |
from the command-line.</para></footnote> |
| 33222 |
Unfortunately, these enhancements are still a work in progress |
| 33223 |
and not (yet) usable in scripts.</para> |
| 33224 |
</listitem> |
| 33225 |
|
| 33226 |
<listitem> |
| 33227 |
<para><anchor id="casemodparamsub"> |
| 33228 |
<link linkend="paramsubref">Parameter substitution</link> |
| 33229 |
gets <firstterm>case-modification</firstterm> operators.</para> |
| 33230 |
|
| 33231 |
<para><programlisting>#!/bin/bash4 |
| 33232 |
|
| 33233 |
var=veryMixedUpVariable |
| 33234 |
echo ${var} # veryMixedUpVariable |
| 33235 |
echo ${var^} # VeryMixedUpVariable |
| 33236 |
# * First char --> uppercase. |
| 33237 |
echo ${var^^} # VERYMIXEDUPVARIABLE |
| 33238 |
# ** All chars --> uppercase. |
| 33239 |
echo ${var,} # veryMixedUpVariable |
| 33240 |
# * First char --> lowercase. |
| 33241 |
echo ${var,,} # verymixedupvariable |
| 33242 |
# ** All chars --> lowercase.</programlisting></para> |
| 33243 |
</listitem> |
| 33244 |
|
| 33245 |
<listitem> |
| 33246 |
<para><anchor id="declarecasemod"></para> |
| 33247 |
<para>The <link linkend="declareref">declare</link> builtin now |
| 33248 |
accepts the <option>-l</option> <firstterm>lowercase</firstterm> |
| 33249 |
and <option>-c</option> <firstterm>capitalize</firstterm> |
| 33250 |
options.</para> |
| 33251 |
|
| 33252 |
<para><programlisting>#!/bin/bash4 |
| 33253 |
|
| 33254 |
declare -l var1 # Will change to lowercase |
| 33255 |
var1=MixedCaseVARIABLE |
| 33256 |
echo "$var1" # mixedcasevariable |
| 33257 |
# Same effect as echo $var1 | tr A-Z a-z |
| 33258 |
|
| 33259 |
declare -c var2 # Changes only initial char to uppercase. |
| 33260 |
var2=originally_lowercase |
| 33261 |
echo "$var2" # Originally_lowercase |
| 33262 |
# NOT the same effect as echo $var2 | tr a-z A-Z</programlisting></para> |
| 33263 |
</listitem> |
| 33264 |
|
| 33265 |
<listitem> |
| 33266 |
<para><anchor id="braceexpref4"> |
| 33267 |
<link linkend="braceexpref">Brace expansion</link> has more options.</para> |
| 33268 |
|
| 33269 |
<para><firstterm>Increment/decrement</firstterm>, specified in the |
| 33270 |
final term within braces.</para> |
| 33271 |
<para><programlisting>#!/bin/bash4 |
| 33272 |
|
| 33273 |
echo {40..60..2} |
| 33274 |
# 40 42 44 46 48 50 52 54 56 58 60 |
| 33275 |
# All the even numbers, between 40 and 60. |
| 33276 |
|
| 33277 |
echo {60..40..2} |
| 33278 |
# 60 58 56 54 52 50 48 46 44 42 40 |
| 33279 |
# All the even numbers, between 40 and 60, counting backwards. |
| 33280 |
# In effect, a decrement. |
| 33281 |
echo {60..40..-2} |
| 33282 |
# The same output. The minus sign is not necessary. |
| 33283 |
|
| 33284 |
# But, what about letters and symbols? |
| 33285 |
echo {X..d} |
| 33286 |
# X Y Z [ ] ^ _ ` a b c d |
| 33287 |
# Does not echo the \ which escapes a space.</programlisting></para> |
| 33288 |
|
| 33289 |
<para><firstterm>Zero-padding</firstterm>, specified in the |
| 33290 |
first term within braces, prefixes each term in the output |
| 33291 |
with the <emphasis>same number</emphasis> of zeroes.</para> |
| 33292 |
|
| 33293 |
<screen> |
| 33294 |
<prompt>bash4$ </prompt><userinput>echo {010..15}</userinput> |
| 33295 |
<computeroutput>010 011 012 013 014 015</computeroutput> |
| 33296 |
|
| 33297 |
|
| 33298 |
<prompt>bash4$ </prompt><userinput>echo {000..10}</userinput> |
| 33299 |
<computeroutput>000 001 002 003 004 005 006 007 008 009 010</computeroutput> |
| 33300 |
</screen> |
| 33301 |
|
| 33302 |
</listitem> |
| 33303 |
|
| 33304 |
<listitem> |
| 33305 |
<para><anchor id="substrextref4"></para> |
| 33306 |
<para><link linkend="substrextref4"><firstterm>Substring |
| 33307 |
extraction</firstterm> on <firstterm>positional |
| 33308 |
parameters</firstterm></link> now starts with <link |
| 33309 |
linkend="scrnameparam">$0</link> as the |
| 33310 |
<firstterm>zero-index</firstterm>. (This corrects an |
| 33311 |
inconsistency in the treatment of positional parameters.)</para> |
| 33312 |
|
| 33313 |
<para><programlisting>#!/bin/bash |
| 33314 |
# show-params.bash |
| 33315 |
# Requires version 4+ of Bash. |
| 33316 |
|
| 33317 |
# Invoke this scripts with at least one positional parameter. |
| 33318 |
|
| 33319 |
E_BADPARAMS=99 |
| 33320 |
|
| 33321 |
if [ -z "$1" ] |
| 33322 |
then |
| 33323 |
echo "Usage $0 param1 ..." |
| 33324 |
exit $E_BADPARAMS |
| 33325 |
fi |
| 33326 |
|
| 33327 |
echo ${@:0} |
| 33328 |
|
| 33329 |
# bash3 show-params.bash4 one two three |
| 33330 |
# one two three |
| 33331 |
|
| 33332 |
# bash4 show-params.bash4 one two three |
| 33333 |
# show-params.bash4 one two three |
| 33334 |
|
| 33335 |
# $0 $1 $2 $3</programlisting></para> |
| 33336 |
|
| 33337 |
</listitem> |
| 33338 |
|
| 33339 |
<listitem> |
| 33340 |
<para><anchor id="globstarref">The new <token>**</token> |
| 33341 |
<link linkend="globbingref">globbing</link> operator |
| 33342 |
matches filenames and directories |
| 33343 |
<link linkend="recursionref0">recursively</link>.</para> |
| 33344 |
|
| 33345 |
<para><programlisting>#!/bin/bash4 |
| 33346 |
# filelist.bash4 |
| 33347 |
|
| 33348 |
shopt -s globstar # Must enable globstar, otherwise ** doesn't work. |
| 33349 |
# The globstar shell option is new to version 4 of Bash. |
| 33350 |
|
| 33351 |
echo "Using *"; echo |
| 33352 |
for filename in * |
| 33353 |
do |
| 33354 |
echo "$filename" |
| 33355 |
done # Lists only files in current directory ($PWD). |
| 33356 |
|
| 33357 |
echo; echo "--------------"; echo |
| 33358 |
|
| 33359 |
echo "Using **" |
| 33360 |
for filename in ** |
| 33361 |
do |
| 33362 |
echo "$filename" |
| 33363 |
done # Lists complete file tree, recursively. |
| 33364 |
|
| 33365 |
exit |
| 33366 |
|
| 33367 |
Using * |
| 33368 |
|
| 33369 |
allmyfiles |
| 33370 |
filelist.bash4 |
| 33371 |
|
| 33372 |
-------------- |
| 33373 |
|
| 33374 |
Using ** |
| 33375 |
|
| 33376 |
allmyfiles |
| 33377 |
allmyfiles/file.index.txt |
| 33378 |
allmyfiles/my_music |
| 33379 |
allmyfiles/my_music/me-singing-60s-folksongs.ogg |
| 33380 |
allmyfiles/my_music/me-singing-opera.ogg |
| 33381 |
allmyfiles/my_music/piano-lesson.1.ogg |
| 33382 |
allmyfiles/my_pictures |
| 33383 |
allmyfiles/my_pictures/at-beach-with-Jade.png |
| 33384 |
allmyfiles/my_pictures/picnic-with-Melissa.png |
| 33385 |
filelist.bash4</programlisting></para> |
| 33386 |
|
| 33387 |
</listitem> |
| 33388 |
|
| 33389 |
<listitem> |
| 33390 |
<para>The new <link linkend="bashpidref">$BASHPID</link> |
| 33391 |
internal variable.</para> |
| 33392 |
</listitem> |
| 33393 |
|
| 33394 |
<listitem> |
| 33395 |
<para><anchor id="cnfh"></para> |
| 33396 |
<para>There is a new <link linkend="builtinref">builtin</link> |
| 33397 |
error-handling function named |
| 33398 |
<command>command_not_found_handle</command>.</para> |
| 33399 |
<para><programlisting>#!/bin/bash4 |
| 33400 |
|
| 33401 |
command_not_found_handle () |
| 33402 |
{ # Accepts implicit parameters. |
| 33403 |
echo "The following command is not valid: \""$1\""" |
| 33404 |
echo "With the following argument(s): \""$2\"" \""$3\""" # $4, $5 ... |
| 33405 |
} # $1, $2, etc. are not explicitly passed to the function. |
| 33406 |
|
| 33407 |
bad_command arg1 arg2 |
| 33408 |
|
| 33409 |
# The following command is not valid: "bad_command" |
| 33410 |
# With the following argument(s): "arg1" "arg2"</programlisting></para> |
| 33411 |
</listitem> |
| 33412 |
|
| 33413 |
</itemizedlist> |
| 33414 |
|
| 33415 |
<sidebar> |
| 33416 |
<para><emphasis>Editorial comment</emphasis></para> |
| 33417 |
<para>Associative arrays? Coprocesses? Whatever happened |
| 33418 |
to the lean and mean Bash we have come to know and love? |
| 33419 |
Could it be suffering from (horrors!) <quote>feature |
| 33420 |
creep</quote>? Or perhaps even Korn shell envy?</para> |
| 33421 |
<para><emphasis>Note to Chet Ramey:</emphasis> Please add only |
| 33422 |
<emphasis>essential</emphasis> features in future Bash |
| 33423 |
releases -- perhaps <firstterm>for-each</firstterm> |
| 33424 |
loops and support for multi-dimensional arrays. |
| 33425 |
<footnote><para>And while you're at it, consider fixing |
| 33426 |
the notorious <link linkend="pipereadref0">piped read</link> |
| 33427 |
problem.</para></footnote> |
| 33428 |
Most Bash users won't need, won't use, and likely won't greatly |
| 33429 |
appreciate complex <quote>features</quote> like built-in |
| 33430 |
debuggers, Perl interfaces, and bolt-on rocket boosters.</para> |
| 33431 |
</sidebar> |
| 33432 |
|
| 33433 |
|
| 33434 |
<sect2> <!-- Bash, Version 4.1 --> |
| 33435 |
<title>Bash, version 4.1</title> |
| 33436 |
|
| 33437 |
<para><anchor id="bash41">Version 4.1 of Bash, released in May, |
| 33438 |
2010, was primarily a bugfix update.</para> |
| 33439 |
|
| 33440 |
<itemizedlist> |
| 33441 |
|
| 33442 |
<listitem> |
| 33443 |
<para>The <link linkend="printfref">printf</link> command |
| 33444 |
now accepts a <option>-v</option> option for setting <link |
| 33445 |
linkend="arrayref">array</link> indices.</para> |
| 33446 |
</listitem> |
| 33447 |
|
| 33448 |
<listitem> |
| 33449 |
<para>Within <link linkend="dblbrackets">double brackets</link>, |
| 33450 |
the <command>></command> and <command><</command> |
| 33451 |
string comparison operators now conform to the <link |
| 33452 |
linkend="localeref">locale</link>. Since the locale setting may |
| 33453 |
affect the sorting order of string expressions, this |
| 33454 |
has side-effects on comparison tests within |
| 33455 |
<emphasis>[[ ... ]]</emphasis> expressions.</para> |
| 33456 |
</listitem> |
| 33457 |
|
| 33458 |
<listitem> |
| 33459 |
<para>The <link linkend="readref">read</link> builtin |
| 33460 |
now takes a <option>-N</option> option (<firstterm>read -N |
| 33461 |
chars</firstterm>), which causes the <firstterm>read</firstterm> |
| 33462 |
to terminate after <firstterm>chars</firstterm> |
| 33463 |
characters.</para> |
| 33464 |
|
| 33465 |
<example id="readn"> |
| 33466 |
<title>Reading N characters</title> |
| 33467 |
<programlisting>&readn;</programlisting> |
| 33468 |
</example> |
| 33469 |
</listitem> |
| 33470 |
|
| 33471 |
<listitem> |
| 33472 |
<para><link linkend="heredocref">Here documents</link> |
| 33473 |
embedded in <link linkend="commandsubref0"> |
| 33474 |
<userinput>$( ... )</userinput> command substitution</link> |
| 33475 |
constructs may terminate with a simple |
| 33476 |
<command>)</command>.</para> |
| 33477 |
|
| 33478 |
<example id="herecommsub"> |
| 33479 |
<title>Using a <firstterm>here document</firstterm> |
| 33480 |
to set a variable</title> |
| 33481 |
<programlisting>&herecommsub;</programlisting> |
| 33482 |
</example> |
| 33483 |
</listitem> |
| 33484 |
|
| 33485 |
</itemizedlist> |
| 33486 |
|
| 33487 |
</sect2> <!-- Bash, Version 4.1 --> |
| 33488 |
|
| 33489 |
|
| 33490 |
<sect2> <!-- Bash, Version 4.2 --> |
| 33491 |
<title>Bash, version 4.2</title> |
| 33492 |
|
| 33493 |
<para><anchor id="bash42">Version 4.2 of Bash, released |
| 33494 |
in February, 2011, contains a number of new features and |
| 33495 |
enhancements, in addition to bugfixes.</para> |
| 33496 |
|
| 33497 |
<itemizedlist> |
| 33498 |
|
| 33499 |
<listitem> |
| 33500 |
<para>Bash now supports the the |
| 33501 |
<replaceable>\u</replaceable> |
| 33502 |
and <replaceable>\U</replaceable> |
| 33503 |
<firstterm>Unicode</firstterm> escape.</para> |
| 33504 |
|
| 33505 |
<para><anchor id="unicoderef"></para> |
| 33506 |
<sidebar><para>Unicode is a cross-platform standard for encoding |
| 33507 |
into numerical values letters and graphic symbols. |
| 33508 |
This permits representing and displaying characters |
| 33509 |
in foreign alphabets and unusual fonts.</para></sidebar> |
| 33510 |
<para><anchor id="unicoderef2"></para> |
| 33511 |
|
| 33512 |
<para><programlisting>echo -e '\u2630' # Horizontal triple bar character. |
| 33513 |
# Equivalent to the more roundabout: |
| 33514 |
echo -e "\xE2\x98\xB0" |
| 33515 |
# Recognized by earlier Bash versions. |
| 33516 |
|
| 33517 |
echo -e '\u220F' # PI (Greek letter and mathematical symbol) |
| 33518 |
echo -e '\u0416' # Capital "ZHE" (Cyrillic letter) |
| 33519 |
echo -e '\u2708' # Airplane (Dingbat font) symbol |
| 33520 |
echo -e '\u2622' # Radioactivity trefoil |
| 33521 |
|
| 33522 |
echo -e "The amplifier circuit requires a 100 \u2126 pull-up resistor." |
| 33523 |
|
| 33524 |
|
| 33525 |
unicode_var='\u2640' |
| 33526 |
echo -e $unicode_var # Female symbol |
| 33527 |
printf "$unicode_var \n" # Female symbol, with newline |
| 33528 |
|
| 33529 |
|
| 33530 |
# And for something a bit more elaborate . . . |
| 33531 |
|
| 33532 |
# We can store Unicode symbols in an associative array, |
| 33533 |
#+ then retrieve them by name. |
| 33534 |
# Run this in a gnome-terminal or a terminal with a large, bold font |
| 33535 |
#+ for better legibility. |
| 33536 |
|
| 33537 |
declare -A symbol # Associative array. |
| 33538 |
|
| 33539 |
symbol[script_E]='\u2130' |
| 33540 |
symbol[script_F]='\u2131' |
| 33541 |
symbol[script_J]='\u2110' |
| 33542 |
symbol[script_M]='\u2133' |
| 33543 |
symbol[Rx]='\u211E' |
| 33544 |
symbol[TEL]='\u2121' |
| 33545 |
symbol[FAX]='\u213B' |
| 33546 |
symbol[care_of]='\u2105' |
| 33547 |
symbol[account]='\u2100' |
| 33548 |
symbol[trademark]='\u2122' |
| 33549 |
|
| 33550 |
|
| 33551 |
echo -ne "${symbol[script_E]} " |
| 33552 |
echo -ne "${symbol[script_F]} " |
| 33553 |
echo -ne "${symbol[script_J]} " |
| 33554 |
echo -ne "${symbol[script_M]} " |
| 33555 |
echo -ne "${symbol[Rx]} " |
| 33556 |
echo -ne "${symbol[TEL]} " |
| 33557 |
echo -ne "${symbol[FAX]} " |
| 33558 |
echo -ne "${symbol[care_of]} " |
| 33559 |
echo -ne "${symbol[account]} " |
| 33560 |
echo -ne "${symbol[trademark]} " |
| 33561 |
echo</programlisting></para> |
| 33562 |
|
| 33563 |
<note> |
| 33564 |
<para>The above example uses the |
| 33565 |
<link linkend="strq"><command>$' ... '</command></link> |
| 33566 |
<firstterm>string-expansion</firstterm> construct.</para> |
| 33567 |
</note> |
| 33568 |
|
| 33569 |
</listitem> |
| 33570 |
|
| 33571 |
|
| 33572 |
<listitem> |
| 33573 |
<para><anchor id="lastpiperef"></para> |
| 33574 |
<para>When the <replaceable>lastpipe</replaceable> shell option |
| 33575 |
is set, the last command in a <link |
| 33576 |
linkend="piperef">pipe</link> <emphasis>doesn't run in a |
| 33577 |
subshell</emphasis>.</para> |
| 33578 |
|
| 33579 |
<example id="lastpipeopt"> |
| 33580 |
<title>Piping input to a <link |
| 33581 |
linkend="readref">read</link></title> |
| 33582 |
<programlisting>&lastpipeopt;</programlisting> |
| 33583 |
</example> |
| 33584 |
|
| 33585 |
<para>This option offers possible <quote>fixups</quote> |
| 33586 |
for these example scripts: |
| 33587 |
<xref linkend="badread"> and |
| 33588 |
<xref linkend="readpipe">.</para> |
| 33589 |
|
| 33590 |
</listitem> |
| 33591 |
|
| 33592 |
|
| 33593 |
|
| 33594 |
|
| 33595 |
<listitem> |
| 33596 |
<para>Negative <link linkend="arrayref">array</link> indices |
| 33597 |
permit counting backwards from the end of an array.</para> |
| 33598 |
|
| 33599 |
<example id="negarray"> |
| 33600 |
<title>Negative array indices</title> |
| 33601 |
<programlisting>&negarray;</programlisting> |
| 33602 |
</example> |
| 33603 |
|
| 33604 |
</listitem> |
| 33605 |
|
| 33606 |
<listitem> |
| 33607 |
<para><link linkend="substrextr01">Substring extraction</link> |
| 33608 |
uses a negative <firstterm>length</firstterm> parameter to |
| 33609 |
specify an offset from the <firstterm>end</firstterm> of the |
| 33610 |
target string.</para> |
| 33611 |
|
| 33612 |
<example id="negoffset"> |
| 33613 |
<title>Negative parameter in string-extraction |
| 33614 |
construct</title> |
| 33615 |
<programlisting>&negoffset;</programlisting> |
| 33616 |
</example> |
| 33617 |
|
| 33618 |
</listitem> |
| 33619 |
</itemizedlist> |
| 33620 |
|
| 33621 |
</sect2> <!-- Bash, Version 4.2 --> |
| 33622 |
|
| 33623 |
|
| 33624 |
|
| 33625 |
</sect1> <!-- Bash, Version 4 --> |
| 33626 |
|
| 33627 |
|
| 33628 |
|
| 33629 |
|
| 33630 |
</chapter> <!-- Bash, versions 2, 3, and 4 --> |
| 33631 |
|
| 33632 |
</part> <!-- Part 5 (Advanced Topics) --> |
| 33633 |
|
| 33634 |
|
| 33635 |
|
| 33636 |
<chapter id="endnotes"> |
| 33637 |
<title>Endnotes</title> |
| 33638 |
|
| 33639 |
|
| 33640 |
<sect1 id="authorsnote"> |
| 33641 |
<title>Author's Note</title> |
| 33642 |
|
| 33643 |
<epigraph> |
| 33644 |
<para><foreignphrase>doce ut discas</foreignphrase></para> |
| 33645 |
<para>(Teach, that you yourself may learn.)</para> |
| 33646 |
</epigraph> |
| 33647 |
|
| 33648 |
|
| 33649 |
<para>How did I come to write a scripting book? It's a strange |
| 33650 |
tale. It seems that a few years back I needed to learn |
| 33651 |
shell scripting -- and what better way to do that than to read a |
| 33652 |
good book on the subject? I was looking to buy a tutorial and |
| 33653 |
reference covering all aspects of the subject. I was looking for a |
| 33654 |
book that would take difficult concepts, turn them inside out, and |
| 33655 |
explain them in excruciating detail, with well-commented examples. |
| 33656 |
<footnote><para>This is the notorious <emphasis>flog |
| 33657 |
it to death</emphasis> technique that works so well |
| 33658 |
with slow learners, eccentrics, odd ducks, fools and |
| 33659 |
geniuses.</para></footnote> |
| 33660 |
In fact, I was looking for <emphasis>this very book</emphasis>, |
| 33661 |
or something very much like it. Unfortunately, <link |
| 33662 |
linkend="kochanref">it didn't exist</link>, and if I wanted it, |
| 33663 |
I'd have to write it. And so, here we are, folks.</para> |
| 33664 |
|
| 33665 |
<para>That reminds me of the apocryphal story about a mad |
| 33666 |
professor. Crazy as a loon, the fellow was. At the sight of a |
| 33667 |
book, any book -- at the library, at a bookstore, anywhere -- |
| 33668 |
he would become totally obsessed with the idea that he could have |
| 33669 |
written it, should have written it -- and done a better job of it |
| 33670 |
to boot. He would thereupon rush home and proceed to do just that, |
| 33671 |
write a book with the very same title. When he died some years |
| 33672 |
later, he allegedly had several thousand books to his credit, |
| 33673 |
probably putting even Asimov to shame. The books might not have |
| 33674 |
been any good, who knows, but does that really matter? Here's |
| 33675 |
a fellow who lived his dream, even if he was obsessed by it, |
| 33676 |
driven by it . . . and somehow I can't help admiring the old |
| 33677 |
coot.</para> |
| 33678 |
|
| 33679 |
</sect1> <!-- Author's Note --> |
| 33680 |
|
| 33681 |
|
| 33682 |
|
| 33683 |
<sect1 id="aboutauthor"> |
| 33684 |
<title>About the Author</title> |
| 33685 |
<subtitle>Who is this guy anyhow?</subtitle> |
| 33686 |
|
| 33687 |
<para><anchor id="nocreds">The author claims no credentials or |
| 33688 |
special qualifications, |
| 33689 |
|
| 33690 |
<footnote><para>In fact, he has no credentials or special |
| 33691 |
qualifications. He's a school dropout with no formal credentials |
| 33692 |
or professional experience whatsoever. None. Zero. Nada. Aside |
| 33693 |
from the <emphasis>ABS Guide</emphasis>, his major claim to |
| 33694 |
fame is a First Place in the sack race at the Colfax Elementary |
| 33695 |
School Field Day in June, 1958.</para></footnote> |
| 33696 |
|
| 33697 |
other than a compulsion to write. |
| 33698 |
|
| 33699 |
<footnote><para>Those who can, do. Those who can't . . . get an |
| 33700 |
MCSE.</para></footnote> |
| 33701 |
|
| 33702 |
</para> |
| 33703 |
|
| 33704 |
<para>This book is somewhat of a departure from his other major work, |
| 33705 |
<ulink url="http://bash.deta.in/hmw60.zip"> |
| 33706 |
HOW-2 Meet Women: The Shy Man's Guide to |
| 33707 |
Relationships</ulink>. He has also written the <ulink |
| 33708 |
url="http://tldp.org/HOWTO/Software-Building-HOWTO.html">Software-Building |
| 33709 |
HOWTO</ulink>. Of late, he has been trying his |
| 33710 |
(heavy) hand at fiction: <ulink |
| 33711 |
url="http://bash.deta.in/dave-dawson-over-berlin.epub">Dave Dawson |
| 33712 |
Over Berlin (First Installment)</ulink> |
| 33713 |
<ulink url="http://bash.deta.in/dave-dawson-over-berlin.II.epub">Dave Dawson |
| 33714 |
Over Berlin (Second Installment)</ulink> and |
| 33715 |
<ulink url="http://bash.deta.in/dave-dawson-over-berlin.III.epub">Dave Dawson |
| 33716 |
Over Berlin (Third Installment)</ulink> |
| 33717 |
|
| 33718 |
. He also has a few |
| 33719 |
<emphasis>Instructables</emphasis> (<ulink |
| 33720 |
url="http://www.instructables.com/id/Arduino-Morse-Code-Shield/">here</ulink>, |
| 33721 |
<ulink |
| 33722 |
url="http://www.instructables.com/id/Haywired-Hackduino/">here</ulink>, |
| 33723 |
<ulink |
| 33724 |
url="http://www.instructables.com/id/Arduino-DIY-SD-Card-Logging-Shield/">here</ulink>, |
| 33725 |
<ulink |
| 33726 |
url="http://www.instructables.com/id/Binguino-An-Arduino-based-Bingo-Number-Generato/">here</ulink>, |
| 33727 |
<ulink |
| 33728 |
url="http://www.instructables.com/id/The-Raspberry-Pi-Lapdock-Connection/">here</ulink>, |
| 33729 |
|
| 33730 |
<ulink |
| 33731 |
url="http://www.instructables.com/id/The-Raspberry-Pi-Arduino-Connection/">here</ulink>, and |
| 33732 |
<ulink |
| 33733 |
url="http://www.instructables.com/id/Switchable-Dual-Voltage-33v5v-Hacduino/">here</ulink> |
| 33734 |
to his (dis)credit.</para> |
| 33735 |
|
| 33736 |
<para>A Linux user since 1995 (Slackware 2.2, kernel 1.2.1), |
| 33737 |
the author has emitted a few |
| 33738 |
software truffles, including the <ulink |
| 33739 |
url="http://ibiblio.org/pub/Linux/utils/file/cruft-0.2.tar.gz">cruft</ulink> |
| 33740 |
one-time pad encryption utility, the <ulink |
| 33741 |
url="http://ibiblio.org/pub/Linux/apps/financial/mcalc-1.6.tar.gz">mcalc</ulink> |
| 33742 |
mortgage calculator, the <ulink |
| 33743 |
url="http://ibiblio.org/pub/Linux/games/amusements/judge-1.0.tar.gz">judge</ulink> |
| 33744 |
Scrabble® adjudicator, the <ulink |
| 33745 |
url="http://ibiblio.org/pub/Linux/libs/yawl-0.3.2.tar.gz">yawl</ulink> |
| 33746 |
word gaming list package, and the <ulink |
| 33747 |
url="http://bash.deta.in/qky.README.html">Quacky</ulink> |
| 33748 |
anagramming gaming package. He got off to a rather shaky start in the |
| 33749 |
computer game -- programming FORTRAN IV on a CDC 3800 (on paper coding |
| 33750 |
pads, with occasional forays on a keypunch machine and a Friden |
| 33751 |
Flexowriter) -- and is not the least bit nostalgic for those |
| 33752 |
days.</para> |
| 33753 |
|
| 33754 |
<para>Living in an out-of-the-way community with wife and orange |
| 33755 |
tabby, he cherishes human frailty, especially his own. |
| 33756 |
<footnote><para>Sometimes it seems as if he has spent |
| 33757 |
his entire life flouting conventional wisdom and defying the |
| 33758 |
sonorous Voice of Authority: <quote><emphasis>Hey, you |
| 33759 |
can't do that!</emphasis></quote></para></footnote> |
| 33760 |
</para> |
| 33761 |
|
| 33762 |
</sect1> <!-- About the Author --> |
| 33763 |
|
| 33764 |
|
| 33765 |
|
| 33766 |
<sect1 id="wherehelp"> |
| 33767 |
<title>Where to Go For Help</title> |
| 33768 |
|
| 33769 |
<para><ulink url="mailto:[email protected]">The author</ulink> |
| 33770 |
is no longer supporting or updating this document. He will not |
| 33771 |
answer questions about this book or about general scripting topics. |
| 33772 |
</para> |
| 33773 |
|
| 33774 |
<sidebar> |
| 33775 |
<para>If you need assistance with a schoolwork assignment, |
| 33776 |
read the pertinent sections of this and other reference works. |
| 33777 |
Do your best to solve the problem using your own wits and |
| 33778 |
resources. <emphasis>Please do not waste the author's time.</emphasis> |
| 33779 |
You will get neither help nor sympathy. |
| 33780 |
<footnote><para>Well, if you <emphasis>absolutely</emphasis> |
| 33781 |
insist, you can try modifying <xref linkend="homework"> to suit |
| 33782 |
your purposes.</para></footnote> |
| 33783 |
</para> |
| 33784 |
<para>Likewise, kindly refrain from annoying the author |
| 33785 |
with solicitations, offers of employment, or <quote>business |
| 33786 |
opportunities.</quote> He is doing just fine, and requires |
| 33787 |
neither help nor sympathy, thank you.</para> |
| 33788 |
<para>Please note that the author will <emphasis>not</emphasis> answer |
| 33789 |
scripting questions for Sun/Solaris/Oracle or Apple systems. The |
| 33790 |
endarkened execs and the arachnoid corporate attorneys of |
| 33791 |
those particular outfits have been using litigation in a |
| 33792 |
predatory manner and/or as a weapon against the Open Source |
| 33793 |
Community. Any Solaris or Apple users needing scripting help |
| 33794 |
will therefore kindly direct their concerns to corporate |
| 33795 |
customer service.</para> |
| 33796 |
|
| 33797 |
</sidebar> |
| 33798 |
|
| 33799 |
<epigraph> |
| 33800 |
<para>... sophisticated in mechanism but possibly agile |
| 33801 |
operating under noises being extremely suppressed ...</para> |
| 33802 |
<para>--<emphasis>CI-300 printer manual</emphasis></para> |
| 33803 |
</epigraph> |
| 33804 |
|
| 33805 |
</sect1> <!-- Where to Go For Help --> |
| 33806 |
|
| 33807 |
|
| 33808 |
|
| 33809 |
<sect1 id="toolsused"> |
| 33810 |
<title>Tools Used to Produce This Book</title> |
| 33811 |
|
| 33812 |
|
| 33813 |
<sect2 id="software-hardware"> |
| 33814 |
<title>Hardware</title> |
| 33815 |
|
| 33816 |
<para>A used IBM Thinkpad, model 760XL laptop (P166, 104 meg RAM) |
| 33817 |
running Red Hat 7.1/7.3. Sure, it's slow and has a funky |
| 33818 |
keyboard, but it beats the heck out of a No. 2 pencil and a |
| 33819 |
Big Chief tablet.</para> |
| 33820 |
|
| 33821 |
<para> |
| 33822 |
<emphasis>Update:</emphasis> upgraded to a |
| 33823 |
770Z Thinkpad (P2-366, 192 meg RAM) running FC3. Anyone |
| 33824 |
feel like donating a later-model laptop to a starving writer |
| 33825 |
<g>? |
| 33826 |
</para> |
| 33827 |
|
| 33828 |
<para> |
| 33829 |
<emphasis>Update:</emphasis> upgraded to a T61 Thinkpad |
| 33830 |
running Mandriva 2011. No longer starving <g>, |
| 33831 |
but not too proud to accept donations. |
| 33832 |
</para> |
| 33833 |
|
| 33834 |
</sect2> <!-- Hardware --> |
| 33835 |
|
| 33836 |
|
| 33837 |
<sect2 id="software-printware"> |
| 33838 |
<title>Software and Printware</title> |
| 33839 |
|
| 33840 |
<orderedlist id="software-printware2" numeration="lowerroman"> |
| 33841 |
|
| 33842 |
<listitem> |
| 33843 |
<para>Bram Moolenaar's powerful SGML-aware <ulink |
| 33844 |
url="http://www.vim.org">vim</ulink> text editor.</para> |
| 33845 |
</listitem> |
| 33846 |
|
| 33847 |
<listitem> |
| 33848 |
<para><ulink |
| 33849 |
url="http://www.netfolder.com/DSSSL/">OpenJade</ulink>, |
| 33850 |
a DSSSL rendering engine for converting SGML documents into other |
| 33851 |
formats.</para> |
| 33852 |
</listitem> |
| 33853 |
|
| 33854 |
<listitem> |
| 33855 |
<para><ulink url="http://nwalsh.com/docbook/dsssl/"> Norman |
| 33856 |
Walsh's DSSSL stylesheets</ulink>.</para> |
| 33857 |
</listitem> |
| 33858 |
|
| 33859 |
<listitem> |
| 33860 |
<para><citetitle pubwork="book">DocBook, The Definitive |
| 33861 |
Guide</citetitle>, by Norman Walsh and Leonard Muellner |
| 33862 |
(O'Reilly, ISBN 1-56592-580-7). This is still the standard |
| 33863 |
reference for anyone attempting to write a document in Docbook |
| 33864 |
SGML format.</para> |
| 33865 |
</listitem> |
| 33866 |
|
| 33867 |
</orderedlist> |
| 33868 |
|
| 33869 |
</sect2> <!-- Software and Printware --> |
| 33870 |
|
| 33871 |
</sect1> <!-- Tools Used --> |
| 33872 |
|
| 33873 |
|
| 33874 |
|
| 33875 |
<sect1 id="credits"> |
| 33876 |
<title>Credits</title> |
| 33877 |
|
| 33878 |
<para><emphasis>Community participation made this project |
| 33879 |
possible.</emphasis> The author gratefully acknowledges that |
| 33880 |
writing this book would have been unthinkable without |
| 33881 |
help and feedback from all you people out there.</para> |
| 33882 |
|
| 33883 |
<para><ulink url="mailto:[email protected]">Philippe Martin</ulink> |
| 33884 |
translated the first version (0.1) of this document into |
| 33885 |
DocBook/SGML. While not on the job at a small French company as a |
| 33886 |
software developer, he enjoys working on GNU/Linux documentation |
| 33887 |
and software, reading literature, playing music, and, for his |
| 33888 |
peace of mind, making merry with friends. You may run across him |
| 33889 |
somewhere in France or in the Basque Country, or you can email him |
| 33890 |
at <ulink url="mailto:[email protected]">[email protected]</ulink>.</para> |
| 33891 |
|
| 33892 |
<para>Philippe Martin also pointed out that positional parameters |
| 33893 |
past $9 are possible using {bracket} notation. (See <xref |
| 33894 |
linkend="ex17">).</para> |
| 33895 |
|
| 33896 |
<para><ulink url="mailto:[email protected]">Stéphane |
| 33897 |
Chazelas</ulink> sent a long list of corrections, additions, |
| 33898 |
and example scripts. More than a contributor, he had, in effect, |
| 33899 |
for a while taken on the role of <emphasis>co-editor</emphasis> |
| 33900 |
for this document. <foreignphrase>Merci |
| 33901 |
beaucoup!</foreignphrase></para> |
| 33902 |
|
| 33903 |
<para>Paulo Marcel Coelho Aragao offered many corrections, both major |
| 33904 |
and minor, and contributed quite a number of helpful |
| 33905 |
suggestions.</para> |
| 33906 |
|
| 33907 |
<para>I would like to especially thank <emphasis>Patrick |
| 33908 |
Callahan</emphasis>, <emphasis>Mike Novak</emphasis>, and |
| 33909 |
<emphasis>Pal Domokos</emphasis> for catching bugs, pointing out |
| 33910 |
ambiguities, and for suggesting clarifications and changes in the |
| 33911 |
preliminary version (0.1) of this document. Their lively |
| 33912 |
discussion of shell scripting and general documentation issues |
| 33913 |
inspired me to try to make this document more readable.</para> |
| 33914 |
|
| 33915 |
<para>I'm grateful to Jim Van Zandt for pointing out errors and |
| 33916 |
omissions in version 0.2 of this document. He also contributed |
| 33917 |
an instructive <link linkend="zfifo">example script</link>.</para> |
| 33918 |
|
| 33919 |
<para>Many thanks to <ulink |
| 33920 |
url="mailto:[email protected]">Jordi Sanfeliu</ulink> |
| 33921 |
for giving permission to use his fine tree script (<xref |
| 33922 |
linkend="tree">), and to Rick Boivie for revising it.</para> |
| 33923 |
|
| 33924 |
<para>Likewise, thanks to <ulink |
| 33925 |
url="mailto:[email protected]">Michel Charpentier</ulink> for |
| 33926 |
permission to use his <link linkend="dcref">dc</link> factoring script |
| 33927 |
(<xref linkend="factr">).</para> |
| 33928 |
|
| 33929 |
<para>Kudos to <ulink |
| 33930 |
url="mailto:[email protected]">Noah Friedman</ulink> |
| 33931 |
for permission to use his string function script (<xref |
| 33932 |
linkend="string">).</para> |
| 33933 |
|
| 33934 |
<para><ulink url="mailto:[email protected]">Emmanuel |
| 33935 |
Rouat</ulink> suggested corrections and additions on <link |
| 33936 |
linkend="commandsubref">command substitution</link>, |
| 33937 |
<link linkend="aliasref">aliases</link>, and <link |
| 33938 |
linkend="pathmanagement">path management</link>. He also |
| 33939 |
contributed a very nice sample <filename>.bashrc</filename> file |
| 33940 |
(<xref linkend="sample-bashrc">).</para> |
| 33941 |
|
| 33942 |
<para><ulink url="mailto:[email protected]">Heiner Steven</ulink> |
| 33943 |
kindly gave permission to use his base conversion script, <xref |
| 33944 |
linkend="base">. He also made a number of corrections and many |
| 33945 |
helpful suggestions. Special thanks.</para> |
| 33946 |
|
| 33947 |
<para>Rick Boivie contributed the delightfully recursive |
| 33948 |
<emphasis>pb.sh</emphasis> script (<xref linkend="pbook">), |
| 33949 |
revised the <emphasis>tree.sh</emphasis> script (<xref |
| 33950 |
linkend="tree">), and suggested performance improvements |
| 33951 |
for the <emphasis>monthlypmt.sh</emphasis> script (<xref |
| 33952 |
linkend="monthlypmt">).</para> |
| 33953 |
|
| 33954 |
<para>Florian Wisser enlightened me on some of the fine points of |
| 33955 |
testing strings (see <xref linkend="strtest">), and on other |
| 33956 |
matters.</para> |
| 33957 |
|
| 33958 |
<para>Oleg Philon sent suggestions concerning <link |
| 33959 |
linkend="cutref">cut</link> and <link |
| 33960 |
linkend="pidofref">pidof</link>.</para> |
| 33961 |
|
| 33962 |
<para>Michael Zick extended the <link linkend="emptyarray">empty |
| 33963 |
array</link> example to demonstrate some surprising array |
| 33964 |
properties. He also contributed the <emphasis>isspammer</emphasis> |
| 33965 |
scripts (<xref linkend="isspammer"> and <xref |
| 33966 |
linkend="isspammer2">).</para> |
| 33967 |
|
| 33968 |
<para>Marc-Jano Knopp sent corrections and clarifications on DOS |
| 33969 |
batch files.</para> |
| 33970 |
|
| 33971 |
<para>Hyun Jin Cha found several typos in the document in the |
| 33972 |
process of doing a Korean translation. Thanks for pointing |
| 33973 |
these out.</para> |
| 33974 |
|
| 33975 |
<para>Andreas Abraham sent in a long list of typographical |
| 33976 |
errors and other corrections. Special thanks!</para> |
| 33977 |
|
| 33978 |
|
| 33979 |
<para>Others contributing scripts, making helpful suggestions, and |
| 33980 |
pointing out errors were Gabor Kiss, Leopold Toetsch, |
| 33981 |
Peter Tillier, Marcus Berglof, Tony Richardson, Nick Drage |
| 33982 |
(script ideas!), Rich Bartell, Jess Thrysoee, Adam Lazur, Bram |
| 33983 |
Moolenaar, Baris Cicek, Greg Keraunen, Keith Matthews, Sandro |
| 33984 |
Magi, Albert Reiner, Dim Segebart, Rory Winston, Lee Bigelow, |
| 33985 |
Wayne Pollock, <quote>jipe,</quote> <quote>bojster,</quote> |
| 33986 |
<quote>nyal,</quote> <quote>Hobbit,</quote> <quote>Ender,</quote> |
| 33987 |
<quote>Little Monster</quote> (Alexis), <quote>Mark,</quote> |
| 33988 |
<quote>Patsie,</quote> <quote>vladz,</quote> Peggy Russell, |
| 33989 |
Emilio Conti, Ian. D. Allen, Hans-Joerg Diers, Arun Giridhar, |
| 33990 |
Dennis Leeuw, Dan Jacobson, Aurelio Marinho Jargas, Edward |
| 33991 |
Scholtz, Jean Helou, Chris Martin, Lee Maschmeyer, Bruno Haible, |
| 33992 |
Wilbert Berendsen, Sebastien Godard, Bjön Eriksson, John |
| 33993 |
MacDonald, John Lange, Joshua Tschida, Troy Engel, Manfred |
| 33994 |
Schwarb, Amit Singh, Bill Gradwohl, E. Choroba, David Lombard, |
| 33995 |
Jason Parker, Steve Parker, Bruce W. Clare, William Park, Vernia |
| 33996 |
Damiano, Mihai Maties, Mark Alexander, Jeremy Impson, Ken Fuchs, |
| 33997 |
Jared Martin, Frank Wang, Sylvain Fourmanoit, Matthew Sage, |
| 33998 |
Matthew Walker, Kenny Stauffer, Filip Moritz, Andrzej Stefanski, |
| 33999 |
Daniel Albers, Jeffrey Haemer, Stefano Palmeri, Nils Radtke, |
| 34000 |
Sigurd Solaas, Serghey Rodin, Jeroen Domburg, Alfredo Pironti, |
| 34001 |
Phil Braham, Bruno de Oliveira Schneider, Stefano Falsetto, |
| 34002 |
Chris Morgan, Walter Dnes, Linc Fessenden, Michael Iatrou, Pharis |
| 34003 |
Monalo, Jesse Gough, Fabian Kreutz, Mark Norman, Harald Koenig, |
| 34004 |
Dan Stromberg, Peter Knowles, Francisco Lobo, Mariusz Gniazdowski, |
| 34005 |
Sebastian Arming, Chetankumar Phulpagare, Benno Schulenberg, |
| 34006 |
Tedman Eng, Jochen DeSmet, Juan Nicolas Ruiz, Oliver Beckstein, |
| 34007 |
Achmed Darwish, Dotan Barak, Richard Neill, Albert Siersema, |
| 34008 |
Omair Eshkenazi, Geoff Lee, Graham Ewart, JuanJo Ciarlante, |
| 34009 |
Cliff Bamford, Nathan Coulter, Ramses Rodriguez Martinez, |
| 34010 |
Evgeniy Ivanov, Craig Barnes, George Dimitriu, Kevin LeBlanc, |
| 34011 |
Antonio Macchi, Tomas Pospisek, David Wheeler, Erik Brandsberg, |
| 34012 |
YongYe, Andreas Kühne, Pádraig Brady, Joseph |
| 34013 |
Steinhauser, and David Lawyer (himself an author of four |
| 34014 |
HOWTOs).</para> |
| 34015 |
|
| 34016 |
<para>My gratitude to <ulink url="mailto:[email protected]">Chet |
| 34017 |
Ramey</ulink> and Brian Fox for writing <firstterm>Bash</firstterm>, |
| 34018 |
and building into it elegant and powerful scripting |
| 34019 |
capabilities rivaling those of <firstterm>ksh</firstterm>.</para> |
| 34020 |
|
| 34021 |
<para>Very special thanks to the hard-working volunteers at |
| 34022 |
the <ulink url="http://www.tldp.org">Linux Documentation |
| 34023 |
Project</ulink>. The LDP hosts a repository of Linux knowledge |
| 34024 |
and lore, and has, to a great extent, enabled the publication |
| 34025 |
of this book.</para> |
| 34026 |
|
| 34027 |
<para>Thanks and appreciation to IBM, Red Hat, Google, the <ulink |
| 34028 |
url="http://www.fsf.org">Free Software Foundation</ulink>, and |
| 34029 |
all the good people fighting the good fight to keep Open Source |
| 34030 |
software free and open.</para> |
| 34031 |
|
| 34032 |
<para>Belated thanks to my fourth grade teacher, Miss Spencer, |
| 34033 |
for emotional support and for convincing me that maybe, just |
| 34034 |
maybe I wasn't a total loss.</para> |
| 34035 |
|
| 34036 |
<para>Thanks most of all to my wife, Anita, for her encouragement, |
| 34037 |
inspiration, and emotional support.</para> |
| 34038 |
|
| 34039 |
</sect1> <!-- Credits --> |
| 34040 |
|
| 34041 |
<sect1 id="disclaimer"> |
| 34042 |
<title>Disclaimer</title> |
| 34043 |
|
| 34044 |
|
| 34045 |
<para>(This is a variant of the standard <ulink |
| 34046 |
url="http://www.tldp.org">LDP</ulink> disclaimer.)</para> |
| 34047 |
|
| 34048 |
<para>No liability for the contents of this document can be |
| 34049 |
accepted. Use the concepts, examples and information at your |
| 34050 |
own risk. There may be errors, omissions, and inaccuracies |
| 34051 |
that could cause you to lose data, harm your system, or induce |
| 34052 |
involuntary electrocution, so <emphasis>proceed with appropriate |
| 34053 |
caution</emphasis>. The author takes no responsibility for any |
| 34054 |
damages, incidental or otherwise.</para> |
| 34055 |
|
| 34056 |
<para>As it happens, it is highly unlikely that either you or |
| 34057 |
your system will suffer ill effects, aside from uncontrollable |
| 34058 |
hiccups. In fact, the <foreignphrase>raison |
| 34059 |
d'etre</foreignphrase> of this book is to enable its readers |
| 34060 |
to analyze shell scripts and determine whether they have <link |
| 34061 |
linkend="gotchas">unanticipated consequences</link>.</para> |
| 34062 |
|
| 34063 |
</sect1> <!-- Disclaimer --> |
| 34064 |
|
| 34065 |
</chapter> <!-- End Notes --> |
| 34066 |
|
| 34067 |
|
| 34068 |
|
| 34069 |
<bibliography id="biblio"> |
| 34070 |
<anchor id="biblioref"> |
| 34071 |
|
| 34072 |
<epigraph> |
| 34073 |
<para>Those who do not understand UNIX are condemned to reinvent it, |
| 34074 |
poorly.</para> |
| 34075 |
<para>--Henry Spencer</para> |
| 34076 |
<para><anchor id="denningref"></para> |
| 34077 |
</epigraph> |
| 34078 |
|
| 34079 |
|
| 34080 |
<biblioentry> |
| 34081 |
<authorgroup> |
| 34082 |
<editor><firstname>Peter</firstname><surname>Denning</surname></editor> |
| 34083 |
</authorgroup> |
| 34084 |
<title>Computers Under Attack: Intruders, Worms, and Viruses</title> |
| 34085 |
<publisher> |
| 34086 |
<publishername>ACM Press</publishername> |
| 34087 |
</publisher> |
| 34088 |
<copyright> |
| 34089 |
<year>1990</year> |
| 34090 |
</copyright> |
| 34091 |
<isbn>0-201-53067-8</isbn> |
| 34092 |
<abstract><para>This compendium contains a couple of articles on |
| 34093 |
shell script viruses.</para> |
| 34094 |
<para>*</para> |
| 34095 |
</abstract> |
| 34096 |
</biblioentry> |
| 34097 |
|
| 34098 |
<biblioentry> |
| 34099 |
<authorgroup> |
| 34100 |
<author><firstname>Ken</firstname><surname>Burtch</surname></author> |
| 34101 |
</authorgroup> |
| 34102 |
<title><ulink url="http://www.samspublishing.com/title/0672326426">Linux Shell Scripting with Bash</ulink></title> |
| 34103 |
<edition>1st edition</edition> |
| 34104 |
<publisher> |
| 34105 |
<publishername>Sams Publishing (Pearson)</publishername> |
| 34106 |
</publisher> |
| 34107 |
<copyright> |
| 34108 |
<year>2004</year> |
| 34109 |
</copyright> |
| 34110 |
<isbn>0672326426</isbn> |
| 34111 |
<abstract><para> |
| 34112 |
Covers much of the same material as the <emphasis>ABS |
| 34113 |
Guide</emphasis>, though in a different style.</para> |
| 34114 |
<para>*</para> |
| 34115 |
<para><anchor id="dgsedref"></para> |
| 34116 |
</abstract> |
| 34117 |
</biblioentry> |
| 34118 |
|
| 34119 |
|
| 34120 |
<biblioentry> |
| 34121 |
<authorgroup> |
| 34122 |
<author><firstname>Daniel</firstname><surname>Goldman</surname></author> |
| 34123 |
</authorgroup> |
| 34124 |
<title><ulink url="http://www.sed-book.com/">Definitive Guide |
| 34125 |
to Sed</ulink></title> |
| 34126 |
<edition>1st edition</edition> |
| 34127 |
<copyright> |
| 34128 |
<year>2013</year> |
| 34129 |
</copyright> |
| 34130 |
<abstract><para> |
| 34131 |
This ebook is an excellent introduction to |
| 34132 |
<firstterm>sed</firstterm>. Rather than being a conversion from |
| 34133 |
a printed volume, it was specifically designed and formatted |
| 34134 |
for viewing on an ebook reader. Well-written, informative, |
| 34135 |
and useful as a reference as well as a tutorial. Highly |
| 34136 |
recommended.</para> |
| 34137 |
<para>*</para> |
| 34138 |
</abstract> |
| 34139 |
</biblioentry> |
| 34140 |
|
| 34141 |
|
| 34142 |
|
| 34143 |
<biblioentry> |
| 34144 |
<authorgroup> |
| 34145 |
<author><firstname>Dale</firstname><surname>Dougherty</surname></author> |
| 34146 |
<author><firstname>Arnold</firstname><surname>Robbins</surname></author> |
| 34147 |
</authorgroup> |
| 34148 |
<title>Sed and Awk</title> |
| 34149 |
<edition>2nd edition</edition> |
| 34150 |
<publisher> |
| 34151 |
<publishername>O'Reilly and Associates</publishername> |
| 34152 |
</publisher> |
| 34153 |
<copyright> |
| 34154 |
<year>1997</year> |
| 34155 |
</copyright> |
| 34156 |
<isbn>1-156592-225-5</isbn> |
| 34157 |
<abstract><para> |
| 34158 |
Unfolding the full power of shell scripting requires at least a passing |
| 34159 |
familiarity with <link linkend="sedref"><firstterm>sed</firstterm> |
| 34160 |
and <firstterm>awk</firstterm></link>. This is the classic |
| 34161 |
tutorial. It includes an excellent introduction to |
| 34162 |
<firstterm>Regular Expressions</firstterm>. Recommended.</para> |
| 34163 |
<para>*</para> |
| 34164 |
</abstract> |
| 34165 |
</biblioentry> |
| 34166 |
|
| 34167 |
<biblioentry> |
| 34168 |
<authorgroup> |
| 34169 |
<author><firstname>Jeffrey</firstname><surname>Friedl</surname></author> |
| 34170 |
</authorgroup> |
| 34171 |
<title>Mastering Regular Expressions</title> |
| 34172 |
<publisher> |
| 34173 |
<publishername>O'Reilly and Associates</publishername> |
| 34174 |
</publisher> |
| 34175 |
<copyright> |
| 34176 |
<year>2002</year> |
| 34177 |
</copyright> |
| 34178 |
<isbn>0-596-00289-0</isbn> |
| 34179 |
<abstract><para>Still the best all-around reference on <link |
| 34180 |
linkend="regexref">Regular Expressions</link>.</para> |
| 34181 |
<para>*</para> |
| 34182 |
</abstract> |
| 34183 |
</biblioentry> |
| 34184 |
|
| 34185 |
<biblioentry> |
| 34186 |
<authorgroup> |
| 34187 |
<author><firstname>Aeleen</firstname><surname>Frisch</surname></author> |
| 34188 |
</authorgroup> |
| 34189 |
<title>Essential System Administration</title> |
| 34190 |
<edition>3rd edition</edition> |
| 34191 |
<publisher> |
| 34192 |
<publishername>O'Reilly and Associates</publishername> |
| 34193 |
</publisher> |
| 34194 |
<copyright> |
| 34195 |
<year>2002</year> |
| 34196 |
</copyright> |
| 34197 |
<isbn>0-596-00343-9</isbn> |
| 34198 |
<abstract><para>This excellent manual provides a decent introduction |
| 34199 |
to shell scripting from a sys admin point of view. It includes |
| 34200 |
comprehensive explanations of the startup and initialization |
| 34201 |
scripts in a UNIX system.</para> |
| 34202 |
<para>*</para> |
| 34203 |
<para><anchor id="kochanref"></para> |
| 34204 |
</abstract> |
| 34205 |
</biblioentry> |
| 34206 |
|
| 34207 |
<biblioentry> |
| 34208 |
<authorgroup> |
| 34209 |
<author><firstname>Stephen</firstname><surname>Kochan</surname></author> |
| 34210 |
<author><firstname>Patrick</firstname><surname>Wood</surname></author> |
| 34211 |
</authorgroup> |
| 34212 |
<title>Unix Shell Programming</title> |
| 34213 |
<publisher> |
| 34214 |
<publishername>Hayden</publishername> |
| 34215 |
</publisher> |
| 34216 |
<copyright> |
| 34217 |
<year>1990</year> |
| 34218 |
</copyright> |
| 34219 |
<isbn>067248448X</isbn> |
| 34220 |
<abstract> |
| 34221 |
<para>Still considered a standard reference, though somewhat dated, and |
| 34222 |
a bit <quote>wooden</quote> stylistically speaking. |
| 34223 |
<footnote><para>It was hard to resist the obvious pun. No slight |
| 34224 |
intended, since the book is a pretty decent introduction to |
| 34225 |
the basic concepts of shell scripting.</para></footnote> |
| 34226 |
In fact, this book was the <firstterm>ABS Guide</firstterm> author's |
| 34227 |
first exposure to UNIX shell scripting, lo these many years ago.</para> |
| 34228 |
<para>*</para> |
| 34229 |
</abstract> |
| 34230 |
</biblioentry> |
| 34231 |
|
| 34232 |
<biblioentry> |
| 34233 |
<authorgroup> |
| 34234 |
<author><firstname>Neil</firstname><surname>Matthew</surname></author> |
| 34235 |
<author><firstname>Richard</firstname><surname>Stones</surname></author> |
| 34236 |
</authorgroup> |
| 34237 |
<title>Beginning Linux Programming</title> |
| 34238 |
<publisher> |
| 34239 |
<publishername>Wrox Press</publishername> |
| 34240 |
</publisher> |
| 34241 |
<copyright> |
| 34242 |
<year>1996</year> |
| 34243 |
</copyright> |
| 34244 |
<isbn>1874416680</isbn> |
| 34245 |
<abstract><para>Surprisingly good in-depth coverage of various |
| 34246 |
programming languages available for Linux, including a fairly |
| 34247 |
strong chapter on shell scripting.</para> |
| 34248 |
<para>*</para> |
| 34249 |
</abstract> |
| 34250 |
</biblioentry> |
| 34251 |
|
| 34252 |
<biblioentry id="mayerref"> |
| 34253 |
<authorgroup> |
| 34254 |
<author><firstname>Herbert</firstname><surname>Mayer</surname></author> |
| 34255 |
</authorgroup> |
| 34256 |
<title>Advanced C Programming on the IBM PC</title> |
| 34257 |
<publisher> |
| 34258 |
<publishername>Windcrest Books</publishername> |
| 34259 |
</publisher> |
| 34260 |
<copyright> |
| 34261 |
<year>1989</year> |
| 34262 |
</copyright> |
| 34263 |
<isbn>0830693637</isbn> |
| 34264 |
<abstract><para>Excellent coverage of algorithms and general |
| 34265 |
programming practices. Highly recommended, but unfortunately |
| 34266 |
out of print.</para> |
| 34267 |
<para>*</para> |
| 34268 |
</abstract> |
| 34269 |
</biblioentry> |
| 34270 |
|
| 34271 |
<biblioentry> |
| 34272 |
<authorgroup> |
| 34273 |
<author><firstname>David</firstname><surname>Medinets</surname></author> |
| 34274 |
</authorgroup> |
| 34275 |
<title>Unix Shell Programming Tools</title> |
| 34276 |
<publisher> |
| 34277 |
<publishername>McGraw-Hill</publishername> |
| 34278 |
</publisher> |
| 34279 |
<copyright> |
| 34280 |
<year>1999</year> |
| 34281 |
</copyright> |
| 34282 |
<isbn>0070397333</isbn> |
| 34283 |
<abstract><para>Pretty good treatment of shell scripting, with |
| 34284 |
examples, and a short intro to Tcl and Perl.</para> |
| 34285 |
<para>*</para> |
| 34286 |
</abstract> |
| 34287 |
</biblioentry> |
| 34288 |
|
| 34289 |
<biblioentry> |
| 34290 |
<authorgroup> |
| 34291 |
<author><firstname>Cameron</firstname><surname>Newham</surname></author> |
| 34292 |
<author><firstname>Bill</firstname><surname>Rosenblatt</surname></author> |
| 34293 |
</authorgroup> |
| 34294 |
<title>Learning the Bash Shell</title> |
| 34295 |
<edition>2nd edition</edition> |
| 34296 |
<publisher> |
| 34297 |
<publishername>O'Reilly and Associates</publishername> |
| 34298 |
</publisher> |
| 34299 |
<copyright> |
| 34300 |
<year>1998</year> |
| 34301 |
</copyright> |
| 34302 |
<isbn>1-56592-347-2</isbn> |
| 34303 |
<abstract><para>This is a valiant effort at a decent shell primer, |
| 34304 |
but sadly deficient in its coverage of writing scripts and |
| 34305 |
lacking sufficient examples.</para> |
| 34306 |
<para>*</para> |
| 34307 |
</abstract> |
| 34308 |
</biblioentry> |
| 34309 |
|
| 34310 |
<biblioentry> |
| 34311 |
<authorgroup> |
| 34312 |
<author><firstname>Anatole</firstname><surname>Olczak</surname></author> |
| 34313 |
</authorgroup> |
| 34314 |
<title>Bourne Shell Quick Reference Guide</title> |
| 34315 |
<publisher> |
| 34316 |
<publishername>ASP, Inc.</publishername> |
| 34317 |
</publisher> |
| 34318 |
<copyright> |
| 34319 |
<year>1991</year> |
| 34320 |
</copyright> |
| 34321 |
<isbn>093573922X</isbn> |
| 34322 |
<abstract><para>A very handy pocket reference, despite lacking |
| 34323 |
coverage of Bash-specific features.</para> |
| 34324 |
<para>*</para> |
| 34325 |
</abstract> |
| 34326 |
</biblioentry> |
| 34327 |
|
| 34328 |
<biblioentry> |
| 34329 |
<authorgroup> |
| 34330 |
<author><firstname>Jerry</firstname><surname>Peek</surname></author> |
| 34331 |
<author><firstname>Tim</firstname><surname>O'Reilly</surname></author> |
| 34332 |
<author><firstname>Mike</firstname><surname>Loukides</surname></author> |
| 34333 |
</authorgroup> |
| 34334 |
<title>Unix Power Tools</title> |
| 34335 |
<edition>3rd edition</edition> |
| 34336 |
<publisher> |
| 34337 |
<publishername>O'Reilly and Associates</publishername> |
| 34338 |
</publisher> |
| 34339 |
<publisher> |
| 34340 |
<publishername>Random House</publishername> |
| 34341 |
</publisher> |
| 34342 |
<copyright> |
| 34343 |
<year>2002</year> |
| 34344 |
</copyright> |
| 34345 |
<isbn>0-596-00330-7</isbn> |
| 34346 |
<abstract><para>Contains a couple of sections of very informative |
| 34347 |
in-depth articles on shell programming, but falls short of being |
| 34348 |
a self-teaching manual. It reproduces much of the <link |
| 34349 |
linkend="regexref">Regular Expressions</link> tutorial from the |
| 34350 |
Dougherty and Robbins book, above. The comprehensive coverage |
| 34351 |
of UNIX commands makes this book worthy of a place on your |
| 34352 |
bookshelf.</para> |
| 34353 |
<para>*</para> |
| 34354 |
</abstract> |
| 34355 |
</biblioentry> |
| 34356 |
|
| 34357 |
<biblioentry> |
| 34358 |
<authorgroup> |
| 34359 |
<author><firstname>Clifford</firstname><surname>Pickover</surname></author> |
| 34360 |
</authorgroup> |
| 34361 |
<title>Computers, Pattern, Chaos, and Beauty</title> |
| 34362 |
<publisher> |
| 34363 |
<publishername>St. Martin's Press</publishername> |
| 34364 |
</publisher> |
| 34365 |
<copyright> |
| 34366 |
<year>1990</year> |
| 34367 |
</copyright> |
| 34368 |
<isbn>0-312-04123-3</isbn> |
| 34369 |
<abstract><para>A treasure trove of ideas and recipes for |
| 34370 |
computer-based exploration of mathematical oddities.</para> |
| 34371 |
<para>*</para> |
| 34372 |
</abstract> |
| 34373 |
</biblioentry> |
| 34374 |
|
| 34375 |
<biblioentry> |
| 34376 |
<authorgroup> |
| 34377 |
<author><firstname>George</firstname><surname>Polya</surname></author> |
| 34378 |
</authorgroup> |
| 34379 |
<title>How To Solve It</title> |
| 34380 |
<publisher> |
| 34381 |
<publishername>Princeton University Press</publishername> |
| 34382 |
</publisher> |
| 34383 |
<copyright> |
| 34384 |
<year>1973</year> |
| 34385 |
</copyright> |
| 34386 |
<isbn>0-691-02356-5</isbn> |
| 34387 |
<abstract><para>The classic tutorial on problem-solving methods |
| 34388 |
(algorithms), with special emphasis on how to teach them.</para> |
| 34389 |
<para>*</para> |
| 34390 |
</abstract> |
| 34391 |
</biblioentry> |
| 34392 |
|
| 34393 |
<biblioentry> |
| 34394 |
<authorgroup> |
| 34395 |
<author><firstname>Chet</firstname><surname>Ramey</surname></author> |
| 34396 |
<author><firstname>Brian</firstname><surname>Fox</surname></author> |
| 34397 |
</authorgroup> |
| 34398 |
<title><ulink |
| 34399 |
url="http://www.network-theory.co.uk/bash/manual/">The GNU Bash Reference Manual</ulink></title> |
| 34400 |
<publisher> |
| 34401 |
<publishername>Network Theory Ltd</publishername> |
| 34402 |
</publisher> |
| 34403 |
<copyright> |
| 34404 |
<year>2003</year> |
| 34405 |
</copyright> |
| 34406 |
<isbn>0-9541617-7-7</isbn> |
| 34407 |
<abstract><para>This manual is the definitive reference for |
| 34408 |
GNU Bash. The authors of this manual, Chet Ramey and Brian Fox, |
| 34409 |
are the original developers of GNU Bash. For each copy sold, |
| 34410 |
the publisher donates $1 to the Free Software Foundation.</para> |
| 34411 |
<para>*</para> |
| 34412 |
</abstract> |
| 34413 |
</biblioentry> |
| 34414 |
|
| 34415 |
<biblioentry> |
| 34416 |
<authorgroup> |
| 34417 |
<author><firstname>Arnold</firstname><surname>Robbins</surname></author> |
| 34418 |
</authorgroup> |
| 34419 |
<title>Bash Reference Card</title> |
| 34420 |
<publisher> |
| 34421 |
<publishername>SSC</publishername> |
| 34422 |
</publisher> |
| 34423 |
<copyright> |
| 34424 |
<year>1998</year> |
| 34425 |
</copyright> |
| 34426 |
<isbn>1-58731-010-5</isbn> |
| 34427 |
<abstract> |
| 34428 |
<para>Excellent Bash pocket reference (don't leave home without it, |
| 34429 |
especially if you're a sysadmin). A bargain at $4.95, but |
| 34430 |
unfortunately no longer available for free download.</para> |
| 34431 |
<para>*</para> |
| 34432 |
</abstract> |
| 34433 |
</biblioentry> |
| 34434 |
|
| 34435 |
<biblioentry> |
| 34436 |
<authorgroup> |
| 34437 |
<author><firstname>Arnold</firstname><surname>Robbins</surname></author> |
| 34438 |
</authorgroup> |
| 34439 |
<title>Effective Awk Programming</title> |
| 34440 |
<publisher> |
| 34441 |
<publishername>Free Software Foundation / O'Reilly and Associates</publishername> |
| 34442 |
</publisher> |
| 34443 |
<copyright> |
| 34444 |
<year>2000</year> |
| 34445 |
</copyright> |
| 34446 |
<isbn>1-882114-26-4</isbn> |
| 34447 |
<abstract> |
| 34448 |
<para>The absolute best <link linkend="awkref">awk</link> |
| 34449 |
tutorial and reference. The free electronic version of this |
| 34450 |
book is part of the <firstterm>awk</firstterm> documentation, |
| 34451 |
and printed copies of the latest version are available from |
| 34452 |
O'Reilly and Associates.</para> |
| 34453 |
<para>This book served as an inspiration for the author |
| 34454 |
of the <firstterm>ABS Guide</firstterm>.</para> |
| 34455 |
<para>*</para> |
| 34456 |
</abstract> |
| 34457 |
</biblioentry> |
| 34458 |
|
| 34459 |
<biblioentry> |
| 34460 |
<authorgroup> |
| 34461 |
<author><firstname>Bill</firstname><surname>Rosenblatt</surname></author> |
| 34462 |
</authorgroup> |
| 34463 |
<title>Learning the Korn Shell</title> |
| 34464 |
<publisher> |
| 34465 |
<publishername>O'Reilly and Associates</publishername> |
| 34466 |
</publisher> |
| 34467 |
<copyright> |
| 34468 |
<year>1993</year> |
| 34469 |
</copyright> |
| 34470 |
<isbn>1-56592-054-6</isbn> |
| 34471 |
<abstract> |
| 34472 |
<para><anchor id="kornshellref">This well-written book contains |
| 34473 |
some excellent pointers on shell scripting in general.</para> |
| 34474 |
<para>*</para> |
| 34475 |
</abstract> |
| 34476 |
</biblioentry> |
| 34477 |
|
| 34478 |
<biblioentry> |
| 34479 |
<authorgroup> |
| 34480 |
<author><firstname>Paul</firstname><surname>Sheer</surname></author> |
| 34481 |
</authorgroup> |
| 34482 |
<title>LINUX: Rute User's Tutorial and Exposition</title> |
| 34483 |
<edition>1st edition</edition> |
| 34484 |
<publisher> |
| 34485 |
<publishername></publishername> |
| 34486 |
</publisher> |
| 34487 |
<copyright> |
| 34488 |
<year>2002</year> |
| 34489 |
</copyright> |
| 34490 |
<isbn>0-13-033351-4</isbn> |
| 34491 |
<abstract> |
| 34492 |
<para>Very detailed and readable introduction to Linux system |
| 34493 |
administration.</para> |
| 34494 |
<para>The book is available in print, or |
| 34495 |
<ulink |
| 34496 |
url="http://burks.brighton.ac.uk/burks/linux/rute/rute.htm">on-line</ulink>.</para> |
| 34497 |
<para>*</para> |
| 34498 |
</abstract> |
| 34499 |
</biblioentry> |
| 34500 |
|
| 34501 |
<biblioentry> |
| 34502 |
<authorgroup> |
| 34503 |
<author><firstname>Ellen</firstname><surname>Siever</surname></author> |
| 34504 |
<author><surname>the staff of O'Reilly and Associates</surname></author> |
| 34505 |
</authorgroup> |
| 34506 |
<title>Linux in a Nutshell</title> |
| 34507 |
<edition>2nd edition</edition> |
| 34508 |
<publisher> |
| 34509 |
<publishername>O'Reilly and Associates</publishername> |
| 34510 |
</publisher> |
| 34511 |
<copyright> |
| 34512 |
<year>1999</year> |
| 34513 |
</copyright> |
| 34514 |
<isbn>1-56592-585-8</isbn> |
| 34515 |
<abstract><para>The all-around best Linux command reference. |
| 34516 |
It even has a Bash section.</para> |
| 34517 |
<para>*</para> |
| 34518 |
</abstract> |
| 34519 |
</biblioentry> |
| 34520 |
|
| 34521 |
<biblioentry> |
| 34522 |
<authorgroup> |
| 34523 |
<author><firstname>Dave</firstname><surname>Taylor</surname></author> |
| 34524 |
</authorgroup> |
| 34525 |
<title>Wicked Cool Shell Scripts: 101 Scripts for Linux, Mac OS X, and Unix Systems</title> |
| 34526 |
<edition>1st edition</edition> |
| 34527 |
<publisher> |
| 34528 |
<publishername>No Starch Press</publishername> |
| 34529 |
</publisher> |
| 34530 |
<copyright> |
| 34531 |
<year>2004</year> |
| 34532 |
</copyright> |
| 34533 |
<isbn>1-59327-012-7</isbn> |
| 34534 |
<abstract><para>Pretty much what the title promises . . .</para> |
| 34535 |
<para>*</para> |
| 34536 |
</abstract> |
| 34537 |
</biblioentry> |
| 34538 |
|
| 34539 |
<biblioentry> |
| 34540 |
<title>The UNIX CD Bookshelf</title> |
| 34541 |
<edition>3rd edition</edition> |
| 34542 |
<publisher> |
| 34543 |
<publishername>O'Reilly and Associates</publishername> |
| 34544 |
</publisher> |
| 34545 |
<copyright> |
| 34546 |
<year>2003</year> |
| 34547 |
</copyright> |
| 34548 |
<isbn>0-596-00392-7</isbn> |
| 34549 |
<abstract><para>An array of seven UNIX books on CD ROM, including |
| 34550 |
<citetitle pubwork="book">UNIX Power Tools</citetitle>, |
| 34551 |
<citetitle pubwork="book">Sed and Awk</citetitle>, and <citetitle |
| 34552 |
pubwork="book">Learning the Korn Shell</citetitle>. A complete |
| 34553 |
set of all the UNIX references and tutorials you would ever need |
| 34554 |
at about $130. Buy this one, even if it means going into debt |
| 34555 |
and not paying the rent.</para> |
| 34556 |
<para>Update: Seems to have somehow fallen out of print. |
| 34557 |
Ah, well. You can still buy the dead-tree editions of these books.</para> |
| 34558 |
<para>*</para> |
| 34559 |
</abstract> |
| 34560 |
</biblioentry> |
| 34561 |
|
| 34562 |
<biblioentry> |
| 34563 |
<abstract> |
| 34564 |
<para>The O'Reilly books on Perl. (Actually, |
| 34565 |
<emphasis>any</emphasis> O'Reilly books.)</para> |
| 34566 |
</abstract> |
| 34567 |
</biblioentry> |
| 34568 |
|
| 34569 |
<biblioentry> |
| 34570 |
<abstract> |
| 34571 |
<para>* * *</para> |
| 34572 |
<para><command>Other Resources</command></para> |
| 34573 |
</abstract> |
| 34574 |
</biblioentry> |
| 34575 |
|
| 34576 |
<biblioentry> |
| 34577 |
<abstract><para>Fioretti, Marco, <quote>Scripting for X |
| 34578 |
Productivity,</quote> <ulink url="linuxjournal.com"><citetitle |
| 34579 |
pubwork="journal">Linux Journal</citetitle></ulink>, Issue 113, |
| 34580 |
September, 2003, pp. 86-9.</para></abstract> |
| 34581 |
</biblioentry> |
| 34582 |
|
| 34583 |
<biblioentry> |
| 34584 |
<abstract> |
| 34585 |
<para>Ben Okopnik's well-written <citetitle |
| 34586 |
pubwork="article">introductory Bash scripting</citetitle> |
| 34587 |
articles in issues 53, 54, 55, 57, and 59 of the |
| 34588 |
<ulink url="http://www.linuxgazette.net"><citetitle |
| 34589 |
pubwork="journal">Linux Gazette</citetitle></ulink>, and his |
| 34590 |
explanation of <quote>The Deep, Dark Secrets of Bash</quote> |
| 34591 |
in issue 56.</para> |
| 34592 |
</abstract> |
| 34593 |
</biblioentry> |
| 34594 |
|
| 34595 |
<biblioentry> |
| 34596 |
<abstract><para>Chet Ramey's <citetitle pubwork="article">Bash - |
| 34597 |
The GNU Shell</citetitle>, a two-part series published in issues 3 |
| 34598 |
and 4 of the <ulink url="http://www.linuxjournal.com"><citetitle |
| 34599 |
pubwork="journal">Linux Journal</citetitle></ulink>, July-August |
| 34600 |
1994.</para> |
| 34601 |
</abstract> |
| 34602 |
</biblioentry> |
| 34603 |
|
| 34604 |
<biblioentry> |
| 34605 |
<abstract> |
| 34606 |
<para>Mike G's <ulink |
| 34607 |
url="http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html">Bash-Programming-Intro |
| 34608 |
HOWTO</ulink>.</para> |
| 34609 |
</abstract> |
| 34610 |
</biblioentry> |
| 34611 |
|
| 34612 |
<biblioentry> |
| 34613 |
<abstract> |
| 34614 |
<para>Richard's <ulink url="http://www.injunea.demon.co.uk/index.htm">Unix |
| 34615 |
Scripting Universe</ulink>.</para> |
| 34616 |
</abstract> |
| 34617 |
</biblioentry> |
| 34618 |
|
| 34619 |
<biblioentry> |
| 34620 |
<abstract> |
| 34621 |
<para><anchor id="bashfaq">Chet Ramey's |
| 34622 |
<ulink url="http://tiswww.case.edu/php/chet/bash/FAQ">Bash |
| 34623 |
FAQ</ulink>.</para> |
| 34624 |
</abstract> |
| 34625 |
</biblioentry> |
| 34626 |
|
| 34627 |
<biblioentry> |
| 34628 |
<abstract> |
| 34629 |
<para><ulink url="http://wooledge.org:8000/BashFAQ"> |
| 34630 |
Greg's WIKI: Bash FAQ</ulink>.</para> |
| 34631 |
</abstract> |
| 34632 |
</biblioentry> |
| 34633 |
|
| 34634 |
<biblioentry> |
| 34635 |
<abstract> |
| 34636 |
<para>Example shell scripts at <ulink |
| 34637 |
url="http://alge.anart.no/linux/scripts/">Lucc's Shell Scripts |
| 34638 |
</ulink>.</para> |
| 34639 |
</abstract> |
| 34640 |
</biblioentry> |
| 34641 |
|
| 34642 |
<biblioentry> |
| 34643 |
<abstract> |
| 34644 |
<para>Example shell scripts at <ulink |
| 34645 |
url="http://www.shelldorado.com">SHELLdorado </ulink>.</para> |
| 34646 |
</abstract> |
| 34647 |
</biblioentry> |
| 34648 |
|
| 34649 |
<biblioentry> |
| 34650 |
<abstract> |
| 34651 |
<para>Example shell scripts at <ulink |
| 34652 |
url="http://www.splode.com/~friedman/software/scripts/src/">Noah |
| 34653 |
Friedman's script site</ulink>.</para> |
| 34654 |
</abstract> |
| 34655 |
</biblioentry> |
| 34656 |
|
| 34657 |
<biblioentry> |
| 34658 |
<abstract> |
| 34659 |
<para><ulink url="http://bashcookbook.com/bashinfo/">Examples</ulink> |
| 34660 |
from the <citetitle pubwork="book">The Bash Scripting |
| 34661 |
Cookbook</citetitle>, by Albing, Vossen, and Newham.</para> |
| 34662 |
</abstract> |
| 34663 |
</biblioentry> |
| 34664 |
|
| 34665 |
<biblioentry> |
| 34666 |
<abstract> |
| 34667 |
<para>Example shell scripts at <ulink |
| 34668 |
url="http://www.zazzybob.com">zazzybob</ulink>.</para> |
| 34669 |
</abstract> |
| 34670 |
</biblioentry> |
| 34671 |
|
| 34672 |
<biblioentry> |
| 34673 |
<abstract> |
| 34674 |
<para>Steve Parker's <ulink |
| 34675 |
url="http://steve-parker.org/sh/sh.shtml">Shell Programming |
| 34676 |
Stuff</ulink>. In fact, all of his shell scripting |
| 34677 |
books are highly recommended. See also Steve's <ulink |
| 34678 |
url="http://nixshell.wordpress.com/2011/07/13/arcade-games-written-in-a-shell-script/">Arcade |
| 34679 |
Games written in a shell script</ulink>.</para> |
| 34680 |
</abstract> |
| 34681 |
</biblioentry> |
| 34682 |
|
| 34683 |
<biblioentry> |
| 34684 |
<abstract> |
| 34685 |
<para>An excellent collection of Bash scripting tips, tricks, |
| 34686 |
and resources at the <ulink |
| 34687 |
url="http://www.bash-hackers.org/wiki.doku.php">Bash Hackers |
| 34688 |
Wiki</ulink>.</para> |
| 34689 |
</abstract> |
| 34690 |
</biblioentry> |
| 34691 |
|
| 34692 |
<biblioentry> |
| 34693 |
<abstract> |
| 34694 |
<para>Giles Orr's <ulink |
| 34695 |
url="http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/">Bash-Prompt |
| 34696 |
HOWTO</ulink>.</para> |
| 34697 |
</abstract> |
| 34698 |
</biblioentry> |
| 34699 |
|
| 34700 |
<biblioentry> |
| 34701 |
<abstract> |
| 34702 |
<para>The <ulink |
| 34703 |
url="http://www.pixelbeat.org/cmdline.html"><emphasis>Pixelbeat</emphasis> |
| 34704 |
command-line reference</ulink>.</para> |
| 34705 |
</abstract> |
| 34706 |
</biblioentry> |
| 34707 |
|
| 34708 |
<biblioentry> |
| 34709 |
<abstract> |
| 34710 |
<para>Very nice <command>sed</command>, |
| 34711 |
<command>awk</command>, and regular expression tutorials at |
| 34712 |
<ulink url="http://www.grymoire.com/Unix/index.html">The UNIX |
| 34713 |
Grymoire</ulink>.</para> |
| 34714 |
</abstract> |
| 34715 |
</biblioentry> |
| 34716 |
|
| 34717 |
<biblioentry> |
| 34718 |
<abstract> |
| 34719 |
<para>The GNU <ulink |
| 34720 |
url="http://www.gnu.org/software/sed/manual/">sed</ulink> |
| 34721 |
and |
| 34722 |
<ulink url="http://www.gnu.org/software/gawk/manual/"> |
| 34723 |
gawk</ulink> manuals. As you recall, <link |
| 34724 |
linkend="gnugawk">gawk</link> is the enhanced GNU version of |
| 34725 |
<command>awk</command>.</para> |
| 34726 |
</abstract> |
| 34727 |
</biblioentry> |
| 34728 |
|
| 34729 |
<biblioentry> |
| 34730 |
<abstract> |
| 34731 |
<para>Many interesting sed scripts at the <ulink |
| 34732 |
url="http://sed.sourceforge.net/grabbag/"> seder's grab bag</ulink>.</para> |
| 34733 |
</abstract> |
| 34734 |
</biblioentry> |
| 34735 |
|
| 34736 |
<biblioentry> |
| 34737 |
<abstract> |
| 34738 |
<para>Tips and tricks at <ulink |
| 34739 |
url="http://linuxreviews.org"> Linux Reviews</ulink>.</para> |
| 34740 |
</abstract> |
| 34741 |
</biblioentry> |
| 34742 |
|
| 34743 |
<biblioentry> |
| 34744 |
<abstract> |
| 34745 |
<para>Trent Fisher's <ulink |
| 34746 |
url="http://www.cs.pdx.edu/~trent/gnu/groff/groff.html">groff |
| 34747 |
tutorial</ulink>.</para> |
| 34748 |
</abstract> |
| 34749 |
</biblioentry> |
| 34750 |
|
| 34751 |
<biblioentry> |
| 34752 |
<abstract> |
| 34753 |
<para>David Wheeler's <ulink |
| 34754 |
url="http://www.dwheeler.com/essays/filenames-in-shell.html">Filenames |
| 34755 |
in Shell</ulink> essay.</para> |
| 34756 |
</abstract> |
| 34757 |
</biblioentry> |
| 34758 |
|
| 34759 |
<biblioentry> |
| 34760 |
<abstract> |
| 34761 |
<para><quote>Shelltris</quote> and <quote>shellitaire</quote> |
| 34762 |
at <ulink url="http://www.shellscriptgames.com">Shell Script |
| 34763 |
Games</ulink>.</para> |
| 34764 |
</abstract> |
| 34765 |
</biblioentry> |
| 34766 |
|
| 34767 |
<biblioentry> |
| 34768 |
<abstract> |
| 34769 |
<para>YongYe's wonderfully complex <ulink |
| 34770 |
url="http://bash.deta.in/Tetris_Game.sh">Tetris game |
| 34771 |
script</ulink>.</para> |
| 34772 |
</abstract> |
| 34773 |
</biblioentry> |
| 34774 |
|
| 34775 |
<biblioentry> |
| 34776 |
<abstract> |
| 34777 |
<para>Mark Komarinski's <ulink |
| 34778 |
url="http://www.tldp.org/HOWTO/Printing-Usage-HOWTO.html">Printing-Usage |
| 34779 |
HOWTO</ulink>.</para> |
| 34780 |
</abstract> |
| 34781 |
</biblioentry> |
| 34782 |
|
| 34783 |
<biblioentry> |
| 34784 |
<abstract> |
| 34785 |
<para><ulink url="http://www.linux-usb.org/USB-guide/book1.html">The |
| 34786 |
Linux USB subsystem</ulink> (helpful in writing scripts affecting |
| 34787 |
USB peripherals).</para> |
| 34788 |
</abstract> |
| 34789 |
</biblioentry> |
| 34790 |
|
| 34791 |
<biblioentry> |
| 34792 |
<abstract> |
| 34793 |
<para>There is some nice material on <link |
| 34794 |
linkend="ioredirref">I/O redirection</link> in <ulink |
| 34795 |
url="http://sunsite.ualberta.ca/Documentation/Gnu/textutils-2.0/html_chapter/textutils_10.html"> |
| 34796 |
chapter 10 of the textutils documentation</ulink> at the <ulink |
| 34797 |
url="http://sunsite.ualberta.ca/Documentation"> University of |
| 34798 |
Alberta site</ulink>.</para> |
| 34799 |
</abstract> |
| 34800 |
</biblioentry> |
| 34801 |
|
| 34802 |
<biblioentry> |
| 34803 |
<abstract> |
| 34804 |
<para><ulink url="mailto:[email protected]">Rick |
| 34805 |
Hohensee</ulink> has written the |
| 34806 |
<firstterm>osimpa</firstterm> i386 assembler |
| 34807 |
entirely as Bash scripts.</para> |
| 34808 |
</abstract> |
| 34809 |
</biblioentry> |
| 34810 |
|
| 34811 |
<biblioentry> |
| 34812 |
<abstract> |
| 34813 |
<para><firstterm>dgatwood</firstterm> |
| 34814 |
has a very nice <ulink url="http://www.shellscriptgames.com/"> |
| 34815 |
shell script games</ulink> site, featuring a Tetris® |
| 34816 |
clone and solitaire.</para> |
| 34817 |
</abstract> |
| 34818 |
</biblioentry> |
| 34819 |
|
| 34820 |
<biblioentry> |
| 34821 |
<abstract> |
| 34822 |
<para>Aurelio Marinho Jargas has written a <ulink |
| 34823 |
url="http://txt2regex.sf.net">Regular expression |
| 34824 |
wizard</ulink>. He has also written an informative <ulink |
| 34825 |
url="http://guia-er.sf.net">book</ulink> on Regular Expressions, |
| 34826 |
in Portuguese.</para> |
| 34827 |
</abstract> |
| 34828 |
</biblioentry> |
| 34829 |
|
| 34830 |
<biblioentry> |
| 34831 |
<abstract> |
| 34832 |
<para><ulink url="mailto:[email protected]">Ben |
| 34833 |
Tomkins</ulink> has created the <ulink |
| 34834 |
url="http://bashnavigator.sourceforge.net"> |
| 34835 |
Bash Navigator</ulink> directory management tool.</para> |
| 34836 |
</abstract> |
| 34837 |
</biblioentry> |
| 34838 |
|
| 34839 |
<biblioentry> |
| 34840 |
<abstract> |
| 34841 |
<para><ulink url="mailto:[email protected]">William Park</ulink> |
| 34842 |
has been working on a project |
| 34843 |
to incorporate certain <firstterm>Awk</firstterm> and |
| 34844 |
<firstterm>Python</firstterm> features into Bash. Among these is |
| 34845 |
a <firstterm>gdbm</firstterm> interface. He has released |
| 34846 |
<firstterm>bashdiff</firstterm> |
| 34847 |
on <ulink url="http://freshmeat.net">Freshmeat.net</ulink>. He |
| 34848 |
has an <ulink |
| 34849 |
url="http://linuxgazette.net/108/park.html">article</ulink> |
| 34850 |
in the November, 2004 issue of the <ulink |
| 34851 |
url="http://www.linuxgazette.net"><citetitle |
| 34852 |
pubwork="journal">Linux Gazette</citetitle></ulink> |
| 34853 |
on adding string functions to Bash, with a <ulink |
| 34854 |
url="http://linuxgazette.net/109/park.html">followup |
| 34855 |
article</ulink> in the December issue, and <ulink |
| 34856 |
url="http://linuxgazette.net/110/park.htm">yet another</ulink> |
| 34857 |
in the January, 2005 issue.</para> |
| 34858 |
</abstract> |
| 34859 |
</biblioentry> |
| 34860 |
|
| 34861 |
<biblioentry> |
| 34862 |
<abstract> |
| 34863 |
<para>Peter Knowles has written an |
| 34864 |
<ulink url="http://booklistgensh.peterknowles.com/">elaborate |
| 34865 |
Bash script</ulink> that generates a book list on the <ulink |
| 34866 |
url="http://www.dottocomu.com/b/archives/002571.html">Sony |
| 34867 |
Librie</ulink> e-book reader. This useful tool facilitates |
| 34868 |
loading non-DRM user content on the <emphasis>Librie</emphasis> |
| 34869 |
(and the newer <emphasis>PRS-xxx-series</emphasis> devices).</para> |
| 34870 |
|
| 34871 |
</abstract> |
| 34872 |
</biblioentry> |
| 34873 |
|
| 34874 |
<biblioentry> |
| 34875 |
<abstract> |
| 34876 |
<para>Tim Waugh's <ulink |
| 34877 |
url="http://cyberelk.net/tim/xmlto/">xmlto</ulink> is an |
| 34878 |
elaborate Bash script for converting Docbook XML documents to |
| 34879 |
other formats.</para> |
| 34880 |
</abstract> |
| 34881 |
</biblioentry> |
| 34882 |
|
| 34883 |
<biblioentry> |
| 34884 |
<abstract> |
| 34885 |
<para>Philip Patterson's <ulink |
| 34886 |
url="http://www.gossiplabs.org">logforbash</ulink> |
| 34887 |
logging/debugging script.</para> |
| 34888 |
</abstract> |
| 34889 |
</biblioentry> |
| 34890 |
|
| 34891 |
<biblioentry> |
| 34892 |
<abstract> |
| 34893 |
<para><ulink |
| 34894 |
url="http://auctiongallery.sourceforge.net">AuctionGallery</ulink>, |
| 34895 |
an application for eBay <quote>power sellers</quote> coded |
| 34896 |
in Bash.</para> |
| 34897 |
</abstract> |
| 34898 |
</biblioentry> |
| 34899 |
|
| 34900 |
<biblioentry> |
| 34901 |
<abstract> |
| 34902 |
<para>Of historical interest are Colin Needham's |
| 34903 |
<firstterm>original International Movie Database (IMDB) |
| 34904 |
reader polling scripts</firstterm>, which nicely illustrate |
| 34905 |
the use of <link linkend="awkref">awk</link> for string |
| 34906 |
parsing. Unfortunately, the URL link is broken.</para> |
| 34907 |
<para>---</para> |
| 34908 |
</abstract> |
| 34909 |
</biblioentry> |
| 34910 |
|
| 34911 |
<biblioentry> |
| 34912 |
<abstract> |
| 34913 |
<para>Fritz Mehner has written a <ulink |
| 34914 |
url="http://vim.sourceforge.net/scripts/script.php?script_id=365">bash-support |
| 34915 |
plugin</ulink> for the <firstterm>vim</firstterm> text editor. |
| 34916 |
He has also also come up with his own <ulink |
| 34917 |
url="http://lug.fh-swf.de/vim/vim-bash/StyleGuideShell.en.pdf">stylesheet |
| 34918 |
for Bash</ulink>. Compare it with the <link linkend="unofficialst">ABS Guide |
| 34919 |
Unofficial Stylesheet</link>.</para> |
| 34920 |
<para>---</para> |
| 34921 |
</abstract> |
| 34922 |
</biblioentry> |
| 34923 |
|
| 34924 |
<biblioentry> |
| 34925 |
<abstract> |
| 34926 |
<para><emphasis>Penguin Pete</emphasis> has quite a number of |
| 34927 |
shell scripting tips and hints on <ulink |
| 34928 |
url="http://www.penguinpetes.com">his superb |
| 34929 |
site</ulink>. Highly recommended.</para> |
| 34930 |
</abstract> |
| 34931 |
</biblioentry> |
| 34932 |
|
| 34933 |
<biblioentry> |
| 34934 |
<abstract> |
| 34935 |
<para>The excellent <citetitle pubwork="book"> Bash Reference |
| 34936 |
Manual</citetitle>, by Chet Ramey and Brian Fox, distributed as |
| 34937 |
part of the <firstterm>bash-2-doc</firstterm> package (available |
| 34938 |
as an <link linkend="rpmref">rpm</link>). See especially the |
| 34939 |
instructive example scripts in this package.</para> |
| 34940 |
</abstract> |
| 34941 |
</biblioentry> |
| 34942 |
|
| 34943 |
<biblioentry> |
| 34944 |
<abstract> |
| 34945 |
<para>John Lion's classic, <ulink |
| 34946 |
url="http://www.lemis.com/grog/Documentation/Lions/index.html"> |
| 34947 |
<emphasis>A Commentary on the Sixth Edition UNIX Operating |
| 34948 |
System</emphasis></ulink>.</para> |
| 34949 |
</abstract> |
| 34950 |
</biblioentry> |
| 34951 |
|
| 34952 |
<biblioentry> |
| 34953 |
<abstract> |
| 34954 |
<para>The <ulink |
| 34955 |
url="news:comp.unix.shell">comp.os.unix.shell</ulink> |
| 34956 |
newsgroup.</para> |
| 34957 |
</abstract> |
| 34958 |
</biblioentry> |
| 34959 |
|
| 34960 |
<biblioentry> |
| 34961 |
<abstract> |
| 34962 |
<para><anchor id="ddlink"></para> |
| 34963 |
<para>The <ulink |
| 34964 |
url="http://www.linuxquestions.org/questions/showthread.php?t=362506"><firstterm>dd</firstterm> |
| 34965 |
thread</ulink> on <ulink |
| 34966 |
url="http://www.linuxquestions.org">Linux Questions</ulink>.</para> |
| 34967 |
</abstract> |
| 34968 |
</biblioentry> |
| 34969 |
|
| 34970 |
<biblioentry> |
| 34971 |
<abstract> |
| 34972 |
<para>The |
| 34973 |
<ulink |
| 34974 |
url="http://www.newsville.com/cgi-bin/getfaq?file=comp.unix.shell/comp.unix.shell_FAQ_-_Answers_to_Frequently_Asked_Questions">comp.os.unix.shell |
| 34975 |
FAQ</ulink>.</para> |
| 34976 |
|
| 34977 |
</abstract> |
| 34978 |
</biblioentry> |
| 34979 |
|
| 34980 |
<biblioentry> |
| 34981 |
<abstract> |
| 34982 |
<para>Assorted comp.os.unix <ulink |
| 34983 |
url="http://www.faqs.org/faqs/by-newsgroup/comp/comp.unix.shell.html"> |
| 34984 |
FAQs</ulink>.</para> |
| 34985 |
</abstract> |
| 34986 |
</biblioentry> |
| 34987 |
|
| 34988 |
<biblioentry> |
| 34989 |
<abstract> |
| 34990 |
<para>The <ulink |
| 34991 |
url="http://en.wikipedia.org/wiki/Dc_(Unix)"><firstterm>Wikipedia</firstterm> |
| 34992 |
article</ulink> covering <link linkend="dcref">dc</link>.</para> |
| 34993 |
</abstract> |
| 34994 |
</biblioentry> |
| 34995 |
|
| 34996 |
<biblioentry> |
| 34997 |
<abstract> |
| 34998 |
<para>The <link linkend="manref">manpages</link> for |
| 34999 |
<command>bash</command> and <command>bash2</command>, |
| 35000 |
<command>date</command>, <command>expect</command>, |
| 35001 |
<command>expr</command>, <command>find</command>, |
| 35002 |
<command>grep</command>, <command>gzip</command>, |
| 35003 |
<command>ln</command>, <command>patch</command>, |
| 35004 |
<command>tar</command>, <command>tr</command>, |
| 35005 |
<command>bc</command>, <command>xargs</command>. |
| 35006 |
The <firstterm>texinfo</firstterm> documentation on |
| 35007 |
<command>bash</command>, <command>dd</command>, |
| 35008 |
<command>m4</command>, <command>gawk</command>, and |
| 35009 |
<command>sed</command>.</para> |
| 35010 |
</abstract> |
| 35011 |
</biblioentry> |
| 35012 |
|
| 35013 |
|
| 35014 |
</bibliography> |
| 35015 |
|
| 35016 |
|
| 35017 |
|
| 35018 |
<appendix id="contributed-scripts"> |
| 35019 |
<title>Contributed Scripts</title> |
| 35020 |
|
| 35021 |
<para>These scripts, while not fitting into the text of this document, do |
| 35022 |
illustrate some interesting shell programming techniques. Some are useful, |
| 35023 |
too. Have fun analyzing and running them.</para> |
| 35024 |
|
| 35025 |
<example id="mailformat"> |
| 35026 |
<title><firstterm>mailformat</firstterm>: Formatting an e-mail |
| 35027 |
message</title> |
| 35028 |
<programlisting>&mailformat;</programlisting> |
| 35029 |
</example> |
| 35030 |
|
| 35031 |
<example id="rn"> |
| 35032 |
<title><firstterm>rn</firstterm>: A simple-minded file renaming |
| 35033 |
utility</title> |
| 35034 |
<para>This script is a modification of <xref |
| 35035 |
linkend="lowercase">.</para> |
| 35036 |
<programlisting>&rn;</programlisting> |
| 35037 |
</example> |
| 35038 |
|
| 35039 |
<example id="blankrename"> |
| 35040 |
<title><firstterm>blank-rename</firstterm>: Renames filenames containing |
| 35041 |
blanks</title> |
| 35042 |
<para>This is an even simpler-minded version of previous script.</para> |
| 35043 |
<programlisting>&blankrename;</programlisting> |
| 35044 |
</example> |
| 35045 |
|
| 35046 |
<example id="encryptedpw"> |
| 35047 |
<title><firstterm>encryptedpw</firstterm>: Uploading to an ftp site, |
| 35048 |
using a locally encrypted password</title> |
| 35049 |
<programlisting>&encryptedpw;</programlisting> |
| 35050 |
</example> |
| 35051 |
|
| 35052 |
<example id="copycd"> |
| 35053 |
<title><firstterm>copy-cd</firstterm>: Copying a data CD</title> |
| 35054 |
<programlisting>©cd;</programlisting> |
| 35055 |
</example> |
| 35056 |
|
| 35057 |
<example id="collatz"> |
| 35058 |
<title>Collatz series</title> |
| 35059 |
<programlisting>&collatz;</programlisting> |
| 35060 |
</example> |
| 35061 |
|
| 35062 |
<para><anchor id="daysbetween0"></para> |
| 35063 |
<example id="daysbetween"> |
| 35064 |
<title><firstterm>days-between</firstterm>: Days between two |
| 35065 |
dates</title> |
| 35066 |
<programlisting>&daysbetween;</programlisting> |
| 35067 |
</example> |
| 35068 |
|
| 35069 |
<example id="makedict"> |
| 35070 |
<title>Making a <firstterm>dictionary</firstterm></title> |
| 35071 |
<programlisting>&makedict;</programlisting> |
| 35072 |
</example> |
| 35073 |
|
| 35074 |
<para><anchor id="soundex0"></para> |
| 35075 |
<example id="soundex"> |
| 35076 |
<title>Soundex conversion</title> |
| 35077 |
<programlisting>&soundex;</programlisting> |
| 35078 |
</example> |
| 35079 |
|
| 35080 |
<para><anchor id="liferef"></para> |
| 35081 |
<example id="lifeslow"> |
| 35082 |
<title><firstterm>Game of Life</firstterm></title> |
| 35083 |
<programlisting>&lifeslow;</programlisting> |
| 35084 |
</example> |
| 35085 |
|
| 35086 |
<example id="gen0data"> |
| 35087 |
<title>Data file for <firstterm>Game of Life</firstterm></title> |
| 35088 |
<programlisting>&gen0data;</programlisting> |
| 35089 |
</example> |
| 35090 |
|
| 35091 |
|
| 35092 |
|
| 35093 |
<para>+++</para> |
| 35094 |
|
| 35095 |
<para>The following script is by Mark Moraes of the University |
| 35096 |
of Toronto. See the file <filename>Moraes-COPYRIGHT</filename> |
| 35097 |
for permissions and restrictions. This file is included in the |
| 35098 |
combined <link linkend="where_tarball">HTML/source tarball</link> |
| 35099 |
of the <emphasis>ABS Guide</emphasis>.</para> |
| 35100 |
|
| 35101 |
<example id="behead"> |
| 35102 |
<title><firstterm>behead</firstterm>: Removing mail and news |
| 35103 |
message headers</title> |
| 35104 |
<programlisting>&behead;</programlisting> |
| 35105 |
</example> |
| 35106 |
|
| 35107 |
<para>+</para> |
| 35108 |
|
| 35109 |
<para>Antek Sawicki contributed the following script, which makes very |
| 35110 |
clever use of the parameter substitution operators discussed in |
| 35111 |
<xref linkend="Parameter-Substitution">.</para> |
| 35112 |
|
| 35113 |
<para><anchor id="pw0"></para> |
| 35114 |
<example id="pw"> |
| 35115 |
<title><firstterm>password</firstterm>: Generating random |
| 35116 |
8-character passwords</title> |
| 35117 |
<programlisting>&pw;</programlisting> |
| 35118 |
</example> |
| 35119 |
|
| 35120 |
<para>+</para> |
| 35121 |
|
| 35122 |
<para><anchor id="zfifo">James R. Van Zandt contributed this script |
| 35123 |
which uses named pipes and, in his words, <quote>really exercises |
| 35124 |
quoting and escaping.</quote></para> |
| 35125 |
|
| 35126 |
<example id="fifo"> |
| 35127 |
<title><firstterm>fifo</firstterm>: Making daily backups, using |
| 35128 |
named pipes</title> |
| 35129 |
<programlisting>&fifo;</programlisting> |
| 35130 |
</example> |
| 35131 |
|
| 35132 |
<para>+</para> |
| 35133 |
|
| 35134 |
<para><anchor id="primes1"></para> |
| 35135 |
<para>Stéphane Chazelas used the following script to |
| 35136 |
demonstrate generating prime numbers without arrays.</para> |
| 35137 |
|
| 35138 |
<para><anchor id="primes00"></para> |
| 35139 |
<example id="primes"> |
| 35140 |
<title>Generating prime numbers using the modulo operator</title> |
| 35141 |
<programlisting>ℙ</programlisting> |
| 35142 |
</example> |
| 35143 |
|
| 35144 |
<para>+</para> |
| 35145 |
|
| 35146 |
<para>Rick Boivie's revision of Jordi Sanfeliu's |
| 35147 |
<emphasis>tree</emphasis> script.</para> |
| 35148 |
|
| 35149 |
<example id="tree"> |
| 35150 |
<title><firstterm>tree</firstterm>: Displaying a directory tree</title> |
| 35151 |
<programlisting>&tree;</programlisting> |
| 35152 |
</example> |
| 35153 |
|
| 35154 |
<para>Patsie's version of a directory <firstterm>tree</firstterm> |
| 35155 |
script.</para> |
| 35156 |
|
| 35157 |
<example id="tree2"> |
| 35158 |
<title><firstterm>tree2</firstterm>: Alternate directory tree script</title> |
| 35159 |
<programlisting>&tree2;</programlisting> |
| 35160 |
</example> |
| 35161 |
|
| 35162 |
<para>Noah Friedman permitted use of his <emphasis>string |
| 35163 |
function</emphasis> script. It essentially reproduces some |
| 35164 |
of the <firstterm>C</firstterm>-library string manipulation |
| 35165 |
functions.</para> |
| 35166 |
|
| 35167 |
<example id="string"> |
| 35168 |
<title><firstterm>string functions</firstterm>: C-style string |
| 35169 |
functions</title> |
| 35170 |
<programlisting>&string;</programlisting> |
| 35171 |
</example> |
| 35172 |
|
| 35173 |
<para>Michael Zick's complex array example uses the <link |
| 35174 |
linkend="md5sumref">md5sum</link> check sum command to encode directory |
| 35175 |
information.</para> |
| 35176 |
|
| 35177 |
<example id="directoryinfo"> |
| 35178 |
<title>Directory information</title> |
| 35179 |
<programlisting>&directoryinfo;</programlisting> |
| 35180 |
</example> |
| 35181 |
|
| 35182 |
|
| 35183 |
<para>Stéphane Chazelas demonstrates object-oriented programming in a |
| 35184 |
Bash script.</para> |
| 35185 |
|
| 35186 |
<para>Mariusz Gniazdowski contributed a <link linkend="hashref">hash</link> |
| 35187 |
library for use in scripts.</para> |
| 35188 |
|
| 35189 |
<example id="hashlib"> |
| 35190 |
<title>Library of hash functions</title> |
| 35191 |
<programlisting>&hashlib;</programlisting> |
| 35192 |
</example> |
| 35193 |
|
| 35194 |
<para>Here is an example script using the foregoing hash library.</para> |
| 35195 |
|
| 35196 |
<example id="hashexample"> |
| 35197 |
<title>Colorizing text using hash functions</title> |
| 35198 |
<programlisting>&hashexample;</programlisting> |
| 35199 |
</example> |
| 35200 |
|
| 35201 |
<para><anchor id="hashex2_0">An example illustrating the mechanics |
| 35202 |
of hashing, but from a different point of view.</para> |
| 35203 |
|
| 35204 |
<example id="hashex2"> |
| 35205 |
<title>More on hash functions</title> |
| 35206 |
<programlisting>&hashex2;</programlisting> |
| 35207 |
</example> |
| 35208 |
|
| 35209 |
<para>Now for a script that installs and mounts |
| 35210 |
those cute USB keychain solid-state <quote>hard drives.</quote></para> |
| 35211 |
|
| 35212 |
<example id="usbinst"> |
| 35213 |
<title>Mounting USB keychain storage devices</title> |
| 35214 |
<programlisting>&usbinst;</programlisting> |
| 35215 |
</example> |
| 35216 |
|
| 35217 |
<para>Converting a text file to HTML format.</para> |
| 35218 |
|
| 35219 |
<example id="tohtml"> |
| 35220 |
<title>Converting to HTML</title> |
| 35221 |
<programlisting>&tohtml;</programlisting> |
| 35222 |
</example> |
| 35223 |
|
| 35224 |
<para>Here is something to warm the hearts of webmasters and mistresses: |
| 35225 |
a script that saves weblogs.</para> |
| 35226 |
|
| 35227 |
<example id="archivweblogs"> |
| 35228 |
<title>Preserving weblogs</title> |
| 35229 |
<programlisting>&archiveweblogs;</programlisting> |
| 35230 |
</example> |
| 35231 |
|
| 35232 |
<para><anchor id="protectliteral0">How to keep the shell from |
| 35233 |
expanding and reinterpreting text strings.</para> |
| 35234 |
|
| 35235 |
<example id="protectliteral"> |
| 35236 |
<title>Protecting literal strings</title> |
| 35237 |
<programlisting>&protectliteral;</programlisting> |
| 35238 |
</example> |
| 35239 |
|
| 35240 |
<para><anchor id="unprotectliteral0">But, what if you |
| 35241 |
<emphasis>want</emphasis> the shell to expand |
| 35242 |
and reinterpret strings?</para> |
| 35243 |
|
| 35244 |
<example id="unprotectliteral"> |
| 35245 |
<title>Unprotecting literal strings</title> |
| 35246 |
<programlisting>&unprotectliteral;</programlisting> |
| 35247 |
</example> |
| 35248 |
|
| 35249 |
|
| 35250 |
<para>This interesting script helps hunt down spammers.</para> |
| 35251 |
|
| 35252 |
<para><anchor id="isspammer2_0"></para> |
| 35253 |
<example id="isspammer2"> |
| 35254 |
<title>Spammer Identification</title> |
| 35255 |
<programlisting>&isspammer2;</programlisting> |
| 35256 |
</example> |
| 35257 |
|
| 35258 |
|
| 35259 |
<para>Another anti-spam script.</para> |
| 35260 |
|
| 35261 |
<para><anchor id="whx0"></para> |
| 35262 |
<example id="whx"> |
| 35263 |
<title>Spammer Hunt</title> |
| 35264 |
<programlisting>&whx;</programlisting> |
| 35265 |
</example> |
| 35266 |
|
| 35267 |
|
| 35268 |
<para><quote>Little Monster's</quote> front end to <link |
| 35269 |
linkend="wgetref">wget</link>.</para> |
| 35270 |
|
| 35271 |
<example id="wgetter2"> |
| 35272 |
<title>Making <firstterm>wget</firstterm> easier to use</title> |
| 35273 |
<programlisting>&wgetter2;</programlisting> |
| 35274 |
</example> |
| 35275 |
|
| 35276 |
<example id="bashpodder"> |
| 35277 |
<title>A <firstterm>podcasting</firstterm> script</title> |
| 35278 |
<programlisting>&bashpodder;</programlisting> |
| 35279 |
</example> |
| 35280 |
|
| 35281 |
<example id="nightlybackup"> |
| 35282 |
<title>Nightly backup to a firewire HD</title> |
| 35283 |
<programlisting>&nightlybackup;</programlisting> |
| 35284 |
</example> |
| 35285 |
|
| 35286 |
<example id="cdll"> |
| 35287 |
<title>An expanded <firstterm>cd</firstterm> command</title> |
| 35288 |
<programlisting>&cdll;</programlisting> |
| 35289 |
</example> |
| 35290 |
|
| 35291 |
<example id="soundcardon"> |
| 35292 |
<title>A soundcard setup script</title> |
| 35293 |
<programlisting>&soundcardon;</programlisting> |
| 35294 |
</example> |
| 35295 |
|
| 35296 |
<para><anchor id="findsplit0"></para> |
| 35297 |
<example id="findsplit"> |
| 35298 |
<title>Locating split paragraphs in a text file</title> |
| 35299 |
<programlisting>&findsplit;</programlisting> |
| 35300 |
</example> |
| 35301 |
|
| 35302 |
<para><anchor id="insertionsort0"></para> |
| 35303 |
<example id="insertionsort"> |
| 35304 |
<title>Insertion sort</title> |
| 35305 |
<programlisting>&insertionsort;</programlisting> |
| 35306 |
</example> |
| 35307 |
|
| 35308 |
<example id="stddev"> |
| 35309 |
<title>Standard Deviation</title> |
| 35310 |
<programlisting>&stddev;</programlisting> |
| 35311 |
</example> |
| 35312 |
|
| 35313 |
<example id="padsw"> |
| 35314 |
<title>A <firstterm>pad</firstterm> file generator for shareware |
| 35315 |
authors</title> |
| 35316 |
<programlisting>&padsw;</programlisting> |
| 35317 |
</example> |
| 35318 |
|
| 35319 |
<example id="maned"> |
| 35320 |
<title>A <firstterm>man page</firstterm> editor</title> |
| 35321 |
<programlisting>&maned;</programlisting> |
| 35322 |
</example> |
| 35323 |
|
| 35324 |
<example id="petals"> |
| 35325 |
<title>Petals Around the Rose</title> |
| 35326 |
<programlisting>&petals;</programlisting> |
| 35327 |
</example> |
| 35328 |
|
| 35329 |
<example id="qky"> |
| 35330 |
<title>Quacky: a Perquackey-type word game</title> |
| 35331 |
<programlisting>&qky;</programlisting> |
| 35332 |
</example> |
| 35333 |
|
| 35334 |
<example id="nim"> |
| 35335 |
<title>Nim</title> |
| 35336 |
<programlisting>&nim;</programlisting> |
| 35337 |
</example> |
| 35338 |
|
| 35339 |
<example id="stopwatch"> |
| 35340 |
<title>A command-line stopwatch</title> |
| 35341 |
<programlisting>&stopwatch;</programlisting> |
| 35342 |
</example> |
| 35343 |
|
| 35344 |
<example id="homework"> |
| 35345 |
<title>An all-purpose shell scripting homework assignment solution</title> |
| 35346 |
<programlisting>&homework;</programlisting> |
| 35347 |
</example> |
| 35348 |
|
| 35349 |
<para<anchor id="ktour0"></para> |
| 35350 |
<example id="ktour"> |
| 35351 |
<title>The Knight's Tour</title> |
| 35352 |
<programlisting>&ktour;</programlisting> |
| 35353 |
</example> |
| 35354 |
|
| 35355 |
<example id="msquare"> |
| 35356 |
<title>Magic Squares</title> |
| 35357 |
<programlisting>&msquare;</programlisting> |
| 35358 |
</example> |
| 35359 |
|
| 35360 |
<example id="fifteen"> |
| 35361 |
<title>Fifteen Puzzle</title> |
| 35362 |
<programlisting>&fifteen;</programlisting> |
| 35363 |
</example> |
| 35364 |
|
| 35365 |
<para><anchor id="hanoi2ref"></para> |
| 35366 |
<example id="hanoi2"> |
| 35367 |
<title><firstterm>The Towers of Hanoi, graphic |
| 35368 |
version</firstterm></title> |
| 35369 |
<programlisting>&hanoi2;</programlisting> |
| 35370 |
</example> |
| 35371 |
|
| 35372 |
<para><anchor id="hanoi2aref"></para> |
| 35373 |
<example id="hanoi2a"> |
| 35374 |
<title><firstterm>The Towers of Hanoi, alternate graphic |
| 35375 |
version</firstterm></title> |
| 35376 |
<programlisting>&hanoi2a;</programlisting> |
| 35377 |
</example> |
| 35378 |
|
| 35379 |
<example id="usegetopt"> |
| 35380 |
<title>An alternate version of the |
| 35381 |
<link linkend="getoptsimple">getopt-simple.sh</link> script</title> |
| 35382 |
<programlisting>&usegetopt;</programlisting> |
| 35383 |
</example> |
| 35384 |
|
| 35385 |
<example id="usegetopt2"> |
| 35386 |
<title>The version of the |
| 35387 |
<firstterm>UseGetOpt.sh</firstterm> example used in the <link |
| 35388 |
linkend="tabexpansion">Tab Expansion appendix</link></title> |
| 35389 |
<programlisting>&usegetopt2;</programlisting> |
| 35390 |
</example> |
| 35391 |
|
| 35392 |
<example id="showallc"> |
| 35393 |
<title>Cycling through all the possible color backgrounds</title> |
| 35394 |
<programlisting>&showallc;</programlisting> |
| 35395 |
</example> |
| 35396 |
|
| 35397 |
<example id="samorse"> |
| 35398 |
<title>Morse Code Practice</title> |
| 35399 |
<programlisting>&samorse;</programlisting> |
| 35400 |
</example> |
| 35401 |
|
| 35402 |
<example id="base64"> |
| 35403 |
<title>Base64 encoding/decoding</title> |
| 35404 |
<programlisting>&base64;</programlisting> |
| 35405 |
</example> |
| 35406 |
|
| 35407 |
<example id="sedappend"> |
| 35408 |
<title>Inserting text in a file using |
| 35409 |
<firstterm>sed</firstterm></title> |
| 35410 |
<programlisting>&sedappend;</programlisting> |
| 35411 |
</example> |
| 35412 |
|
| 35413 |
<example id="gronsfeld"> |
| 35414 |
<title>The Gronsfeld Cipher</title> |
| 35415 |
<programlisting>&gronsfeld;</programlisting> |
| 35416 |
</example> |
| 35417 |
|
| 35418 |
<example id="bingo"> |
| 35419 |
<title>Bingo Number Generator</title> |
| 35420 |
<programlisting>&bingo;</programlisting> |
| 35421 |
</example> |
| 35422 |
|
| 35423 |
<para><anchor id="basicsrev0">To end this section, a review of the |
| 35424 |
basics . . . and more.</para> |
| 35425 |
|
| 35426 |
<example id="basicsreviewed"> |
| 35427 |
<title>Basics Reviewed</title> |
| 35428 |
<programlisting>&basicsreviewed;</programlisting> |
| 35429 |
</example> |
| 35430 |
|
| 35431 |
<example id="testexectime"> |
| 35432 |
<title>Testing execution times of various commands</title> |
| 35433 |
<programlisting>&testexectime;</programlisting> |
| 35434 |
</example> |
| 35435 |
|
| 35436 |
<example id="assocarrtest"> |
| 35437 |
<title>Associative arrays vs. conventional arrays (execution |
| 35438 |
times)</title> |
| 35439 |
<programlisting>&assocarrtest;</programlisting> |
| 35440 |
</example> |
| 35441 |
|
| 35442 |
|
| 35443 |
</appendix> |
| 35444 |
<!-- End Contributed Scripts appendix --> |
| 35445 |
|
| 35446 |
|
| 35447 |
|
| 35448 |
|
| 35449 |
<appendix id="refcards"> |
| 35450 |
<title>Reference Cards</title> |
| 35451 |
|
| 35452 |
<para>The following reference cards provide a useful |
| 35453 |
<emphasis>summary</emphasis> of certain scripting concepts. |
| 35454 |
The foregoing text treats these matters in more depth, as well as |
| 35455 |
giving usage examples.</para> |
| 35456 |
|
| 35457 |
<para><anchor id="specshvartab"></para> |
| 35458 |
<table pgwide=0> |
| 35459 |
<title>Special Shell Variables</title> |
| 35460 |
<tgroup cols="2"> |
| 35461 |
<thead> |
| 35462 |
<row> |
| 35463 |
<entry>Variable</entry> |
| 35464 |
<entry>Meaning</entry> |
| 35465 |
</row> |
| 35466 |
</thead> |
| 35467 |
<tbody> |
| 35468 |
<row> |
| 35469 |
<entry><option>$0</option></entry> |
| 35470 |
<entry>Filename of script</entry> |
| 35471 |
</row> |
| 35472 |
<row> |
| 35473 |
<entry><option>$1</option></entry> |
| 35474 |
<entry>Positional parameter #1</entry> |
| 35475 |
</row> |
| 35476 |
<row> |
| 35477 |
<entry><option>$2 - $9</option></entry> |
| 35478 |
<entry>Positional parameters #2 - #9</entry> |
| 35479 |
</row> |
| 35480 |
<row> |
| 35481 |
<entry><option>${10}</option></entry> |
| 35482 |
<entry>Positional parameter #10</entry> |
| 35483 |
</row> |
| 35484 |
<row> |
| 35485 |
<entry><option>$#</option></entry> |
| 35486 |
<entry>Number of positional parameters</entry> |
| 35487 |
</row> |
| 35488 |
<row> |
| 35489 |
<entry><option>"$*"</option></entry> |
| 35490 |
<entry>All the positional parameters (as a single word) *</entry> |
| 35491 |
</row> |
| 35492 |
<row> |
| 35493 |
<entry><option>"$@"</option></entry> |
| 35494 |
<entry>All the positional parameters (as separate strings)</entry> |
| 35495 |
</row> |
| 35496 |
<row> |
| 35497 |
<entry><option>${#*}</option></entry> |
| 35498 |
<entry>Number of positional parameters</entry> |
| 35499 |
</row> |
| 35500 |
<row> |
| 35501 |
<entry><option>${#@}</option></entry> |
| 35502 |
<entry>Number of positional parameters</entry> |
| 35503 |
</row> |
| 35504 |
<row> |
| 35505 |
<entry><option>$?</option></entry> |
| 35506 |
<entry>Return value</entry> |
| 35507 |
</row> |
| 35508 |
<row> |
| 35509 |
<entry><option>$$</option></entry> |
| 35510 |
<entry>Process ID (PID) of script</entry> |
| 35511 |
</row> |
| 35512 |
<row> |
| 35513 |
<entry><option>$-</option></entry> |
| 35514 |
<entry>Flags passed to script (using |
| 35515 |
<firstterm>set</firstterm>)</entry> |
| 35516 |
</row> |
| 35517 |
<row> |
| 35518 |
<entry><option>$_</option></entry> |
| 35519 |
<entry>Last argument of previous command</entry> |
| 35520 |
</row> |
| 35521 |
<row> |
| 35522 |
<entry><option>$!</option></entry> |
| 35523 |
<entry>Process ID (PID) of last job run in background</entry> |
| 35524 |
</row> |
| 35525 |
</tbody> |
| 35526 |
</tgroup> |
| 35527 |
</table> |
| 35528 |
|
| 35529 |
<para><command>*</command> <emphasis>Must be quoted</emphasis>, |
| 35530 |
otherwise it defaults to |
| 35531 |
<varname>$@</varname>.</para> |
| 35532 |
|
| 35533 |
|
| 35534 |
<para><anchor id="bincomptab"></para> |
| 35535 |
<table> |
| 35536 |
<title>TEST Operators: Binary Comparison</title> |
| 35537 |
<tgroup cols="5"> |
| 35538 |
<thead> |
| 35539 |
<row> |
| 35540 |
<entry>Operator</entry> |
| 35541 |
<entry>Meaning</entry> |
| 35542 |
<entry>-----</entry> |
| 35543 |
<entry>Operator</entry> |
| 35544 |
<entry>Meaning</entry> |
| 35545 |
</row> |
| 35546 |
</thead> |
| 35547 |
<tbody> |
| 35548 |
<row> |
| 35549 |
<entry></entry> |
| 35550 |
<entry></entry> |
| 35551 |
</row> |
| 35552 |
<row> |
| 35553 |
<entry><link linkend="icomparison1">Arithmetic |
| 35554 |
Comparison</link></entry> |
| 35555 |
<entry></entry> |
| 35556 |
<entry></entry> |
| 35557 |
<entry><link linkend="scomparison1">String |
| 35558 |
Comparison</link></entry> |
| 35559 |
<entry></entry> |
| 35560 |
</row> |
| 35561 |
<row> |
| 35562 |
<entry><option>-eq</option></entry> |
| 35563 |
<entry>Equal to</entry> |
| 35564 |
<entry></entry> |
| 35565 |
<entry><option>=</option></entry> |
| 35566 |
<entry>Equal to</entry> |
| 35567 |
</row> |
| 35568 |
<row> |
| 35569 |
<entry></entry> |
| 35570 |
<entry></entry> |
| 35571 |
<entry></entry> |
| 35572 |
<entry><option>==</option></entry> |
| 35573 |
<entry>Equal to</entry> |
| 35574 |
</row> |
| 35575 |
<row> |
| 35576 |
<entry><option>-ne</option></entry> |
| 35577 |
<entry>Not equal to</entry> |
| 35578 |
<entry></entry> |
| 35579 |
<entry><option>!=</option></entry> |
| 35580 |
<entry>Not equal to</entry> |
| 35581 |
</row> |
| 35582 |
<row> |
| 35583 |
<entry><option>-lt</option></entry> |
| 35584 |
<entry>Less than</entry> |
| 35585 |
<entry></entry> |
| 35586 |
<entry><option>\<</option></entry> |
| 35587 |
<entry>Less than (<link linkend="asciidef">ASCII</link>) *</entry> |
| 35588 |
</row> |
| 35589 |
<row> |
| 35590 |
<entry><option>-le</option></entry> |
| 35591 |
<entry>Less than or equal to</entry> |
| 35592 |
<entry></entry> |
| 35593 |
<entry></entry> |
| 35594 |
</row> |
| 35595 |
<row> |
| 35596 |
<entry><option>-gt</option></entry> |
| 35597 |
<entry>Greater than</entry> |
| 35598 |
<entry></entry> |
| 35599 |
<entry><option>\></option></entry> |
| 35600 |
<entry>Greater than (ASCII) *</entry> |
| 35601 |
</row> |
| 35602 |
<row> |
| 35603 |
<entry><option>-ge</option></entry> |
| 35604 |
<entry>Greater than or equal to</entry> |
| 35605 |
<entry></entry> |
| 35606 |
<entry></entry> |
| 35607 |
</row> |
| 35608 |
<row> |
| 35609 |
<entry></entry> |
| 35610 |
<entry></entry> |
| 35611 |
<entry></entry> |
| 35612 |
<entry><option>-z</option></entry> |
| 35613 |
<entry>String is empty</entry> |
| 35614 |
</row> |
| 35615 |
<row> |
| 35616 |
<entry></entry> |
| 35617 |
<entry></entry> |
| 35618 |
<entry></entry> |
| 35619 |
<entry><option>-n</option></entry> |
| 35620 |
<entry>String is not empty</entry> |
| 35621 |
</row> |
| 35622 |
<row> |
| 35623 |
<entry></entry> |
| 35624 |
<entry></entry> |
| 35625 |
</row> |
| 35626 |
<row> |
| 35627 |
<entry>Arithmetic Comparison</entry> |
| 35628 |
<entry><link linkend="dblprx">within double |
| 35629 |
parentheses</link> (( ... ))</entry> |
| 35630 |
</row> |
| 35631 |
<row> |
| 35632 |
<entry><option>></option></entry> |
| 35633 |
<entry>Greater than</entry> |
| 35634 |
</row> |
| 35635 |
<row> |
| 35636 |
<entry><option>>=</option></entry> |
| 35637 |
<entry>Greater than or equal to</entry> |
| 35638 |
</row> |
| 35639 |
<row> |
| 35640 |
<entry><option><</option></entry> |
| 35641 |
<entry>Less than</entry> |
| 35642 |
</row> |
| 35643 |
<row> |
| 35644 |
<entry><option><=</option></entry> |
| 35645 |
<entry>Less than or equal to</entry> |
| 35646 |
</row> |
| 35647 |
</tbody> |
| 35648 |
</tgroup> |
| 35649 |
</table> |
| 35650 |
|
| 35651 |
|
| 35652 |
<para><command>*</command> <emphasis>If within a |
| 35653 |
double-bracket</emphasis> <token>[[ ... ]]</token> <emphasis>test construct, |
| 35654 |
then no escape</emphasis> <token>\</token> <emphasis>is |
| 35655 |
needed.</emphasis></para> |
| 35656 |
|
| 35657 |
<para><anchor id="filestab"></para> |
| 35658 |
<table> |
| 35659 |
<title>TEST Operators: Files</title> |
| 35660 |
<tgroup cols="5"> |
| 35661 |
<thead> |
| 35662 |
<row> |
| 35663 |
<entry>Operator</entry> |
| 35664 |
<entry>Tests Whether</entry> |
| 35665 |
<entry>-----</entry> |
| 35666 |
<entry>Operator</entry> |
| 35667 |
<entry>Tests Whether</entry> |
| 35668 |
</row> |
| 35669 |
</thead> |
| 35670 |
<tbody> |
| 35671 |
<row> |
| 35672 |
<entry><option>-e</option></entry> |
| 35673 |
<entry>File exists</entry> |
| 35674 |
<entry></entry> |
| 35675 |
<entry><option>-s</option></entry> |
| 35676 |
<entry>File is not zero size</entry> |
| 35677 |
</row> |
| 35678 |
<row> |
| 35679 |
<entry><option>-f</option></entry> |
| 35680 |
<entry>File is a <firstterm>regular</firstterm> file</entry> |
| 35681 |
</row> |
| 35682 |
<row> |
| 35683 |
<entry><option>-d</option></entry> |
| 35684 |
<entry>File is a <firstterm>directory</firstterm></entry> |
| 35685 |
<entry></entry> |
| 35686 |
<entry><option>-r</option></entry> |
| 35687 |
<entry>File has <firstterm>read</firstterm> |
| 35688 |
permission</entry> |
| 35689 |
</row> |
| 35690 |
<row> |
| 35691 |
<entry><option>-h</option></entry> |
| 35692 |
<entry>File is a <link linkend="symlinkref">symbolic link</link></entry> |
| 35693 |
<entry></entry> |
| 35694 |
<entry><option>-w</option></entry> |
| 35695 |
<entry>File has <firstterm>write</firstterm> |
| 35696 |
permission</entry> |
| 35697 |
</row> |
| 35698 |
<row> |
| 35699 |
<entry><option>-L</option></entry> |
| 35700 |
<entry>File is a <firstterm>symbolic link</firstterm></entry> |
| 35701 |
<entry></entry> |
| 35702 |
<entry><option>-x</option></entry> |
| 35703 |
<entry>File has <firstterm>execute</firstterm> |
| 35704 |
permission</entry> |
| 35705 |
</row> |
| 35706 |
<row> |
| 35707 |
<entry><option>-b</option></entry> |
| 35708 |
<entry>File is a <link linkend="blockdevref">block |
| 35709 |
device</link></entry> |
| 35710 |
</row> |
| 35711 |
<row> |
| 35712 |
<entry><option>-c</option></entry> |
| 35713 |
<entry>File is a <link linkend="chardevref">character |
| 35714 |
device</link></entry> |
| 35715 |
<entry></entry> |
| 35716 |
<entry><option>-g</option></entry> |
| 35717 |
<entry><firstterm>sgid</firstterm> flag set</entry> |
| 35718 |
</row> |
| 35719 |
<row> |
| 35720 |
<entry><option>-p</option></entry> |
| 35721 |
<entry>File is a <link linkend="piperef">pipe</link></entry> |
| 35722 |
<entry></entry> |
| 35723 |
<entry><option>-u</option></entry> |
| 35724 |
<entry><firstterm>suid</firstterm> flag set</entry> |
| 35725 |
</row> |
| 35726 |
<row> |
| 35727 |
<entry><option>-S</option></entry> |
| 35728 |
<entry>File is a <link linkend="socketref">socket</link></entry> |
| 35729 |
<entry></entry> |
| 35730 |
<entry><option>-k</option></entry> |
| 35731 |
<entry><quote>sticky bit</quote> set</entry> |
| 35732 |
</row> |
| 35733 |
<row> |
| 35734 |
<entry><option>-t</option></entry> |
| 35735 |
<entry>File is associated with a |
| 35736 |
<firstterm>terminal</firstterm></entry> |
| 35737 |
</row> |
| 35738 |
<row><entry></entry></row> |
| 35739 |
<row> |
| 35740 |
<entry><option>-N</option></entry> |
| 35741 |
<entry>File modified since it was last read</entry> |
| 35742 |
<entry></entry> |
| 35743 |
<entry><option>F1 -nt F2</option></entry> |
| 35744 |
<entry>File F1 is <emphasis>newer</emphasis> than F2 *</entry> |
| 35745 |
</row> |
| 35746 |
<row> |
| 35747 |
<entry><option>-O</option></entry> |
| 35748 |
<entry>You own the file</entry> |
| 35749 |
<entry></entry> |
| 35750 |
<entry><option>F1 -ot F2</option></entry> |
| 35751 |
<entry>File F1 is <emphasis>older</emphasis> than F2 *</entry> |
| 35752 |
</row> |
| 35753 |
<row> |
| 35754 |
<entry><option>-G</option></entry> |
| 35755 |
<entry><firstterm>Group id</firstterm> of file same as |
| 35756 |
yours</entry> |
| 35757 |
<entry></entry> |
| 35758 |
<entry><option>F1 -ef F2</option></entry> |
| 35759 |
<entry>Files F1 and F2 are <firstterm>hard links</firstterm> |
| 35760 |
to the same file *</entry> |
| 35761 |
</row> |
| 35762 |
<row><entry></entry></row> |
| 35763 |
<row> |
| 35764 |
<entry><option>!</option></entry> |
| 35765 |
<entry>NOT (inverts sense of above tests)</entry> |
| 35766 |
</row> |
| 35767 |
</tbody> |
| 35768 |
</tgroup> |
| 35769 |
</table> |
| 35770 |
|
| 35771 |
|
| 35772 |
<para><command>*</command> <firstterm>Binary</firstterm> operator |
| 35773 |
(requires two operands).</para> |
| 35774 |
|
| 35775 |
|
| 35776 |
|
| 35777 |
|
| 35778 |
<para><anchor id="parsubtab"></para> |
| 35779 |
<table pgwide=0> |
| 35780 |
<title>Parameter Substitution and Expansion</title> |
| 35781 |
<tgroup cols="2"> |
| 35782 |
<thead> |
| 35783 |
<row> |
| 35784 |
<entry>Expression</entry> |
| 35785 |
<entry>Meaning</entry> |
| 35786 |
</row> |
| 35787 |
</thead> |
| 35788 |
<tbody> |
| 35789 |
<row> |
| 35790 |
<entry><option>${var}</option></entry> |
| 35791 |
<entry>Value of <parameter>var</parameter> (same as |
| 35792 |
<parameter>$var</parameter>)</entry> |
| 35793 |
</row> |
| 35794 |
<row><entry></entry><entry></entry></row> |
| 35795 |
<row> |
| 35796 |
<entry><option>${var-$DEFAULT}</option></entry> |
| 35797 |
<entry>If <parameter>var</parameter> not set, <link |
| 35798 |
linkend="evalref">evaluate</link> expression |
| 35799 |
as <parameter>$DEFAULT</parameter> *</entry> |
| 35800 |
</row> |
| 35801 |
<row> |
| 35802 |
<entry><option>${var:-$DEFAULT}</option></entry> |
| 35803 |
<entry>If <parameter>var</parameter> not set or is empty, |
| 35804 |
<firstterm>evaluate</firstterm> expression as |
| 35805 |
<parameter>$DEFAULT</parameter> |
| 35806 |
*</entry> |
| 35807 |
</row> |
| 35808 |
<row><entry></entry><entry></entry></row> |
| 35809 |
<row> |
| 35810 |
<entry><option>${var=$DEFAULT}</option></entry> |
| 35811 |
<entry>If <parameter>var</parameter> not set, evaluate expression |
| 35812 |
as <parameter>$DEFAULT</parameter> *</entry> |
| 35813 |
</row> |
| 35814 |
<row> |
| 35815 |
<entry><option>${var:=$DEFAULT}</option></entry> |
| 35816 |
<entry>If <parameter>var</parameter> not set or is empty, evaluate expression |
| 35817 |
as <parameter>$DEFAULT</parameter> *</entry> |
| 35818 |
</row> |
| 35819 |
<row><entry></entry><entry></entry></row> |
| 35820 |
<row> |
| 35821 |
<entry><option>${var+$OTHER}</option></entry> |
| 35822 |
<entry>If <parameter>var</parameter> set, evaluate expression as |
| 35823 |
<parameter>$OTHER</parameter>, otherwise as null string</entry> |
| 35824 |
</row> |
| 35825 |
<row> |
| 35826 |
<entry><option>${var:+$OTHER}</option></entry> |
| 35827 |
<entry>If <parameter>var</parameter> set, evaluate expression as |
| 35828 |
<parameter>$OTHER</parameter>, otherwise as null string</entry> |
| 35829 |
</row> |
| 35830 |
<row><entry></entry><entry></entry></row> |
| 35831 |
<row> |
| 35832 |
<entry><option>${var?$ERR_MSG}</option></entry> |
| 35833 |
<entry>If <parameter>var</parameter> not set, print |
| 35834 |
<parameter>$ERR_MSG</parameter> and abort script |
| 35835 |
with an exit status of <errorcode>1</errorcode>.*</entry> |
| 35836 |
</row> |
| 35837 |
<row> |
| 35838 |
<entry><option>${var:?$ERR_MSG}</option></entry> |
| 35839 |
<entry>If <parameter>var</parameter> not set, print |
| 35840 |
<parameter>$ERR_MSG</parameter> and abort script |
| 35841 |
with an exit status of <errorcode>1</errorcode>.*</entry> |
| 35842 |
</row> |
| 35843 |
<row><entry></entry><entry></entry></row> |
| 35844 |
<row> |
| 35845 |
<entry><option>${!varprefix*}</option></entry> |
| 35846 |
<entry>Matches all previously declared variables beginning with |
| 35847 |
<parameter>varprefix</parameter></entry> |
| 35848 |
</row> |
| 35849 |
<row> |
| 35850 |
<entry><option>${!varprefix@}</option></entry> |
| 35851 |
<entry>Matches all previously declared variables beginning with |
| 35852 |
<parameter>varprefix</parameter></entry> |
| 35853 |
</row> |
| 35854 |
</tbody> |
| 35855 |
</tgroup> |
| 35856 |
</table> |
| 35857 |
|
| 35858 |
<para><command>*</command> If <parameter>var</parameter> |
| 35859 |
<emphasis>is</emphasis> set, evaluate the expression as |
| 35860 |
<parameter>$var</parameter> with no side-effects.</para> |
| 35861 |
<para><command># Note</command> that some of the above behavior |
| 35862 |
of operators has changed from earlier versions of Bash.</para> |
| 35863 |
|
| 35864 |
|
| 35865 |
<para><anchor id="stringopstab"></para> |
| 35866 |
<table pgwide=0> |
| 35867 |
<title>String Operations</title> |
| 35868 |
<tgroup cols="2"> |
| 35869 |
<thead> |
| 35870 |
<row> |
| 35871 |
<entry>Expression</entry> |
| 35872 |
<entry>Meaning</entry> |
| 35873 |
</row> |
| 35874 |
</thead> |
| 35875 |
<tbody> |
| 35876 |
<row> |
| 35877 |
<entry><option>${#string}</option></entry> |
| 35878 |
<entry>Length of <parameter>$string</parameter></entry> |
| 35879 |
</row> |
| 35880 |
<row><entry></entry><entry></entry></row> |
| 35881 |
<row> |
| 35882 |
<entry><option>${string:position}</option></entry> |
| 35883 |
<entry>Extract substring from <parameter>$string</parameter> |
| 35884 |
at <parameter>$position</parameter></entry> |
| 35885 |
</row> |
| 35886 |
<row> |
| 35887 |
<entry><option>${string:position:length}</option></entry> |
| 35888 |
<entry>Extract <parameter>$length</parameter> |
| 35889 |
characters substring from <parameter>$string</parameter> |
| 35890 |
at <parameter>$position</parameter> [zero-indexed, |
| 35891 |
first character is at position 0]</entry> |
| 35892 |
</row> |
| 35893 |
<row><entry></entry><entry></entry></row> |
| 35894 |
<row> |
| 35895 |
<entry><option>${string#substring}</option></entry> |
| 35896 |
<entry>Strip shortest match of |
| 35897 |
<parameter>$substring</parameter> from front of |
| 35898 |
<parameter>$string</parameter></entry> |
| 35899 |
</row> |
| 35900 |
<row> |
| 35901 |
<entry><option>${string##substring}</option></entry> |
| 35902 |
<entry>Strip longest match of |
| 35903 |
<parameter>$substring</parameter> from front of |
| 35904 |
<parameter>$string</parameter></entry> |
| 35905 |
</row> |
| 35906 |
<row> |
| 35907 |
<entry><option>${string%substring}</option></entry> |
| 35908 |
<entry>Strip shortest match of |
| 35909 |
<parameter>$substring</parameter> from back of |
| 35910 |
<parameter>$string</parameter></entry> |
| 35911 |
</row> |
| 35912 |
<row> |
| 35913 |
<entry><option>${string%%substring}</option></entry> |
| 35914 |
<entry>Strip longest match of |
| 35915 |
<parameter>$substring</parameter> from back of |
| 35916 |
<parameter>$string</parameter></entry> |
| 35917 |
</row> |
| 35918 |
<row><entry></entry><entry></entry></row> |
| 35919 |
<row> |
| 35920 |
<entry><option>${string/substring/replacement}</option></entry> |
| 35921 |
<entry>Replace first match of |
| 35922 |
<parameter>$substring</parameter> with |
| 35923 |
<parameter>$replacement</parameter></entry> |
| 35924 |
</row> |
| 35925 |
<row> |
| 35926 |
<entry><option>${string//substring/replacement}</option></entry> |
| 35927 |
<entry>Replace <emphasis>all</emphasis> matches of |
| 35928 |
<parameter>$substring</parameter> with |
| 35929 |
<parameter>$replacement</parameter></entry> |
| 35930 |
</row> |
| 35931 |
<row> |
| 35932 |
<entry><option>${string/#substring/replacement}</option></entry> |
| 35933 |
<entry>If <parameter>$substring</parameter> |
| 35934 |
matches <emphasis>front</emphasis> end of |
| 35935 |
<parameter>$string</parameter>, substitute |
| 35936 |
<parameter>$replacement</parameter> for |
| 35937 |
<parameter>$substring</parameter></entry> |
| 35938 |
</row> |
| 35939 |
<row> |
| 35940 |
<entry><option>${string/%substring/replacement}</option></entry> |
| 35941 |
<entry>If <parameter>$substring</parameter> |
| 35942 |
matches <emphasis>back</emphasis> end of |
| 35943 |
<parameter>$string</parameter>, substitute |
| 35944 |
<parameter>$replacement</parameter> for |
| 35945 |
<parameter>$substring</parameter></entry> |
| 35946 |
</row> |
| 35947 |
<row><entry></entry><entry></entry></row> |
| 35948 |
<row><entry></entry><entry></entry></row> |
| 35949 |
<row> |
| 35950 |
<entry><option>expr match "$string" '$substring'</option></entry> |
| 35951 |
<entry>Length of matching <parameter>$substring</parameter>* |
| 35952 |
at beginning of <parameter>$string</parameter></entry> |
| 35953 |
</row> |
| 35954 |
<row> |
| 35955 |
<entry><option>expr "$string" : '$substring'</option></entry> |
| 35956 |
<entry>Length of matching <parameter>$substring</parameter>* |
| 35957 |
at beginning of <parameter>$string</parameter></entry> |
| 35958 |
</row> |
| 35959 |
<row> |
| 35960 |
<entry><option>expr index "$string" $substring</option></entry> |
| 35961 |
<entry>Numerical position in <parameter>$string</parameter> |
| 35962 |
of first character in <parameter>$substring</parameter>* |
| 35963 |
that matches [0 if no match, first character counts as |
| 35964 |
position 1]</entry> |
| 35965 |
</row> |
| 35966 |
<row> |
| 35967 |
<entry><option>expr substr $string $position |
| 35968 |
$length</option></entry> |
| 35969 |
<entry>Extract <parameter>$length</parameter> characters |
| 35970 |
from <parameter>$string</parameter> starting at |
| 35971 |
<parameter>$position</parameter> [0 if no match, first |
| 35972 |
character counts as position 1]</entry> |
| 35973 |
</row> |
| 35974 |
<row> |
| 35975 |
<entry><option>expr match "$string" |
| 35976 |
'\($substring\)'</option></entry> |
| 35977 |
<entry>Extract <parameter>$substring</parameter>*, searching |
| 35978 |
from beginning of <parameter>$string</parameter></entry> |
| 35979 |
</row> |
| 35980 |
<row> |
| 35981 |
<entry><option>expr "$string" : |
| 35982 |
'\($substring\)'</option></entry> |
| 35983 |
<entry>Extract <parameter>$substring</parameter>* , searching |
| 35984 |
from beginning of <parameter>$string</parameter></entry> |
| 35985 |
</row> |
| 35986 |
<row> |
| 35987 |
<entry><option>expr match "$string" |
| 35988 |
'.*\($substring\)'</option></entry> |
| 35989 |
<entry>Extract <parameter>$substring</parameter>*, searching |
| 35990 |
from end of <parameter>$string</parameter></entry> |
| 35991 |
</row> |
| 35992 |
<row> |
| 35993 |
<entry><option>expr "$string" : |
| 35994 |
'.*\($substring\)'</option></entry> |
| 35995 |
<entry>Extract <parameter>$substring</parameter>*, searching |
| 35996 |
from end of <parameter>$string</parameter></entry> |
| 35997 |
</row> |
| 35998 |
</tbody> |
| 35999 |
</tgroup> |
| 36000 |
</table> |
| 36001 |
|
| 36002 |
<para><command>*</command> Where <parameter>$substring</parameter> is a |
| 36003 |
<link linkend="regexref">Regular Expression</link>.</para> |
| 36004 |
|
| 36005 |
|
| 36006 |
|
| 36007 |
<para><anchor id="misctab"></para> |
| 36008 |
<table pgwide=0> |
| 36009 |
<title>Miscellaneous Constructs</title> |
| 36010 |
<tgroup cols="2"> |
| 36011 |
<thead> |
| 36012 |
|
| 36013 |
<row> |
| 36014 |
<entry>Expression</entry> |
| 36015 |
<entry>Interpretation</entry> |
| 36016 |
</row> |
| 36017 |
</thead> |
| 36018 |
<tbody> |
| 36019 |
<row><entry></entry><entry></entry></row> |
| 36020 |
<row> |
| 36021 |
<entry><link linkend="bracketsref">Brackets</link></entry><entry></entry> |
| 36022 |
</row> |
| 36023 |
<row> |
| 36024 |
<entry><option>if [ CONDITION ]</option></entry> |
| 36025 |
<entry><link linkend="leftbracket">Test construct</link></entry> |
| 36026 |
</row> |
| 36027 |
<row> |
| 36028 |
<entry><option>if [[ CONDITION ]]</option></entry> |
| 36029 |
<entry><link linkend="dblbrackets">Extended test construct</link></entry> |
| 36030 |
</row> |
| 36031 |
<row> |
| 36032 |
<entry><option>Array[1]=element1</option></entry> |
| 36033 |
<entry><link linkend="arrayref">Array initialization</link></entry> |
| 36034 |
</row> |
| 36035 |
<row> |
| 36036 |
<entry><option> [a-z]</option></entry> |
| 36037 |
<entry><link linkend="bracketsref">Range of |
| 36038 |
characters</link> within a <link linkend="regexref">Regular |
| 36039 |
Expression</link></entry> |
| 36040 |
</row> |
| 36041 |
<row><entry></entry><entry></entry></row> |
| 36042 |
<row> |
| 36043 |
<entry>Curly Brackets</entry><entry></entry> |
| 36044 |
</row> |
| 36045 |
<row> |
| 36046 |
<entry><option>${variable}</option></entry> |
| 36047 |
<entry><link linkend="paramsubref">Parameter substitution</link></entry> |
| 36048 |
</row> |
| 36049 |
<row> |
| 36050 |
<entry><option>${!variable}</option></entry> |
| 36051 |
<entry><link linkend="ivrref">Indirect variable reference</link></entry> |
| 36052 |
</row> |
| 36053 |
<row> |
| 36054 |
<entry><option>{ command1; command2; . . . commandN; }</option></entry> |
| 36055 |
<entry><link linkend="codeblockref">Block of code</link></entry> |
| 36056 |
</row> |
| 36057 |
<row> |
| 36058 |
<entry><option>{string1,string2,string3,...}</option></entry> |
| 36059 |
<entry><link linkend="braceexpref">Brace expansion</link></entry> |
| 36060 |
</row> |
| 36061 |
<row> |
| 36062 |
<entry><option>{a..z}</option></entry> |
| 36063 |
<entry><link linkend="braceexpref3">Extended brace expansion</link></entry> |
| 36064 |
</row> |
| 36065 |
<row> |
| 36066 |
<entry><option>{}</option></entry> |
| 36067 |
<entry>Text replacement, after <link |
| 36068 |
linkend="curlybracketsref">find</link> and <link |
| 36069 |
linkend="xargscurlyref">xargs</link></entry> |
| 36070 |
</row> |
| 36071 |
<row><entry></entry><entry></entry></row> |
| 36072 |
<row><entry></entry><entry></entry></row> |
| 36073 |
<row> |
| 36074 |
<entry><link linkend="parensref">Parentheses</link></entry><entry></entry> |
| 36075 |
</row> |
| 36076 |
<row> |
| 36077 |
<entry><option>( command1; command2 )</option></entry> |
| 36078 |
<entry>Command group executed within a <link |
| 36079 |
linkend="subshellsref">subshell</link></entry> |
| 36080 |
</row> |
| 36081 |
<row> |
| 36082 |
<entry><option>Array=(element1 element2 element3)</option></entry> |
| 36083 |
<entry><link linkend="arrayinit0">Array initialization</link></entry> |
| 36084 |
</row> |
| 36085 |
<row> |
| 36086 |
<entry><option>result=$(COMMAND)</option></entry> |
| 36087 |
<entry><link linkend="csparens">Command substitution</link>, |
| 36088 |
new style</entry> |
| 36089 |
</row> |
| 36090 |
<row> |
| 36091 |
<entry><option>>(COMMAND)</option></entry> |
| 36092 |
<entry><link linkend="processsubref">Process substitution</link></entry> |
| 36093 |
</row> |
| 36094 |
<row> |
| 36095 |
<entry><option><(COMMAND)</option></entry> |
| 36096 |
<entry>Process substitution</entry> |
| 36097 |
</row> |
| 36098 |
<row><entry></entry><entry></entry></row> |
| 36099 |
<row> |
| 36100 |
<entry><link linkend="dblparens">Double Parentheses</link></entry><entry></entry> |
| 36101 |
</row> |
| 36102 |
<row> |
| 36103 |
<entry><option>(( var = 78 ))</option></entry> |
| 36104 |
<entry><link linkend="dblparensref">Integer arithmetic</link></entry> |
| 36105 |
</row> |
| 36106 |
<row> |
| 36107 |
<entry><option>var=$(( 20 + 5 ))</option></entry> |
| 36108 |
<entry>Integer arithmetic, with variable assignment</entry> |
| 36109 |
</row> |
| 36110 |
<row> |
| 36111 |
<entry><option>(( var++ ))</option></entry> |
| 36112 |
<entry><firstterm>C-style</firstterm> <link |
| 36113 |
linkend="plusplusref"> variable increment</link></entry> |
| 36114 |
</row> |
| 36115 |
<row> |
| 36116 |
<entry><option>(( var-- ))</option></entry> |
| 36117 |
<entry><firstterm>C-style</firstterm> <link |
| 36118 |
linkend="plusplusref"> variable decrement</link></entry> |
| 36119 |
</row> |
| 36120 |
<row> |
| 36121 |
<entry><option>(( var0 = var1<98?9:21 ))</option></entry> |
| 36122 |
<entry><firstterm>C-style</firstterm> <link |
| 36123 |
linkend="cstrinary"> ternary</link> operation</entry> |
| 36124 |
</row> |
| 36125 |
<row><entry></entry><entry></entry></row> |
| 36126 |
<row> |
| 36127 |
<entry><link linkend="quotingref">Quoting</link></entry><entry></entry> |
| 36128 |
</row> |
| 36129 |
<row> |
| 36130 |
<entry><option>"$variable"</option></entry> |
| 36131 |
<entry><link linkend="dblquo">"Weak" quoting</link></entry> |
| 36132 |
</row> |
| 36133 |
<row> |
| 36134 |
<entry><option>'string'</option></entry> |
| 36135 |
<entry><link linkend="snglquo">'Strong' quoting</link></entry> |
| 36136 |
</row> |
| 36137 |
<row><entry></entry><entry></entry></row> |
| 36138 |
<row> |
| 36139 |
<entry><link linkend="backquotesref">Back Quotes</link></entry><entry></entry> |
| 36140 |
</row> |
| 36141 |
<row> |
| 36142 |
<entry><option>result=`COMMAND`</option></entry> |
| 36143 |
<entry><link linkend="commandsubref">Command |
| 36144 |
substitution</link>, classic style</entry> |
| 36145 |
</row> |
| 36146 |
</tbody> |
| 36147 |
</tgroup> |
| 36148 |
</table> |
| 36149 |
|
| 36150 |
</appendix> |
| 36151 |
<!-- Reference Cards appendix --> |
| 36152 |
|
| 36153 |
|
| 36154 |
|
| 36155 |
|
| 36156 |
|
| 36157 |
<appendix id="sedawk"> |
| 36158 |
<title>A Sed and Awk Micro-Primer</title> |
| 36159 |
|
| 36160 |
<para><anchor id="sedref"></para> |
| 36161 |
|
| 36162 |
<para>This is a very brief introduction to the <command>sed</command> |
| 36163 |
and <command>awk</command> text processing utilities. We will |
| 36164 |
deal with only a few basic commands here, but that will suffice |
| 36165 |
for understanding simple sed and awk constructs within shell |
| 36166 |
scripts.</para> |
| 36167 |
|
| 36168 |
|
| 36169 |
<para><command>sed</command>: a non-interactive |
| 36170 |
text file editor</para> |
| 36171 |
|
| 36172 |
<para><command>awk</command>: a field-oriented pattern processing |
| 36173 |
language with a <command>C</command>-style syntax</para> |
| 36174 |
|
| 36175 |
<para>For all their differences, the two utilities share a similar |
| 36176 |
invocation syntax, use <link linkend="regexref">regular |
| 36177 |
expressions </link>, read input by default |
| 36178 |
from <filename>stdin</filename>, and output to |
| 36179 |
<filename>stdout</filename>. These are well-behaved UNIX tools, |
| 36180 |
and they work together well. The output from one can be piped |
| 36181 |
to the other, and their combined capabilities give shell scripts |
| 36182 |
some of the power of <link linkend="perlref">Perl</link>.</para> |
| 36183 |
|
| 36184 |
<note><para>One important difference between the utilities is |
| 36185 |
that while shell scripts can easily pass arguments to sed, it |
| 36186 |
is more cumbersome for awk (see <xref linkend="coltotaler"> |
| 36187 |
and <xref linkend="coltotaler2">). |
| 36188 |
</para></note> |
| 36189 |
|
| 36190 |
|
| 36191 |
<sect1> |
| 36192 |
<title>Sed</title> |
| 36193 |
|
| 36194 |
<para><firstterm>Sed</firstterm> is a non-interactive |
| 36195 |
<footnote><para><firstterm>Sed</firstterm> executes without |
| 36196 |
user intervention.</para></footnote> |
| 36197 |
<command>s</command>tream <command>ed</command>itor. It |
| 36198 |
receives text input, whether from <filename>stdin</filename> |
| 36199 |
or from a file, performs certain operations on specified lines |
| 36200 |
of the input, one line at a time, then outputs the result to |
| 36201 |
<filename>stdout</filename> or to a file. Within a shell script, |
| 36202 |
<firstterm>sed</firstterm> is usually one of several tool |
| 36203 |
components in a pipe.</para> |
| 36204 |
|
| 36205 |
<para><firstterm>Sed</firstterm> determines which lines of |
| 36206 |
its input that it will operate on from the <firstterm>address |
| 36207 |
range</firstterm> passed to it. |
| 36208 |
<footnote><para>If no address range is specified, the default |
| 36209 |
is <emphasis>all</emphasis> lines.</para></footnote> |
| 36210 |
Specify this address range either by line number or by a |
| 36211 |
pattern to match. For example, <replaceable>3d</replaceable> |
| 36212 |
signals <firstterm>sed</firstterm> to delete line 3 of the |
| 36213 |
input, and <replaceable>/Windows/d</replaceable> tells sed |
| 36214 |
that you want every line of the input containing a match to |
| 36215 |
<quote>Windows</quote> deleted.</para> |
| 36216 |
|
| 36217 |
<para>Of all the operations in the <firstterm>sed</firstterm> |
| 36218 |
toolkit, we will focus primarily on the three most commonly |
| 36219 |
used ones. These are <command>p</command>rinting (to |
| 36220 |
<filename>stdout</filename>), <command>d</command>eletion, |
| 36221 |
and <command>s</command>ubstitution.</para> |
| 36222 |
|
| 36223 |
|
| 36224 |
<para><anchor id="sedbasictable"></para> |
| 36225 |
<table> |
| 36226 |
<title>Basic sed operators</title> |
| 36227 |
<tgroup cols="3"> |
| 36228 |
<thead> |
| 36229 |
<row> |
| 36230 |
<entry>Operator</entry> |
| 36231 |
<entry>Name</entry> |
| 36232 |
<entry>Effect</entry> |
| 36233 |
</row> |
| 36234 |
</thead> |
| 36235 |
<tbody> |
| 36236 |
<row> |
| 36237 |
<entry><option>[address-range]/p</option></entry> |
| 36238 |
<entry>print</entry> |
| 36239 |
<entry>Print [specified address range]</entry> |
| 36240 |
</row> |
| 36241 |
<row> |
| 36242 |
<entry><option>[address-range]/d</option></entry> |
| 36243 |
<entry>delete</entry> |
| 36244 |
<entry>Delete [specified address range]</entry> |
| 36245 |
</row> |
| 36246 |
<row> |
| 36247 |
<entry><option>s/pattern1/pattern2/</option></entry> |
| 36248 |
<entry>substitute</entry> |
| 36249 |
<entry>Substitute pattern2 for first instance of pattern1 in a line</entry> |
| 36250 |
</row> |
| 36251 |
<row> |
| 36252 |
<entry><option>[address-range]/s/pattern1/pattern2/</option></entry> |
| 36253 |
<entry>substitute</entry> |
| 36254 |
<entry>Substitute pattern2 for first instance of pattern1 in a |
| 36255 |
line, over <replaceable>address-range</replaceable></entry> |
| 36256 |
</row> |
| 36257 |
<row> |
| 36258 |
<entry><option>[address-range]/y/pattern1/pattern2/</option></entry> |
| 36259 |
<entry>transform</entry> |
| 36260 |
<entry>replace any character in pattern1 with the |
| 36261 |
corresponding character in pattern2, over |
| 36262 |
<replaceable>address-range</replaceable> (equivalent of |
| 36263 |
<command>tr</command>)</entry> |
| 36264 |
</row> |
| 36265 |
<row> |
| 36266 |
<entry><option>[address] i pattern Filename</option></entry> |
| 36267 |
<entry>insert</entry> |
| 36268 |
<entry>Insert pattern at address indicated in file Filename. |
| 36269 |
Usually used with <option>-i</option> |
| 36270 |
<replaceable>in-place</replaceable> option.</entry> |
| 36271 |
</row> |
| 36272 |
<row> |
| 36273 |
<entry><option>g</option></entry> |
| 36274 |
<entry>global</entry> |
| 36275 |
<entry>Operate on <emphasis>every</emphasis> pattern match |
| 36276 |
within each matched line of input</entry> |
| 36277 |
</row> |
| 36278 |
</tbody> |
| 36279 |
</tgroup> |
| 36280 |
</table> |
| 36281 |
|
| 36282 |
<note><para>Unless the <option>g</option> |
| 36283 |
(<firstterm>global</firstterm>) operator is appended to a |
| 36284 |
<firstterm>substitute</firstterm> command, the substitution |
| 36285 |
operates only on the <emphasis>first</emphasis> instance of a |
| 36286 |
pattern match within each line.</para></note> |
| 36287 |
|
| 36288 |
<para>From the command-line and in a shell script, a sed operation may |
| 36289 |
require quoting and certain options.</para> |
| 36290 |
|
| 36291 |
<para><programlisting>sed -e '/^$/d' $filename |
| 36292 |
# The -e option causes the next string to be interpreted as an editing instruction. |
| 36293 |
# (If passing only a single instruction to sed, the "-e" is optional.) |
| 36294 |
# The "strong" quotes ('') protect the RE characters in the instruction |
| 36295 |
#+ from reinterpretation as special characters by the body of the script. |
| 36296 |
# (This reserves RE expansion of the instruction for sed.) |
| 36297 |
# |
| 36298 |
# Operates on the text contained in file $filename. |
| 36299 |
</programlisting></para> |
| 36300 |
|
| 36301 |
<para>In certain cases, a <firstterm>sed</firstterm> editing command will |
| 36302 |
not work with single quotes.</para> |
| 36303 |
|
| 36304 |
<para><programlisting> |
| 36305 |
filename=file1.txt |
| 36306 |
pattern=BEGIN |
| 36307 |
|
| 36308 |
sed "/^$pattern/d" "$filename" # Works as specified. |
| 36309 |
# sed '/^$pattern/d' "$filename" has unexpected results. |
| 36310 |
# In this instance, with strong quoting (' ... '), |
| 36311 |
#+ "$pattern" will not expand to "BEGIN".</programlisting></para> |
| 36312 |
|
| 36313 |
|
| 36314 |
<note><para><firstterm>Sed</firstterm> uses the <option>-e</option> |
| 36315 |
option to specify that the following string is an instruction |
| 36316 |
or set of instructions. If there is only a single instruction |
| 36317 |
contained in the string, then this may be omitted.</para></note> |
| 36318 |
|
| 36319 |
<para><programlisting>sed -n '/xzy/p' $filename |
| 36320 |
# The -n option tells sed to print only those lines matching the pattern. |
| 36321 |
# Otherwise all input lines would print. |
| 36322 |
# The -e option not necessary here since there is only a single editing instruction. |
| 36323 |
</programlisting></para> |
| 36324 |
|
| 36325 |
<para><anchor id="sedoptable"></para> |
| 36326 |
<table> |
| 36327 |
<title>Examples of sed operators</title> |
| 36328 |
<tgroup cols="2"> |
| 36329 |
<thead> |
| 36330 |
<row> |
| 36331 |
<entry>Notation</entry> |
| 36332 |
<entry>Effect</entry> |
| 36333 |
</row> |
| 36334 |
</thead> |
| 36335 |
<tbody> |
| 36336 |
<row> |
| 36337 |
<entry><option>8d</option></entry> |
| 36338 |
<entry>Delete 8th line of input.</entry> |
| 36339 |
</row> |
| 36340 |
<row> |
| 36341 |
<entry><option>/^$/d</option></entry> |
| 36342 |
<entry>Delete all blank lines.</entry> |
| 36343 |
</row> |
| 36344 |
<row> |
| 36345 |
<entry><option>1,/^$/d</option></entry> |
| 36346 |
<entry>Delete from beginning of input up to, and including |
| 36347 |
first blank line.</entry> |
| 36348 |
</row> |
| 36349 |
<row> |
| 36350 |
<entry><option>/Jones/p</option></entry> |
| 36351 |
<entry>Print only lines containing <quote>Jones</quote> (with |
| 36352 |
<token>-n</token> option).</entry> |
| 36353 |
</row> |
| 36354 |
<row> |
| 36355 |
<entry><option>s/Windows/Linux/</option></entry> |
| 36356 |
<entry>Substitute <quote>Linux</quote> for first instance |
| 36357 |
of <quote>Windows</quote> found in each input line.</entry> |
| 36358 |
</row> |
| 36359 |
<row> |
| 36360 |
<entry><option>s/BSOD/stability/g</option></entry> |
| 36361 |
<entry>Substitute <quote>stability</quote> for every instance |
| 36362 |
of <quote>BSOD</quote> found in each input line.</entry> |
| 36363 |
</row> |
| 36364 |
<row> |
| 36365 |
<entry><option>s/ *$//</option></entry> |
| 36366 |
<entry>Delete all spaces at the end of every line.</entry> |
| 36367 |
</row> |
| 36368 |
<row> |
| 36369 |
<entry><option>s/00*/0/g</option></entry> |
| 36370 |
<entry>Compress all consecutive sequences of zeroes into |
| 36371 |
a single zero.</entry> |
| 36372 |
</row> |
| 36373 |
<row> |
| 36374 |
<entry><option>echo "Working on it." | sed -e '1i How far are you along?'</option></entry> |
| 36375 |
<entry>Prints "How far are you along?" as first line, |
| 36376 |
"Working on it" as second.</entry> |
| 36377 |
</row> |
| 36378 |
<row> |
| 36379 |
<entry><option>5i 'Linux is great.' file.txt</option></entry> |
| 36380 |
<entry>Inserts 'Linux is great.' at line 5 of the file |
| 36381 |
file.txt.</entry> |
| 36382 |
</row> |
| 36383 |
<row> |
| 36384 |
<entry><option>/GUI/d</option></entry> |
| 36385 |
<entry>Delete all lines containing <quote>GUI</quote>.</entry> |
| 36386 |
</row> |
| 36387 |
<row> |
| 36388 |
<entry><option>s/GUI//g</option></entry> |
| 36389 |
<entry>Delete all instances of <quote>GUI</quote>, leaving the |
| 36390 |
remainder of each line intact.</entry> |
| 36391 |
</row> |
| 36392 |
</tbody> |
| 36393 |
</tgroup> |
| 36394 |
</table> |
| 36395 |
|
| 36396 |
|
| 36397 |
<para>Substituting a zero-length string for another is equivalent |
| 36398 |
to deleting that string within a line of input. This leaves the |
| 36399 |
remainder of the line intact. Applying <userinput>s/GUI//</userinput> |
| 36400 |
to the line |
| 36401 |
<screen><userinput>The most important parts of any application are its GUI and sound effects</userinput></screen> |
| 36402 |
results in |
| 36403 |
<screen><computeroutput>The most important parts of any application are its and sound effects</computeroutput></screen></para> |
| 36404 |
|
| 36405 |
<para>A backslash forces the <command>sed</command> replacement |
| 36406 |
command to continue on to the next line. This has the effect of |
| 36407 |
using the <firstterm>newline</firstterm> at the end of the first |
| 36408 |
line as the <firstterm>replacement string</firstterm>. |
| 36409 |
|
| 36410 |
<programlisting>s/^ */\ |
| 36411 |
/g</programlisting> |
| 36412 |
|
| 36413 |
This substitution replaces line-beginning spaces with a |
| 36414 |
newline. The net result is to replace paragraph indents with a |
| 36415 |
blank line between paragraphs.</para> |
| 36416 |
|
| 36417 |
<para>An address range followed by one or more operations may require |
| 36418 |
open and closed curly brackets, with appropriate newlines. |
| 36419 |
<programlisting>/[0-9A-Za-z]/,/^$/{ |
| 36420 |
/^$/d |
| 36421 |
}</programlisting> |
| 36422 |
This deletes only the first of each set of consecutive blank |
| 36423 |
lines. That might be useful for single-spacing a text file, |
| 36424 |
but retaining the blank line(s) between paragraphs.</para> |
| 36425 |
|
| 36426 |
|
| 36427 |
<note><para>The usual delimiter that <firstterm>sed</firstterm> uses is |
| 36428 |
<token>/</token>. However, <emphasis>sed</emphasis> allows other |
| 36429 |
delimiters, such as <token>%</token>. This is useful when |
| 36430 |
<token>/</token> is part of a replacement string, as in a file pathname. |
| 36431 |
See <xref linkend="findstring"> and <xref |
| 36432 |
linkend="stripc">.</para></note> |
| 36433 |
|
| 36434 |
<para><anchor id="doublespace"></para> |
| 36435 |
<tip><para>A quick way to double-space a text file is <userinput>sed G |
| 36436 |
filename</userinput>.</para></tip> |
| 36437 |
|
| 36438 |
<para>For illustrative examples of sed within shell scripts, see: |
| 36439 |
<orderedlist> |
| 36440 |
<listitem><para><xref linkend="ex3"></para></listitem> |
| 36441 |
<listitem><para><xref linkend="ex4"></para></listitem> |
| 36442 |
<listitem><para><xref linkend="ex57"></para></listitem> |
| 36443 |
<listitem><para><xref linkend="rn"></para></listitem> |
| 36444 |
<listitem><para><xref linkend="grp"></para></listitem> |
| 36445 |
<listitem><para><xref linkend="col"></para></listitem> |
| 36446 |
<listitem><para><xref linkend="behead"></para></listitem> |
| 36447 |
<listitem><para><xref linkend="tree"></para></listitem> |
| 36448 |
<listitem><para><xref linkend="tree2"></para></listitem> |
| 36449 |
<listitem><para><xref linkend="stripc"></para></listitem> |
| 36450 |
<listitem><para><xref linkend="findstring"></para></listitem> |
| 36451 |
<listitem><para><xref linkend="base"></para></listitem> |
| 36452 |
<listitem><para><xref linkend="mailformat"></para></listitem> |
| 36453 |
<listitem><para><xref linkend="rnd"></para></listitem> |
| 36454 |
<listitem><para><xref linkend="wf"></para></listitem> |
| 36455 |
<listitem><para><xref linkend="lifeslow"></para></listitem> |
| 36456 |
<listitem><para><xref linkend="selfdocument"></para></listitem> |
| 36457 |
<listitem><para><xref linkend="dictlookup"></para></listitem> |
| 36458 |
<listitem><para><xref linkend="whx"></para></listitem> |
| 36459 |
<listitem><para><xref linkend="bashpodder"></para></listitem> |
| 36460 |
<listitem><para><xref linkend="tohtml"></para></listitem> |
| 36461 |
<listitem><para><xref linkend="stopwatch"></para></listitem> |
| 36462 |
<listitem><para><xref linkend="sedappend"></para></listitem> |
| 36463 |
</orderedlist> |
| 36464 |
</para> |
| 36465 |
|
| 36466 |
<para>For a more extensive treatment of <firstterm>sed</firstterm>, |
| 36467 |
refer to the <link linkend="dgsedref">pertinent references</link> |
| 36468 |
in the <xref linkend="biblio">.</para> |
| 36469 |
|
| 36470 |
</sect1> |
| 36471 |
<!-- End sed primer --> |
| 36472 |
|
| 36473 |
|
| 36474 |
|
| 36475 |
<sect1 id="awk"> |
| 36476 |
<title>Awk</title> |
| 36477 |
|
| 36478 |
<para><anchor id="awkref"></para> |
| 36479 |
|
| 36480 |
<para><firstterm>Awk</firstterm> |
| 36481 |
<footnote><para>Its name derives from the initials of its authors, |
| 36482 |
<command>A</command>ho, <command>W</command>einberg, and |
| 36483 |
<command>K</command>ernighan.</para></footnote> |
| 36484 |
is a full-featured text processing language with a syntax |
| 36485 |
reminiscent of <firstterm>C</firstterm>. While it possesses an |
| 36486 |
extensive set of operators and capabilities, we will cover only |
| 36487 |
a few of these here - the ones most useful in shell scripts.</para> |
| 36488 |
|
| 36489 |
<para>Awk breaks each line of input passed to it into |
| 36490 |
<anchor id="fieldref2"> |
| 36491 |
<link linkend="fieldref">fields</link>. By default, a field |
| 36492 |
is a string of consecutive characters delimited by <link |
| 36493 |
linkend="whitespaceref">whitespace</link>, though there are options |
| 36494 |
for changing this. Awk parses and operates on each separate |
| 36495 |
field. This makes it ideal for handling structured text files |
| 36496 |
-- especially tables -- data organized into consistent chunks, |
| 36497 |
such as rows and columns.</para> |
| 36498 |
|
| 36499 |
<para><link linkend="snglquo">Strong quoting</link> and <link |
| 36500 |
linkend="codeblockref">curly brackets</link> enclose blocks of |
| 36501 |
awk code within a shell script.</para> |
| 36502 |
|
| 36503 |
<para><programlisting># $1 is field #1, $2 is field #2, etc. |
| 36504 |
|
| 36505 |
echo one two | awk '{print $1}' |
| 36506 |
# one |
| 36507 |
|
| 36508 |
echo one two | awk '{print $2}' |
| 36509 |
# two |
| 36510 |
|
| 36511 |
# But what is field #0 ($0)? |
| 36512 |
echo one two | awk '{print $0}' |
| 36513 |
# one two |
| 36514 |
# All the fields! |
| 36515 |
|
| 36516 |
|
| 36517 |
awk '{print $3}' $filename |
| 36518 |
# Prints field #3 of file $filename to stdout. |
| 36519 |
|
| 36520 |
awk '{print $1 $5 $6}' $filename |
| 36521 |
# Prints fields #1, #5, and #6 of file $filename. |
| 36522 |
|
| 36523 |
awk '{print $0}' $filename |
| 36524 |
# Prints the entire file! |
| 36525 |
# Same effect as: cat $filename . . . or . . . sed '' $filename</programlisting></para> |
| 36526 |
|
| 36527 |
<para>We have just seen the awk <firstterm>print</firstterm> command |
| 36528 |
in action. The only other feature of awk we need to deal with |
| 36529 |
here is variables. Awk handles variables similarly to shell |
| 36530 |
scripts, though a bit more flexibly.</para> |
| 36531 |
|
| 36532 |
<para><programlisting>{ total += ${column_number} }</programlisting> |
| 36533 |
This adds the value of <parameter>column_number</parameter> to |
| 36534 |
the running total of <parameter>total</parameter>>. Finally, to print |
| 36535 |
<quote>total</quote>, there is an <command>END</command> command |
| 36536 |
block, executed after the script has processed all its input. |
| 36537 |
<programlisting>END { print total }</programlisting></para> |
| 36538 |
|
| 36539 |
<para>Corresponding to the <command>END</command>, there is a |
| 36540 |
<command>BEGIN</command>, for a code block to be performed before awk |
| 36541 |
starts processing its input.</para> |
| 36542 |
|
| 36543 |
<para>The following example illustrates how <command>awk</command> can |
| 36544 |
add text-parsing tools to a shell script.</para> |
| 36545 |
|
| 36546 |
<example id="lettercount2"> |
| 36547 |
<title>Counting Letter Occurrences</title> |
| 36548 |
<programlisting>&lettercount2;</programlisting> |
| 36549 |
</example> |
| 36550 |
|
| 36551 |
<para>For simpler examples of awk within shell scripts, see: |
| 36552 |
<orderedlist> |
| 36553 |
<listitem><para><xref linkend="ex44"></para></listitem> |
| 36554 |
<listitem><para><xref linkend="redir4"></para></listitem> |
| 36555 |
<listitem><para><xref linkend="stripc"></para></listitem> |
| 36556 |
<listitem><para><xref linkend="coltotaler"></para></listitem> |
| 36557 |
<listitem><para><xref linkend="coltotaler2"></para></listitem> |
| 36558 |
<listitem><para><xref linkend="coltotaler3"></para></listitem> |
| 36559 |
<listitem><para><xref linkend="pidid"></para></listitem> |
| 36560 |
<listitem><para><xref linkend="constat"></para></listitem> |
| 36561 |
<listitem><para><xref linkend="fileinfo"></para></listitem> |
| 36562 |
<listitem><para><xref linkend="blotout"></para></listitem> |
| 36563 |
<listitem><para><xref linkend="seedingrandom"></para></listitem> |
| 36564 |
<listitem><para><xref linkend="idelete"></para></listitem> |
| 36565 |
<listitem><para><xref linkend="substringex"></para></listitem> |
| 36566 |
<listitem><para><xref linkend="sumproduct"></para></listitem> |
| 36567 |
<listitem><para><xref linkend="userlist"></para></listitem> |
| 36568 |
<listitem><para><xref linkend="prasc"></para></listitem> |
| 36569 |
<listitem><para><xref linkend="hypot"></para></listitem> |
| 36570 |
<listitem><para><xref linkend="ascii3sh"></para></listitem> |
| 36571 |
</orderedlist> |
| 36572 |
</para> |
| 36573 |
|
| 36574 |
<para>That's all the awk we'll cover here, folks, but there's lots |
| 36575 |
more to learn. See the appropriate references in the <xref |
| 36576 |
linkend="biblio">.</para> |
| 36577 |
|
| 36578 |
|
| 36579 |
</sect1> |
| 36580 |
<!-- End awk primer --> |
| 36581 |
|
| 36582 |
</appendix> |
| 36583 |
<!-- End sed/awk appendix --> |
| 36584 |
|
| 36585 |
|
| 36586 |
<appendix id="pathmanagement"> |
| 36587 |
<title>Parsing and Managing Pathnames</title> |
| 36588 |
|
| 36589 |
<para>Emmanual Rouat contributed the following example of parsing |
| 36590 |
and transforming <firstterm>filenames</firstterm> and, in |
| 36591 |
particular, <link linkend="pathnameref">pathnames</link>. It draws |
| 36592 |
heavily on the functionality of <firstterm>sed</firstterm>.</para> |
| 36593 |
|
| 36594 |
<para> |
| 36595 |
<programlisting>#!/usr/bin/env bash |
| 36596 |
#----------------------------------------------------------- |
| 36597 |
# Management of PATH, LD_LIBRARY_PATH, MANPATH variables... |
| 36598 |
# By Emmanuel Rouat <no-email> |
| 36599 |
# (Inspired by the bash documentation 'pathfuncs' and on |
| 36600 |
# discussions found on stackoverflow: |
| 36601 |
# http://stackoverflow.com/questions/370047/ |
| 36602 |
# http://stackoverflow.com/questions/273909/#346860 ) |
| 36603 |
# Last modified: Sat Sep 22 12:01:55 CEST 2012 |
| 36604 |
# |
| 36605 |
# The following functions handle spaces correctly. |
| 36606 |
# These functions belong in .bash_profile rather than in |
| 36607 |
# .bashrc, I guess. |
| 36608 |
# |
| 36609 |
# The modular aspect of these functions should make it easy |
| 36610 |
# to expand them to handle path substitutions instead |
| 36611 |
# of path removal etc.... |
| 36612 |
# |
| 36613 |
# See http://www.catonmat.net/blog/awk-one-liners-explained-part-two/ |
| 36614 |
# (item 43) for an explanation of the 'duplicate-entries' removal |
| 36615 |
# (it's a nice trick!) |
| 36616 |
#----------------------------------------------------------- |
| 36617 |
|
| 36618 |
# Show $@ (usually PATH) as list. |
| 36619 |
function p_show() { local p="$@" && for p; do [[ ${!p} ]] && |
| 36620 |
echo -e ${!p//:/\\n}; done } |
| 36621 |
|
| 36622 |
# Filter out empty lines, multiple/trailing slashes, and duplicate entries. |
| 36623 |
function p_filter() |
| 36624 |
{ awk '/^[ \t]*$/ {next} {sub(/\/+$/, "");gsub(/\/+/, "/")}!x[$0]++' ;} |
| 36625 |
|
| 36626 |
# Rebuild list of items into ':' separated word (PATH-like). |
| 36627 |
function p_build() { paste -sd: ;} |
| 36628 |
|
| 36629 |
# Clean $1 (typically PATH) and rebuild it |
| 36630 |
function p_clean() |
| 36631 |
{ local p=${1} && eval ${p}='$(p_show ${p} | p_filter | p_build)' ;} |
| 36632 |
|
| 36633 |
# Remove $1 from $2 (found on stackoverflow, with modifications). |
| 36634 |
function p_rm() |
| 36635 |
{ local d=$(echo $1 | p_filter) p=${2} && |
| 36636 |
eval ${p}='$(p_show ${p} | p_filter | grep -xv "${d}" | p_build)' ;} |
| 36637 |
|
| 36638 |
# Same as previous, but filters on a pattern (dangerous... |
| 36639 |
#+ don't use 'bin' or '/' as pattern!). |
| 36640 |
function p_rmpat() |
| 36641 |
{ local d=$(echo $1 | p_filter) p=${2} && eval ${p}='$(p_show ${p} | |
| 36642 |
p_filter | grep -v "${d}" | p_build)' ;} |
| 36643 |
|
| 36644 |
# Delete $1 from $2 and append it cleanly. |
| 36645 |
function p_append() |
| 36646 |
{ local d=$(echo $1 | p_filter) p=${2} && p_rm "${d}" ${p} && |
| 36647 |
eval ${p}='$(p_show ${p} d | p_build)' ;} |
| 36648 |
|
| 36649 |
# Delete $1 from $2 and prepend it cleanly. |
| 36650 |
function p_prepend() |
| 36651 |
{ local d=$(echo $1 | p_filter) p=${2} && p_rm "${d}" ${p} && |
| 36652 |
eval ${p}='$(p_show d ${p} | p_build)' ;} |
| 36653 |
|
| 36654 |
# Some tests: |
| 36655 |
echo |
| 36656 |
MYPATH="/bin:/usr/bin/:/bin://bin/" |
| 36657 |
p_append "/project//my project/bin" MYPATH |
| 36658 |
echo "Append '/project//my project/bin' to '/bin:/usr/bin/:/bin://bin/'" |
| 36659 |
echo "(result should be: /bin:/usr/bin:/project/my project/bin)" |
| 36660 |
echo $MYPATH |
| 36661 |
|
| 36662 |
echo |
| 36663 |
MYOTHERPATH="/bin:/usr/bin/:/bin:/project//my project/bin" |
| 36664 |
p_prepend "/project//my project/bin" MYOTHERPATH |
| 36665 |
echo "Prepend '/project//my project/bin' \ |
| 36666 |
to '/bin:/usr/bin/:/bin:/project//my project/bin/'" |
| 36667 |
echo "(result should be: /project/my project/bin:/bin:/usr/bin)" |
| 36668 |
echo $MYOTHERPATH |
| 36669 |
|
| 36670 |
echo |
| 36671 |
p_prepend "/project//my project/bin" FOOPATH # FOOPATH doesn't exist. |
| 36672 |
echo "Prepend '/project//my project/bin' to an unset variable" |
| 36673 |
echo "(result should be: /project/my project/bin)" |
| 36674 |
echo $FOOPATH |
| 36675 |
|
| 36676 |
echo |
| 36677 |
BARPATH="/a:/b/://b c://a:/my local pub" |
| 36678 |
p_clean BARPATH |
| 36679 |
echo "Clean BARPATH='/a:/b/://b c://a:/my local pub'" |
| 36680 |
echo "(result should be: /a:/b:/b c:/my local pub)" |
| 36681 |
echo $BARPATH |
| 36682 |
</programlisting> |
| 36683 |
</para> |
| 36684 |
|
| 36685 |
<para>***</para> |
| 36686 |
|
| 36687 |
<para>David Wheeler kindly permitted me to use his instructive |
| 36688 |
examples.</para> |
| 36689 |
|
| 36690 |
<para><programlisting>Doing it correctly: A quick summary |
| 36691 |
by David Wheeler |
| 36692 |
http://www.dwheeler.com/essays/filenames-in-shell.html |
| 36693 |
|
| 36694 |
So, how can you process filenames correctly in shell? Here's a quick |
| 36695 |
summary about how to do it correctly, for the impatient who "just want the |
| 36696 |
answer". In short: Double-quote to use "$variable" instead of $variable, |
| 36697 |
set IFS to just newline and tab, prefix all globs/filenames so they cannot |
| 36698 |
begin with "-" when expanded, and use one of a few templates that work |
| 36699 |
correctly. Here are some of those templates that work correctly: |
| 36700 |
|
| 36701 |
|
| 36702 |
IFS="$(printf '\n\t')" |
| 36703 |
# Remove SPACE, so filenames with spaces work well. |
| 36704 |
|
| 36705 |
# Correct glob use: |
| 36706 |
#+ always use "for" loop, prefix glob, check for existence: |
| 36707 |
for file in ./* ; do # Use "./*" ... NEVER bare "*" ... |
| 36708 |
if [ -e "$file" ] ; then # Make sure it isn't an empty match. |
| 36709 |
COMMAND ... "$file" ... |
| 36710 |
fi |
| 36711 |
done |
| 36712 |
|
| 36713 |
|
| 36714 |
|
| 36715 |
# Correct glob use, but requires nonstandard bash extension. |
| 36716 |
shopt -s nullglob # Bash extension, |
| 36717 |
#+ so that empty glob matches will work. |
| 36718 |
for file in ./* ; do # Use "./*", NEVER bare "*" |
| 36719 |
COMMAND ... "$file" ... |
| 36720 |
done |
| 36721 |
|
| 36722 |
|
| 36723 |
|
| 36724 |
# These handle all filenames correctly; |
| 36725 |
#+ can be unwieldy if COMMAND is large: |
| 36726 |
find ... -exec COMMAND... {} \; |
| 36727 |
find ... -exec COMMAND... {} \+ # If multiple files are okay for COMMAND. |
| 36728 |
|
| 36729 |
|
| 36730 |
|
| 36731 |
# This skips filenames with control characters |
| 36732 |
#+ (including tab and newline). |
| 36733 |
IFS="$(printf '\n\t')" |
| 36734 |
controlchars="$(printf '*[\001-\037\177]*')" |
| 36735 |
for file in $(find . ! -name "$controlchars"') ; do |
| 36736 |
COMMAND "$file" ... |
| 36737 |
done |
| 36738 |
|
| 36739 |
|
| 36740 |
|
| 36741 |
# Okay if filenames can't contain tabs or newlines -- |
| 36742 |
#+ beware the assumption. |
| 36743 |
IFS="$(printf '\n\t')" |
| 36744 |
for file in $(find .) ; do |
| 36745 |
COMMAND "$file" ... |
| 36746 |
done |
| 36747 |
|
| 36748 |
|
| 36749 |
|
| 36750 |
# Requires nonstandard but common extensions in find and xargs: |
| 36751 |
find . -print0 | xargs -0 COMMAND |
| 36752 |
|
| 36753 |
# Requires nonstandard extensions to find and to shell (bash works). |
| 36754 |
# variables might not stay set once the loop ends: |
| 36755 |
find . -print0 | while IFS="" read -r -d "" file ; do ... |
| 36756 |
COMMAND "$file" # Use quoted "$file", not $file, everywhere. |
| 36757 |
done |
| 36758 |
|
| 36759 |
|
| 36760 |
|
| 36761 |
# Requires nonstandard extensions to find and to shell (bash works). |
| 36762 |
# Underlying system must include named pipes (FIFOs) |
| 36763 |
#+ or the /dev/fd mechanism. |
| 36764 |
# In this version, variables *do* stay set after the loop ends, |
| 36765 |
# and you can read from stdin. |
| 36766 |
#+ (Change the 4 to another number if fd 4 is needed.) |
| 36767 |
|
| 36768 |
while IFS="" read -r -d "" file <&4 ; do |
| 36769 |
COMMAND "$file" # Use quoted "$file" -- not $file, everywhere. |
| 36770 |
done 4< <(find . -print0) |
| 36771 |
|
| 36772 |
|
| 36773 |
# Named pipe version. |
| 36774 |
# Requires nonstandard extensions to find and to shell's read (bash ok). |
| 36775 |
# Underlying system must include named pipes (FIFOs). |
| 36776 |
# Again, in this version, variables *do* stay set after the loop ends, |
| 36777 |
# and you can read from stdin. |
| 36778 |
# (Change the 4 to something else if fd 4 needed). |
| 36779 |
|
| 36780 |
mkfifo mypipe |
| 36781 |
|
| 36782 |
find . -print0 > mypipe & |
| 36783 |
while IFS="" read -r -d "" file <&4 ; do |
| 36784 |
COMMAND "$file" # Use quoted "$file", not $file, everywhere. |
| 36785 |
done 4< mypipe</programlisting></para> |
| 36786 |
|
| 36787 |
</appendix> |
| 36788 |
<!-- End path management appendix --> |
| 36789 |
|
| 36790 |
|
| 36791 |
<appendix id="exitcodes"> |
| 36792 |
<title>Exit Codes With Special Meanings</title> |
| 36793 |
|
| 36794 |
<para><anchor id="exitcodesref"></para> |
| 36795 |
|
| 36796 |
<table> |
| 36797 |
<title><firstterm>Reserved</firstterm> Exit Codes</title> |
| 36798 |
<tgroup cols="4"> |
| 36799 |
<thead> |
| 36800 |
<row> |
| 36801 |
<entry>Exit Code Number</entry> |
| 36802 |
<entry>Meaning</entry> |
| 36803 |
<entry>Example</entry> |
| 36804 |
<entry>Comments</entry> |
| 36805 |
</row> |
| 36806 |
</thead> |
| 36807 |
<tbody> |
| 36808 |
<row> |
| 36809 |
<entry><option>1</option></entry> |
| 36810 |
<entry>Catchall for general errors</entry> |
| 36811 |
<entry>let "var1 = 1/0"</entry> |
| 36812 |
<entry>Miscellaneous errors, such as <quote>divide by |
| 36813 |
zero</quote> and other impermissible operations</entry> |
| 36814 |
</row> |
| 36815 |
<row> |
| 36816 |
<entry><option>2</option></entry> |
| 36817 |
<entry>Misuse of shell builtins (according to Bash documentation)</entry> |
| 36818 |
<entry>empty_function() {}</entry> |
| 36819 |
<entry><link linkend="missingkeyword">Missing keyword</link> |
| 36820 |
or command, or permission problem (and <link |
| 36821 |
linkend="differr2"><firstterm>diff</firstterm> return code |
| 36822 |
on a failed binary file comparison</link>).</entry> |
| 36823 |
</row> |
| 36824 |
<row> |
| 36825 |
<entry><option>126</option></entry> |
| 36826 |
<entry>Command invoked cannot execute</entry> |
| 36827 |
<entry>/dev/null</entry> |
| 36828 |
<entry>Permission problem or command is not an executable</entry> |
| 36829 |
</row> |
| 36830 |
<row> |
| 36831 |
<entry><option>127</option></entry> |
| 36832 |
<entry><quote>command not found</quote></entry> |
| 36833 |
<entry>illegal_command</entry> |
| 36834 |
<entry>Possible problem with <varname>$PATH</varname> or a typo</entry> |
| 36835 |
</row> |
| 36836 |
<row> |
| 36837 |
<entry><option>128</option></entry> |
| 36838 |
<entry>Invalid argument to <link linkend="exitcommandref">exit</link></entry> |
| 36839 |
<entry>exit 3.14159</entry> |
| 36840 |
<entry><command>exit</command> takes only integer args in the |
| 36841 |
range <returnvalue>0 - 255</returnvalue> (see |
| 36842 |
first footnote)</entry> |
| 36843 |
</row> |
| 36844 |
<row> |
| 36845 |
<entry><option>128+n</option></entry> |
| 36846 |
<entry>Fatal error signal <quote>n</quote></entry> |
| 36847 |
<entry><firstterm>kill -9</firstterm> <varname>$PPID</varname> of script</entry> |
| 36848 |
<entry><userinput>$?</userinput> returns |
| 36849 |
<errorcode>137</errorcode> (128 + 9)</entry> |
| 36850 |
</row> |
| 36851 |
<row> |
| 36852 |
<entry><option>130</option></entry> |
| 36853 |
<entry>Script terminated by Control-C</entry> |
| 36854 |
<entry><emphasis>Ctl-C</emphasis></entry> |
| 36855 |
<entry>Control-C is fatal error signal |
| 36856 |
<errorcode>2</errorcode>, (130 = 128 + 2, see above)</entry> |
| 36857 |
</row> |
| 36858 |
<row> |
| 36859 |
<entry><option>255*</option></entry> |
| 36860 |
<entry>Exit status out of range</entry> |
| 36861 |
<entry>exit <returnvalue>-1</returnvalue></entry> |
| 36862 |
<entry><command>exit</command> takes only integer args in the |
| 36863 |
range <errorcode>0 - 255</errorcode></entry> |
| 36864 |
</row> |
| 36865 |
</tbody> |
| 36866 |
</tgroup> |
| 36867 |
</table> |
| 36868 |
|
| 36869 |
<para>According to the above table, exit codes <errorcode>1 - 2, |
| 36870 |
126 - 165, and 255</errorcode> |
| 36871 |
|
| 36872 |
<footnote><para><anchor id="excoor">Out of range exit values |
| 36873 |
can result in unexpected exit codes. An exit value |
| 36874 |
greater than <errorcode>255</errorcode> returns an |
| 36875 |
exit code <link linkend="moduloref">modulo</link> |
| 36876 |
<errorcode>256</errorcode>. For example, <firstterm>exit |
| 36877 |
3809</firstterm> gives an exit code of <errorcode>225</errorcode> |
| 36878 |
(3809 % 256 = 225).</para></footnote> |
| 36879 |
|
| 36880 |
have special meanings, and should therefore be avoided for |
| 36881 |
user-specified exit parameters. Ending a script with <firstterm>exit |
| 36882 |
127</firstterm> would certainly cause confusion when troubleshooting |
| 36883 |
(is the error code a <quote>command not found</quote> or a |
| 36884 |
user-defined one?). However, many scripts use an <firstterm>exit |
| 36885 |
1</firstterm> as a general bailout-upon-error. Since exit code |
| 36886 |
<errorcode>1</errorcode> signifies so many possible errors, |
| 36887 |
it is not particularly useful in debugging.</para> |
| 36888 |
|
| 36889 |
<para><anchor id="sysexitsref"></para> |
| 36890 |
<para>There has been an attempt to systematize exit status numbers |
| 36891 |
(see <filename |
| 36892 |
class="headerfile">/usr/include/sysexits.h</filename>), |
| 36893 |
but this is intended for C and C++ programmers. A similar |
| 36894 |
standard for scripting might be appropriate. The author of |
| 36895 |
this document proposes restricting user-defined exit codes to |
| 36896 |
the range <returnvalue>64 - 113</returnvalue> (in addition to |
| 36897 |
<returnvalue>0</returnvalue>, for success), to conform with |
| 36898 |
the C/C++ standard. This would allot 50 valid codes, and make |
| 36899 |
troubleshooting scripts more straightforward. |
| 36900 |
<footnote><para>An update of <filename |
| 36901 |
class="headerfile">/usr/include/sysexits.h</filename> |
| 36902 |
allocates previously unused exit codes from <returnvalue>64 |
| 36903 |
- 78</returnvalue>. It may be anticipated that the range of |
| 36904 |
unallotted exit codes will be further restricted in the future. |
| 36905 |
The author of this document will <emphasis>not</emphasis> do |
| 36906 |
fixups on the scripting examples to conform to the changing |
| 36907 |
standard. This should not cause any problems, since there |
| 36908 |
is no overlap or conflict in usage of exit codes between |
| 36909 |
compiled C/C++ binaries and shell scripts.</para></footnote> |
| 36910 |
All user-defined exit codes in the accompanying examples to |
| 36911 |
this document conform to this standard, except where overriding |
| 36912 |
circumstances exist, as in <xref linkend="tmdin">.</para> |
| 36913 |
|
| 36914 |
<note><para>Issuing a <link linkend="xstatvarref">$?</link> from |
| 36915 |
the command-line after a shell script exits gives |
| 36916 |
results consistent with the table above only from the |
| 36917 |
Bash or <firstterm>sh</firstterm> prompt. Running the |
| 36918 |
<firstterm>C-shell</firstterm> or <firstterm>tcsh</firstterm> |
| 36919 |
may give different values in some cases.</para></note> |
| 36920 |
|
| 36921 |
|
| 36922 |
</appendix> |
| 36923 |
<!-- End Reserved Exit Code appendix --> |
| 36924 |
|
| 36925 |
|
| 36926 |
<appendix id="ioredirintro"> |
| 36927 |
<title>A Detailed Introduction to I/O and I/O Redirection</title> |
| 36928 |
|
| 36929 |
<para><emphasis>written by Stéphane Chazelas, and revised |
| 36930 |
by the document author</emphasis></para> |
| 36931 |
|
| 36932 |
<para><anchor id="stdinoutdef"></para> |
| 36933 |
<para>A command expects the first three <link linkend="fdref">file |
| 36934 |
descriptors</link> to be available. The first, <firstterm>fd |
| 36935 |
0</firstterm> (standard input, <filename>stdin</filename>), |
| 36936 |
is for reading. The other two (<firstterm>fd 1</firstterm>, |
| 36937 |
<filename>stdout</filename> and <firstterm>fd 2</firstterm>, |
| 36938 |
<filename>stderr</filename>) are for writing.</para> |
| 36939 |
|
| 36940 |
<para>There is a <filename>stdin</filename>, <filename>stdout</filename>, |
| 36941 |
and a <filename>stderr</filename> associated with each command. |
| 36942 |
<userinput>ls 2>&1</userinput> means temporarily connecting the |
| 36943 |
<filename>stderr</filename> of the <command>ls</command> command to the |
| 36944 |
same <quote>resource</quote> as the shell's |
| 36945 |
<filename>stdout</filename>.</para> |
| 36946 |
|
| 36947 |
<para>By convention, a command reads its input from fd 0 |
| 36948 |
(<filename>stdin</filename>), prints normal output to fd |
| 36949 |
1 (<filename>stdout</filename>), and error ouput to fd 2 |
| 36950 |
(<filename>stderr</filename>). If one of those three fd's is |
| 36951 |
not open, you may encounter problems:</para> |
| 36952 |
|
| 36953 |
<screen> |
| 36954 |
<prompt>bash$ </prompt><userinput>cat /etc/passwd >&-</userinput> |
| 36955 |
<computeroutput>cat: standard output: Bad file descriptor</computeroutput> |
| 36956 |
</screen> |
| 36957 |
|
| 36958 |
<para>For example, when <command>xterm</command> runs, it first |
| 36959 |
initializes itself. Before running the user's shell, |
| 36960 |
<command>xterm</command> opens the terminal device |
| 36961 |
(/dev/pts/<n> or something similar) three times.</para> |
| 36962 |
|
| 36963 |
<para>At this point, Bash inherits these three file descriptors, |
| 36964 |
and each command (child process) run by Bash inherits |
| 36965 |
them in turn, except when you redirect the command. <link |
| 36966 |
linkend="ioredirref">Redirection</link> means reassigning |
| 36967 |
one of the file descriptors to another file (or a pipe, or |
| 36968 |
anything permissible). File descriptors may be reassigned |
| 36969 |
locally (for a command, a command group, a <link |
| 36970 |
linkend="subshellsref">subshell</link>, a <link |
| 36971 |
linkend="redirref">while or if or case or for loop</link>...), |
| 36972 |
or globally, for the remainder of the shell (using <link |
| 36973 |
linkend="execref">exec</link>).</para> |
| 36974 |
|
| 36975 |
<para><userinput>ls > /dev/null</userinput> means |
| 36976 |
running <command>ls</command> with its fd 1 connected to |
| 36977 |
<filename>/dev/null</filename>.</para> |
| 36978 |
|
| 36979 |
<para> |
| 36980 |
<screen> |
| 36981 |
<prompt>bash$ </prompt><userinput>lsof -a -p $$ -d0,1,2</userinput> |
| 36982 |
<computeroutput>COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME |
| 36983 |
bash 363 bozo 0u CHR 136,1 3 /dev/pts/1 |
| 36984 |
bash 363 bozo 1u CHR 136,1 3 /dev/pts/1 |
| 36985 |
bash 363 bozo 2u CHR 136,1 3 /dev/pts/1</computeroutput> |
| 36986 |
|
| 36987 |
|
| 36988 |
<prompt>bash$ </prompt><userinput>exec 2> /dev/null</userinput> |
| 36989 |
<prompt>bash$ </prompt><userinput>lsof -a -p $$ -d0,1,2</userinput> |
| 36990 |
<computeroutput>COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME |
| 36991 |
bash 371 bozo 0u CHR 136,1 3 /dev/pts/1 |
| 36992 |
bash 371 bozo 1u CHR 136,1 3 /dev/pts/1 |
| 36993 |
bash 371 bozo 2w CHR 1,3 120 /dev/null</computeroutput> |
| 36994 |
|
| 36995 |
|
| 36996 |
<prompt>bash$ </prompt><userinput>bash -c 'lsof -a -p $$ -d0,1,2' | cat</userinput> |
| 36997 |
<computeroutput>COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME |
| 36998 |
lsof 379 root 0u CHR 136,1 3 /dev/pts/1 |
| 36999 |
lsof 379 root 1w FIFO 0,0 7118 pipe |
| 37000 |
lsof 379 root 2u CHR 136,1 3 /dev/pts/1</computeroutput> |
| 37001 |
|
| 37002 |
|
| 37003 |
<prompt>bash$ </prompt><userinput>echo "$(bash -c 'lsof -a -p $$ -d0,1,2' 2>&1)"</userinput> |
| 37004 |
<computeroutput>COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME |
| 37005 |
lsof 426 root 0u CHR 136,1 3 /dev/pts/1 |
| 37006 |
lsof 426 root 1w FIFO 0,0 7520 pipe |
| 37007 |
lsof 426 root 2w FIFO 0,0 7520 pipe</computeroutput> |
| 37008 |
</screen> |
| 37009 |
</para> |
| 37010 |
|
| 37011 |
<para>This works for different types of redirection.</para> |
| 37012 |
|
| 37013 |
<para><userinput>Exercise:</userinput> Analyze the following script. |
| 37014 |
|
| 37015 |
<programlisting>#! /usr/bin/env bash |
| 37016 |
|
| 37017 |
mkfifo /tmp/fifo1 /tmp/fifo2 |
| 37018 |
while read a; do echo "FIFO1: $a"; done < /tmp/fifo1 & exec 7> /tmp/fifo1 |
| 37019 |
exec 8> >(while read a; do echo "FD8: $a, to fd7"; done >&7) |
| 37020 |
|
| 37021 |
exec 3>&1 |
| 37022 |
( |
| 37023 |
( |
| 37024 |
( |
| 37025 |
while read a; do echo "FIFO2: $a"; done < /tmp/fifo2 | tee /dev/stderr \ |
| 37026 |
| tee /dev/fd/4 | tee /dev/fd/5 | tee /dev/fd/6 >&7 & exec 3> /tmp/fifo2 |
| 37027 |
|
| 37028 |
echo 1st, to stdout |
| 37029 |
sleep 1 |
| 37030 |
echo 2nd, to stderr >&2 |
| 37031 |
sleep 1 |
| 37032 |
echo 3rd, to fd 3 >&3 |
| 37033 |
sleep 1 |
| 37034 |
echo 4th, to fd 4 >&4 |
| 37035 |
sleep 1 |
| 37036 |
echo 5th, to fd 5 >&5 |
| 37037 |
sleep 1 |
| 37038 |
echo 6th, through a pipe | sed 's/.*/PIPE: &, to fd 5/' >&5 |
| 37039 |
sleep 1 |
| 37040 |
echo 7th, to fd 6 >&6 |
| 37041 |
sleep 1 |
| 37042 |
echo 8th, to fd 7 >&7 |
| 37043 |
sleep 1 |
| 37044 |
echo 9th, to fd 8 >&8 |
| 37045 |
|
| 37046 |
) 4>&1 >&3 3>&- | while read a; do echo "FD4: $a"; done 1>&3 5>&- 6>&- |
| 37047 |
) 5>&1 >&3 | while read a; do echo "FD5: $a"; done 1>&3 6>&- |
| 37048 |
) 6>&1 >&3 | while read a; do echo "FD6: $a"; done 3>&- |
| 37049 |
|
| 37050 |
rm -f /tmp/fifo1 /tmp/fifo2 |
| 37051 |
|
| 37052 |
|
| 37053 |
# For each command and subshell, figure out which fd points to what. |
| 37054 |
# Good luck! |
| 37055 |
|
| 37056 |
exit 0</programlisting> |
| 37057 |
</para> |
| 37058 |
|
| 37059 |
</appendix> |
| 37060 |
<!-- A Detailed Introduction to I/O and I/O Redirection --> |
| 37061 |
|
| 37062 |
|
| 37063 |
<appendix id="command-line-options"> |
| 37064 |
<title>Command-Line Options</title> |
| 37065 |
|
| 37066 |
<para>Many executables, whether binaries or script files, accept |
| 37067 |
options to modify their run-time behavior. For example: from |
| 37068 |
the command-line, typing <command>command -o</command> |
| 37069 |
would invoke <emphasis>command</emphasis>, with option |
| 37070 |
<option>o</option>.</para> |
| 37071 |
|
| 37072 |
|
| 37073 |
<sect1 id="standard-options"> |
| 37074 |
<title>Standard Command-Line Options</title> |
| 37075 |
|
| 37076 |
<para>Over time, there has evolved a loose standard for the |
| 37077 |
meanings of command-line option flags. The GNU utilities conform |
| 37078 |
more closely to this <quote>standard</quote> than older UNIX |
| 37079 |
utilities.</para> |
| 37080 |
|
| 37081 |
<para>Traditionally, UNIX command-line options consist of a dash, |
| 37082 |
followed by one or more lowercase letters. The GNU utilities |
| 37083 |
added a double-dash, followed by a complete word or compound |
| 37084 |
word.</para> |
| 37085 |
|
| 37086 |
|
| 37087 |
<para>The two most widely-accepted options are:</para> |
| 37088 |
|
| 37089 |
<itemizedlist id="widelyaccopt"> |
| 37090 |
|
| 37091 |
<listitem> |
| 37092 |
<para><option>-h</option></para> |
| 37093 |
<para><option>--help</option></para> |
| 37094 |
<para><firstterm>Help</firstterm>: Give usage message and exit.</para> |
| 37095 |
</listitem> |
| 37096 |
|
| 37097 |
<listitem> |
| 37098 |
<para><option>-v</option></para> |
| 37099 |
<para><option>--version</option></para> |
| 37100 |
<para><firstterm>Version</firstterm>: Show program version and exit.</para> |
| 37101 |
</listitem> |
| 37102 |
|
| 37103 |
</itemizedlist> |
| 37104 |
|
| 37105 |
|
| 37106 |
<para>Other common options are:</para> |
| 37107 |
|
| 37108 |
<itemizedlist id="otheroptns"> |
| 37109 |
|
| 37110 |
<listitem> |
| 37111 |
<para><option>-a</option></para> |
| 37112 |
<para><option>--all</option></para> |
| 37113 |
<para><firstterm>All</firstterm>: show <emphasis>all</emphasis> |
| 37114 |
information or operate on <emphasis>all</emphasis> arguments.</para> |
| 37115 |
|
| 37116 |
</listitem> |
| 37117 |
|
| 37118 |
<listitem> |
| 37119 |
<para><option>-l</option></para> |
| 37120 |
<para><option>--list</option></para> |
| 37121 |
<para><firstterm>List</firstterm>: list files or arguments without |
| 37122 |
taking other action.</para> |
| 37123 |
</listitem> |
| 37124 |
|
| 37125 |
<listitem> |
| 37126 |
<para><option>-o</option></para> |
| 37127 |
<para><firstterm>Output</firstterm> filename</para> |
| 37128 |
</listitem> |
| 37129 |
|
| 37130 |
<listitem> |
| 37131 |
<para><option>-q</option></para> |
| 37132 |
<para><option>--quiet</option></para> |
| 37133 |
<para><firstterm>Quiet</firstterm>: suppress |
| 37134 |
<filename>stdout</filename>.</para> |
| 37135 |
</listitem> |
| 37136 |
|
| 37137 |
<listitem> |
| 37138 |
<para><option>-r</option></para> |
| 37139 |
<para><option>-R</option></para> |
| 37140 |
<para><option>--recursive</option></para> |
| 37141 |
<para><firstterm>Recursive</firstterm>: Operate recursively (down |
| 37142 |
directory tree).</para> |
| 37143 |
</listitem> |
| 37144 |
|
| 37145 |
<listitem> |
| 37146 |
<para><option>-v</option></para> |
| 37147 |
<para><option>--verbose</option></para> |
| 37148 |
<para><firstterm>Verbose</firstterm>: output additional information to |
| 37149 |
<filename>stdout</filename> or <filename>stderr</filename>.</para> |
| 37150 |
</listitem> |
| 37151 |
|
| 37152 |
<listitem> |
| 37153 |
<para><option>-z</option></para> |
| 37154 |
<para><option>--compress</option></para> |
| 37155 |
<para><firstterm>Compress</firstterm>: apply compression (usually |
| 37156 |
<link linkend="gzipref">gzip</link>).</para> |
| 37157 |
</listitem> |
| 37158 |
|
| 37159 |
</itemizedlist> |
| 37160 |
|
| 37161 |
|
| 37162 |
<para>However:</para> |
| 37163 |
|
| 37164 |
<itemizedlist id="exceptionsopts"> |
| 37165 |
|
| 37166 |
<listitem> |
| 37167 |
<para>In <command>tar</command> and <command>gawk</command>:</para> |
| 37168 |
<para><option>-f</option></para> |
| 37169 |
<para><option>--file</option></para> |
| 37170 |
<para><firstterm>File</firstterm>: filename follows.</para> |
| 37171 |
</listitem> |
| 37172 |
|
| 37173 |
<listitem> |
| 37174 |
<para>In <command>cp</command>, <command>mv</command>, |
| 37175 |
<command>rm</command>:</para> |
| 37176 |
<para><option>-f</option></para> |
| 37177 |
<para><option>--force</option></para> |
| 37178 |
<para><firstterm>Force</firstterm>: force overwrite of target file(s).</para> |
| 37179 |
</listitem> |
| 37180 |
|
| 37181 |
</itemizedlist> |
| 37182 |
|
| 37183 |
|
| 37184 |
<caution><para>Many UNIX and Linux utilities deviate from this |
| 37185 |
<quote>standard,</quote> so it is dangerous to |
| 37186 |
<emphasis>assume</emphasis> that a given option will behave in a |
| 37187 |
standard way. Always check the man page for the command in question |
| 37188 |
when in doubt.</para></caution> |
| 37189 |
|
| 37190 |
<para>A complete table of recommended options for the GNU utilities |
| 37191 |
is available at <ulink |
| 37192 |
url="http://www.gnu.org/prep/standards/">the GNU standards page</ulink>.</para> |
| 37193 |
|
| 37194 |
</sect1> |
| 37195 |
|
| 37196 |
|
| 37197 |
|
| 37198 |
|
| 37199 |
<sect1 id="bash-options"> |
| 37200 |
<title>Bash Command-Line Options</title> |
| 37201 |
|
| 37202 |
<para><anchor id="clopts"></para> |
| 37203 |
<para><firstterm>Bash</firstterm> itself has a number of command-line |
| 37204 |
options. Here are some of the more useful ones.</para> |
| 37205 |
|
| 37206 |
<itemizedlist id="bash-commline-opts"> |
| 37207 |
|
| 37208 |
<listitem> |
| 37209 |
<para><option>-c</option></para> |
| 37210 |
<para><emphasis>Read commands from the following string and assign any |
| 37211 |
arguments to the <link linkend="posparamref">positional |
| 37212 |
parameters</link>.</emphasis></para> |
| 37213 |
<para> |
| 37214 |
<screen> |
| 37215 |
<prompt>bash$ </prompt><userinput>bash -c 'set a b c d; IFS="+-;"; echo "$*"'</userinput> |
| 37216 |
<computeroutput>a+b+c+d</computeroutput> |
| 37217 |
</screen> |
| 37218 |
</para> |
| 37219 |
</listitem> |
| 37220 |
|
| 37221 |
<listitem> |
| 37222 |
<para><option>-r</option></para> |
| 37223 |
<para><option>--restricted</option></para> |
| 37224 |
<para><emphasis>Runs the shell, or a script, in <link |
| 37225 |
linkend="restrictedshref">restricted mode</link>.</emphasis></para> |
| 37226 |
</listitem> |
| 37227 |
|
| 37228 |
<listitem> |
| 37229 |
<para><option>--posix</option></para> |
| 37230 |
<para><emphasis>Forces Bash to conform to <link |
| 37231 |
linkend="posix2ref">POSIX</link> mode.</emphasis></para> |
| 37232 |
</listitem> |
| 37233 |
|
| 37234 |
<listitem> |
| 37235 |
<para><option>--version</option></para> |
| 37236 |
<para><emphasis>Display Bash version information and |
| 37237 |
exit.</emphasis></para> |
| 37238 |
</listitem> |
| 37239 |
|
| 37240 |
<listitem> |
| 37241 |
<para><option>--</option></para> |
| 37242 |
<para><emphasis>End of options. Anything further on the command |
| 37243 |
line is an argument, not an option.</emphasis></para> |
| 37244 |
</listitem> |
| 37245 |
|
| 37246 |
</itemizedlist> |
| 37247 |
|
| 37248 |
</sect1> |
| 37249 |
|
| 37250 |
|
| 37251 |
</appendix> |
| 37252 |
<!-- End Command-Line Options appendix --> |
| 37253 |
|
| 37254 |
|
| 37255 |
<appendix id="files"> |
| 37256 |
<title> Important Files</title> |
| 37257 |
|
| 37258 |
<variablelist id="filesref"> |
| 37259 |
<title><anchor id="filesref1">startup files</title> |
| 37260 |
|
| 37261 |
<varlistentry> |
| 37262 |
<term></term> |
| 37263 |
<listitem> |
| 37264 |
<para>These files contain the aliases and <link |
| 37265 |
linkend="envref">environmental variables</link> |
| 37266 |
made available to Bash running as a user shell and to all |
| 37267 |
Bash scripts invoked after system initialization.</para> |
| 37268 |
</listitem> |
| 37269 |
</varlistentry> |
| 37270 |
|
| 37271 |
|
| 37272 |
<varlistentry> |
| 37273 |
<term><filename>/etc/profile</filename></term> |
| 37274 |
<listitem> |
| 37275 |
<para>Systemwide defaults, mostly setting the environment |
| 37276 |
(all Bourne-type shells, not just Bash |
| 37277 |
<footnote><para>This does not apply to <command>csh</command>, |
| 37278 |
<command>tcsh</command>, and other shells not related to or |
| 37279 |
descended from the classic Bourne shell |
| 37280 |
(<command>sh</command>).</para></footnote>)</para> |
| 37281 |
</listitem> |
| 37282 |
</varlistentry> |
| 37283 |
|
| 37284 |
<varlistentry> |
| 37285 |
<term><filename>/etc/bashrc</filename></term> |
| 37286 |
<listitem> |
| 37287 |
<para>systemwide functions and <link |
| 37288 |
linkend="aliasref">aliases</link> for Bash</para> |
| 37289 |
</listitem> |
| 37290 |
</varlistentry> |
| 37291 |
|
| 37292 |
<varlistentry> |
| 37293 |
<term><filename><varname>$HOME</varname>/.bash_profile</filename></term> |
| 37294 |
<listitem> |
| 37295 |
<para>user-specific Bash environmental default settings, |
| 37296 |
found in each user's home directory (the local counterpart |
| 37297 |
to <filename>/etc/profile</filename>)</para> |
| 37298 |
</listitem> |
| 37299 |
</varlistentry> |
| 37300 |
|
| 37301 |
<varlistentry> |
| 37302 |
<term><filename><varname>$HOME</varname>/.bashrc</filename></term> |
| 37303 |
<listitem> |
| 37304 |
<para>user-specific Bash init file, found in each user's home |
| 37305 |
directory (the local counterpart to |
| 37306 |
<filename>/etc/bashrc</filename>). Only interactive |
| 37307 |
shells and user scripts read this file. See |
| 37308 |
<xref linkend="sample-bashrc"> for a sample |
| 37309 |
<filename>.bashrc</filename> file.</para> |
| 37310 |
</listitem> |
| 37311 |
</varlistentry> |
| 37312 |
|
| 37313 |
</variablelist> |
| 37314 |
|
| 37315 |
|
| 37316 |
<variablelist id="logoutfileref"> |
| 37317 |
<title><anchor id="logoutfileref1">logout file</title> |
| 37318 |
<varlistentry> |
| 37319 |
<term><filename><varname>$HOME</varname>/.bash_logout</filename></term> |
| 37320 |
|
| 37321 |
<listitem> |
| 37322 |
<para>user-specific instruction file, found in |
| 37323 |
each user's home directory. Upon exit from a login (Bash) |
| 37324 |
shell, the commands in this file execute.</para> |
| 37325 |
</listitem> |
| 37326 |
|
| 37327 |
</varlistentry> |
| 37328 |
</variablelist> |
| 37329 |
|
| 37330 |
|
| 37331 |
<variablelist id="datafilesref"> |
| 37332 |
<title><anchor id="datafilesref1">data files</title> |
| 37333 |
|
| 37334 |
<varlistentry> |
| 37335 |
<term><filename>/etc/passwd</filename></term> |
| 37336 |
<listitem> |
| 37337 |
<para>A listing of all the user accounts on the system, |
| 37338 |
their identities, their home directories, the groups they |
| 37339 |
belong to, and their default shell. Note that the user |
| 37340 |
passwords are <emphasis>not</emphasis> |
| 37341 |
stored in this file, |
| 37342 |
<footnote><para>In older versions of UNIX, passwords |
| 37343 |
<emphasis>were</emphasis> stored in |
| 37344 |
<filename>/etc/passwd</filename>, and that explains |
| 37345 |
the name of the file.</para></footnote> |
| 37346 |
but in <filename>/etc/shadow</filename> in encrypted form.</para> |
| 37347 |
</listitem> |
| 37348 |
</varlistentry> |
| 37349 |
</variablelist> |
| 37350 |
|
| 37351 |
|
| 37352 |
<variablelist id="sysconfref"> |
| 37353 |
<title><anchor id="sysconfref1">system configuration files</title> |
| 37354 |
|
| 37355 |
<varlistentry> |
| 37356 |
<term><filename>/etc/sysconfig/hwconf</filename></term> |
| 37357 |
<listitem> |
| 37358 |
<para>Listing and description of attached hardware devices. |
| 37359 |
This information is in text form and can be extracted and |
| 37360 |
parsed.</para> |
| 37361 |
<para> |
| 37362 |
<screen> |
| 37363 |
<prompt>bash$ </prompt><userinput>grep -A 5 AUDIO /etc/sysconfig/hwconf</userinput> |
| 37364 |
<computeroutput>class: AUDIO |
| 37365 |
bus: PCI |
| 37366 |
detached: 0 |
| 37367 |
driver: snd-intel8x0 |
| 37368 |
desc: "Intel Corporation 82801CA/CAM AC'97 Audio Controller" |
| 37369 |
vendorId: 8086</computeroutput> |
| 37370 |
</screen> |
| 37371 |
</para> |
| 37372 |
<note><para>This file is present on Red Hat and Fedora Core |
| 37373 |
installations, but may be missing from other |
| 37374 |
distros.</para></note> |
| 37375 |
</listitem> |
| 37376 |
</varlistentry> |
| 37377 |
|
| 37378 |
</variablelist> |
| 37379 |
|
| 37380 |
</appendix> |
| 37381 |
<!-- End Files appendix --> |
| 37382 |
|
| 37383 |
|
| 37384 |
|
| 37385 |
|
| 37386 |
|
| 37387 |
<appendix id="systemdirs"> |
| 37388 |
|
| 37389 |
<title>Important System Directories</title> |
| 37390 |
|
| 37391 |
|
| 37392 |
<para>Sysadmins and anyone else writing administrative scripts |
| 37393 |
should be intimately familiar with the following system |
| 37394 |
directories.</para> |
| 37395 |
|
| 37396 |
<itemizedlist> |
| 37397 |
|
| 37398 |
<listitem> |
| 37399 |
<para><filename class="directory">/bin</filename></para> |
| 37400 |
<para>Binaries (executables). Basic system programs |
| 37401 |
and utilities (such as <command>bash</command>).</para> |
| 37402 |
</listitem> |
| 37403 |
|
| 37404 |
<listitem> |
| 37405 |
<para><filename class="directory">/usr/bin</filename> |
| 37406 |
|
| 37407 |
<footnote> |
| 37408 |
<para>Some early UNIX systems had a fast, small-capacity fixed |
| 37409 |
disk (containing <filename class="directory">/</filename>, |
| 37410 |
the root partition), and a second drive which |
| 37411 |
was larger, but slower (containing <filename |
| 37412 |
class="directory">/usr</filename> and other |
| 37413 |
partitions). The most frequently used programs and |
| 37414 |
utilities therefore resided on the small-but-fast |
| 37415 |
drive, in <filename class="directory">/bin</filename>, |
| 37416 |
and the others on the slower drive, in <filename |
| 37417 |
class="directory">/usr/bin</filename>.</para> |
| 37418 |
|
| 37419 |
<para>This likewise accounts for the split between |
| 37420 |
<filename class="directory">/sbin</filename> and |
| 37421 |
<filename class="directory">/usr/sbin</filename>, |
| 37422 |
<filename class="directory">/lib</filename> and <filename |
| 37423 |
class="directory">/usr/lib</filename>, etc.</para> |
| 37424 |
</footnote> |
| 37425 |
|
| 37426 |
</para> |
| 37427 |
|
| 37428 |
<para>More system binaries.</para> |
| 37429 |
|
| 37430 |
</listitem> |
| 37431 |
|
| 37432 |
<listitem> |
| 37433 |
<para><filename class="directory">/usr/local/bin</filename></para> |
| 37434 |
<para>Miscellaneous binaries local to the particular machine.</para> |
| 37435 |
</listitem> |
| 37436 |
|
| 37437 |
<listitem> |
| 37438 |
<para><filename class="directory">/sbin</filename></para> |
| 37439 |
<para>System binaries. Basic system administrative programs |
| 37440 |
and utilities (such as <command>fsck</command>).</para> |
| 37441 |
</listitem> |
| 37442 |
|
| 37443 |
<listitem> |
| 37444 |
<para><filename class="directory">/usr/sbin</filename></para> |
| 37445 |
<para>More system administrative programs and utilities.</para> |
| 37446 |
</listitem> |
| 37447 |
|
| 37448 |
<listitem> |
| 37449 |
<para><filename class="directory">/etc</filename></para> |
| 37450 |
|
| 37451 |
<para><firstterm>Et cetera</firstterm>. Systemwide configuration |
| 37452 |
scripts.</para> |
| 37453 |
|
| 37454 |
<para>Of particular interest are the |
| 37455 |
<link |
| 37456 |
linkend="fstabref"><filename>/etc/fstab</filename></link> |
| 37457 |
(filesystem table), |
| 37458 |
<filename>/etc/mtab</filename> |
| 37459 |
(mounted filesystem table), and the <link |
| 37460 |
linkend="inittabref"><filename>/etc/inittab</filename></link> |
| 37461 |
files.</para> |
| 37462 |
|
| 37463 |
</listitem> |
| 37464 |
|
| 37465 |
<listitem> |
| 37466 |
<para><filename class="directory">/etc/rc.d</filename></para> |
| 37467 |
<para>Boot scripts, on Red Hat and derivative distributions |
| 37468 |
of Linux.</para> |
| 37469 |
</listitem> |
| 37470 |
|
| 37471 |
<listitem> |
| 37472 |
<para><filename class="directory">/usr/share/doc</filename></para> |
| 37473 |
<para>Documentation for installed packages.</para> |
| 37474 |
</listitem> |
| 37475 |
|
| 37476 |
<listitem> |
| 37477 |
<para><filename class="directory">/usr/man</filename></para> |
| 37478 |
<para>The systemwide <link linkend="manref">manpages</link>.</para> |
| 37479 |
</listitem> |
| 37480 |
|
| 37481 |
<listitem> |
| 37482 |
<para><filename class="directory">/dev</filename></para> |
| 37483 |
<para>Device directory. Entries (but <emphasis>not</emphasis> |
| 37484 |
mount points) for physical and virtual devices. |
| 37485 |
See <xref linkend="devproc">.</para> |
| 37486 |
</listitem> |
| 37487 |
|
| 37488 |
<listitem> |
| 37489 |
<para><filename class="directory">/proc</filename></para> |
| 37490 |
<para>Process directory. Contains information and statistics |
| 37491 |
about running processes and kernel parameters. |
| 37492 |
See <xref linkend="devproc">.</para> |
| 37493 |
</listitem> |
| 37494 |
|
| 37495 |
<listitem> |
| 37496 |
<para><filename class="directory">/sys</filename></para> |
| 37497 |
<para>Systemwide device directory. Contains information and |
| 37498 |
statistics about device and device names. This is newly |
| 37499 |
added to Linux with the 2.6.X kernels.</para> |
| 37500 |
</listitem> |
| 37501 |
|
| 37502 |
<listitem> |
| 37503 |
<para><filename class="directory">/mnt</filename></para> |
| 37504 |
<para><firstterm>Mount</firstterm>. Directory for mounting |
| 37505 |
hard drive partitions, such as <filename |
| 37506 |
class="directory">/mnt/dos</filename>, and physical |
| 37507 |
devices. In newer Linux distros, the <filename |
| 37508 |
class="directory">/media</filename> directory has taken |
| 37509 |
over as the preferred mount point for I/O devices.</para> |
| 37510 |
</listitem> |
| 37511 |
|
| 37512 |
<listitem> |
| 37513 |
<para><filename class="directory">/media</filename></para> |
| 37514 |
<para>In newer Linux distros, the preferred mount point for |
| 37515 |
I/O devices, such as CD/DVD drives or USB flash drives.</para> |
| 37516 |
</listitem> |
| 37517 |
|
| 37518 |
<listitem> |
| 37519 |
<para><filename class="directory">/var</filename></para> |
| 37520 |
<para><firstterm>Variable</firstterm> (changeable) system |
| 37521 |
files. This is a catchall <quote>scratchpad</quote> |
| 37522 |
directory for data generated while a Linux/UNIX machine |
| 37523 |
is running.</para> |
| 37524 |
</listitem> |
| 37525 |
|
| 37526 |
<listitem> |
| 37527 |
<para><filename class="directory">/var/log</filename></para> |
| 37528 |
<para>Systemwide log files.</para> |
| 37529 |
</listitem> |
| 37530 |
|
| 37531 |
<listitem> |
| 37532 |
<para><filename class="directory">/var/spool/mail</filename></para> |
| 37533 |
<para>User mail spool.</para> |
| 37534 |
</listitem> |
| 37535 |
|
| 37536 |
<listitem> |
| 37537 |
<para><filename class="directory">/lib</filename></para> |
| 37538 |
<para>Systemwide library files.</para> |
| 37539 |
</listitem> |
| 37540 |
|
| 37541 |
<listitem> |
| 37542 |
<para><filename class="directory">/usr/lib</filename></para> |
| 37543 |
<para>More systemwide library files.</para> |
| 37544 |
</listitem> |
| 37545 |
|
| 37546 |
<listitem> |
| 37547 |
<para><filename class="directory">/tmp</filename></para> |
| 37548 |
<para>System temporary files.</para> |
| 37549 |
</listitem> |
| 37550 |
|
| 37551 |
<listitem> |
| 37552 |
<para><filename class="directory">/boot</filename></para> |
| 37553 |
<para>System <firstterm>boot</firstterm> directory. The kernel, |
| 37554 |
module links, system map, and boot manager reside here.</para> |
| 37555 |
<warning><para>Altering files in this directory may result in an |
| 37556 |
unbootable system.</para></warning> |
| 37557 |
</listitem> |
| 37558 |
|
| 37559 |
</itemizedlist> |
| 37560 |
|
| 37561 |
</appendix> |
| 37562 |
|
| 37563 |
|
| 37564 |
|
| 37565 |
<appendix id="tabexpansion"> |
| 37566 |
|
| 37567 |
&TABEXP; |
| 37568 |
|
| 37569 |
</appendix> |
| 37570 |
<!-- End Tab Expansion appendix --> |
| 37571 |
|
| 37572 |
|
| 37573 |
|
| 37574 |
<appendix id="localization"> |
| 37575 |
<title>Localization</title> |
| 37576 |
|
| 37577 |
|
| 37578 |
<para>Localization is an undocumented Bash feature.</para> |
| 37579 |
|
| 37580 |
<para><anchor id="localeref">A localized shell script echoes |
| 37581 |
its text output in the language defined as the system's locale. |
| 37582 |
A Linux user in Berlin, Germany, would get script output in German, |
| 37583 |
whereas his cousin in Berlin, Maryland, would get output from |
| 37584 |
the same script in English.</para> |
| 37585 |
|
| 37586 |
<para>To create a localized script, use the following template to |
| 37587 |
write all messages to the user (error messages, prompts, |
| 37588 |
etc.).</para> |
| 37589 |
|
| 37590 |
<para> |
| 37591 |
<programlisting>#!/bin/bash |
| 37592 |
# localized.sh |
| 37593 |
# Script by Stéphane Chazelas, |
| 37594 |
#+ modified by Bruno Haible, bugfixed by Alfredo Pironti. |
| 37595 |
|
| 37596 |
. gettext.sh |
| 37597 |
|
| 37598 |
E_CDERROR=65 |
| 37599 |
|
| 37600 |
error() |
| 37601 |
{ |
| 37602 |
printf "$@" >&2 |
| 37603 |
exit $E_CDERROR |
| 37604 |
} |
| 37605 |
|
| 37606 |
cd $var || error "`eval_gettext \"Can\'t cd to \\\$var.\"`" |
| 37607 |
# The triple backslashes (escapes) in front of $var needed |
| 37608 |
#+ "because eval_gettext expects a string |
| 37609 |
#+ where the variable values have not yet been substituted." |
| 37610 |
# -- per Bruno Haible |
| 37611 |
read -p "`gettext \"Enter the value: \"`" var |
| 37612 |
# ... |
| 37613 |
|
| 37614 |
|
| 37615 |
# ------------------------------------------------------------------ |
| 37616 |
# Alfredo Pironti comments: |
| 37617 |
|
| 37618 |
# This script has been modified to not use the $"..." syntax in |
| 37619 |
#+ favor of the "`gettext \"...\"`" syntax. |
| 37620 |
# This is ok, but with the new localized.sh program, the commands |
| 37621 |
#+ "bash -D filename" and "bash --dump-po-string filename" |
| 37622 |
#+ will produce no output |
| 37623 |
#+ (because those command are only searching for the $"..." strings)! |
| 37624 |
# The ONLY way to extract strings from the new file is to use the |
| 37625 |
# 'xgettext' program. However, the xgettext program is buggy. |
| 37626 |
|
| 37627 |
# Note that 'xgettext' has another bug. |
| 37628 |
# |
| 37629 |
# The shell fragment: |
| 37630 |
# gettext -s "I like Bash" |
| 37631 |
# will be correctly extracted, but . . . |
| 37632 |
# xgettext -s "I like Bash" |
| 37633 |
# . . . fails! |
| 37634 |
# 'xgettext' will extract "-s" because |
| 37635 |
#+ the command only extracts the |
| 37636 |
#+ very first argument after the 'gettext' word. |
| 37637 |
|
| 37638 |
|
| 37639 |
# Escape characters: |
| 37640 |
# |
| 37641 |
# To localize a sentence like |
| 37642 |
# echo -e "Hello\tworld!" |
| 37643 |
#+ you must use |
| 37644 |
# echo -e "`gettext \"Hello\\tworld\"`" |
| 37645 |
# The "double escape character" before the `t' is needed because |
| 37646 |
#+ 'gettext' will search for a string like: 'Hello\tworld' |
| 37647 |
# This is because gettext will read one literal `\') |
| 37648 |
#+ and will output a string like "Bonjour\tmonde", |
| 37649 |
#+ so the 'echo' command will display the message correctly. |
| 37650 |
# |
| 37651 |
# You may not use |
| 37652 |
# echo "`gettext -e \"Hello\tworld\"`" |
| 37653 |
#+ due to the xgettext bug explained above. |
| 37654 |
|
| 37655 |
|
| 37656 |
|
| 37657 |
# Let's localize the following shell fragment: |
| 37658 |
# echo "-h display help and exit" |
| 37659 |
# |
| 37660 |
# First, one could do this: |
| 37661 |
# echo "`gettext \"-h display help and exit\"`" |
| 37662 |
# This way 'xgettext' will work ok, |
| 37663 |
#+ but the 'gettext' program will read "-h" as an option! |
| 37664 |
# |
| 37665 |
# One solution could be |
| 37666 |
# echo "`gettext -- \"-h display help and exit\"`" |
| 37667 |
# This way 'gettext' will work, |
| 37668 |
#+ but 'xgettext' will extract "--", as referred to above. |
| 37669 |
# |
| 37670 |
# The workaround you may use to get this string localized is |
| 37671 |
# echo -e "`gettext \"\\0-h display help and exit\"`" |
| 37672 |
# We have added a \0 (NULL) at the beginning of the sentence. |
| 37673 |
# This way 'gettext' works correctly, as does 'xgettext.' |
| 37674 |
# Moreover, the NULL character won't change the behavior |
| 37675 |
#+ of the 'echo' command. |
| 37676 |
# ------------------------------------------------------------------</programlisting> |
| 37677 |
</para> |
| 37678 |
|
| 37679 |
|
| 37680 |
<para> |
| 37681 |
<screen><prompt>bash$ </prompt><userinput>bash -D localized.sh</userinput> |
| 37682 |
<computeroutput>"Can't cd to %s." |
| 37683 |
"Enter the value: "</computeroutput></screen> |
| 37684 |
|
| 37685 |
This lists all the localized text. (The <option>-D</option> |
| 37686 |
option lists double-quoted strings prefixed by a <token>$</token>, |
| 37687 |
without executing the script.)</para> |
| 37688 |
|
| 37689 |
<para> |
| 37690 |
<screen><prompt>bash$ </prompt><userinput>bash --dump-po-strings localized.sh</userinput> |
| 37691 |
<computeroutput>#: a:6 |
| 37692 |
msgid "Can't cd to %s." |
| 37693 |
msgstr "" |
| 37694 |
#: a:7 |
| 37695 |
msgid "Enter the value: " |
| 37696 |
msgstr ""</computeroutput></screen> |
| 37697 |
|
| 37698 |
The <option>--dump-po-strings</option> option to Bash |
| 37699 |
resembles the <option>-D</option> option, but uses <link |
| 37700 |
linkend="gettextref">gettext</link> <quote>po</quote> format. |
| 37701 |
|
| 37702 |
</para> |
| 37703 |
|
| 37704 |
<note> |
| 37705 |
<para>Bruno Haible points out:</para> |
| 37706 |
|
| 37707 |
<para>Starting with gettext-0.12.2, <command>xgettext -o - localized.sh</command> |
| 37708 |
is recommended instead of <command>bash --dump-po-strings |
| 37709 |
localized.sh</command>, because <command>xgettext</command> . . .</para> |
| 37710 |
|
| 37711 |
<para>1. understands the gettext and eval_gettext commands |
| 37712 |
(whereas bash --dump-po-strings understands only its deprecated |
| 37713 |
$"..." syntax)</para> |
| 37714 |
|
| 37715 |
<para>2. can extract comments placed by the programmer, intended |
| 37716 |
to be read by the translator.</para> |
| 37717 |
|
| 37718 |
<para>This shell code is then not specific to Bash any |
| 37719 |
more; it works the same way with Bash 1.x and other /bin/sh |
| 37720 |
implementations.</para> |
| 37721 |
</note> |
| 37722 |
|
| 37723 |
|
| 37724 |
<para>Now, build a <filename>language.po</filename> |
| 37725 |
file for each language that the script will be translated |
| 37726 |
into, specifying the <replaceable>msgstr</replaceable>. Alfredo |
| 37727 |
Pironti gives the following example:</para> |
| 37728 |
|
| 37729 |
<para>fr.po: |
| 37730 |
|
| 37731 |
<programlisting>#: a:6 |
| 37732 |
msgid "Can't cd to $var." |
| 37733 |
msgstr "Impossible de se positionner dans le repertoire $var." |
| 37734 |
#: a:7 |
| 37735 |
msgid "Enter the value: " |
| 37736 |
msgstr "Entrez la valeur : " |
| 37737 |
|
| 37738 |
# The string are dumped with the variable names, not with the %s syntax, |
| 37739 |
#+ similar to C programs. |
| 37740 |
#+ This is a very cool feature if the programmer uses |
| 37741 |
#+ variable names that make sense!</programlisting> |
| 37742 |
</para> |
| 37743 |
|
| 37744 |
<para>Then, run <link linkend="msgfmtref">msgfmt</link>.</para> |
| 37745 |
<para><userinput>msgfmt -o localized.sh.mo fr.po</userinput></para> |
| 37746 |
|
| 37747 |
<para>Place the resulting <filename>localized.sh.mo</filename> file in the |
| 37748 |
<filename class="directory">/usr/local/share/locale/fr/LC_MESSAGES</filename> |
| 37749 |
directory, and at the beginning of the script, insert the lines: |
| 37750 |
|
| 37751 |
<programlisting>TEXTDOMAINDIR=/usr/local/share/locale |
| 37752 |
TEXTDOMAIN=localized.sh</programlisting> |
| 37753 |
</para> |
| 37754 |
|
| 37755 |
<para>If a user on a French system runs the script, she will get |
| 37756 |
French messages.</para> |
| 37757 |
|
| 37758 |
<note> |
| 37759 |
<para>With older versions of Bash or other shells, localization requires |
| 37760 |
<link linkend="gettextref">gettext</link>, using the |
| 37761 |
<option>-s</option> option. In this case, the script becomes:</para> |
| 37762 |
|
| 37763 |
<para><anchor id="gettextexample"> |
| 37764 |
<programlisting>#!/bin/bash |
| 37765 |
# localized.sh |
| 37766 |
|
| 37767 |
E_CDERROR=65 |
| 37768 |
|
| 37769 |
error() { |
| 37770 |
local format=$1 |
| 37771 |
shift |
| 37772 |
printf "$(gettext -s "$format")" "$@" >&2 |
| 37773 |
exit $E_CDERROR |
| 37774 |
} |
| 37775 |
cd $var || error "Can't cd to %s." "$var" |
| 37776 |
read -p "$(gettext -s "Enter the value: ")" var |
| 37777 |
# ...</programlisting> |
| 37778 |
</para> |
| 37779 |
</note> |
| 37780 |
|
| 37781 |
<para>The <varname>TEXTDOMAIN</varname> and |
| 37782 |
<varname>TEXTDOMAINDIR</varname> variables need to be set and |
| 37783 |
exported to the environment. This should be done within the |
| 37784 |
script itself.</para> |
| 37785 |
|
| 37786 |
<para>---</para> |
| 37787 |
|
| 37788 |
<para>This appendix written by Stéphane Chazelas, |
| 37789 |
with modifications suggested by Alfredo Pironti, |
| 37790 |
and by Bruno Haible, maintainer of GNU <link |
| 37791 |
linkend="gettextref">gettext</link>.</para> |
| 37792 |
|
| 37793 |
</appendix> |
| 37794 |
<!-- Localization --> |
| 37795 |
|
| 37796 |
|
| 37797 |
|
| 37798 |
<appendix id="histcommands"> |
| 37799 |
<title>History Commands</title> |
| 37800 |
|
| 37801 |
<para>The Bash shell provides command-line tools for editing and |
| 37802 |
manipulating a user's <firstterm>command history</firstterm>. This |
| 37803 |
is primarily a convenience, a means of saving keystrokes.</para> |
| 37804 |
|
| 37805 |
<para>Bash history commands: |
| 37806 |
<orderedlist> |
| 37807 |
<listitem><para><command>history</command></para></listitem> |
| 37808 |
<listitem><para><command>fc</command></para></listitem> |
| 37809 |
</orderedlist> |
| 37810 |
</para> |
| 37811 |
|
| 37812 |
<para> |
| 37813 |
<screen><prompt>bash$ </prompt><userinput>history</userinput> |
| 37814 |
<computeroutput> 1 mount /mnt/cdrom |
| 37815 |
2 cd /mnt/cdrom |
| 37816 |
3 ls |
| 37817 |
...</computeroutput> |
| 37818 |
</screen> |
| 37819 |
</para> |
| 37820 |
|
| 37821 |
<para>Internal variables associated with Bash history commands: |
| 37822 |
<orderedlist> |
| 37823 |
<listitem><para>$HISTCMD</para></listitem> |
| 37824 |
<listitem><para>$HISTCONTROL</para></listitem> |
| 37825 |
<listitem><para>$HISTIGNORE</para></listitem> |
| 37826 |
<listitem><para>$HISTFILE</para></listitem> |
| 37827 |
<listitem><para>$HISTFILESIZE</para></listitem> |
| 37828 |
<listitem><para>$HISTSIZE</para></listitem> |
| 37829 |
<listitem><para>$HISTTIMEFORMAT (Bash, ver. 3.0 or later)</para></listitem> |
| 37830 |
<listitem><para>!!</para></listitem> |
| 37831 |
<listitem><para>!$</para></listitem> |
| 37832 |
<listitem><para>!#</para></listitem> |
| 37833 |
<listitem><para>!N</para></listitem> |
| 37834 |
<listitem><para>!-N</para></listitem> |
| 37835 |
<listitem><para>!STRING</para></listitem> |
| 37836 |
<listitem><para>!?STRING?</para></listitem> |
| 37837 |
<listitem><para>^STRING^string^</para></listitem> |
| 37838 |
</orderedlist> |
| 37839 |
</para> |
| 37840 |
|
| 37841 |
<para>Unfortunately, the Bash history tools find no use in |
| 37842 |
scripting.</para> |
| 37843 |
|
| 37844 |
<para><programlisting>#!/bin/bash |
| 37845 |
# history.sh |
| 37846 |
# A (vain) attempt to use the 'history' command in a script. |
| 37847 |
|
| 37848 |
history # No output. |
| 37849 |
|
| 37850 |
var=$(history); echo "$var" # $var is empty. |
| 37851 |
|
| 37852 |
# History commands are, by default, disabled within a script. |
| 37853 |
# However, as dhw points out, |
| 37854 |
#+ set -o history |
| 37855 |
#+ enables the history mechanism. |
| 37856 |
|
| 37857 |
set -o history |
| 37858 |
var=$(history); echo "$var" # 1 var=$(history)</programlisting> |
| 37859 |
</para> |
| 37860 |
|
| 37861 |
<para> |
| 37862 |
<screen><prompt>bash$ </prompt><userinput>./history.sh</userinput> |
| 37863 |
<computeroutput>(no output)</computeroutput> |
| 37864 |
</screen> |
| 37865 |
</para> |
| 37866 |
|
| 37867 |
<para>The <ulink |
| 37868 |
url="http://samrowe.com/wordpress/advancing-in-the-bash-shell/">Advancing |
| 37869 |
in the Bash Shell</ulink> site gives a good introduction to |
| 37870 |
the use of history commands in Bash.</para> |
| 37871 |
|
| 37872 |
|
| 37873 |
|
| 37874 |
</appendix> |
| 37875 |
<!-- History Commands --> |
| 37876 |
|
| 37877 |
|
| 37878 |
<appendix id="sample-bashrc"> |
| 37879 |
<title>Sample <filename>.bashrc</filename> and |
| 37880 |
<filename>.bash_profile</filename> Files</title> |
| 37881 |
|
| 37882 |
<para>The <filename>~/.bashrc</filename> file determines the |
| 37883 |
behavior of interactive shells. A good look at this file can |
| 37884 |
lead to a better understanding of Bash.</para> |
| 37885 |
|
| 37886 |
<para><ulink url="mailto:[email protected]">Emmanuel |
| 37887 |
Rouat</ulink> contributed the following very elaborate |
| 37888 |
<filename>.bashrc</filename> file, written for a Linux system. |
| 37889 |
He welcomes reader feedback on it.</para> |
| 37890 |
|
| 37891 |
<para>Study the file carefully, and feel free to reuse code |
| 37892 |
snippets and functions from it in your own |
| 37893 |
<filename>.bashrc</filename> file or even in your scripts.</para> |
| 37894 |
|
| 37895 |
|
| 37896 |
<example id="bashrc"> |
| 37897 |
<title>Sample <filename>.bashrc</filename> file</title> |
| 37898 |
<programlisting>&bashrc;</programlisting> |
| 37899 |
</example> |
| 37900 |
|
| 37901 |
<para>And, here is a snippet from Andrzej Szelachowski's instructive |
| 37902 |
<filename>.bash_profile</filename> file.</para> |
| 37903 |
|
| 37904 |
<example id="bashprof"> |
| 37905 |
<title><filename>.bash_profile</filename> file</title> |
| 37906 |
<programlisting>&bashprof;</programlisting> |
| 37907 |
</example> |
| 37908 |
|
| 37909 |
</appendix> |
| 37910 |
<!-- End Sample .bashrc File appendix --> |
| 37911 |
|
| 37912 |
|
| 37913 |
<appendix id="dosbatch"> |
| 37914 |
<title>Converting DOS Batch Files to Shell Scripts</title> |
| 37915 |
|
| 37916 |
<para><anchor id="dosbatch1"></para> |
| 37917 |
<para>Quite a number of programmers learned scripting on a PC running |
| 37918 |
DOS. Even the crippled DOS batch file language allowed writing some |
| 37919 |
fairly powerful scripts and applications, though they often required |
| 37920 |
extensive kludges and workarounds. Occasionally, the need still |
| 37921 |
arises to convert an old DOS batch file to a UNIX shell script. This |
| 37922 |
is generally not difficult, as DOS batch file operators are only a |
| 37923 |
limited subset of the equivalent shell scripting ones.</para> |
| 37924 |
|
| 37925 |
<table> |
| 37926 |
<title>Batch file keywords / variables / operators, and their shell equivalents</title> |
| 37927 |
<tgroup cols="3"> |
| 37928 |
<thead> |
| 37929 |
<row> |
| 37930 |
<entry>Batch File Operator</entry> |
| 37931 |
<entry>Shell Script Equivalent</entry> |
| 37932 |
<entry>Meaning</entry> |
| 37933 |
</row> |
| 37934 |
</thead> |
| 37935 |
<tbody> |
| 37936 |
<row> |
| 37937 |
<entry><option>%</option></entry> |
| 37938 |
<entry>$</entry> |
| 37939 |
<entry>command-line parameter prefix</entry> |
| 37940 |
</row> |
| 37941 |
<row> |
| 37942 |
<entry><option>/</option></entry> |
| 37943 |
<entry>-</entry> |
| 37944 |
<entry>command option flag</entry> |
| 37945 |
</row> |
| 37946 |
<row> |
| 37947 |
<entry><option>\</option></entry> |
| 37948 |
<entry>/</entry> |
| 37949 |
<entry>directory path separator</entry> |
| 37950 |
</row> |
| 37951 |
<row> |
| 37952 |
<entry><option>==</option></entry> |
| 37953 |
<entry>=</entry> |
| 37954 |
<entry>(equal-to) string comparison test</entry> |
| 37955 |
</row> |
| 37956 |
<row> |
| 37957 |
<entry><option>!==!</option></entry> |
| 37958 |
<entry>!=</entry> |
| 37959 |
<entry>(not equal-to) string comparison test</entry> |
| 37960 |
</row> |
| 37961 |
<row> |
| 37962 |
<entry><option>|</option></entry> |
| 37963 |
<entry>|</entry> |
| 37964 |
<entry>pipe</entry> |
| 37965 |
</row> |
| 37966 |
<row> |
| 37967 |
<entry><option>@</option></entry> |
| 37968 |
<entry>set <option>+v</option></entry> |
| 37969 |
<entry>do not echo current command</entry> |
| 37970 |
</row> |
| 37971 |
<row> |
| 37972 |
<entry><option>*</option></entry> |
| 37973 |
<entry>*</entry> |
| 37974 |
<entry>filename <quote>wild card</quote></entry> |
| 37975 |
</row> |
| 37976 |
<row> |
| 37977 |
<entry><option>></option></entry> |
| 37978 |
<entry>></entry> |
| 37979 |
<entry>file redirection (overwrite)</entry> |
| 37980 |
</row> |
| 37981 |
<row> |
| 37982 |
<entry><option>>></option></entry> |
| 37983 |
<entry>>></entry> |
| 37984 |
<entry>file redirection (append)</entry> |
| 37985 |
</row> |
| 37986 |
<row> |
| 37987 |
<entry><option><</option></entry> |
| 37988 |
<entry><</entry> |
| 37989 |
<entry>redirect <filename>stdin</filename></entry> |
| 37990 |
</row> |
| 37991 |
<row> |
| 37992 |
<entry><option>%VAR%</option></entry> |
| 37993 |
<entry>$VAR</entry> |
| 37994 |
<entry>environmental variable</entry> |
| 37995 |
</row> |
| 37996 |
<row> |
| 37997 |
<entry><option>REM</option></entry> |
| 37998 |
<entry>#</entry> |
| 37999 |
<entry>comment</entry> |
| 38000 |
</row> |
| 38001 |
<row> |
| 38002 |
<entry><option>NOT</option></entry> |
| 38003 |
<entry>!</entry> |
| 38004 |
<entry>negate following test</entry> |
| 38005 |
</row> |
| 38006 |
<row> |
| 38007 |
<entry><option>NUL</option></entry> |
| 38008 |
<entry><filename>/dev/null</filename></entry> |
| 38009 |
<entry><quote>black hole</quote> for burying command output</entry> |
| 38010 |
</row> |
| 38011 |
<row> |
| 38012 |
<entry><option>ECHO</option></entry> |
| 38013 |
<entry>echo</entry> |
| 38014 |
<entry>echo (many more option in Bash)</entry> |
| 38015 |
</row> |
| 38016 |
<row> |
| 38017 |
<entry><option>ECHO.</option></entry> |
| 38018 |
<entry>echo</entry> |
| 38019 |
<entry>echo blank line</entry> |
| 38020 |
</row> |
| 38021 |
<row> |
| 38022 |
<entry><option>ECHO OFF</option></entry> |
| 38023 |
<entry>set <option>+v</option></entry> |
| 38024 |
<entry>do not echo command(s) following</entry> |
| 38025 |
</row> |
| 38026 |
<row> |
| 38027 |
<entry><option>FOR %%VAR IN (LIST) DO</option></entry> |
| 38028 |
<entry>for var in [list]; do</entry> |
| 38029 |
<entry><quote>for</quote> loop</entry> |
| 38030 |
</row> |
| 38031 |
<row> |
| 38032 |
<entry><option>:LABEL</option></entry> |
| 38033 |
<entry>none (unnecessary)</entry> |
| 38034 |
<entry>label</entry> |
| 38035 |
</row> |
| 38036 |
<row> |
| 38037 |
<entry><option>GOTO</option></entry> |
| 38038 |
<entry>none (use a function)</entry> |
| 38039 |
<entry>jump to another location in the script</entry> |
| 38040 |
</row> |
| 38041 |
<row> |
| 38042 |
<entry><option>PAUSE</option></entry> |
| 38043 |
<entry>sleep</entry> |
| 38044 |
<entry>pause or wait an interval</entry> |
| 38045 |
</row> |
| 38046 |
<row> |
| 38047 |
<entry><option>CHOICE</option></entry> |
| 38048 |
<entry>case or select</entry> |
| 38049 |
<entry>menu choice</entry> |
| 38050 |
</row> |
| 38051 |
<row> |
| 38052 |
<entry><option>IF</option></entry> |
| 38053 |
<entry>if</entry> |
| 38054 |
<entry>if-test</entry> |
| 38055 |
</row> |
| 38056 |
<row> |
| 38057 |
<entry><option>IF EXIST <replaceable>FILENAME</replaceable></option></entry> |
| 38058 |
<entry>if [ -e filename ]</entry> |
| 38059 |
<entry>test if file exists</entry> |
| 38060 |
</row> |
| 38061 |
<row> |
| 38062 |
<entry><option>IF !%N==!</option></entry> |
| 38063 |
<entry>if [ -z "$N" ]</entry> |
| 38064 |
<entry>if replaceable parameter <quote>N</quote> not present</entry> |
| 38065 |
</row> |
| 38066 |
<row> |
| 38067 |
<entry><option>CALL</option></entry> |
| 38068 |
<entry>source or . (dot operator)</entry> |
| 38069 |
<entry><quote>include</quote> another script</entry> |
| 38070 |
</row> |
| 38071 |
<row> |
| 38072 |
<entry><option>COMMAND /C</option></entry> |
| 38073 |
<entry>source or . (dot operator)</entry> |
| 38074 |
<entry><quote>include</quote> another script (same as |
| 38075 |
CALL)</entry> |
| 38076 |
</row> |
| 38077 |
<row> |
| 38078 |
<entry><option>SET</option></entry> |
| 38079 |
<entry>export</entry> |
| 38080 |
<entry>set an environmental variable</entry> |
| 38081 |
</row> |
| 38082 |
<row> |
| 38083 |
<entry><option>SHIFT</option></entry> |
| 38084 |
<entry>shift</entry> |
| 38085 |
<entry>left shift command-line argument list</entry> |
| 38086 |
</row> |
| 38087 |
<row> |
| 38088 |
<entry><option>SGN</option></entry> |
| 38089 |
<entry>-lt or -gt</entry> |
| 38090 |
<entry>sign (of integer)</entry> |
| 38091 |
</row> |
| 38092 |
<row> |
| 38093 |
<entry><option>ERRORLEVEL</option></entry> |
| 38094 |
<entry>$?</entry> |
| 38095 |
<entry>exit status</entry> |
| 38096 |
</row> |
| 38097 |
<row> |
| 38098 |
<entry><option>CON</option></entry> |
| 38099 |
<entry><filename>stdin</filename></entry> |
| 38100 |
<entry><quote>console</quote> (<filename>stdin</filename>)</entry> |
| 38101 |
</row> |
| 38102 |
<row> |
| 38103 |
<entry><option>PRN</option></entry> |
| 38104 |
<entry><filename>/dev/lp0</filename></entry> |
| 38105 |
<entry>(generic) printer device</entry> |
| 38106 |
</row> |
| 38107 |
<row> |
| 38108 |
<entry><option>LPT1</option></entry> |
| 38109 |
<entry><filename>/dev/lp0</filename></entry> |
| 38110 |
<entry>first printer device</entry> |
| 38111 |
</row> |
| 38112 |
<row> |
| 38113 |
<entry><option>COM1</option></entry> |
| 38114 |
<entry><filename>/dev/ttyS0</filename></entry> |
| 38115 |
<entry>first serial port</entry> |
| 38116 |
</row> |
| 38117 |
</tbody> |
| 38118 |
</tgroup> |
| 38119 |
</table> |
| 38120 |
|
| 38121 |
|
| 38122 |
|
| 38123 |
<para><anchor id="dosunixequiv"></para> |
| 38124 |
<para>Batch files usually contain DOS commands. These must be |
| 38125 |
translated into their UNIX equivalents in order to convert a |
| 38126 |
batch file into a shell script.</para> |
| 38127 |
|
| 38128 |
<table> |
| 38129 |
<title>DOS commands and their UNIX equivalents</title> |
| 38130 |
<tgroup cols="3"> |
| 38131 |
<thead> |
| 38132 |
<row> |
| 38133 |
<entry>DOS Command</entry> |
| 38134 |
<entry>UNIX Equivalent</entry> |
| 38135 |
<entry>Effect</entry> |
| 38136 |
</row> |
| 38137 |
</thead> |
| 38138 |
<tbody> |
| 38139 |
<row> |
| 38140 |
<entry><option>ASSIGN</option></entry> |
| 38141 |
<entry>ln</entry> |
| 38142 |
<entry>link file or directory</entry> |
| 38143 |
</row> |
| 38144 |
<row> |
| 38145 |
<entry><option>ATTRIB</option></entry> |
| 38146 |
<entry>chmod</entry> |
| 38147 |
<entry>change file permissions</entry> |
| 38148 |
</row> |
| 38149 |
<row> |
| 38150 |
<entry><option>CD</option></entry> |
| 38151 |
<entry>cd</entry> |
| 38152 |
<entry>change directory</entry> |
| 38153 |
</row> |
| 38154 |
<row> |
| 38155 |
<entry><option>CHDIR</option></entry> |
| 38156 |
<entry>cd</entry> |
| 38157 |
<entry>change directory</entry> |
| 38158 |
</row> |
| 38159 |
<row> |
| 38160 |
<entry><option>CLS</option></entry> |
| 38161 |
<entry>clear</entry> |
| 38162 |
<entry>clear screen</entry> |
| 38163 |
</row> |
| 38164 |
<row> |
| 38165 |
<entry><option>COMP</option></entry> |
| 38166 |
<entry>diff, comm, cmp</entry> |
| 38167 |
<entry>file compare</entry> |
| 38168 |
</row> |
| 38169 |
<row> |
| 38170 |
<entry><option>COPY</option></entry> |
| 38171 |
<entry>cp</entry> |
| 38172 |
<entry>file copy</entry> |
| 38173 |
</row> |
| 38174 |
<row> |
| 38175 |
<entry><option>Ctl-C</option></entry> |
| 38176 |
<entry>Ctl-C</entry> |
| 38177 |
<entry>break (signal)</entry> |
| 38178 |
</row> |
| 38179 |
<row> |
| 38180 |
<entry><option>Ctl-Z</option></entry> |
| 38181 |
<entry>Ctl-D</entry> |
| 38182 |
<entry>EOF (end-of-file)</entry> |
| 38183 |
</row> |
| 38184 |
<row> |
| 38185 |
<entry><option>DEL</option></entry> |
| 38186 |
<entry>rm</entry> |
| 38187 |
<entry>delete file(s)</entry> |
| 38188 |
</row> |
| 38189 |
<row> |
| 38190 |
<entry><option>DELTREE</option></entry> |
| 38191 |
<entry>rm -rf</entry> |
| 38192 |
<entry>delete directory recursively</entry> |
| 38193 |
</row> |
| 38194 |
<row> |
| 38195 |
<entry><option>DIR</option></entry> |
| 38196 |
<entry>ls -l</entry> |
| 38197 |
<entry>directory listing</entry> |
| 38198 |
</row> |
| 38199 |
<row> |
| 38200 |
<entry><option>ERASE</option></entry> |
| 38201 |
<entry>rm</entry> |
| 38202 |
<entry>delete file(s)</entry> |
| 38203 |
</row> |
| 38204 |
<row> |
| 38205 |
<entry><option>EXIT</option></entry> |
| 38206 |
<entry>exit</entry> |
| 38207 |
<entry>exit current process</entry> |
| 38208 |
</row> |
| 38209 |
<row> |
| 38210 |
<entry><option>FC</option></entry> |
| 38211 |
<entry>comm, cmp</entry> |
| 38212 |
<entry>file compare</entry> |
| 38213 |
</row> |
| 38214 |
<row> |
| 38215 |
<entry><option>FIND</option></entry> |
| 38216 |
<entry>grep</entry> |
| 38217 |
<entry>find strings in files</entry> |
| 38218 |
</row> |
| 38219 |
<row> |
| 38220 |
<entry><option>MD</option></entry> |
| 38221 |
<entry>mkdir</entry> |
| 38222 |
<entry>make directory</entry> |
| 38223 |
</row> |
| 38224 |
<row> |
| 38225 |
<entry><option>MKDIR</option></entry> |
| 38226 |
<entry>mkdir</entry> |
| 38227 |
<entry>make directory</entry> |
| 38228 |
</row> |
| 38229 |
<row> |
| 38230 |
<entry><option>MORE</option></entry> |
| 38231 |
<entry>more</entry> |
| 38232 |
<entry>text file paging filter</entry> |
| 38233 |
</row> |
| 38234 |
<row> |
| 38235 |
<entry><option>MOVE</option></entry> |
| 38236 |
<entry>mv</entry> |
| 38237 |
<entry>move</entry> |
| 38238 |
</row> |
| 38239 |
<row> |
| 38240 |
<entry><option>PATH</option></entry> |
| 38241 |
<entry>$PATH</entry> |
| 38242 |
<entry>path to executables</entry> |
| 38243 |
</row> |
| 38244 |
<row> |
| 38245 |
<entry><option>REN</option></entry> |
| 38246 |
<entry>mv</entry> |
| 38247 |
<entry>rename (move)</entry> |
| 38248 |
</row> |
| 38249 |
<row> |
| 38250 |
<entry><option>RENAME</option></entry> |
| 38251 |
<entry>mv</entry> |
| 38252 |
<entry>rename (move)</entry> |
| 38253 |
</row> |
| 38254 |
<row> |
| 38255 |
<entry><option>RD</option></entry> |
| 38256 |
<entry>rmdir</entry> |
| 38257 |
<entry>remove directory</entry> |
| 38258 |
</row> |
| 38259 |
<row> |
| 38260 |
<entry><option>RMDIR</option></entry> |
| 38261 |
<entry>rmdir</entry> |
| 38262 |
<entry>remove directory</entry> |
| 38263 |
</row> |
| 38264 |
<row> |
| 38265 |
<entry><option>SORT</option></entry> |
| 38266 |
<entry>sort</entry> |
| 38267 |
<entry>sort file</entry> |
| 38268 |
</row> |
| 38269 |
<row> |
| 38270 |
<entry><option>TIME</option></entry> |
| 38271 |
<entry>date</entry> |
| 38272 |
<entry>display system time</entry> |
| 38273 |
</row> |
| 38274 |
<row> |
| 38275 |
<entry><option>TYPE</option></entry> |
| 38276 |
<entry>cat</entry> |
| 38277 |
<entry>output file to <filename>stdout</filename></entry> |
| 38278 |
</row> |
| 38279 |
<row> |
| 38280 |
<entry><option>XCOPY</option></entry> |
| 38281 |
<entry>cp</entry> |
| 38282 |
<entry>(extended) file copy</entry> |
| 38283 |
</row> |
| 38284 |
</tbody> |
| 38285 |
</tgroup> |
| 38286 |
</table> |
| 38287 |
|
| 38288 |
|
| 38289 |
<note> |
| 38290 |
<para>Virtually all UNIX and shell operators and commands have |
| 38291 |
many more options and enhancements than their DOS and batch file |
| 38292 |
counterparts. Many DOS batch files rely on auxiliary utilities, |
| 38293 |
such as <command>ask.com</command>, a crippled counterpart to |
| 38294 |
<link linkend="readref">read</link>.</para> |
| 38295 |
|
| 38296 |
<para>DOS supports only a very limited and incompatible subset of |
| 38297 |
filename <link linkend="globbingref">wild-card expansion</link>, |
| 38298 |
recognizing just the <token>*</token> and <token>?</token> |
| 38299 |
characters.</para> |
| 38300 |
</note> |
| 38301 |
|
| 38302 |
<para>Converting a DOS batch file into a shell script is generally |
| 38303 |
straightforward, and the result ofttimes reads better than the |
| 38304 |
original.</para> |
| 38305 |
|
| 38306 |
<example id="VIEWDAT"> |
| 38307 |
<title>VIEWDATA.BAT: DOS Batch File</title> |
| 38308 |
<programlisting>&VIEWDAT;</programlisting> |
| 38309 |
</example> |
| 38310 |
|
| 38311 |
<para> |
| 38312 |
The script conversion is somewhat of an improvement. |
| 38313 |
<footnote><para>Various readers have suggested modifications |
| 38314 |
of the above batch file to prettify it and make it more |
| 38315 |
compact and efficient. In the opinion of the <emphasis>ABS |
| 38316 |
Guide</emphasis> author, this is wasted effort. A Bash script |
| 38317 |
can access a DOS filesystem, or even an NTFS partition (with |
| 38318 |
the help of <ulink url="http://www.ntfs-3g.org">ntfs-3g</ulink>) |
| 38319 |
to do batch or scripted operations.</para></footnote> |
| 38320 |
</para> |
| 38321 |
|
| 38322 |
<example id="viewdata"> |
| 38323 |
<title><firstterm>viewdata.sh</firstterm>: Shell Script Conversion |
| 38324 |
of VIEWDATA.BAT</title> |
| 38325 |
<programlisting>&viewdata;</programlisting> |
| 38326 |
</example> |
| 38327 |
|
| 38328 |
|
| 38329 |
|
| 38330 |
<para>Ted Davis' <ulink url="http://www.maem.umr.edu/batch/">Shell |
| 38331 |
Scripts on the PC</ulink> site had a set of comprehensive |
| 38332 |
tutorials on the old-fashioned art of batch file |
| 38333 |
programming. Unfortunately the page has vanished without a |
| 38334 |
trace.</para> |
| 38335 |
|
| 38336 |
|
| 38337 |
</appendix> |
| 38338 |
<!-- End DOS Batch File Conversion appendix --> |
| 38339 |
|
| 38340 |
|
| 38341 |
<appendix id="exercises"> |
| 38342 |
<title>Exercises</title> |
| 38343 |
|
| 38344 |
<para>The exercises that follow test and extend your knowledge |
| 38345 |
of scripting. Think of them as a challenge, as an entertaining way |
| 38346 |
to take you further along the stony path toward UNIX wizardry.</para> |
| 38347 |
|
| 38348 |
<para> |
| 38349 |
<literallayout> |
| 38350 |
On a dingy side street in a run-down section of Hoboken, New Jersey, |
| 38351 |
there sits a nondescript squat two-story brick building with an inscription |
| 38352 |
incised on a marble plate in its wall: |
| 38353 |
|
| 38354 |
<computeroutput>Bash Scripting Hall of Fame</computeroutput>. |
| 38355 |
|
| 38356 |
Inside, among various dusty uninteresting exhibits is a corroding, |
| 38357 |
cobweb-festooned brass plaque inscribed with a short, very short |
| 38358 |
list of those few persons who have successfully mastered the material |
| 38359 |
in the <firstterm>Advanced Bash Scripting Guide</firstterm>, as evidenced by their performance |
| 38360 |
on the following Exercise sections. |
| 38361 |
|
| 38362 |
(Alas, the author of the <firstterm>ABS Guide</firstterm> is not represented among the exhibits. |
| 38363 |
This is possibly due to malicious rumors about <link linkend="nocreds">lack of credentials</link> and |
| 38364 |
<link linkend="ktour0">deficient scripting skills</link>.) |
| 38365 |
</literallayout> |
| 38366 |
</para> |
| 38367 |
|
| 38368 |
<sect1 id="scriptanalysis"> |
| 38369 |
<title>Analyzing Scripts</title> |
| 38370 |
|
| 38371 |
<para>Examine the following script. Run it, then explain what it |
| 38372 |
does. Annotate the script and rewrite it in a more compact and |
| 38373 |
elegant manner.</para> |
| 38374 |
|
| 38375 |
<para> |
| 38376 |
<programlisting>#!/bin/bash |
| 38377 |
|
| 38378 |
MAX=10000 |
| 38379 |
|
| 38380 |
|
| 38381 |
for((nr=1; nr<$MAX; nr++)) |
| 38382 |
do |
| 38383 |
|
| 38384 |
let "t1 = nr % 5" |
| 38385 |
if [ "$t1" -ne 3 ] |
| 38386 |
then |
| 38387 |
continue |
| 38388 |
fi |
| 38389 |
|
| 38390 |
let "t2 = nr % 7" |
| 38391 |
if [ "$t2" -ne 4 ] |
| 38392 |
then |
| 38393 |
continue |
| 38394 |
fi |
| 38395 |
|
| 38396 |
let "t3 = nr % 9" |
| 38397 |
if [ "$t3" -ne 5 ] |
| 38398 |
then |
| 38399 |
continue |
| 38400 |
fi |
| 38401 |
|
| 38402 |
break # What happens when you comment out this line? Why? |
| 38403 |
|
| 38404 |
done |
| 38405 |
|
| 38406 |
echo "Number = $nr" |
| 38407 |
|
| 38408 |
|
| 38409 |
exit 0</programlisting> |
| 38410 |
</para> |
| 38411 |
|
| 38412 |
<para>---</para> |
| 38413 |
|
| 38414 |
<para>Explain what the following script does. It is really just |
| 38415 |
a parameterized command-line pipe.</para> |
| 38416 |
|
| 38417 |
<para> |
| 38418 |
<programlisting>#!/bin/bash |
| 38419 |
|
| 38420 |
DIRNAME=/usr/bin |
| 38421 |
FILETYPE="shell script" |
| 38422 |
LOGFILE=logfile |
| 38423 |
|
| 38424 |
file "$DIRNAME"/* | fgrep "$FILETYPE" | tee $LOGFILE | wc -l |
| 38425 |
|
| 38426 |
exit 0</programlisting> |
| 38427 |
</para> |
| 38428 |
|
| 38429 |
<para>---</para> |
| 38430 |
|
| 38431 |
<para>Examine and explain the following script. For hints, you |
| 38432 |
might refer to the listings for <link |
| 38433 |
linkend="findref">find</link> and <link |
| 38434 |
linkend="statref">stat</link>.</para> |
| 38435 |
|
| 38436 |
<para> |
| 38437 |
<programlisting>#!/bin/bash |
| 38438 |
|
| 38439 |
# Author: Nathan Coulter |
| 38440 |
# This code is released to the public domain. |
| 38441 |
# The author gave permission to use this code snippet in the ABS Guide. |
| 38442 |
|
| 38443 |
find -maxdepth 1 -type f -printf '%f\000' | { |
| 38444 |
while read -d $'\000'; do |
| 38445 |
mv "$REPLY" "$(date -d "$(stat -c '%y' "$REPLY") " '+%Y%m%d%H%M%S' |
| 38446 |
)-$REPLY" |
| 38447 |
done |
| 38448 |
} |
| 38449 |
|
| 38450 |
# Warning: Test-drive this script in a "scratch" directory. |
| 38451 |
# It will somehow affect all the files there.</programlisting> |
| 38452 |
</para> |
| 38453 |
|
| 38454 |
<para>---</para> |
| 38455 |
|
| 38456 |
<para>A reader sent in the following code snippet.</para> |
| 38457 |
|
| 38458 |
<para> |
| 38459 |
<programlisting>while read LINE |
| 38460 |
do |
| 38461 |
echo $LINE |
| 38462 |
done < `tail -f /var/log/messages`</programlisting> |
| 38463 |
</para> |
| 38464 |
|
| 38465 |
<para>He wished to write a script tracking changes to the system log |
| 38466 |
file, <filename>/var/log/messages</filename>. Unfortunately, |
| 38467 |
the above code block hangs and does nothing |
| 38468 |
useful. Why? Fix this so it does work. (Hint: |
| 38469 |
rather than <link linkend="redirref">redirecting the |
| 38470 |
<filename>stdin</filename> of the loop</link>, try a <link |
| 38471 |
linkend="piperef">pipe</link>.)</para> |
| 38472 |
|
| 38473 |
|
| 38474 |
<para>---</para> |
| 38475 |
|
| 38476 |
<para>Analyze the following <quote>one-liner</quote> (here |
| 38477 |
split into two lines for clarity) contributed by Rory |
| 38478 |
Winston:</para> |
| 38479 |
|
| 38480 |
<para> |
| 38481 |
<programlisting>export SUM=0; for f in $(find src -name "*.java"); |
| 38482 |
do export SUM=$(($SUM + $(wc -l $f | awk '{ print $1 }'))); done; echo $SUM</programlisting> |
| 38483 |
</para> |
| 38484 |
|
| 38485 |
<para>Hint: First, break the script up into bite-sized |
| 38486 |
sections. Then, carefully examine its use of <link |
| 38487 |
linkend="dblparens">double-parentheses</link> arithmetic, |
| 38488 |
the <link linkend="exportref">export</link> command, |
| 38489 |
the <link linkend="findref">find</link> command, the |
| 38490 |
<link linkend="wcref">wc</link> command, and <link |
| 38491 |
linkend="awkref">awk</link>.</para> |
| 38492 |
|
| 38493 |
<para>---</para> |
| 38494 |
|
| 38495 |
|
| 38496 |
<para>Analyze <xref linkend="lifeslow">, and reorganize it in a |
| 38497 |
simplified and more logical style. See how many of the variables |
| 38498 |
can be eliminated, and try to optimize the script to speed up |
| 38499 |
its execution time.</para> |
| 38500 |
|
| 38501 |
<para>Alter the script so that it accepts any ordinary ASCII |
| 38502 |
text file as input for its initial <quote>generation</quote>. The |
| 38503 |
script will read the first <parameter>$ROW*$COL</parameter> |
| 38504 |
characters, and set the occurrences of vowels as |
| 38505 |
<quote>living</quote> cells. Hint: be sure to translate the |
| 38506 |
spaces in the input file to underscore characters.</para> |
| 38507 |
|
| 38508 |
</sect1> |
| 38509 |
<!-- End Analyzing Scripts section --> |
| 38510 |
|
| 38511 |
|
| 38512 |
|
| 38513 |
<sect1 id="writingscripts"> |
| 38514 |
<title>Writing Scripts</title> |
| 38515 |
|
| 38516 |
<para><anchor id="writingscripts1"></para> |
| 38517 |
|
| 38518 |
<para>Write a script to carry out each of the following tasks.</para> |
| 38519 |
|
| 38520 |
|
| 38521 |
<variablelist id="exeasy"> |
| 38522 |
<title><anchor id="exeasy1">EASY</title> |
| 38523 |
|
| 38524 |
<varlistentry> |
| 38525 |
<term><command>Self-reproducing Script</command></term> |
| 38526 |
<listitem> |
| 38527 |
<para>Write a script that backs itself up, that is, copies |
| 38528 |
itself to a file named <filename>backup.sh</filename>.</para> |
| 38529 |
<para>Hint: Use the <link linkend="catref">cat</link> command |
| 38530 |
and the appropriate <link linkend="scrnameparam">positional |
| 38531 |
parameter</link>.</para> |
| 38532 |
</listitem> |
| 38533 |
</varlistentry> |
| 38534 |
|
| 38535 |
<varlistentry> |
| 38536 |
<term><command>Home Directory Listing</command></term> |
| 38537 |
<listitem> |
| 38538 |
<para>Perform a recursive directory listing on the user's home |
| 38539 |
directory and save the information to a file. Compress |
| 38540 |
the file, have the script prompt the user to insert |
| 38541 |
a USB flash drive, then press <keycap>ENTER</keycap>. |
| 38542 |
Finally, save the file to the flash drive after making |
| 38543 |
certain the flash drive has properly mounted by parsing |
| 38544 |
the output of <link linkend="dfref">df</link>. Note that |
| 38545 |
the flash drive must be <firstterm>unmounted</firstterm> |
| 38546 |
before it is removed.</para> |
| 38547 |
</listitem> |
| 38548 |
</varlistentry> |
| 38549 |
|
| 38550 |
<varlistentry> |
| 38551 |
<term><command>Converting <link linkend="forloopref1">for</link> |
| 38552 |
loops to <link linkend="whileloopref">while</link> and <link |
| 38553 |
linkend="untilloopref">until</link> loops</command></term> |
| 38554 |
<listitem> |
| 38555 |
<para>Convert the <firstterm>for loops</firstterm> in <xref |
| 38556 |
linkend="ex22"> to <firstterm>while |
| 38557 |
loops</firstterm>. Hint: store the data in an <link |
| 38558 |
linkend="arrayref">array</link> and step through the array |
| 38559 |
elements.</para> |
| 38560 |
<para>Having already done the <quote>heavy lifting,</quote> |
| 38561 |
now convert the loops in the example to <firstterm> until |
| 38562 |
loops</firstterm>.</para> |
| 38563 |
</listitem> |
| 38564 |
</varlistentry> |
| 38565 |
|
| 38566 |
<varlistentry> |
| 38567 |
<term><command>Changing the line spacing of a text file</command></term> |
| 38568 |
<listitem> |
| 38569 |
|
| 38570 |
<para>Write a script that reads each line of a target file, then |
| 38571 |
writes the line back to <filename>stdout</filename>, but with |
| 38572 |
an extra blank line following. This has the effect of |
| 38573 |
<emphasis>double-spacing</emphasis> the file.</para> |
| 38574 |
|
| 38575 |
<para>Include all necessary code to check whether the script |
| 38576 |
gets the necessary command-line argument (a filename), |
| 38577 |
and whether the specified file exists.</para> |
| 38578 |
|
| 38579 |
<para>When the script runs correctly, modify it to |
| 38580 |
<emphasis>triple-space</emphasis> the target file.</para> |
| 38581 |
|
| 38582 |
<para>Finally, write a script to remove all blank lines from |
| 38583 |
the target file, <emphasis>single-spacing</emphasis> it.</para> |
| 38584 |
|
| 38585 |
</listitem> |
| 38586 |
</varlistentry> |
| 38587 |
|
| 38588 |
<varlistentry> |
| 38589 |
<term><command>Backwards Listing</command></term> |
| 38590 |
<listitem> |
| 38591 |
<para>Write a script that echoes itself to |
| 38592 |
<filename>stdout</filename>, but |
| 38593 |
<emphasis>backwards</emphasis>.</para> |
| 38594 |
</listitem> |
| 38595 |
</varlistentry> |
| 38596 |
|
| 38597 |
<varlistentry> |
| 38598 |
<term><command>Automatically Decompressing Files</command></term> |
| 38599 |
<listitem> |
| 38600 |
<para>Given a list of filenames as input, this script |
| 38601 |
queries each target file (parsing the output of the |
| 38602 |
<link linkend="fileref">file</link> command) for |
| 38603 |
the type of compression used on it. Then the script |
| 38604 |
automatically invokes the appropriate decompression command |
| 38605 |
(<command>gunzip</command>, <command>bunzip2</command>, |
| 38606 |
<command>unzip</command>, <command>uncompress</command>, |
| 38607 |
or whatever). If a target file is not compressed, the |
| 38608 |
script emits a warning message, but takes no other action |
| 38609 |
on that particular file.</para> |
| 38610 |
</listitem> |
| 38611 |
</varlistentry> |
| 38612 |
|
| 38613 |
<varlistentry> |
| 38614 |
<term><command>Unique System ID</command></term> |
| 38615 |
<listitem> |
| 38616 |
<para>Generate a <quote>unique</quote> 6-digit hexadecimal |
| 38617 |
identifier for your computer. Do <emphasis>not</emphasis> |
| 38618 |
use the flawed <link linkend="hostidref">hostid</link> |
| 38619 |
command. Hint: <command><link |
| 38620 |
linkend="md5sumref">md5sum</link> |
| 38621 |
<link |
| 38622 |
linkend="datafilesref1"><filename>/etc/passwd</filename></link></command>, |
| 38623 |
then select the first 6 digits of output.</para> |
| 38624 |
</listitem> |
| 38625 |
</varlistentry> |
| 38626 |
|
| 38627 |
<varlistentry> |
| 38628 |
<term><command>Backup</command></term> |
| 38629 |
<listitem> |
| 38630 |
<para>Archive as a <quote>tarball</quote> |
| 38631 |
(<filename>*.tar.gz</filename> file) all the files |
| 38632 |
in your home directory tree |
| 38633 |
(<filename>/home/your-name</filename>) that have |
| 38634 |
been modified in the last 24 hours. Hint: use <link |
| 38635 |
linkend="findref">find</link>.</para> |
| 38636 |
<para>Optional: you may use this as the basis of a |
| 38637 |
<firstterm>backup</firstterm> script.</para> |
| 38638 |
</listitem> |
| 38639 |
</varlistentry> |
| 38640 |
|
| 38641 |
<varlistentry> |
| 38642 |
<term><command>Checking whether a process is still running</command></term> |
| 38643 |
<listitem> |
| 38644 |
<para>Given a <link linkend="processidref">process ID</link> |
| 38645 |
(<firstterm>PID</firstterm>) as an argument, this script |
| 38646 |
will check, at user-specified intervals, whether |
| 38647 |
the given process is still running. You may use |
| 38648 |
the <link linkend="ppssref">ps</link> and <link |
| 38649 |
linkend="sleepref">sleep</link> commands.</para> |
| 38650 |
</listitem> |
| 38651 |
</varlistentry> |
| 38652 |
|
| 38653 |
<varlistentry> |
| 38654 |
<term><command>Primes</command></term> |
| 38655 |
<listitem> |
| 38656 |
<para>Print (to <filename>stdout</filename>) all |
| 38657 |
prime numbers between 60000 and 63000. The output |
| 38658 |
should be nicely formatted in columns (hint: |
| 38659 |
use <link linkend="printfref">printf</link>).</para> |
| 38660 |
</listitem> |
| 38661 |
</varlistentry> |
| 38662 |
|
| 38663 |
<varlistentry> |
| 38664 |
<term><command>Lottery Numbers</command></term> |
| 38665 |
<listitem> |
| 38666 |
<para>One type of lottery involves picking five |
| 38667 |
different numbers, in the range of 1 - 50. Write a |
| 38668 |
script that generates five pseudorandom numbers in this |
| 38669 |
range, <emphasis>with no duplicates</emphasis>. The |
| 38670 |
script will give the option of echoing the numbers to |
| 38671 |
<filename>stdout</filename> or saving them to a file, |
| 38672 |
along with the date and time the particular number set |
| 38673 |
was generated. (If your script consistently generates |
| 38674 |
<emphasis>winning</emphasis> lottery numbers, then you |
| 38675 |
can retire on the proceeds and leave shell scripting to |
| 38676 |
those of us who have to work for a living.)</para> |
| 38677 |
</listitem> |
| 38678 |
</varlistentry> |
| 38679 |
|
| 38680 |
</variablelist> |
| 38681 |
|
| 38682 |
|
| 38683 |
<variablelist id="exmedium"> |
| 38684 |
<title><anchor id="exmedium1">INTERMEDIATE</title> |
| 38685 |
|
| 38686 |
<varlistentry> |
| 38687 |
<term><command>Integer or String</command></term> |
| 38688 |
<listitem> |
| 38689 |
<para>Write a script <link linkend="functionref">function</link> |
| 38690 |
that determines if an argument passed to it is an integer |
| 38691 |
or a string. The function will return TRUE (0) if |
| 38692 |
passed an integer, and FALSE (1) if passed a string.</para> |
| 38693 |
<para>Hint: What does the following expression return |
| 38694 |
when <varname>$1</varname> is <emphasis>not</emphasis> |
| 38695 |
an integer?</para> |
| 38696 |
<para><varname>expr $1 + 0</varname></para> |
| 38697 |
</listitem> |
| 38698 |
</varlistentry> |
| 38699 |
|
| 38700 |
<varlistentry> |
| 38701 |
<term><command><link linkend="asciidef">ASCII</link> |
| 38702 |
to Integer</command></term> |
| 38703 |
<listitem> |
| 38704 |
<para>The <firstterm>atoi</firstterm> function in |
| 38705 |
<command>C</command> converts a string character to |
| 38706 |
an integer. Write a shell script function that performs |
| 38707 |
the same operation. Likewise, write a shell script function |
| 38708 |
that does the inverse, mirroring the <command>C</command> |
| 38709 |
<firstterm>itoa</firstterm> function which converts an |
| 38710 |
integer into an ASCII character.</para> |
| 38711 |
</listitem> |
| 38712 |
</varlistentry> |
| 38713 |
|
| 38714 |
<varlistentry> |
| 38715 |
<term><command>Managing Disk Space</command></term> |
| 38716 |
<listitem> |
| 38717 |
<para>List, one at a time, all files larger than 100K in |
| 38718 |
the <filename class="directory">/home/username</filename> |
| 38719 |
directory tree. Give the user the option to delete or |
| 38720 |
compress the file, then proceed to show the next one. Write |
| 38721 |
to a logfile the names of all deleted files and the |
| 38722 |
deletion times.</para> |
| 38723 |
</listitem> |
| 38724 |
</varlistentry> |
| 38725 |
|
| 38726 |
<varlistentry> |
| 38727 |
<term><command>Banner</command></term> |
| 38728 |
<listitem> |
| 38729 |
<para>Simulate the functionality of the deprecated <link |
| 38730 |
linkend="bannerref">banner</link> command in a script.</para> |
| 38731 |
</listitem> |
| 38732 |
</varlistentry> |
| 38733 |
|
| 38734 |
<varlistentry> |
| 38735 |
<term><command>Removing Inactive Accounts</command></term> |
| 38736 |
<listitem> |
| 38737 |
<para>Inactive accounts on a network server waste disk space and may |
| 38738 |
become a security risk. Write an administrative script |
| 38739 |
(to be invoked by <firstterm>root</firstterm> or the <link |
| 38740 |
linkend="cronref">cron daemon</link>) that checks |
| 38741 |
for and deletes user accounts that have not been accessed |
| 38742 |
within the last 90 days.</para> |
| 38743 |
</listitem> |
| 38744 |
</varlistentry> |
| 38745 |
|
| 38746 |
<varlistentry> |
| 38747 |
<term><command>Enforcing Disk Quotas</command></term> |
| 38748 |
<listitem> |
| 38749 |
<para>Write a script for a multi-user system that checks users' |
| 38750 |
disk usage. If a user surpasses a preset limit |
| 38751 |
(500 MB, for example) in her <filename |
| 38752 |
class="directory">/home/username</filename> |
| 38753 |
directory, then the script automatically sends her a |
| 38754 |
<quote>pigout</quote> warning e-mail.</para> <para>The |
| 38755 |
script will use the <link linkend="duref">du</link> |
| 38756 |
and <link linkend="commmail1">mail</link> commands. As |
| 38757 |
an option, it will allow setting and enforcing quotas |
| 38758 |
using the <link linkend="quotaref">quota</link> and <link |
| 38759 |
linkend="setquotaref">setquota</link> commands.</para> |
| 38760 |
</listitem> |
| 38761 |
</varlistentry> |
| 38762 |
|
| 38763 |
<varlistentry> |
| 38764 |
<term><command>Logged in User Information</command></term> |
| 38765 |
<listitem> |
| 38766 |
|
| 38767 |
<para>For all logged in users, show their real names and the time |
| 38768 |
and date of their last login.</para> |
| 38769 |
|
| 38770 |
<para>Hint: use <link linkend="whoref">who</link>, |
| 38771 |
<link linkend="lastlogref">lastlog</link>, |
| 38772 |
and parse <link |
| 38773 |
linkend="datafilesref1"><filename>/etc/passwd</filename></link>.</para> |
| 38774 |
|
| 38775 |
</listitem> |
| 38776 |
</varlistentry> |
| 38777 |
|
| 38778 |
<varlistentry> |
| 38779 |
<term><command>Safe Delete</command></term> |
| 38780 |
<listitem> |
| 38781 |
<para>Implement, as a script, a <quote>safe</quote> delete |
| 38782 |
command, <filename>sdel.sh</filename>. Filenames passed as |
| 38783 |
command-line arguments to this script are not deleted, |
| 38784 |
but instead <link linkend="gzipref">gzipped</link> |
| 38785 |
if not already compressed (use <link |
| 38786 |
linkend="fileref">file</link> to check), then moved |
| 38787 |
to a <filename class="directory">~/TRASH</filename> |
| 38788 |
directory. Upon invocation, the script checks the <filename |
| 38789 |
class="directory">~/TRASH</filename> directory for files |
| 38790 |
older than 48 hours and <link linkend="rmref">permanently |
| 38791 |
deletes</link> them. (An better alternative might be to |
| 38792 |
have a second script handle this, periodically invoked |
| 38793 |
by the <link linkend="cronref">cron daemon</link>.)</para> |
| 38794 |
<para><emphasis>Extra credit:</emphasis> Write the script |
| 38795 |
so it can handle files and directories <link |
| 38796 |
linkend="rmrecurs">recursively</link>. This would give it |
| 38797 |
the capability of <quote>safely deleting</quote> entire |
| 38798 |
directory structures.</para> |
| 38799 |
</listitem> |
| 38800 |
</varlistentry> |
| 38801 |
|
| 38802 |
<varlistentry> |
| 38803 |
<term><command>Making Change</command></term> |
| 38804 |
<listitem> |
| 38805 |
<para>What is the most efficient way to make change for $1.68, |
| 38806 |
using only coins in common circulations (up to 25c)? It's |
| 38807 |
6 quarters, 1 dime, a nickel, and three cents.</para> |
| 38808 |
<para>Given any arbitrary command-line input in dollars and |
| 38809 |
cents ($*.??), calculate the change, using the minimum |
| 38810 |
number of coins. If your home country is not the United |
| 38811 |
States, you may use your local currency units instead. The |
| 38812 |
script will need to parse the command-line input, then |
| 38813 |
change it to multiples of the smallest monetary unit (cents |
| 38814 |
or whatever). Hint: look at <xref linkend="ex61">.</para> |
| 38815 |
</listitem> |
| 38816 |
</varlistentry> |
| 38817 |
|
| 38818 |
<varlistentry> |
| 38819 |
<term><command>Quadratic Equations</command></term> |
| 38820 |
<listitem> |
| 38821 |
<para>Solve a <firstterm>quadratic</firstterm> equation of the form |
| 38822 |
<parameter>Ax^2 + Bx + C = 0</parameter>. Have a script take |
| 38823 |
as arguments the coefficients, <userinput>A</userinput>, |
| 38824 |
<userinput>B</userinput>, and <userinput>C</userinput>, |
| 38825 |
and return the solutions to five decimal places.</para> |
| 38826 |
|
| 38827 |
<para>Hint: pipe the coefficients to <link |
| 38828 |
linkend="bcref">bc</link>, using the well-known formula, |
| 38829 |
<parameter>x = ( -B +/- sqrt( B^2 - 4AC ) ) / 2A</parameter>.</para> |
| 38830 |
</listitem> |
| 38831 |
</varlistentry> |
| 38832 |
|
| 38833 |
<varlistentry> |
| 38834 |
<term><command>Table of Logarithms</command></term> |
| 38835 |
<listitem> |
| 38836 |
<para>Using the <link linkend="bcref">bc</link> and <link |
| 38837 |
linkend="printfref">printf</link> commands, print out a |
| 38838 |
nicely-formatted table of eight-place natural logarithms |
| 38839 |
in the interval between 0.00 and 100.00, in steps of |
| 38840 |
.01.</para> |
| 38841 |
<para>Hint: <firstterm>bc</firstterm> requires the |
| 38842 |
<option>-l</option> option to load the math library.</para> |
| 38843 |
</listitem> |
| 38844 |
</varlistentry> |
| 38845 |
|
| 38846 |
<varlistentry> |
| 38847 |
<term><command>Unicode Table</command></term> |
| 38848 |
<listitem> |
| 38849 |
<para>Using <xref linkend="asciish"> as a template, |
| 38850 |
write a script that prints to a file a complete |
| 38851 |
<link linkend="unicoderef">Unicode</link> table.</para> |
| 38852 |
<para>Hint: Use the <option>-e</option> option to |
| 38853 |
<link linkend="echoref">echo</link>: |
| 38854 |
<command>echo -e '\uXXXX'</command>, where |
| 38855 |
<replaceable>XXXX</replaceable> |
| 38856 |
is the Unicode numerical character designation. |
| 38857 |
This requires <link linkend="bash42">version 4.2</link> |
| 38858 |
or later of Bash.</para> |
| 38859 |
</listitem> |
| 38860 |
</varlistentry> |
| 38861 |
|
| 38862 |
<varlistentry> |
| 38863 |
<term><command>Sum of Matching Numbers</command></term> |
| 38864 |
<listitem> |
| 38865 |
<para>Find the sum of all five-digit numbers (in the range |
| 38866 |
10000 - 99999) containing <emphasis>exactly two</emphasis> |
| 38867 |
out of the following set of digits: { 4, 5, 6 }. These may |
| 38868 |
repeat within the same number, and if so, they count once |
| 38869 |
for each occurrence.</para> |
| 38870 |
<para>Some examples of <firstterm>matching numbers</firstterm> are |
| 38871 |
42057, 74638, and 89515.</para> |
| 38872 |
</listitem> |
| 38873 |
</varlistentry> |
| 38874 |
|
| 38875 |
<varlistentry> |
| 38876 |
<term><command>Lucky Numbers</command></term> |
| 38877 |
<listitem> |
| 38878 |
<para>A <firstterm>lucky number</firstterm> is one whose |
| 38879 |
individual digits add up to 7, in successive additions. For |
| 38880 |
example, 62431 is a <firstterm>lucky number</firstterm> |
| 38881 |
(6 + 2 + 4 + 3 + 1 = 16, 1 + 6 = 7). Find all the |
| 38882 |
<firstterm>lucky numbers</firstterm> between 1000 and |
| 38883 |
10000.</para> |
| 38884 |
</listitem> |
| 38885 |
</varlistentry> |
| 38886 |
|
| 38887 |
<varlistentry> |
| 38888 |
<term><command>Craps</command></term> |
| 38889 |
<listitem> |
| 38890 |
<para>Borrowing the ASCII graphics from <xref linkend="petals">, |
| 38891 |
write a script that plays the well-known gambling game of |
| 38892 |
<firstterm>craps</firstterm>. The script will accept bets |
| 38893 |
from one or more players, roll the dice, and keep track of |
| 38894 |
wins and losses, as well as of each player's bankroll.</para> |
| 38895 |
</listitem> |
| 38896 |
</varlistentry> |
| 38897 |
|
| 38898 |
<varlistentry> |
| 38899 |
<term><command>Tic-tac-toe</command></term> |
| 38900 |
<listitem> |
| 38901 |
<para>Write a script that plays the child's game of |
| 38902 |
<firstterm>tic-tac-toe</firstterm> against a human |
| 38903 |
player. The script will let the human choose whether |
| 38904 |
to take the first move. The script will follow |
| 38905 |
an optimal strategy, and therefore never lose. To simplify |
| 38906 |
matters, you may use ASCII graphics:</para> |
| 38907 |
|
| 38908 |
<para><programlisting> o | x | |
| 38909 |
---------- |
| 38910 |
| x | |
| 38911 |
---------- |
| 38912 |
| o | |
| 38913 |
|
| 38914 |
Your move, human (row, column)?</programlisting></para> |
| 38915 |
</listitem> |
| 38916 |
</varlistentry> |
| 38917 |
|
| 38918 |
<varlistentry> |
| 38919 |
<term><command>Alphabetizing a String</command></term> |
| 38920 |
<listitem> |
| 38921 |
<para>Alphabetize (in ASCII order) an arbitrary string |
| 38922 |
read from the command-line.</para> |
| 38923 |
</listitem> |
| 38924 |
</varlistentry> |
| 38925 |
|
| 38926 |
<varlistentry> |
| 38927 |
<term><command>Parsing</command></term> |
| 38928 |
<listitem> |
| 38929 |
<para>Parse <link |
| 38930 |
linkend="datafilesref1"><filename>/etc/passwd</filename></link>, |
| 38931 |
and output its contents in nice, easy-to-read tabular |
| 38932 |
form.</para> |
| 38933 |
</listitem> |
| 38934 |
</varlistentry> |
| 38935 |
|
| 38936 |
<varlistentry> |
| 38937 |
<term><command>Logging Logins</command></term> |
| 38938 |
<listitem> |
| 38939 |
<para>Parse <filename>/var/log/messages</filename> to |
| 38940 |
produce a nicely formatted file of user logins and login |
| 38941 |
times. The script may need to run as |
| 38942 |
<firstterm>root</firstterm>. (Hint: Search for the string |
| 38943 |
<quote>LOGIN.</quote>)</para> |
| 38944 |
</listitem> |
| 38945 |
</varlistentry> |
| 38946 |
|
| 38947 |
<varlistentry> |
| 38948 |
<term><command>Pretty-Printing a Data File</command></term> |
| 38949 |
<listitem> |
| 38950 |
<para>Certain database and spreadsheet packages use |
| 38951 |
save-files with the fields separated by commas, commonly |
| 38952 |
referred to as <firstterm>comma-separated values</firstterm> |
| 38953 |
or CSVs. Other applications often need to parse these |
| 38954 |
files.</para> <para>Given a data file with comma-separated |
| 38955 |
<link linkend="fieldref">fields</link>, of the form: |
| 38956 |
<programlisting>Jones,Bill,235 S. Williams St.,Denver,CO,80221,(303) 244-7989 |
| 38957 |
Smith,Tom,404 Polk Ave.,Los Angeles,CA,90003,(213) 879-5612 |
| 38958 |
...</programlisting> |
| 38959 |
Reformat the data and print it out to |
| 38960 |
<filename>stdout</filename> in labeled, evenly-spaced columns.</para> |
| 38961 |
</listitem> |
| 38962 |
</varlistentry> |
| 38963 |
|
| 38964 |
<varlistentry> |
| 38965 |
<term><command>Justification</command></term> |
| 38966 |
<listitem> |
| 38967 |
<para>Given ASCII text input either from |
| 38968 |
<filename>stdin</filename> or a file, adjust |
| 38969 |
the word spacing to right-justify each line to a |
| 38970 |
user-specified line-width, then send the output to |
| 38971 |
<filename>stdout</filename>.</para> |
| 38972 |
</listitem> |
| 38973 |
</varlistentry> |
| 38974 |
|
| 38975 |
<varlistentry> |
| 38976 |
<term><command>Mailing List</command></term> |
| 38977 |
<listitem> |
| 38978 |
<para>Using the <link linkend="commmail1">mail</link> command, |
| 38979 |
write a script that manages a simple mailing list. The |
| 38980 |
script automatically e-mails the monthly company newsletter, |
| 38981 |
read from a specified text file, and sends it to all the |
| 38982 |
addresses on the mailing list, which the script reads from |
| 38983 |
another specified file.</para> |
| 38984 |
</listitem> |
| 38985 |
</varlistentry> |
| 38986 |
|
| 38987 |
<varlistentry> |
| 38988 |
<term><command>Generating Passwords</command></term> |
| 38989 |
<listitem> |
| 38990 |
<para>Generate pseudorandom 8-character passwords, using |
| 38991 |
characters in the ranges [0-9], [A-Z], [a-z]. Each password |
| 38992 |
must contain at least two digits.</para> |
| 38993 |
</listitem> |
| 38994 |
</varlistentry> |
| 38995 |
|
| 38996 |
<varlistentry> |
| 38997 |
<term><command>Monitoring a User</command></term> |
| 38998 |
<listitem> |
| 38999 |
|
| 39000 |
<para>You suspect that one particular user on the network |
| 39001 |
has been abusing her privileges and possibly attempting to |
| 39002 |
hack the system. Write a script to automatically monitor |
| 39003 |
and log her activities when she's signed on. The log file |
| 39004 |
will save entries for the previous week, and delete those |
| 39005 |
entries more than seven days old.</para> |
| 39006 |
|
| 39007 |
<para>You may use <link linkend="lastref">last</link>, |
| 39008 |
<link linkend="lastlogref">lastlog</link>, and <link |
| 39009 |
linkend="lastcommref">lastcomm</link> to aid your |
| 39010 |
surveillance of the suspected fiend.</para> |
| 39011 |
|
| 39012 |
</listitem> |
| 39013 |
</varlistentry> |
| 39014 |
|
| 39015 |
<varlistentry> |
| 39016 |
<term><command>Checking for Broken Links</command></term> |
| 39017 |
<listitem> |
| 39018 |
<para>Using <link linkend="lynxref">lynx</link> with the |
| 39019 |
<option>-traversal</option> option, write a script that |
| 39020 |
checks a Web site for broken links.</para> |
| 39021 |
</listitem> |
| 39022 |
</varlistentry> |
| 39023 |
|
| 39024 |
</variablelist> |
| 39025 |
|
| 39026 |
|
| 39027 |
<variablelist id="exdifficult"> |
| 39028 |
<title><anchor id="exdifficult1">DIFFICULT</title> |
| 39029 |
|
| 39030 |
<varlistentry> |
| 39031 |
<term><command>Testing Passwords</command></term> |
| 39032 |
<listitem> |
| 39033 |
<para>Write a script to check and validate passwords. The object |
| 39034 |
is to flag <quote>weak</quote> or easily guessed password |
| 39035 |
candidates.</para> |
| 39036 |
|
| 39037 |
<para>A trial password will be input to the script as a |
| 39038 |
command-line parameter. To be considered acceptable, |
| 39039 |
a password must meet the following minimum qualifications: |
| 39040 |
|
| 39041 |
<itemizedlist> |
| 39042 |
<listitem> |
| 39043 |
<para>Minimum length of 8 characters</para> |
| 39044 |
</listitem> |
| 39045 |
|
| 39046 |
<listitem> |
| 39047 |
<para>Must contain at least one numeric character</para> |
| 39048 |
</listitem> |
| 39049 |
|
| 39050 |
<listitem> |
| 39051 |
<para>Must contain at least one of the following |
| 39052 |
non-alphabetic characters: <token>@</token>, |
| 39053 |
<token>#</token>, <token>$</token>, <token>%</token>, |
| 39054 |
<token>&</token>, <token>*</token>, <token>+</token>, |
| 39055 |
<token>-</token>, <token>=</token></para> |
| 39056 |
</listitem> |
| 39057 |
</itemizedlist></para> |
| 39058 |
|
| 39059 |
<para>Optional: |
| 39060 |
|
| 39061 |
<itemizedlist> |
| 39062 |
<listitem> |
| 39063 |
<para>Do a dictionary check on every sequence of at least |
| 39064 |
four consecutive alphabetic characters in the password under |
| 39065 |
test. This will eliminate passwords containing embedded |
| 39066 |
<quote>words</quote> found in a standard dictionary.</para> |
| 39067 |
</listitem> |
| 39068 |
|
| 39069 |
<listitem> |
| 39070 |
<para>Enable the script to check all the passwords on your |
| 39071 |
system. These do not reside in |
| 39072 |
<link |
| 39073 |
linkend="datafilesref1"><filename>/etc/passwd</filename></link>.</para> |
| 39074 |
</listitem> |
| 39075 |
</itemizedlist></para> |
| 39076 |
|
| 39077 |
<para>This exercise tests mastery of <link |
| 39078 |
linkend="regexref">Regular Expressions</link>.</para> |
| 39079 |
|
| 39080 |
</listitem> |
| 39081 |
</varlistentry> |
| 39082 |
|
| 39083 |
<varlistentry> |
| 39084 |
<term><command>Cross Reference</command></term> |
| 39085 |
<listitem> |
| 39086 |
<para>Write a script that generates a |
| 39087 |
<firstterm>cross-reference</firstterm> |
| 39088 |
(<firstterm>concordance</firstterm>) on a target file. |
| 39089 |
The output will be a listing of all word occurrences in |
| 39090 |
the target file, along with the line numbers in which |
| 39091 |
each word occurs. Traditionally, <firstterm>linked |
| 39092 |
list</firstterm> constructs would be used in such |
| 39093 |
applications. Therefore, you should investigate <link |
| 39094 |
linkend="arrayref">arrays</link> in the course of |
| 39095 |
this exercise. <xref linkend="wf"> is probably |
| 39096 |
<emphasis>not</emphasis> a good place to start.</para> |
| 39097 |
</listitem> |
| 39098 |
</varlistentry> |
| 39099 |
|
| 39100 |
<varlistentry> |
| 39101 |
<term><anchor id="newtonsqrt"><command>Square Root</command></term> |
| 39102 |
<listitem> |
| 39103 |
<para>Write a script to calculate square roots of numbers |
| 39104 |
using <firstterm>Newton's Method</firstterm>.</para> |
| 39105 |
|
| 39106 |
<para>The algorithm for this, expressed as a snippet of Bash |
| 39107 |
<link linkend="pseudocoderef">pseudo-code</link> is:</para> |
| 39108 |
|
| 39109 |
<para><programlisting># (Isaac) Newton's Method for speedy extraction |
| 39110 |
#+ of square roots. |
| 39111 |
|
| 39112 |
guess = $argument |
| 39113 |
# $argument is the number to find the square root of. |
| 39114 |
# $guess is each successive calculated "guess" -- or trial solution -- |
| 39115 |
#+ of the square root. |
| 39116 |
# Our first "guess" at a square root is the argument itself. |
| 39117 |
|
| 39118 |
oldguess = 0 |
| 39119 |
# $oldguess is the previous $guess. |
| 39120 |
|
| 39121 |
tolerance = .000001 |
| 39122 |
# To how close a tolerance we wish to calculate. |
| 39123 |
|
| 39124 |
loopcnt = 0 |
| 39125 |
# Let's keep track of how many times through the loop. |
| 39126 |
# Some arguments will require more loop iterations than others. |
| 39127 |
|
| 39128 |
|
| 39129 |
while [ ABS( $guess $oldguess ) -gt $tolerance ] |
| 39130 |
# ^^^^^^^^^^^^^^^^^^^^^^^ Fix up syntax, of course. |
| 39131 |
|
| 39132 |
# "ABS" is a (floating point) function to find the absolute value |
| 39133 |
#+ of the difference between the two terms. |
| 39134 |
# So, as long as difference between current and previous |
| 39135 |
#+ trial solution (guess) exceeds the tolerance, keep looping. |
| 39136 |
|
| 39137 |
do |
| 39138 |
oldguess = $guess # Update $oldguess to previous $guess. |
| 39139 |
|
| 39140 |
# ======================================================= |
| 39141 |
guess = ( $oldguess + ( $argument / $oldguess ) ) / 2.0 |
| 39142 |
# = 1/2 ( ($oldguess **2 + $argument) / $oldguess ) |
| 39143 |
# equivalent to: |
| 39144 |
# = 1/2 ( $oldguess + $argument / $oldguess ) |
| 39145 |
# that is, "averaging out" the trial solution and |
| 39146 |
#+ the proportion of argument deviation |
| 39147 |
#+ (in effect, splitting the error in half). |
| 39148 |
# This converges on an accurate solution |
| 39149 |
#+ with surprisingly few loop iterations . . . |
| 39150 |
#+ for arguments > $tolerance, of course. |
| 39151 |
# ======================================================= |
| 39152 |
|
| 39153 |
(( loopcnt++ )) # Update loop counter. |
| 39154 |
done</programlisting></para> |
| 39155 |
|
| 39156 |
<para>It's a simple enough recipe, and |
| 39157 |
<emphasis>seems</emphasis> at first glance easy enough to |
| 39158 |
convert into a working Bash script. The problem, though, |
| 39159 |
is that Bash has <link linkend="nofloatingpoint">no native |
| 39160 |
support for floating point numbers</link>. So, the script |
| 39161 |
writer needs to use <link linkend="bcref">bc</link> or |
| 39162 |
possibly <link linkend="awkref">awk</link> to convert the |
| 39163 |
numbers and do the calculations. It could get rather messy |
| 39164 |
. . .</para> |
| 39165 |
|
| 39166 |
</listitem> |
| 39167 |
</varlistentry> |
| 39168 |
|
| 39169 |
|
| 39170 |
|
| 39171 |
|
| 39172 |
<varlistentry> |
| 39173 |
<term><command>Logging File Accesses</command></term> |
| 39174 |
<listitem> |
| 39175 |
<para>Log all accesses to the files in <filename |
| 39176 |
class="directory">/etc</filename> during the course of |
| 39177 |
a single day. This information should include the filename, |
| 39178 |
user name, and access time. If any alterations to the |
| 39179 |
files take place, that will be flagged. Write this data |
| 39180 |
as tabular (tab-separated) formatted records in a logfile.</para> |
| 39181 |
</listitem> |
| 39182 |
</varlistentry> |
| 39183 |
|
| 39184 |
<varlistentry> |
| 39185 |
<term><command>Monitoring Processes</command></term> |
| 39186 |
<listitem> |
| 39187 |
<para>Write a script to continually monitor all running |
| 39188 |
processes and to keep track of how many child processes each |
| 39189 |
parent spawns. If a process spawns more than five children, |
| 39190 |
then the script sends an e-mail to the system administrator |
| 39191 |
(or <firstterm>root</firstterm>) with all relevant |
| 39192 |
information, including the time, PID of the parent, PIDs |
| 39193 |
of the children, etc. The script appends a report to a log |
| 39194 |
file every ten minutes. </para> |
| 39195 |
</listitem> |
| 39196 |
</varlistentry> |
| 39197 |
|
| 39198 |
<varlistentry> |
| 39199 |
<term><command>Strip Comments</command></term> |
| 39200 |
<listitem> |
| 39201 |
<para>Strip all comments from a shell script whose name |
| 39202 |
is specified on the command-line. Note that the initial |
| 39203 |
<link linkend="shabangref">#! line</link> must not be |
| 39204 |
stripped out.</para> |
| 39205 |
</listitem> |
| 39206 |
</varlistentry> |
| 39207 |
|
| 39208 |
<varlistentry> |
| 39209 |
<term><command>Strip HTML Tags</command></term> |
| 39210 |
<listitem> |
| 39211 |
<para>Strip all the HTML tags from a specified HTML file, then |
| 39212 |
reformat it into lines between 60 and 75 characters |
| 39213 |
in length. Reset paragraph and block spacing, as |
| 39214 |
appropriate, and convert HTML tables to their approximate |
| 39215 |
text equivalent.</para> |
| 39216 |
</listitem> |
| 39217 |
</varlistentry> |
| 39218 |
|
| 39219 |
<varlistentry> |
| 39220 |
<term><command>XML Conversion</command></term> |
| 39221 |
<listitem> |
| 39222 |
<para>Convert an XML file to both HTML and text format.</para> |
| 39223 |
<para>Optional: A script that converts Docbook/SGML to XML.</para> |
| 39224 |
</listitem> |
| 39225 |
</varlistentry> |
| 39226 |
|
| 39227 |
<varlistentry> |
| 39228 |
<term><anchor id="cspammers"><command>Chasing Spammers</command></term> |
| 39229 |
<listitem> |
| 39230 |
|
| 39231 |
<para> Write a script that analyzes a spam e-mail by doing |
| 39232 |
DNS lookups on the IP addresses in the headers to identify |
| 39233 |
the relay hosts as well as the originating ISP. The |
| 39234 |
script will forward the unaltered spam message to the |
| 39235 |
responsible ISPs. Of course, it will be necessary to |
| 39236 |
filter out <emphasis>your own ISP's IP address</emphasis>, |
| 39237 |
so you don't end up complaining about yourself.</para> |
| 39238 |
|
| 39239 |
<para>As necessary, use the appropriate <link |
| 39240 |
linkend="communinfo1">network analysis commands</link>.</para> |
| 39241 |
|
| 39242 |
<para>For some ideas, see <xref linkend="isspammer"> and <xref |
| 39243 |
linkend="isspammer2">.</para> |
| 39244 |
|
| 39245 |
<para>Optional: Write a script that searches through a list of |
| 39246 |
e-mail messages and deletes the spam according to specified |
| 39247 |
filters.</para> |
| 39248 |
|
| 39249 |
</listitem> |
| 39250 |
</varlistentry> |
| 39251 |
|
| 39252 |
<varlistentry> |
| 39253 |
<term><command>Creating man pages</command></term> |
| 39254 |
<listitem> |
| 39255 |
|
| 39256 |
<para>Write a script that automates the process of creating |
| 39257 |
<link linkend="manref">man pages</link>.</para> |
| 39258 |
|
| 39259 |
<para>Given a text file which contains information to be |
| 39260 |
formatted into a <firstterm>man page</firstterm>, the |
| 39261 |
script will read the file, then invoke the appropriate |
| 39262 |
<link linkend="groffref">groff</link> commands to |
| 39263 |
output the corresponding <firstterm>man page</firstterm> |
| 39264 |
to <filename>stdout</filename>. The text file contains |
| 39265 |
blocks of information under the standard <firstterm>man |
| 39266 |
page</firstterm> headings, i.e., NAME, SYNOPSIS, |
| 39267 |
DESCRIPTION, etc.</para> |
| 39268 |
|
| 39269 |
<para><xref linkend="maned"> is an instructive first step.</para> |
| 39270 |
|
| 39271 |
</listitem> |
| 39272 |
</varlistentry> |
| 39273 |
|
| 39274 |
|
| 39275 |
<varlistentry> |
| 39276 |
<term><command>Hex Dump</command></term> |
| 39277 |
<listitem> |
| 39278 |
<para>Do a hex(adecimal) dump on a binary file |
| 39279 |
specified as an argument to the script. The output should |
| 39280 |
be in neat tabular <link linkend="fieldref">fields</link>, |
| 39281 |
with the first field showing the address, each of the |
| 39282 |
next 8 fields a 4-byte hex number, and the final field |
| 39283 |
the ASCII equivalent of the previous 8 fields.</para> |
| 39284 |
<para>The obvious followup to this is to extend the hex |
| 39285 |
dump script into a disassembler. Using a lookup table, |
| 39286 |
or some other clever gimmick, convert the hex values into |
| 39287 |
80x86 op codes.</para> |
| 39288 |
</listitem> |
| 39289 |
</varlistentry> |
| 39290 |
|
| 39291 |
<varlistentry> |
| 39292 |
<term><command>Emulating a Shift Register</command></term> |
| 39293 |
<listitem> |
| 39294 |
<para>Using <xref linkend="stackex"> as an inspiration, |
| 39295 |
write a script that emulates a 64-bit shift register as |
| 39296 |
an <link linkend="arrayref">array</link>. Implement |
| 39297 |
functions to <firstterm>load</firstterm> the register, |
| 39298 |
<firstterm>shift left</firstterm>, <firstterm>shift |
| 39299 |
right</firstterm>, and <firstterm>rotate</firstterm> |
| 39300 |
it. Finally, write a function that interprets the register |
| 39301 |
contents as eight 8-bit ASCII characters.</para> |
| 39302 |
</listitem> |
| 39303 |
</varlistentry> |
| 39304 |
|
| 39305 |
<varlistentry> |
| 39306 |
<term><command>Calculating Determinants</command></term> |
| 39307 |
<listitem> |
| 39308 |
<para>Write a script that calculates |
| 39309 |
determinants |
| 39310 |
<footnote> |
| 39311 |
<para>For all you clever types who failed intermediate algebra, |
| 39312 |
a <firstterm>determinant</firstterm> is a numerical value |
| 39313 |
associated with a multidimensional |
| 39314 |
<firstterm>matrix</firstterm> (<link |
| 39315 |
linkend="arrayref">array</link> of numbers). |
| 39316 |
<programlisting>For the simple case of a 2 x 2 determinant: |
| 39317 |
|
| 39318 |
|a b| |
| 39319 |
|b a| |
| 39320 |
|
| 39321 |
The solution is a*a - b*b, where "a" and "b" represent numbers.</programlisting> |
| 39322 |
</para></footnote> |
| 39323 |
|
| 39324 |
by <link |
| 39325 |
linkend="recursionref0">recursively</link> expanding the |
| 39326 |
<firstterm>minors</firstterm>. Use a 4 x 4 determinant as |
| 39327 |
a test case.</para> |
| 39328 |
</listitem> |
| 39329 |
</varlistentry> |
| 39330 |
|
| 39331 |
<varlistentry> |
| 39332 |
<term><command>Hidden Words</command></term> |
| 39333 |
<listitem> |
| 39334 |
<para>Write a <quote>word-find</quote> puzzle generator, |
| 39335 |
a script that hides 10 input words in a 10 x 10 array |
| 39336 |
of random letters. The words may be hidden across, down, |
| 39337 |
or diagonally.</para> |
| 39338 |
<para>Optional: Write a script that <emphasis>solves</emphasis> |
| 39339 |
word-find puzzles. To keep this from becoming too difficult, |
| 39340 |
the solution script will find only horizontal and vertical |
| 39341 |
words. (Hint: Treat each row and column as a string, and |
| 39342 |
search for substrings.)</para> |
| 39343 |
</listitem> |
| 39344 |
</varlistentry> |
| 39345 |
|
| 39346 |
<varlistentry> |
| 39347 |
<term><command>Anagramming</command></term> |
| 39348 |
<listitem> |
| 39349 |
<para> Anagram 4-letter input. For example, the |
| 39350 |
anagrams of <emphasis>word</emphasis> are: |
| 39351 |
<emphasis>do or rod row word</emphasis>. You may use |
| 39352 |
<filename>/usr/share/dict/linux.words</filename> as the |
| 39353 |
reference list.</para> |
| 39354 |
</listitem> |
| 39355 |
</varlistentry> |
| 39356 |
|
| 39357 |
<varlistentry> |
| 39358 |
<term><command>Word Ladders</command></term> |
| 39359 |
<listitem> |
| 39360 |
<para>A <quote>word ladder</quote> is a sequence of words, |
| 39361 |
with each successive word in the sequence differing from |
| 39362 |
the previous one by a single letter.</para> |
| 39363 |
|
| 39364 |
<para>For example, to <quote>ladder</quote> from |
| 39365 |
<emphasis>mark</emphasis> to |
| 39366 |
<emphasis>vase</emphasis>:</para> |
| 39367 |
|
| 39368 |
<para> |
| 39369 |
<programlisting> |
| 39370 |
mark --> park --> part --> past --> vast --> vase |
| 39371 |
^ ^ ^ ^ ^</programlisting> |
| 39372 |
</para> |
| 39373 |
|
| 39374 |
<para>Write a script that solves word ladder puzzles. Given |
| 39375 |
a starting and an ending word, the script will list all |
| 39376 |
intermediate steps in the <quote>ladder.</quote> Note |
| 39377 |
that <emphasis>all</emphasis> words in the sequence must |
| 39378 |
be legitimate dictionary words.</para> |
| 39379 |
</listitem> |
| 39380 |
</varlistentry> |
| 39381 |
|
| 39382 |
<varlistentry> |
| 39383 |
<term><command>Fog Index</command></term> |
| 39384 |
<listitem> |
| 39385 |
|
| 39386 |
<para>The <quote>fog index</quote> of a passage of text |
| 39387 |
estimates its reading difficulty, as a number corresponding |
| 39388 |
roughly to a school grade level. For example, a passage |
| 39389 |
with a fog index of 12 should be comprehensible to anyone |
| 39390 |
with 12 years of schooling.</para> |
| 39391 |
|
| 39392 |
<para>The Gunning version of the fog index uses the following |
| 39393 |
algorithm.</para> |
| 39394 |
|
| 39395 |
<orderedlist> |
| 39396 |
|
| 39397 |
<listitem><para>Choose a section of the text at least |
| 39398 |
100 words in length.</para></listitem> |
| 39399 |
|
| 39400 |
<listitem><para>Count the number of sentences (a portion of |
| 39401 |
a sentence truncated by the boundary of the text section |
| 39402 |
counts as one).</para></listitem> |
| 39403 |
|
| 39404 |
<listitem> |
| 39405 |
<para>Find the average number of words per |
| 39406 |
sentence.</para> |
| 39407 |
|
| 39408 |
<para>AVE_WDS_SEN = TOTAL_WORDS / SENTENCES</para> |
| 39409 |
</listitem> |
| 39410 |
|
| 39411 |
<listitem> |
| 39412 |
<para>Count the number of <quote>difficult</quote> |
| 39413 |
words in the segment -- those containing at least |
| 39414 |
3 syllables. Divide this quantity by total words to |
| 39415 |
get the proportion of difficult words.</para> |
| 39416 |
|
| 39417 |
<para>PRO_DIFF_WORDS = LONG_WORDS / TOTAL_WORDS</para> |
| 39418 |
</listitem> |
| 39419 |
|
| 39420 |
<listitem> |
| 39421 |
<para>The Gunning fog index is the sum of the above two |
| 39422 |
quantities, multiplied by 0.4, then rounded to the |
| 39423 |
nearest integer.</para> |
| 39424 |
|
| 39425 |
<para>G_FOG_INDEX = int ( 0.4 * ( AVE_WDS_SEN + PRO_DIFF_WORDS ) )</para> |
| 39426 |
</listitem> |
| 39427 |
|
| 39428 |
</orderedlist> |
| 39429 |
|
| 39430 |
<para>Step 4 is by far the most difficult portion of the |
| 39431 |
exercise. There exist various algorithms for estimating |
| 39432 |
the syllable count of a word. A rule-of-thumb formula |
| 39433 |
might consider the number of letters in a word and the |
| 39434 |
vowel-consonant mix.</para> |
| 39435 |
|
| 39436 |
<para>A strict interpretation of the Gunning fog index does |
| 39437 |
not count compound words and proper nouns as |
| 39438 |
<quote>difficult</quote> words, but this would enormously |
| 39439 |
complicate the script.</para> |
| 39440 |
|
| 39441 |
</listitem> |
| 39442 |
</varlistentry> |
| 39443 |
|
| 39444 |
<varlistentry> |
| 39445 |
<term><command>Calculating PI using Buffon's Needle</command></term> |
| 39446 |
<listitem> |
| 39447 |
|
| 39448 |
<para>The Eighteenth Century French mathematician de Buffon |
| 39449 |
came up with a novel experiment. Repeatedly drop a needle |
| 39450 |
of length <replaceable>n</replaceable> onto a wooden floor |
| 39451 |
composed of long and narrow parallel boards. The cracks |
| 39452 |
separating the equal-width floorboards are a fixed distance |
| 39453 |
<replaceable>d</replaceable> apart. Keep track of the |
| 39454 |
total drops and the number of times the needle intersects |
| 39455 |
a crack on the floor. The ratio of these two quantities |
| 39456 |
turns out to be a fractional multiple of PI.</para> |
| 39457 |
|
| 39458 |
<para>In the spirit of <xref linkend="cannon">, write a |
| 39459 |
script that runs a Monte Carlo simulation of |
| 39460 |
<firstterm>Buffon's Needle</firstterm>. To simplify matters, |
| 39461 |
set the needle length equal to the distance between the |
| 39462 |
cracks, <parameter>n = d</parameter>.</para> |
| 39463 |
|
| 39464 |
<para>Hint: there are actually two critical variables: |
| 39465 |
the distance from the center of the needle to the nearest |
| 39466 |
crack, and the inclination angle of the needle to that crack. |
| 39467 |
You may use <link linkend="bcref">bc</link> to handle |
| 39468 |
the calculations.</para> |
| 39469 |
|
| 39470 |
</listitem> |
| 39471 |
</varlistentry> |
| 39472 |
|
| 39473 |
<varlistentry> |
| 39474 |
<term><command>Playfair Cipher</command></term> |
| 39475 |
<listitem> |
| 39476 |
|
| 39477 |
<para>Implement the Playfair (Wheatstone) Cipher in a |
| 39478 |
script.</para> |
| 39479 |
|
| 39480 |
<para>The Playfair Cipher encrypts text by substitution |
| 39481 |
of <firstterm>digrams</firstterm> (2-letter groupings). |
| 39482 |
It is traditional to use a 5 x 5 letter scrambled-alphabet |
| 39483 |
<firstterm>key square</firstterm> for the encryption and |
| 39484 |
decryption.</para> |
| 39485 |
|
| 39486 |
<para> |
| 39487 |
<programlisting> C O D E S |
| 39488 |
A B F G H |
| 39489 |
I K L M N |
| 39490 |
P Q R T U |
| 39491 |
V W X Y Z |
| 39492 |
|
| 39493 |
Each letter of the alphabet appears once, except "I" also represents |
| 39494 |
"J". The arbitrarily chosen key word, "CODES" comes first, then all |
| 39495 |
the rest of the alphabet, in order from left to right, skipping letters |
| 39496 |
already used. |
| 39497 |
|
| 39498 |
To encrypt, separate the plaintext message into digrams (2-letter |
| 39499 |
groups). If a group has two identical letters, delete the second, and |
| 39500 |
form a new group. If there is a single letter left over at the end, |
| 39501 |
insert a "null" character, typically an "X." |
| 39502 |
|
| 39503 |
THIS IS A TOP SECRET MESSAGE |
| 39504 |
|
| 39505 |
TH IS IS AT OP SE CR ET ME SA GE |
| 39506 |
|
| 39507 |
|
| 39508 |
|
| 39509 |
For each digram, there are three possibilities. |
| 39510 |
----------------------------------------------- |
| 39511 |
|
| 39512 |
1) Both letters will be on the same row of the key square: |
| 39513 |
For each letter, substitute the one immediately to the right, in that |
| 39514 |
row. If necessary, wrap around left to the beginning of the row. |
| 39515 |
|
| 39516 |
or |
| 39517 |
|
| 39518 |
2) Both letters will be in the same column of the key square: |
| 39519 |
For each letter, substitute the one immediately below it, in that |
| 39520 |
row. If necessary, wrap around to the top of the column. |
| 39521 |
|
| 39522 |
or |
| 39523 |
|
| 39524 |
3) Both letters will form the corners of a rectangle within the key square: |
| 39525 |
For each letter, substitute the one on the other corner the rectangle |
| 39526 |
which lies on the same row. |
| 39527 |
|
| 39528 |
|
| 39529 |
The "TH" digram falls under case #3. |
| 39530 |
G H |
| 39531 |
M N |
| 39532 |
T U (Rectangle with "T" and "H" at corners) |
| 39533 |
|
| 39534 |
T --> U |
| 39535 |
H --> G |
| 39536 |
|
| 39537 |
|
| 39538 |
The "SE" digram falls under case #1. |
| 39539 |
C O D E S (Row containing "S" and "E") |
| 39540 |
|
| 39541 |
S --> C (wraps around left to beginning of row) |
| 39542 |
E --> S |
| 39543 |
|
| 39544 |
========================================================================= |
| 39545 |
|
| 39546 |
To decrypt encrypted text, reverse the above procedure under cases #1 |
| 39547 |
and #2 (move in opposite direction for substitution). Under case #3, |
| 39548 |
just take the remaining two corners of the rectangle. |
| 39549 |
|
| 39550 |
|
| 39551 |
Helen Fouche Gaines' classic work, ELEMENTARY CRYPTANALYSIS (1939), gives a |
| 39552 |
fairly detailed description of the Playfair Cipher and its solution methods.</programlisting> |
| 39553 |
</para> |
| 39554 |
|
| 39555 |
<para>This script will have three main sections</para> |
| 39556 |
|
| 39557 |
<orderedlist id="playfairexref" numeration="upperroman"> |
| 39558 |
<listitem><para>Generating the <firstterm>key square</firstterm>, |
| 39559 |
based on a user-input keyword.</para></listitem> |
| 39560 |
<listitem><para>Encrypting a <firstterm>plaintext</firstterm> |
| 39561 |
message.</para></listitem> |
| 39562 |
<listitem><para>Decrypting encrypted |
| 39563 |
text.</para></listitem> |
| 39564 |
</orderedlist> |
| 39565 |
|
| 39566 |
<para>The script will make extensive use of <link |
| 39567 |
linkend="arrayref">arrays</link> and <link |
| 39568 |
linkend="functionref">functions</link>. |
| 39569 |
You may use <xref linkend="gronsfeld"> as an |
| 39570 |
inspiration.</para> |
| 39571 |
|
| 39572 |
</listitem> |
| 39573 |
</varlistentry> |
| 39574 |
|
| 39575 |
</variablelist> |
| 39576 |
|
| 39577 |
|
| 39578 |
<para>--</para> |
| 39579 |
<para>Please do not send the author your solutions to these |
| 39580 |
exercises. There are more appropriate ways to impress him with |
| 39581 |
your cleverness, such as submitting bugfixes and suggestions |
| 39582 |
for improving the book.</para> |
| 39583 |
|
| 39584 |
|
| 39585 |
</sect1> |
| 39586 |
<!-- End Writing Scripts section --> |
| 39587 |
|
| 39588 |
</appendix> |
| 39589 |
<!-- End Exercises appendix --> |
| 39590 |
|
| 39591 |
|
| 39592 |
|
| 39593 |
<appendix id="revisionhistory"> |
| 39594 |
<title>Revision History</title> |
| 39595 |
|
| 39596 |
<synopsis> |
| 39597 |
This document first appeared as a 60-page HOWTO in the late spring |
| 39598 |
of 2000. Since then, it has gone through quite a number of updates |
| 39599 |
and revisions. This book could not have been written without the |
| 39600 |
assistance of the Linux community, and especially of the volunteers |
| 39601 |
of the <ulink url="http://www.tldp.org">Linux Documentation Project</ulink>. |
| 39602 |
</synopsis> |
| 39603 |
|
| 39604 |
<para>Here is the e-mail to the LDP requesting permission to submit |
| 39605 |
version 0.1.</para> |
| 39606 |
|
| 39607 |
<para><programlisting>From [email protected] Sat Jun 10 09:05:33 2000 -0700 |
| 39608 |
Date: Sat, 10 Jun 2000 09:05:28 -0700 (MST) |
| 39609 |
From: "M. Leo Cooper" <[email protected]> |
| 39610 |
X-Sender: thegrendel@localhost |
| 39611 |
To: [email protected] |
| 39612 |
Subject: Permission to submit HOWTO |
| 39613 |
|
| 39614 |
Dear HOWTO Coordinator, |
| 39615 |
|
| 39616 |
I am working on and would like to submit to the LDP a HOWTO on the subject |
| 39617 |
of "Bash Scripting" (shell scripting, using 'bash'). As it happens, |
| 39618 |
I have been writing this document, off and on, for about the last eight |
| 39619 |
months or so, and I could produce a first draft in ASCII text format in |
| 39620 |
a matter of just a few more days. |
| 39621 |
|
| 39622 |
I began writing this out of frustration at being unable to find a |
| 39623 |
decent book on shell scripting. I managed to locate some pretty good |
| 39624 |
articles on various aspects of scripting, but nothing like a complete, |
| 39625 |
beginning-to-end tutorial. Well, in keeping with my philosophy, if all |
| 39626 |
else fails, do it yourself. |
| 39627 |
|
| 39628 |
As it stands, this proposed "Bash-Scripting HOWTO" would serve as a |
| 39629 |
combination tutorial and reference, with the heavier emphasis on the |
| 39630 |
tutorial. It assumes Linux experience, but only a very basic level |
| 39631 |
of programming skills. Interspersed with the text are 79 illustrative |
| 39632 |
example scripts of varying complexity, all liberally commented. There |
| 39633 |
are even exercises for the reader. |
| 39634 |
|
| 39635 |
At this stage, I'm up to 18,000+ words (124k), and that's over 50 pages of |
| 39636 |
text (whew!). |
| 39637 |
|
| 39638 |
|
| 39639 |
I haven't mentioned that I've previously authored an LDP HOWTO, the |
| 39640 |
"Software-Building HOWTO", which I wrote in Linuxdoc/SGML. I don't know |
| 39641 |
if I could handle Docbook/SGML, and I'm glad you have volunteers to do |
| 39642 |
the conversion. You people seem to have gotten on a more organized basis |
| 39643 |
these last few months. Working with Greg Hankins and Tim Bynum was nice, |
| 39644 |
but a professional team is even nicer. |
| 39645 |
|
| 39646 |
Anyhow, please advise. |
| 39647 |
|
| 39648 |
|
| 39649 |
Mendel Cooper |
| 39650 |
[email protected]</programlisting></para> |
| 39651 |
|
| 39652 |
|
| 39653 |
<table frame=none> |
| 39654 |
<title>Revision History</title> |
| 39655 |
<tgroup cols="3" colsep=0 rowsep=0> |
| 39656 |
<thead> |
| 39657 |
<row> |
| 39658 |
<entry>Release</entry> |
| 39659 |
<entry>Date</entry> |
| 39660 |
<entry>Comments</entry> |
| 39661 |
</row> |
| 39662 |
</thead> |
| 39663 |
<tbody> |
| 39664 |
<row> |
| 39665 |
<entry>0.1</entry> |
| 39666 |
<entry>14 Jun 2000</entry> |
| 39667 |
<entry>Initial release.</entry> |
| 39668 |
</row> |
| 39669 |
<row> |
| 39670 |
<entry><option>0.2</option></entry> |
| 39671 |
<entry>30 Oct 2000</entry> |
| 39672 |
<entry>Bugs fixed, plus much additional material and more |
| 39673 |
example scripts.</entry> |
| 39674 |
</row> |
| 39675 |
<row> |
| 39676 |
<entry><option>0.3</option></entry> |
| 39677 |
<entry>12 Feb 2001</entry> |
| 39678 |
<entry>Major update.</entry> |
| 39679 |
</row> |
| 39680 |
<row> |
| 39681 |
<entry><option>0.4</option></entry> |
| 39682 |
<entry>08 Jul 2001</entry> |
| 39683 |
<entry>Complete revision and expansion of the book.</entry> |
| 39684 |
</row> |
| 39685 |
<row> |
| 39686 |
<entry><option>0.5</option></entry> |
| 39687 |
<entry>03 Sep 2001</entry> |
| 39688 |
<entry>Major update: Bugfixes, material added, |
| 39689 |
sections reorganized.</entry> |
| 39690 |
</row> |
| 39691 |
<row> |
| 39692 |
<entry><option>1.0</option></entry> |
| 39693 |
<entry>14 Oct 2001</entry> |
| 39694 |
<entry>Stable release: Bugfixes, reorganization, material |
| 39695 |
added.</entry> |
| 39696 |
</row> |
| 39697 |
<row> |
| 39698 |
<entry><option>1.1</option></entry> |
| 39699 |
<entry>06 Jan 2002</entry> |
| 39700 |
<entry>Bugfixes, material and scripts added.</entry> |
| 39701 |
</row> |
| 39702 |
<row> |
| 39703 |
<entry><option>1.2</option></entry> |
| 39704 |
<entry>31 Mar 2002</entry> |
| 39705 |
<entry>Bugfixes, material and scripts added.</entry> |
| 39706 |
</row> |
| 39707 |
<row> |
| 39708 |
<entry><option>1.3</option></entry> |
| 39709 |
<entry>02 Jun 2002</entry> |
| 39710 |
<entry>TANGERINE release: A few bugfixes, much more material and |
| 39711 |
scripts added.</entry> |
| 39712 |
</row> |
| 39713 |
<row> |
| 39714 |
<entry><option>1.4</option></entry> |
| 39715 |
<entry>16 Jun 2002</entry> |
| 39716 |
<entry>MANGO release: A number of typos fixed, more |
| 39717 |
material and scripts.</entry> |
| 39718 |
</row> |
| 39719 |
<row> |
| 39720 |
<entry><option>1.5</option></entry> |
| 39721 |
<entry>13 Jul 2002</entry> |
| 39722 |
<entry>PAPAYA release: A few bugfixes, much more material and |
| 39723 |
scripts added.</entry> |
| 39724 |
</row> |
| 39725 |
<row> |
| 39726 |
<entry><option>1.6</option></entry> |
| 39727 |
<entry>29 Sep 2002</entry> |
| 39728 |
<entry>POMEGRANATE release: Bugfixes, more material, |
| 39729 |
one more script.</entry> |
| 39730 |
</row> |
| 39731 |
<row> |
| 39732 |
<entry><option>1.7</option></entry> |
| 39733 |
<entry>05 Jan 2003</entry> |
| 39734 |
<entry>COCONUT release: A couple of bugfixes, more material, |
| 39735 |
one more script.</entry> |
| 39736 |
</row> |
| 39737 |
<row> |
| 39738 |
<entry><option>1.8</option></entry> |
| 39739 |
<entry>10 May 2003</entry> |
| 39740 |
<entry>BREADFRUIT release: A number of bugfixes, more scripts and |
| 39741 |
material.</entry> |
| 39742 |
</row> |
| 39743 |
<row> |
| 39744 |
<entry><option>1.9</option></entry> |
| 39745 |
<entry>21 Jun 2003</entry> |
| 39746 |
<entry>PERSIMMON release: Bugfixes, and more material.</entry> |
| 39747 |
</row> |
| 39748 |
<row> |
| 39749 |
<entry><option>2.0</option></entry> |
| 39750 |
<entry>24 Aug 2003</entry> |
| 39751 |
<entry>GOOSEBERRY release: Major update.</entry> |
| 39752 |
</row> |
| 39753 |
<row> |
| 39754 |
<entry><option>2.1</option></entry> |
| 39755 |
<entry>14 Sep 2003</entry> |
| 39756 |
<entry>HUCKLEBERRY release: Bugfixes, and more material.</entry> |
| 39757 |
</row> |
| 39758 |
<row> |
| 39759 |
<entry><option>2.2</option></entry> |
| 39760 |
<entry>31 Oct 2003</entry> |
| 39761 |
<entry>CRANBERRY release: Major update.</entry> |
| 39762 |
</row> |
| 39763 |
<row> |
| 39764 |
<entry><option>2.3</option></entry> |
| 39765 |
<entry>03 Jan 2004</entry> |
| 39766 |
<entry>STRAWBERRY release: Bugfixes and more material.</entry> |
| 39767 |
</row> |
| 39768 |
<row> |
| 39769 |
<entry><option>2.4</option></entry> |
| 39770 |
<entry>25 Jan 2004</entry> |
| 39771 |
<entry>MUSKMELON release: Bugfixes.</entry> |
| 39772 |
</row> |
| 39773 |
<row> |
| 39774 |
<entry><option>2.5</option></entry> |
| 39775 |
<entry>15 Feb 2004</entry> |
| 39776 |
<entry>STARFRUIT release: Bugfixes and more material.</entry> |
| 39777 |
</row> |
| 39778 |
<row> |
| 39779 |
<entry><option>2.6</option></entry> |
| 39780 |
<entry>15 Mar 2004</entry> |
| 39781 |
<entry>SALAL release: Minor update.</entry> |
| 39782 |
</row> |
| 39783 |
<row> |
| 39784 |
<entry><option>2.7</option></entry> |
| 39785 |
<entry>18 Apr 2004</entry> |
| 39786 |
<entry>MULBERRY release: Minor update.</entry> |
| 39787 |
</row> |
| 39788 |
<row> |
| 39789 |
<entry><option>2.8</option></entry> |
| 39790 |
<entry>11 Jul 2004</entry> |
| 39791 |
<entry>ELDERBERRY release: Minor update.</entry> |
| 39792 |
</row> |
| 39793 |
<row> |
| 39794 |
<entry><option>3.0</option></entry> |
| 39795 |
<entry>03 Oct 2004</entry> |
| 39796 |
<entry>LOGANBERRY release: Major update.</entry> |
| 39797 |
</row> |
| 39798 |
<row> |
| 39799 |
<entry><option>3.1</option></entry> |
| 39800 |
<entry>14 Nov 2004</entry> |
| 39801 |
<entry>BAYBERRY release: Bugfix update.</entry> |
| 39802 |
</row> |
| 39803 |
<row> |
| 39804 |
<entry><option>3.2</option></entry> |
| 39805 |
<entry>06 Feb 2005</entry> |
| 39806 |
<entry>BLUEBERRY release: Minor update.</entry> |
| 39807 |
</row> |
| 39808 |
<row> |
| 39809 |
<entry><option>3.3</option></entry> |
| 39810 |
<entry>20 Mar 2005</entry> |
| 39811 |
<entry>RASPBERRY release: Bugfixes, much material added.</entry> |
| 39812 |
</row> |
| 39813 |
<row> |
| 39814 |
<entry><option>3.4</option></entry> |
| 39815 |
<entry>08 May 2005</entry> |
| 39816 |
<entry>TEABERRY release: Bugfixes, stylistic revisions.</entry> |
| 39817 |
</row> |
| 39818 |
<row> |
| 39819 |
<entry><option>3.5</option></entry> |
| 39820 |
<entry>05 Jun 2005</entry> |
| 39821 |
<entry>BOXBERRY release: Bugfixes, some material added.</entry> |
| 39822 |
</row> |
| 39823 |
<row> |
| 39824 |
<entry><option>3.6</option></entry> |
| 39825 |
<entry>28 Aug 2005</entry> |
| 39826 |
<entry>POKEBERRY release: Bugfixes, some material added.</entry> |
| 39827 |
</row> |
| 39828 |
<row> |
| 39829 |
<entry><option>3.7</option></entry> |
| 39830 |
<entry>23 Oct 2005</entry> |
| 39831 |
<entry>WHORTLEBERRY release: Bugfixes, some material added.</entry> |
| 39832 |
</row> |
| 39833 |
<row> |
| 39834 |
<entry><option>3.8</option></entry> |
| 39835 |
<entry>26 Feb 2006</entry> |
| 39836 |
<entry>BLAEBERRY release: Bugfixes, some material added.</entry> |
| 39837 |
</row> |
| 39838 |
<row> |
| 39839 |
<entry><option>3.9</option></entry> |
| 39840 |
<entry>15 May 2006</entry> |
| 39841 |
<entry>SPICEBERRY release: Bugfixes, some material added.</entry> |
| 39842 |
</row> |
| 39843 |
<row> |
| 39844 |
<entry><option>4.0</option></entry> |
| 39845 |
<entry>18 Jun 2006</entry> |
| 39846 |
<entry>WINTERBERRY release: Major reorganization.</entry> |
| 39847 |
</row> |
| 39848 |
<row> |
| 39849 |
<entry><option>4.1</option></entry> |
| 39850 |
<entry>08 Oct 2006</entry> |
| 39851 |
<entry>WAXBERRY release: Minor update.</entry> |
| 39852 |
</row> |
| 39853 |
<row> |
| 39854 |
<entry><option>4.2</option></entry> |
| 39855 |
<entry>10 Dec 2006</entry> |
| 39856 |
<entry>SPARKLEBERRY release: Important update.</entry> |
| 39857 |
</row> |
| 39858 |
<row> |
| 39859 |
<entry><option>4.3</option></entry> |
| 39860 |
<entry>29 Apr 2007</entry> |
| 39861 |
<entry>INKBERRY release: Bugfixes, material added.</entry> |
| 39862 |
</row> |
| 39863 |
<row> |
| 39864 |
<entry><option>5.0</option></entry> |
| 39865 |
<entry>24 Jun 2007</entry> |
| 39866 |
<entry>SERVICEBERRY release: Major update.</entry> |
| 39867 |
</row> |
| 39868 |
<row> |
| 39869 |
<entry><option>5.1</option></entry> |
| 39870 |
<entry>10 Nov 2007</entry> |
| 39871 |
<entry>LINGONBERRY release: Minor update.</entry> |
| 39872 |
</row> |
| 39873 |
<row> |
| 39874 |
<entry><option>5.2</option></entry> |
| 39875 |
<entry>16 Mar 2008</entry> |
| 39876 |
<entry>SILVERBERRY release: Important update.</entry> |
| 39877 |
</row> |
| 39878 |
<row> |
| 39879 |
<entry><option>5.3</option></entry> |
| 39880 |
<entry>11 May 2008</entry> |
| 39881 |
<entry>GOLDENBERRY release: Minor update.</entry> |
| 39882 |
</row> |
| 39883 |
<row> |
| 39884 |
<entry><option>5.4</option></entry> |
| 39885 |
<entry>21 Jul 2008</entry> |
| 39886 |
<entry>ANGLEBERRY release: Major update.</entry> |
| 39887 |
</row> |
| 39888 |
<row> |
| 39889 |
<entry><option>5.5</option></entry> |
| 39890 |
<entry>23 Nov 2008</entry> |
| 39891 |
<entry>FARKLEBERRY release: Minor update.</entry> |
| 39892 |
</row> |
| 39893 |
<row> |
| 39894 |
<entry><option>5.6</option></entry> |
| 39895 |
<entry>26 Jan 2009</entry> |
| 39896 |
<entry>WORCESTERBERRY release: Minor update.</entry> |
| 39897 |
</row> |
| 39898 |
<row> |
| 39899 |
<entry><option>6.0</option></entry> |
| 39900 |
<entry>23 Mar 2009</entry> |
| 39901 |
<entry>THIMBLEBERRY release: Major update.</entry> |
| 39902 |
</row> |
| 39903 |
<row> |
| 39904 |
<entry><option>6.1</option></entry> |
| 39905 |
<entry>30 Sep 2009</entry> |
| 39906 |
<entry>BUFFALOBERRY release: Minor update.</entry> |
| 39907 |
</row> |
| 39908 |
<row> |
| 39909 |
<entry><option>6.2</option></entry> |
| 39910 |
<entry>17 Mar 2010</entry> |
| 39911 |
<entry>ROWANBERRY release: Minor update.</entry> |
| 39912 |
</row> |
| 39913 |
<row> |
| 39914 |
<entry><option>6.3</option></entry> |
| 39915 |
<entry>30 Apr 2011</entry> |
| 39916 |
<entry>SWOZZLEBERRY release: Major update.</entry> |
| 39917 |
</row> |
| 39918 |
<row> |
| 39919 |
<entry><option>6.4</option></entry> |
| 39920 |
<entry>30 Aug 2011</entry> |
| 39921 |
<entry>VORTEXBERRY release: Minor update.</entry> |
| 39922 |
</row> |
| 39923 |
<row> |
| 39924 |
<entry><option>6.5</option></entry> |
| 39925 |
<entry>05 Apr 2012</entry> |
| 39926 |
<entry>TUNGSTENBERRY release: Minor update.</entry> |
| 39927 |
</row> |
| 39928 |
<row> |
| 39929 |
<entry><option>6.6</option></entry> |
| 39930 |
<entry>27 Nov 2012</entry> |
| 39931 |
<entry>YTTERBIUMBERRY release: Minor update.</entry> |
| 39932 |
</row> |
| 39933 |
<row> |
| 39934 |
<entry><option>10</option></entry> |
| 39935 |
<entry>10 Mar 2014</entry> |
| 39936 |
<entry>YTTERBIUMBERRY release: License change.</entry> |
| 39937 |
</row> |
| 39938 |
</tbody> |
| 39939 |
</tgroup> |
| 39940 |
</table> |
| 39941 |
|
| 39942 |
|
| 39943 |
</appendix> <!-- End Revision History appendix --> |
| 39944 |
|
| 39945 |
<appendix id="mirrorsites"> |
| 39946 |
<title>Download and Mirror Sites</title> |
| 39947 |
|
| 39948 |
<para><anchor id="where_tarball"></para> |
| 39949 |
|
| 39950 |
<para>The latest update of this document, as an archived, |
| 39951 |
<link linkend="bzipref">bzip2-ed</link> |
| 39952 |
<quote>tarball</quote> including both the SGML source |
| 39953 |
and rendered HTML, may be downloaded from the <ulink |
| 39954 |
url="http://bash.deta.in/abs-guide-latest.tar.bz2">author's |
| 39955 |
home site</ulink>). |
| 39956 |
|
| 39957 |
A <ulink |
| 39958 |
url="http://bash.deta.in/abs-guide.pdf"> |
| 39959 |
pdf version</ulink> is also available (<ulink |
| 39960 |
url="http://www.mediafire.com/file/xi34ape1bifcnlb/abs-guide.pdf">mirror |
| 39961 |
site</ulink>). |
| 39962 |
There is likewise an <ulink |
| 39963 |
url="http://bash.deta.in/abs-guide.epub"> |
| 39964 |
epub version</ulink>, courtesy of Craig Barnes and Michael Satke. |
| 39965 |
|
| 39966 |
The <ulink |
| 39967 |
url="http://bash.deta.in/Change.log">change |
| 39968 |
log</ulink> gives a detailed revision history. |
| 39969 |
The <emphasis>ABS Guide</emphasis> even has <ulink |
| 39970 |
url="http://freecode.com/projects/advancedbashscriptingguide/"> |
| 39971 |
its own <filename>freshmeat.net/freecode</filename> page</ulink> |
| 39972 |
to keep track of major updates, user comments, and popularity |
| 39973 |
ratings for the project.</para> |
| 39974 |
|
| 39975 |
<para>The legacy hosting site for this document is the <ulink |
| 39976 |
url="http://www.tldp.org/LDP/abs/">Linux Documentation Project</ulink>, |
| 39977 |
which maintains many other Guides and HOWTOs as well.</para> |
| 39978 |
|
| 39979 |
<para>Many thanks to Ronny Bangsund for donating <ulink |
| 39980 |
url="http://bash.deta.in/">server space</ulink> to host |
| 39981 |
this project.</para> |
| 39982 |
|
| 39983 |
|
| 39984 |
</appendix> <!-- Mirror Sites appendix --> |
| 39985 |
|
| 39986 |
|
| 39987 |
|
| 39988 |
<appendix id="todolist"> |
| 39989 |
<title>To Do List</title> |
| 39990 |
|
| 39991 |
<itemizedlist> |
| 39992 |
<listitem> |
| 39993 |
<para>A comprehensive survey of <link |
| 39994 |
linkend="bashcompat">incompatibilities</link> between |
| 39995 |
Bash and the classic <link linkend="bashdef">Bourne |
| 39996 |
shell</link>.</para> |
| 39997 |
</listitem> |
| 39998 |
|
| 39999 |
<listitem> |
| 40000 |
<para>Same as above, but for the Korn shell |
| 40001 |
(<firstterm>ksh</firstterm>).</para> |
| 40002 |
</listitem> |
| 40003 |
|
| 40004 |
|
| 40005 |
</itemizedlist> |
| 40006 |
|
| 40007 |
|
| 40008 |
</appendix> <!-- End Todo List appendix --> |
| 40009 |
|
| 40010 |
|
| 40011 |
<appendix id="copyright"> |
| 40012 |
<title>Copyright</title> |
| 40013 |
|
| 40014 |
<para>The <citetitle pubwork="book">Advanced Bash Scripting |
| 40015 |
Guide</citetitle> is herewith granted to the PUBLIC DOMAIN. |
| 40016 |
This has the following implications and consequences. |
| 40017 |
</para> |
| 40018 |
|
| 40019 |
|
| 40020 |
<para><programlisting> |
| 40021 |
A. All previous releases of the Advanced Bash Scripting Guide |
| 40022 |
are as well granted to the Public Domain. |
| 40023 |
|
| 40024 |
A1. All printed editions, whether authorized by the author or not, |
| 40025 |
are as well granted to the Public Domain. This legally overrides |
| 40026 |
any stated intention or wishes of the publishers. Any statement |
| 40027 |
of copyright is void and invalid. |
| 40028 |
THERE ARE NO EXCEPTIONS TO THIS. |
| 40029 |
|
| 40030 |
A2. Any release of the Advanced Bash Scripting Guide, whether in |
| 40031 |
electronic or print form is granted to the Public Domain by the |
| 40032 |
express directive of the author and previous copyright holder, Mendel |
| 40033 |
Cooper. No other person(s) or entities have ever held a valid copyright. |
| 40034 |
|
| 40035 |
B. As a Public Domain document, unlimited copying and distribution rights |
| 40036 |
are granted. There can be NO restrictions. If anyone has published or will |
| 40037 |
in the future publish an original or modified version of this document, |
| 40038 |
then only additional original material may be copyrighted. The core |
| 40039 |
work will remain in the Public Domain.</programlisting></para> |
| 40040 |
|
| 40041 |
<para>By law, distributors and publishers (including on-line |
| 40042 |
publishers) are prohibited from imposing any conditions, |
| 40043 |
strictures, or provisions on this document, any previous versions, |
| 40044 |
or any derivative versions. The author asserts |
| 40045 |
that he has <emphasis>not</emphasis> entered into any contractual |
| 40046 |
obligations that would alter the foregoing declarations.</para> |
| 40047 |
|
| 40048 |
<para>Essentially, you may freely distribute this book or any |
| 40049 |
derivative thereof in electronic or printed form. If you have previously |
| 40050 |
purchased or are in possession of a printed copy of a current or |
| 40051 |
previous edition, you have the LEGAL RIGHT to copy and/or redistribute |
| 40052 |
it, regardless of any copyright notice. Any copyright notice is |
| 40053 |
void.</para> |
| 40054 |
|
| 40055 |
|
| 40056 |
<para><emphasis>Additionally, the author wishes to state his intention |
| 40057 |
that:</emphasis></para> |
| 40058 |
|
| 40059 |
<para><programlisting>If you copy or distribute this book, kindly DO NOT |
| 40060 |
use the materials within, or any portion thereof, in a patent or copyright |
| 40061 |
lawsuit against the Open Source community, its developers, its |
| 40062 |
distributors, or against any of its associated software or documentation |
| 40063 |
including, but not limited to, the Linux kernel, Open Office, Samba, |
| 40064 |
and Wine. Kindly DO NOT use any of the materials within |
| 40065 |
this book in testimony or depositions as a plaintiff's "expert witness" in |
| 40066 |
any lawsuit against the Open Source community, any of its developers, its |
| 40067 |
distributors, or any of its associated software or documentation. |
| 40068 |
</programlisting></para> |
| 40069 |
|
| 40070 |
|
| 40071 |
<para>A Public Domain license essentially does not restrict ANY |
| 40072 |
legitimate distribution or use of this book. The author especially |
| 40073 |
encourages its (royalty-free!) use for classroom and instructional |
| 40074 |
purposes.</para> |
| 40075 |
|
| 40076 |
<para> |
| 40077 |
To date, limited print rights (Lulu edition) have been granted |
| 40078 |
to one individual and to <emphasis>no one else</emphasis>. Neither |
| 40079 |
that individual nor Lulu holds or ever has held a valid copyright.</para> |
| 40080 |
|
| 40081 |
<warning><para>It has come to the attention of the author that |
| 40082 |
<emphasis>unauthorized</emphasis> electronic and print |
| 40083 |
editions of this book are being sold commercially on <trademark |
| 40084 |
class=registered>itunes</trademark>, <emphasis>amazon.com</emphasis> |
| 40085 |
and elsewhere. These are illegal and pirated editions produced |
| 40086 |
without the author's permission, and readers of this book are |
| 40087 |
strongly urged not to purchase them. In fact, these pirated editions are |
| 40088 |
now legal, but necessarily fall into the Public Domain, and any |
| 40089 |
copyright notices contained within them are invalid and void.</para></warning> |
| 40090 |
|
| 40091 |
<para>The author produced this book in a manner consistent with the |
| 40092 |
spirit of the <ulink url="http://www.tldp.org/manifesto.html">LDP |
| 40093 |
Manifesto</ulink>.</para> |
| 40094 |
|
| 40095 |
|
| 40096 |
<sidebar> |
| 40097 |
<para>Linux is a trademark registered to Linus Torvalds.</para> |
| 40098 |
<para>Fedora is a trademark registered to Red Hat.</para> |
| 40099 |
<para>Unix and UNIX are trademarks registered to the Open Group.</para> |
| 40100 |
<para>MS Windows is a trademark registered to the Microsoft Corp.</para> |
| 40101 |
<para>Solaris is a trademark registered to Oracle, Inc.</para> |
| 40102 |
<para>OSX is a trademark registered to Apple, Inc.</para> |
| 40103 |
<para>Yahoo is a trademark registered to Yahoo, Inc.</para> |
| 40104 |
<para>Pentium is a trademark registered to Intel, Inc.</para> |
| 40105 |
<para>Thinkpad is a trademark registered to Lenovo, Inc.</para> |
| 40106 |
<para>Scrabble is a trademark registered to Hasbro, Inc.</para> |
| 40107 |
<para>Librie, PRS-500, and PRS-505 are trademarks registered to |
| 40108 |
Sony, Inc.</para> |
| 40109 |
<para>All other commercial trademarks mentioned in the body of this work |
| 40110 |
are registered to their respective owners.</para> |
| 40111 |
</sidebar> |
| 40112 |
|
| 40113 |
|
| 40114 |
<para>Hyun Jin Cha has done a <ulink |
| 40115 |
url="http://kldp.org/HOWTO/html/Adv-Bash-Scr-HOWTO/index.html">Korean |
| 40116 |
translation</ulink> of version 1.0.11 of this book. Spanish, |
| 40117 |
Portuguese, <ulink |
| 40118 |
url="http://abs.traduc.org/">French</ulink>, German, <ulink |
| 40119 |
url="http://it.tldp.org/guide/abs/index.html">Italian</ulink>, |
| 40120 |
<ulink |
| 40121 |
url="http://gazette.linux.ru.net/rus/articles/index-abs-guide.html">Russian</ulink>, |
| 40122 |
<ulink url="http://premekvihan.net/bash">Czech</ulink>, <ulink |
| 40123 |
url="http://www.linuxsir.org/bbs/showthread.php?t=256887">Chinese</ulink>, |
| 40124 |
Indonesian, Dutch, Romanian, Bulgarian, and Turkish translations are also |
| 40125 |
available or in progress. If you wish to translate this document |
| 40126 |
into another language, please feel free to do so, subject to |
| 40127 |
the terms stated above. The author wishes to be notified of such |
| 40128 |
efforts.</para> |
| 40129 |
|
| 40130 |
<sidebar><para>Those generous readers desiring to |
| 40131 |
make a donation to the author may contribute a small |
| 40132 |
amount via Paypal to my e-mail address, |
| 40133 |
<email>[email protected]</email>. |
| 40134 |
(An <userinput>Honor Roll of Supporters</userinput> |
| 40135 |
is given at the beginning of the <ulink |
| 40136 |
url="http://bash.deta.in/Change.log">Change Log</ulink>.) |
| 40137 |
This is <emphasis>not</emphasis> a requirement. |
| 40138 |
The <firstterm>ABS Guide</firstterm> is a free and freely |
| 40139 |
distributed document for the use and enjoyment of the Linux |
| 40140 |
community. However, in these difficult times, showing support |
| 40141 |
for voluntary projects and especially to authors of limited |
| 40142 |
means is more critically important than ever.</para></sidebar> |
| 40143 |
|
| 40144 |
|
| 40145 |
</appendix> <!-- End Copyright appendix --> |
| 40146 |
|
| 40147 |
<appendix id="asciitable"> |
| 40148 |
<title>ASCII Table</title> |
| 40149 |
|
| 40150 |
|
| 40151 |
<para>Traditionally, a book of this sort has an <link |
| 40152 |
linkend="asciidef">ASCII</link> Table appendix. |
| 40153 |
This book does not. Instead, here are several short |
| 40154 |
scripts, each of which generates a complete ASCII table.</para> |
| 40155 |
|
| 40156 |
<example id="asciish"> |
| 40157 |
<title>A script that generates an ASCII table</title> |
| 40158 |
<programlisting>&asciish;</programlisting> |
| 40159 |
</example> |
| 40160 |
|
| 40161 |
<example id="ascii2sh"> |
| 40162 |
<title>Another ASCII table script</title> |
| 40163 |
<programlisting>&ascii2sh;</programlisting> |
| 40164 |
</example> |
| 40165 |
|
| 40166 |
<example id="ascii3sh"> |
| 40167 |
<title>A third ASCII table script, using |
| 40168 |
<firstterm>awk</firstterm></title> |
| 40169 |
<programlisting>&ascii3sh;</programlisting> |
| 40170 |
</example> |
| 40171 |
|
| 40172 |
</appendix> <!-- End ASCII Table appendix --> |
| 40173 |
|
| 40174 |
|
| 40175 |
|
| 40176 |
<index id="xrefindex"> <!-- Begin Index --> |
| 40177 |
|
| 40178 |
&INDEX00; |
| 40179 |
|
| 40180 |
</index> <!-- End Index --> |
| 40181 |
|
| 40182 |
|
| 40183 |
|
| 40184 |
</book> |
| 40185 |
|
| 40186 |
<!-- Keep this comment at the end of the file |
| 40187 |
Local variables: |
| 40188 |
mode: sgml |
| 40189 |
sgml-indent-step:2 |
| 40190 |
sgml-indent-data:t |
| 40191 |
End: |
| 40192 |
--> |