4976
Comment:
|
3725
mammamia; http://www.dartmouthindependent.com/forum/viewtopic.php?p=1137226#1137226 order zyban; http://tclan.spb.ru/forum/showthread.php?p=9506#post9506 order zyban; http://www.lookforthenines.com/fo
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
#pragma section-numbers 2 = Bash Pitfalls = [[TableOfContents]] == for i in `ls *.mp3` == One of the most common mistakes ["BASH"] programmers make is to write a loop like this: {{{ for i in `ls *.mp3`; do # Wrong! some command $i # Wrong! done}}} This breaks when the user has a file with a space in its name. Why? Because the output of the `ls *.mp3` command substitution undergoes word splitting. Assuming we have a file named {{{01 - Don't Eat the Yellow Snow.mp3}}} in the current directory, the {{{for}}} loop will iterate over each word in the resulting file name (namely: "01", "-", "Don't", "Eat", and so on). You can't double-quote the substitution either: {{{ for i in "`ls *.mp3`"; do # Wrong! ...}}} This causes the entire output of the {{{ls}}} command to be treated as a single word, and instead of iterating over each file name in the output list, the loop will only execute ''once'', with {{{i}}} taking on a value which is the concatenation of all the file names (with spaces between them). In addition to this, the use of {{{ls}}} is just plain unnecessary. It's an external command, which simply isn't needed to do the job. So, what's the right way to do it? {{{ for i in *.mp3; do # Right! some command "$i" done}}} Let Bash expand the list of filenames for you. The expansion will ''not'' be subject to word splitting. Each filename that's matched by the {{{*.mp3}}} pattern will be treated as a separate word and, the loop will iterate once per file name. The astute reader will notice the double quotes in the second line. This leads to our second common pitfall. == cp $file $target == What's wrong with the command shown above? Well, nothing, '''if''' you happen to know in advance that {{{$file}}} and {{{$target}}} have no white space in them. But if you don't know that in advance, or if you're paranoid, or if you're just trying to develop good habits, then you should quote your variable references to ''avoid'' having them undergo word splitting. {{{ mv "$file" "$target"}}} Without the double quotes, you'll get a command like {{{mv 01 - Don't Eat the Yellow Snow.mp3 /mnt/usb}}} and then you'll get errors like {{{mv: cannot stat `01': No such file or directory}}}. With the double quotes, all's well. == [ $foo = "bar" ] == This is really the same as the previous pitfall, but I repeat it because it's ''so'' important. In the example above, the quotes are in the wrong place. You do ''not'' need to quote a string literal in bash. But you ''should'' quote your variables if you aren't sure whether they could contain white space. {{{ [ "$foo" = bar ] # Right!}}} Another way you could write this in bash involves the {{{[[}}} keyword, which embraces and extends the old {{{test}}} command (also known as {{{[}}}). {{{ [[ $foo = bar ]] # Also right!}}} You don't need to quote variable references within {{{[[ ]]}}} because they don't undergo word splitting in that context. On the other hand, quoting them won't hurt anything either. == [ "$foo" = bar && "$bar" = foo ] == You can't use {{{&&}}} inside the old {{{test}}} (or {{{[}}}) command. The Bash parser sees {{{&&}}} outside of {{{[[ ]]}}} or {{{(( ))}}} and breaks your command into ''two'' commands, before and after the {{{&&}}}. Use one of these instead: {{{ [ "$foo" = bar -a "$bar" = foo ] # Right! [ "$foo" = bar ] && [ "$bar" = foo ] # Also right! [[ $foo = bar && $bar = foo ]] # Also right!}}} == [[ $foo > 7 ]] == The {{{[[ ]]}}} operator is ''not'' used for an ArithmeticExpression. It's used for strings only. If you want to do a numeric comparison against the constant 7, you must use {{{(( ))}}} instead: {{{ ((foo > 7)) # Right!}}} If you use the {{{>}}} operator inside {{{[[ ]]}}}, it's treated as a string comparison, ''not'' an integer comparison. This may work sometimes, but it will fail when you least expect it. If you use {{{>}}} inside {{{[ ]}}}, it's even worse: it's an output redirection. You'll get a file named {{{7}}} in your directory, and the test will succeed as long as {{{$foo}}} is not empty. == grep foo bar | while read line; do ((count++)); done == The code above looks OK at first glance, doesn't it? Sure, it's just a poor implementation of {{{grep -c}}}, but it's intended as a simplistic example. So why doesn't it work? The variable {{{count}}} will be unchanged after the loop terminates, much to the surprise of Bash developers everywhere. The reason this code does not work as expected is because each command in a pipeline is executed in a separate subshell. The changes to the {{{count}}} variable within the loop's subshell aren't reflected within the parent shell (the script in which the code occurs). For solutions to this, please see [wiki:Self:BashFaq#faq24 Bash FAQ #24]. |
mammamia; http://www.dartmouthindependent.com/forum/viewtopic.php?p=1137226#1137226 order zyban; http://tclan.spb.ru/forum/showthread.php?p=9506#post9506 order zyban; http://www.lookforthenines.com/forum/index.php?topic=2515.0 zyban; http://heraku.com/jff////viewtopic.php?p=139525#139525 cheap zyban; http://thehomesteadlady.com/forum/index.php?topic=2426.0 zyban; http://www.altgnk.ru/forum/viewtopic.php?f=2&t=43494 zyban; http://moyvillafc.ie/yabb/YaBB.pl?num=1231548974/0 generic zyban usa; http://www.tramplelovers.com/forums/showthread.php?p=16890#post16890 order cheap zyban; http://www.hightechbum.com/modules.php?name=Forums&file=viewtopic&p=551523&sid=9071d82abf1b2a3f04e49defc66a87d0#551523 buy generic zyban; http://www.lmross.com/forum/viewtopic.php?f=2&t=12316 zyban; http://elamata.com/forum/showthread.php?p=16492#post16492 zyban online order; http://sds4me.talkthis.com/sds4me-post-16858.html#16858 buy zyban usa; http://www.teranovamusic.com/boards/viewtopic.php?p=368672#368672 generic zyban online; http://www.kimbio.pe.kr/phpBB2/viewtopic.php?p=62063#62063 online zyban; http://www.htsociety.com/forums/showthread.php?p=148135#post148135 buy cheap zyban; http://repetitivedrivinginjury.com/rdforum/index.php?topic=1516.0 buy generic zyban online; http://tomstech.dailyforum.net/viewtopic.php?f=92&t=6393 zyban; http://radio-sky.net/forum/index.php?topic=1205.0 zyban; http://www.datawille.com/ohjelmointifoorumi/viewtopic.php?p=235146#235146 zyban purchase; http://yackom.com/forum/viewtopic.php?p=155562#155562 generic zyban uk; http://www.playholdemonline.com/forum/index.php?topic=28732.0 zyban; http://www.easyphpgallery.net/phpBB2/viewtopic.php?p=113691#113691 buy zyban online; http://www.pitbike-mx.be/bbfr//viewtopic.php?p=398514#398514 online zyban; http://www.game-exchange.net/forum/viewtopic.php?f=1&t=7826 zyban; http://venusmelena.net/s4eti/yabb/YaBB.pl?num=1231549014/0 purchase zyban; http://phm2css.com/modules.php?name=Forums&file=viewtopic&p=161559#161559 buy zyban; http://sammerhaddad.com/forum/index.php?topic=1429.0 online zyban buy; http://www.topamateurvoetbal.nl/forum/viewtopic.php?p=420800#420800 purchase zyban; http://www.str-ufa.ru/forum/viewtopic.php?p=7362&sid=e1f138dd7b688a69707deeeeb216a2ea#7362 zyban online; http://www.fotballgutta.no/cgi//index.cgi?action=forum&board=treninger&op=display&num=2304&start= zyban; http://sch1047.kzn.ru/forum/viewtopic.php?p=54574#54574 zyban; http://ssebastiaoonline.com/yabb/YaBB.pl?num=1231549001/0 zyban; http://www.nnki.nnov.ru/forum/index.php?topic=1462.0 buy zyban; http://myexisnuts.com/yabb/YaBB.pl?num=1231549090/0 order generic zyban; http://www.elsathenetherlands.nl/forum/viewtopic.php?p=51946#51946 buy cheap zyban; http://www.etunateater.nu/forum/showthread.php?p=96512#post96512 zyban; http://johamail.com/forum/index.php?topic=1349.0 generic zyban; http://www.hh-vikar.dk/modules.php?name=Forums&file=viewtopic&p=28807#28807 order zyban online; http://gaiabianca.com/forum/index.php?topic=1470.0 order zyban uk; http://www.highball.at/smf/index.php?topic=7643.0 zyban; http://www.yabbserver.com/yabb2_test/Forums/F_0001/YaBB.cgi?num=1231549800/0 zyban; http://www.um-zachodniopomorskie.pl/forum/read.php?f=1&i=65861&t=65861 zyban; http://www.jinjin.co.jp/shop/modules/newbb/viewtopic.php?topic_id=119698&post_id=309110&order=0&viewmode=flat&pid=0&forum=1#forumpost309110 buy zyban; http://www.realitysuxs.com/Nuke/html/modules.php?name=Forums&file=viewtopic&p=219232#219232 order generic zyban; http://oscarent.net/forums/index.php?topic=26356.0 buy zyban; http://www.49cc.lv/forum/index.php?showtopic=27454 buy cheap generic zyban online; http://www.urusitios.com/uruforo/viewtopic.php?p=276057#276057 buy zyban; |
mammamia; http://www.dartmouthindependent.com/forum/viewtopic.php?p=1137226#1137226 order zyban; http://tclan.spb.ru/forum/showthread.php?p=9506#post9506 order zyban; http://www.lookforthenines.com/forum/index.php?topic=2515.0 zyban; http://heraku.com/jff////viewtopic.php?p=139525#139525 cheap zyban; http://thehomesteadlady.com/forum/index.php?topic=2426.0 zyban; http://www.altgnk.ru/forum/viewtopic.php?f=2&t=43494 zyban; http://moyvillafc.ie/yabb/YaBB.pl?num=1231548974/0 generic zyban usa; http://www.tramplelovers.com/forums/showthread.php?p=16890#post16890 order cheap zyban; http://www.hightechbum.com/modules.php?name=Forums&file=viewtopic&p=551523&sid=9071d82abf1b2a3f04e49defc66a87d0#551523 buy generic zyban; http://www.lmross.com/forum/viewtopic.php?f=2&t=12316 zyban; http://elamata.com/forum/showthread.php?p=16492#post16492 zyban online order; http://sds4me.talkthis.com/sds4me-post-16858.html#16858 buy zyban usa; http://www.teranovamusic.com/boards/viewtopic.php?p=368672#368672 generic zyban online; http://www.kimbio.pe.kr/phpBB2/viewtopic.php?p=62063#62063 online zyban; http://www.htsociety.com/forums/showthread.php?p=148135#post148135 buy cheap zyban; http://repetitivedrivinginjury.com/rdforum/index.php?topic=1516.0 buy generic zyban online; http://tomstech.dailyforum.net/viewtopic.php?f=92&t=6393 zyban; http://radio-sky.net/forum/index.php?topic=1205.0 zyban; http://www.datawille.com/ohjelmointifoorumi/viewtopic.php?p=235146#235146 zyban purchase; http://yackom.com/forum/viewtopic.php?p=155562#155562 generic zyban uk; http://www.playholdemonline.com/forum/index.php?topic=28732.0 zyban; http://www.easyphpgallery.net/phpBB2/viewtopic.php?p=113691#113691 buy zyban online; http://www.pitbike-mx.be/bbfr//viewtopic.php?p=398514#398514 online zyban; http://www.game-exchange.net/forum/viewtopic.php?f=1&t=7826 zyban; http://venusmelena.net/s4eti/yabb/YaBB.pl?num=1231549014/0 purchase zyban; http://phm2css.com/modules.php?name=Forums&file=viewtopic&p=161559#161559 buy zyban; http://sammerhaddad.com/forum/index.php?topic=1429.0 online zyban buy; http://www.topamateurvoetbal.nl/forum/viewtopic.php?p=420800#420800 purchase zyban; http://www.str-ufa.ru/forum/viewtopic.php?p=7362&sid=e1f138dd7b688a69707deeeeb216a2ea#7362 zyban online; http://www.fotballgutta.no/cgi//index.cgi?action=forum&board=treninger&op=display&num=2304&start= zyban; http://sch1047.kzn.ru/forum/viewtopic.php?p=54574#54574 zyban; http://ssebastiaoonline.com/yabb/YaBB.pl?num=1231549001/0 zyban; http://www.nnki.nnov.ru/forum/index.php?topic=1462.0 buy zyban; http://myexisnuts.com/yabb/YaBB.pl?num=1231549090/0 order generic zyban; http://www.elsathenetherlands.nl/forum/viewtopic.php?p=51946#51946 buy cheap zyban; http://www.etunateater.nu/forum/showthread.php?p=96512#post96512 zyban; http://johamail.com/forum/index.php?topic=1349.0 generic zyban; http://www.hh-vikar.dk/modules.php?name=Forums&file=viewtopic&p=28807#28807 order zyban online; http://gaiabianca.com/forum/index.php?topic=1470.0 order zyban uk; http://www.highball.at/smf/index.php?topic=7643.0 zyban; http://www.yabbserver.com/yabb2_test/Forums/F_0001/YaBB.cgi?num=1231549800/0 zyban; http://www.um-zachodniopomorskie.pl/forum/read.php?f=1&i=65861&t=65861 zyban; http://www.jinjin.co.jp/shop/modules/newbb/viewtopic.php?topic_id=119698&post_id=309110&order=0&viewmode=flat&pid=0&forum=1#forumpost309110 buy zyban; http://www.realitysuxs.com/Nuke/html/modules.php?name=Forums&file=viewtopic&p=219232#219232 order generic zyban; http://oscarent.net/forums/index.php?topic=26356.0 buy zyban; http://www.49cc.lv/forum/index.php?showtopic=27454 buy cheap generic zyban online; http://www.urusitios.com/uruforo/viewtopic.php?p=276057#276057 buy zyban;