Use Shell scripts to automatically back up source files before vi, cp, and mv.

Source: Internet
Author: User
The following script backs up all source files to be edited to the bak directory, and keeps all paths unchanged. for example, vietchosts copies etchosts to baketchosts. Ca

The following script backs up all source files to be edited to the/bak directory, and keeps all paths unchanged, such as vi/etc/hosts, copy/etc/hosts to/bak/etc/hosts.

  1. # Cat/sh/vii
  2. #! /Usr/bin/bash
  3. # Check whether the source file exists before performing the backup operation
  4. If [-f "$1"]; then
  5. # Create a function to enable any key to continue
  6. # If stty raw is not recognized on your machine, replace the raw in the two parts of the function with cbreak.
  7. Get_char ()
  8. {
  9. SAVEDSTTY = 'stty-G'
  10. Stty-echo
  11. Stty raw
  12. Dd if =/dev/tty bs = 1 count = 1 2>/dev/null
  13. Stty-raw
  14. Stty echo
  15. Stty $ SAVEDSTTY
  16. }
  17. # Create the backup root path/bak and grant everyone the write permission
  18. If [! -D "/bak"]; then
  19. Mkdir/bak
  20. Chmod a + w/bak
  21. Fi
  22. # Determine the path of the target file and create it so that everyone has the write permission on any newly created level Directory.
  23. Echo $1 | grep "/">/dev/null
  24. If ["'echo $? '"=" 0 "]; then
  25. Cd $ {1% /*}
  26. Fi
  27. Pwdnow = 'pwd'
  28. Path =/bak $ {pwdnow}
  29. # Echo $ path
  30. If [! -D "$ path"]; then
  31. Mkdir-p $ path
  32. Patha =$ {pwdnow #/}
  33. Chmod-R a + w/bak/$ {patha % /*}
  34. Fi
  35. # Copying the target file
  36. Datenow = 'date + % Y % m % d'
  37. Timenow = 'date + % H % M % s'
  38. /Usr/bin/cp $1 $ {path}/$ {1 # */}. $ {datenow}. $ {timenow}
  39. Echo "Target file" $1"
  40. Has been copied to $ {path}/$ {1 # */}. $ {datenow}. $ {timenow }"
  41. Echo "Now going to" vi $1 ", Press any key to continue ..."
  42. Char = 'get _ char'
  43. Fi
  44. # Call the vi command
  45. Vi $1
  46. Call scripts
  47. # Chmod a + x/sh/vii
  48. #/Sh/vii/etc/passwd

You can add/sh to the PATH variable, which is more convenient. Likewise, it is best to back up the target file when the cp/mv command is involved to avoid accidental overwriting.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.