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.
- # Cat/sh/vii
- #! /Usr/bin/bash
- # Check whether the source file exists before performing the backup operation
- If [-f "$1"]; then
- # Create a function to enable any key to continue
- # If stty raw is not recognized on your machine, replace the raw in the two parts of the function with cbreak.
- Get_char ()
- {
- SAVEDSTTY = 'stty-G'
- Stty-echo
- Stty raw
- Dd if =/dev/tty bs = 1 count = 1 2>/dev/null
- Stty-raw
- Stty echo
- Stty $ SAVEDSTTY
- }
- # Create the backup root path/bak and grant everyone the write permission
- If [! -D "/bak"]; then
- Mkdir/bak
- Chmod a + w/bak
- Fi
- # Determine the path of the target file and create it so that everyone has the write permission on any newly created level Directory.
- Echo $1 | grep "/">/dev/null
- If ["'echo $? '"=" 0 "]; then
- Cd $ {1% /*}
- Fi
- Pwdnow = 'pwd'
- Path =/bak $ {pwdnow}
- # Echo $ path
- If [! -D "$ path"]; then
- Mkdir-p $ path
- Patha =$ {pwdnow #/}
- Chmod-R a + w/bak/$ {patha % /*}
- Fi
- # Copying the target file
- Datenow = 'date + % Y % m % d'
- Timenow = 'date + % H % M % s'
- /Usr/bin/cp $1 $ {path}/$ {1 # */}. $ {datenow}. $ {timenow}
- Echo "Target file" $1"
- Has been copied to $ {path}/$ {1 # */}. $ {datenow}. $ {timenow }"
- Echo "Now going to" vi $1 ", Press any key to continue ..."
- Char = 'get _ char'
- Fi
- # Call the vi command
- Vi $1
- Call scripts
- # Chmod a + x/sh/vii
- #/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.