|
||||||
| Forums FAQ | Members List | Search | Today's Posts | Mark Forums Read |
| ARCHIVES - Programming & Scripting A place to discuss website design, programming, shell scripts, etc |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
Just thought I'd pass this along. I was doing some reading and found these entries on opensuse.org email list pertaining to Bash and the usage of double-quotes in the "for" keyword structure. The links are shown below and one snippet. Also for those starting out, I found http://www.linuxcommand.org/learning_the_shell.php, to be a really nice site, that uses real world examples, and a very logical approach. It was particularly interesting to view their script, "new_script", used to build a new Bash template. It is very complete with documentation, trap, help/usage, getopts, and shows overall good (belt and suspenders) Bash programming techniques (i.e., best practices). The author of the site is working on a book. If it is a reflection of their site, it should be good.
http://lists.opensuse.org/opensuse/2.../msg02240.html http://lists.opensuse.org/opensuse/2.../msg01738.html Quote:
|
|
|||
|
Similar caution should be exercised with when using find with xargs. E.g.
find old/ -type f -name '*.mp3' | xargs rm -f A filename with a space will cause problems. The way to do it is: find old/ -type f -name '*.mp3' -print0 | xargs -0 rm -f |
|
|||
|
Quote:
Boy find and xargs are very powerful. Your example reminded me also of, #7, "Use xargs outside of find.", in "Learn 10 good UNIX usage habits" I was experimenting with find/xargs to copy newer files in a directory. Any thoughts on the command below: # Look for files newer than the tag file lastUsed. Do only the current directory and # print only the file name followed by a Null. find . -maxdepth 1 -newer ${BackupDir}/lastUsed -type f -printf '%f\0' | xargs -0tI {} cp -a {} ${BackupDir}/{}.bkup One thing you don't see in those "for" examples on the net (or Bash books), is what happens if there is no match. In the example in the opensuse.org email list, if there are no mp3 files in the current directory, $i will be "*.mp3". If you start processing $i, you could end up with some pretty surprising results, especially if you don't quote it and wildcard expansion occurs. I suppose one should at least test to see if $i is a file ( [ -f "$i" ]) before file processing, but also maybe check to see if $i is equal to the pattern ([ "$i" -eq "*.mp3" ]). |
|
|||
|
Quote:
shopt -s nullglob shopt -s failglob See man bash. |
|
|||
|
I'm running Bash 3.2.25/OpenSuSE 10.3, as is. The default behavior in /bin/Bash, interactive or batch, for the "for command", when there are no matches, is to set the variable to the pattern. But as I re-read the Bash reference, the opengroup standard, and bash manpage, that shouldn't be the case.
Quote:
Code:
Thu 03-13-2008 09:41 ===>**for f in *.nomatch; do echo "$f"; done *.nomatch This appears to be the default behavior in Bash in OpenSuSE. A shopt -p, shows nullglob to be off. Something seems wrong. What am I missing here? Interesting. Thanks. |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|