This is an old revision of the document!


Shell

Block comment

With “END” being just an example of any random string that must not appear in the commented section:

: << 'END'
sleep 1
END

Iterate over a list as a string

If you have to define it as a string, sh will work, and for zsh you have to execute before setopts shwordsplit:

setopts shwordsplit
list="a b c"
for x in $list; do echo $x; done

However if you can, it is better to define arrays:

list=(a b c)
for x in $list; do echo $x; done

Do something recursively on all files

Use find, with {} being the file name:

find . -type f -exec <command> {} \;
find -iname "<pattern>" -exec <command> {} \;

Manual solution:

#!/bin/sh
 
function search_dir()
{
    DIR=$1;
 
    for file in $DIR/*; do
        if [[ -f $file ]]; then
            echo "Do something with $file"
        fi;
    done
 
 
    for element in $DIR/* ; do
        if [[ -e $element && -d $element && \
            $(basename "$element") != ".." && \
            $(basename "$element") != "." ]]; then
 
            search_dir "$element";
        fi;
    done;
}
 
search_dir "."
linux/shell.1713519445.txt.gz · Last modified: 2024/04/19 09:37 by cyril
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0