Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
docs:bash:bash_loop [2007/05/14 10:48] – created billh | docs:bash:bash_loop [2010/05/07 11:19] (current) – billh | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== bash loop ====== | ====== bash loop ====== | ||
- | | + | |
+ | ===== while read ===== | ||
+ | | ||
+ | * taken from http:// | ||
+ | * quote //"For splits items on a space, regardless of if they’re quoted | ||
+ | ls -1L *.tiff | while read file;do | ||
+ | du -h -d 0 " | ||
+ | done | ||
+ | </ | ||
+ | * another example:< | ||
+ | ls -1L *.tiff | while read file;do | ||
+ | tiff2pdf -p letter -o " | ||
+ | done | ||
+ | </ | ||
+ | * use a file containing a list of filenames for a copy operation:< | ||
+ | cat fileswithnotesbug.txt | while read file;do | ||
+ | cp " | ||
+ | done | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== for ===== | ||
+ | * :!: This code will NOT work on filenames with spaces unless the IFS variable is set in bash (see man bash for more details) | ||
* note that the symbols used are backticks, not single quotes (backtick is the key left of 1) | * note that the symbols used are backticks, not single quotes (backtick is the key left of 1) | ||
< | < | ||
Line 6: | Line 28: | ||
du -h -d 0 $file | du -h -d 0 $file | ||
done | done | ||
+ | </ | ||
+ | |||
+ | Another example showing how to set up new folders with git repo' | ||
+ | < | ||
+ | $ mkdir ~/git | ||
+ | $ cd ~/git | ||
+ | $ for i in a b c d | ||
+ | do | ||
+ | mkdir $i | ||
+ | cd $i | ||
+ | git init | ||
+ | echo " | ||
+ | git add $i.txt | ||
+ | git commit -m " | ||
+ | cd .. | ||
+ | done | ||
+ | </ | ||
+ | |||
+ | original url: http:// | ||
+ | |||
+ | ===== other notes ===== | ||
+ | * http:// | ||
+ | < | ||
+ | Props to this post, this is a very elegant solution to weird characters in filenames: | ||
+ | http:// | ||
+ | Code: | ||
+ | |||
+ | find . -type f -print | while read i; do touch " | ||
+ | |||
+ | |||
+ | That will fail if any filenames have leading or trailing spaces or end in a backslash. (Not to mention filenames containing newlines.) | ||
+ | |||
+ | Quote: | ||
+ | ' | ||
+ | |||
+ | |||
+ | It does not split on spaces if there is no space in the value of $IFS. | ||
+ | |||
+ | Quote: | ||
+ | ' | ||
+ | |||
+ | |||
+ | It does if more than one variable is given as an argument (and IFS contains a space). | ||
</ | </ |