Guest User

Untitled

a guest
Jul 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.87 KB | None | 0 0
  1. #!/bin/sh
  2. USAGE="Usage: ./disku -d <directory> [ -h] [-o | -s] [file ...]"
  3.  
  4. hflag=
  5. oflag=
  6. sflag=
  7. DIRECTORY=
  8. while getopts ":d:hos" Option
  9. do
  10.     case $Option in
  11.     d )
  12.         DIRECTORY=$OPTARG
  13.         ;;
  14.     h )
  15.         hflag=1
  16.         ;;
  17.     \?)
  18.         echo "Invalid option: -$OPTARG" >&2
  19.         echo $USAGE
  20.         exit 1
  21.         ;;
  22.     o)  
  23.         oflag=1
  24.         ;;
  25.     s )
  26.         sflag=1
  27.         ;;
  28.     esac
  29. done
  30.  
  31.  
  32. #Checking directory is valid
  33. if [ -z $DIRECTORY ]
  34. then
  35.     echo 'Require -d flag to be passed with a valid directory'
  36.     echo $USAGE
  37.     exit 1
  38. fi
  39.  
  40. # check oflag. No recurse so maxdepth = 1
  41. if [ -z "$oflag" ];
  42. then
  43.     cd $DIRECTORY
  44.     find . -maxdepth 1 -type f -printf '%-10s %p\n' | \
  45.     awk '{ x = $1/512; (x==int(x)?x:int(x)+1); s+=x ;printf("%-10d %s\n",x,$2) } END{printf "%-10d %s\n", s,"."} '
  46. else
  47.     find .  -printf '%-10s %p\n' | awk '{ x = $1/512; printf "%-10d %s\n",(x==int(x)?x:int(x)+1),$2 }'
  48. fi
Add Comment
Please, Sign In to add comment