* Anders Johansson
On Wednesday 03 November 2010 23:03:41 Brandon Philips wrote:
Thoughts? [...] # # parse all of the tmpfiles configs # example line: # d /var/lock/subsys 0755 root root - # cat /etc/tmpfiles.d/* | sed -e '/^#.*$/d' -e '/^\s*$/d' | while read line; do eval $(echo $line |\ sed -n "s/\(.\)\s\(.*\)\s\(.*\)\s\(.*\)\s\(.*\)\s\(.*\)/type=\1\nfile=\2\nmode=\3 \nuser=\4\ngroup=\5\nage=\6/p")
if [ ! $type ] || [ ! $file ]; then echo "Error: no type or file" echo $line exit 1 fi
[ $arg_create ] && create_item [ $arg_remove ] && remove_item [ $arg_clean ] && clean_item done
I don't really like constructs like echo $foo | sed. Bash can usually do a much better job on its own, as long as you don't need really complex things. Please consider a construct like this instead
for i in /etc/tmpfiles.d/*; do while read type file mode user group age; do if [ ! $type ] || [ ! $file ]; then echo "Error: no type or file" echo $line exit 1 fi # do the work..... done < $i done
This breaks for empty lines and comments, furthermore the "!" operator is not a safe way to check for an empty string use "-n" or "-z", more generally read should always be used with "-r" to prevent interpretation of escape sequences, and variables should be put in quotes to protect field splitting and globbing. Using built in field splitting allows easily to check for empty lines and the number of arguments per line, also this does not need bash, any POSIX shell is sufficient: for tmpdir_file in /etc/tmpfiles.d/*; do while read -r line; do set -- ${line} [ "$1" = "#" ] && continue [ $# -eq 0 ] && continue if [ $# -lt 3 ]; then printf "Error: no type or file\n" >&2 printf "%s\n" "${line}" >&2 exit 1 fi type="$1"; file="$2"; mode="$3"; user="$4"; group="$5"; age="$6" [ -n "${arg_create}" ] && create_item [ -n "${arg_remove}" ] && remove_item [ -n "${arg_clean}" ] && clean_item done < "${tmpdir_file}" done -- Guido Berhoerster -- To unsubscribe, e-mail: opensuse-factory+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-factory+help@opensuse.org