Advertisement
metalx1000

check switches and arguments

May 25th, 2015
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.86 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #set folder variable
  4. folder="files"
  5. #check if folder exists, if not create it
  6. [ -a "$folder" ] && echo "$folder exists" || mkdir "$folder"
  7.  
  8. move(){
  9.   if [ "$2" != "true" ] #only echo output if '-q' is not set
  10.   then
  11.     echo "Moving $1 for ${folder}..."
  12.   fi
  13.   mv $1  "$folder"
  14. }
  15.  
  16. copy(){
  17.   if [ "$2" != "true" ] #only echo output if '-q' is not set
  18.   then
  19.     echo "Copying $1 for ${folder}..."
  20.   fi
  21.   cp $1 "$folder"
  22. }
  23.  
  24. for var in "$@"
  25. do
  26.   if [ "$var" = "-f" ]
  27.   then
  28.     force="true" #if '-f' is set then we are going to move files
  29.   elif [ "$var" = "-q" ]
  30.   then
  31.     quiet="true" #if '-q' is set then be quiet
  32.   fi
  33. done
  34.  
  35. #loop through all files
  36. for file in *
  37. do
  38.   if [ "$force" = "true" ] # if '-f' is set then move
  39.   then
  40.     move "$file" "$quiet"
  41.   else #if '-f' is not set copy
  42.     copy "$file" "$quiet"
  43.   fi
  44. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement