Wednesday, November 19, 2014

Shell Script to read source file and copy it to target file

  1. #!/bin/bash
  2. # Shell to read source file and copy it to target file. If the file
  3. # is copied successfully then give message 'File copied successfully'
  4. # else give message 'problem copying file'
  5. # -------------------------------------------------------------------------
  6. # Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
  7. # This script is licensed under GNU GPL version 2.0 or above
  8. # -------------------------------------------------------------------------
  9. # This script is part of nixCraft shell script collection (NSSC)
  10. # Visit http://bash.cyberciti.biz/ for more information.
  11. # -------------------------------------------------------------------------
  12.  
  13. echo -n "Enter soruce file name : "
  14. read src
  15. echo -n "Enter target file name : "
  16. read targ
  17.  
  18. if [ ! -f $src ]
  19. then
  20. echo "File $src does not exists"
  21. exit 1
  22. elif [ -f $targ ]
  23. then
  24. echo "File $targ exist, cannot overwrite"
  25. exit 2
  26. fi
  27.  
  28. # copy file
  29. cp $src $targ
  30.  
  31. # store exit status of above cp command. It is use to
  32. # determine if shell command operations is successful or not
  33. status=$?
  34.  
  35. if [ $status -eq 0 ]
  36. then
  37. echo 'File copied successfully'
  38. else
  39. echo 'Problem copuing file'
  40. fi

0 comments: