Unix ja shell-ohjelmointi 2001, demo 6 Unix ja shell-ohjelmointi 2001, demo 6 mallivastaukset

    1. #! /bin/sh
      grep '^....7.-..[02468]' "$@"
      

    2. #! /bin/sh
      sed "s-^$HOME-~-" "$1" >"$1.bak" && mv "$1.bak" "$1"
      
      tai
      #! /bin/sh
      awk '{ sub("^'$HOME'","~"); print }' "$1" >"$1.bak" && mv "$1.bak" "$1"
      

    3. #! /bin/sh
      for i in *.cc ;do [ -f "$i" ] && mv "$i" "${i%.cc}.C" ;done
      
      tai
      #! /bin/sh
      ls *.cc | sed 's/\(.*\)\.cc$/mv \1.cc \1.C/' | sh
      

    1. #! /bin/sh
      grep -En '\[(a-z|A-Z|a-zA-Z)\]' /dev/null "$@"
      

    2. #! /bin/sh
      find "$@" -type f -exec grep -l Pekka {} \;
      
      tai
      #! /bin/sh
      find "$1" -type f | xargs grep -l Pekka
      

  1. #! /bin/sh
    while IFS=: read login pw uid gid gcos home shell
    do ls -ld "$home"/ | awk '$3 != "'$login'" || $1 ~ /d...(.w....|.....w.)/'
    done </etc/passwd
    

  2. #! /bin/awk -f
    BEGIN {
      n=ARGV[1]
      f="%4d"
      if (ARGV[2]=="8") f="%4o"
      if (ARGV[2]=="16") f="%4X"
      ARGC=1
    
      for (i=1; i<=n; ++i) {
        for (j=1; j<=n; ++j) printf f, i*j
        printf "\n"
      }
    }
    

  3. #! /bin/awk -f
    ! cols { cols=NF }
    { if (NF != cols) exit
      diagsum1 += $NR
      diagsum2 += $(NF-NR+1)
      for (i=0; i++<NF;) { 
        rowsum[NR]+=$i 
        colsum[i]+=$i 
      }
    }
    END {
      if (!cols) { printf "empty file has no magic\n"; exit 1 }
      if (NR!=cols) { printf "not square, no magic\n"; exit 2 }
      for (i in rowsum) {
        if (rowsum[i] != rowsum[1]) { printf "row %d fails, bad magic\n", i; exit 3 }
        if (colsum[i] != colsum[1]) { printf "column %d fails, bad magic\n", i; exit 4 }
      }
      if (diagsum1!=diagsum2) { printf "diagonals disagree, bad magic\n"; exit 5 }
      print "magic it is!"
    }
    


File translated from TEX by TTH, version 1.98.
On 12 Mar 2001, 09:53.