25. Examples

Programcode1: lstree
#!/bin/bash

function rlist() {
  local dir=${1:-.}
  local level=${2:-0}
  ls -1 -A $dir | while read -r entry # pipe output to loop
  do
    if [ -d "$dir/$entry" ] # file test
    then
      printf "%${level}s + %s\n" "" "$entry" # formatted print
      rlist "$dir/$entry" $((${level}+1)); # recursive function call
    else
      printf "%${level}s |- %s\n" "" "$entry" 
    fi
  done
}

rlist $1; # function call
Programcode2: Example - log rotate
#!/bin/bash

ls -tr ${1:-.} | while read entry
do
  if [[ -f ${entry} && ! (${entry##*.} = "gz") ]]
  then
    cretime=$(stat -c %W ${entry})
    if ((($(date +%s) - ${cretime}) >= (24*3600*7))) 
    then
      gzip -fS ".$(printf "%.2d" $(date +%w --date=@${cretime})).gz" $entry;
    fi
  fi
done