Shell backup files, rename files, delete specified files 3 scripts share _linux Shell

Source: Internet
Author: User
Tags case statement

Because often use the shell script, so often will write some, but I have never systematically learned the shell script, encountered problems, go to the manual, or Google, to a certain extent to find their foundation is really bad. The following is a little useful script to learn about the shell in the system.

1, backup the file below the directory

Copy Code code as follows:

#!/bin/bash
E_badargs=65

Case $# in//$ #表示转入参数的个数, sh bak.sh./. SH has two parameters./And. Sh
0)//parameter is empty, prompt for error, and exit
echo "Message:param is wrong"
Exit $E _badargs;;
Esac

String= ""

CD//Enter incoming directory

When you back up a file in a subdirectory, the parameters are also connected
If [$#-gt "1"]
Then
string= $string "" $
Fi

for filename in *//Read files in current directory
Todo
If [-D "$filename"]//Determine if the directory is not
Then
/bin/sh/home/zhangy/test/$0 $ (PWD)/$filename $string//is a directory, recursive
Else
If [$#-gt "1"] && [${filename: (-${#2})} = $]//Specify what files to back up
Then
CP $filename $filename "BAK"
Fi
If [$#-eq "1"]
Then
CP $filename $filename "bak"//not specified, all files under the backup directory
Fi
Fi
Done
Exit 0

[Zhangy@blackghost test]$ sh bak.sh. sh) means to back up all files in the current directory (and subdirectories) with the. sh suffix.

2, renaming files

Copy Code code as follows:

#!/bin/bash
E_badargs=65

Case $# in
0|1|2)
echo "Message:param is wrong"
Exit $E _badargs;;
Esac

String= ""
CD $

If [$#-gt "2"]
Then
string= $string "$" "$
Fi

for filename in *
Todo
If [-D "$filename]"
Then
/bin/sh/home/zhangy/test/$0 $ (PWD)/$filename $string
Else
If [$#-gt "2"] && [${filename: (-${#2})} = $]
Then
MV $filename ${filename%$2}$3
Fi
Fi
Done
Exit 0

[Zhangy@blackghost test]$ sh rename.sh/home/zhangy/test. html means to rename all files with the. php suffix under the current directory (including subdirectories) to. html

3, delete the specified file

Copy Code code as follows:

#!/bin/bash
E_badargs=65

Case $# in
0|1)
echo "Message:param is wrong"
Exit $E _badargs;;
Esac

String= ""
CD $

If [$#-gt "1"]
Then
string= $string "" $
Fi

for filename in *
Todo
If [-D "$filename]"
Then
/bin/sh/home/zhangy/test/$0 $ (PWD)/$filename $string
Else
If [$#-gt "1"] && [${filename: (-${#2})} = $]
Then
Rm-f $filename
Fi
Fi
Done
Exit 0


[Zhangy@blackghost test]$ sh del.sh/home/zhangy/test. Log deletes/home/zhangy/test, and all files with the. log suffix under subdirectories

4, summary

By practicing The example above, we summarize the following
1, pass parameters to the shell script, general pass parameters can be index.php?name=tank by URL or form form, like this, but Shell can't, He passed the parameters directly to the back of the shell script, like this rename.sh/home/zhangy/test. php. HTML passed 3 parameters to rename.sh, respectively,/home/zhangy/test. php. HTML
2,case statement, if the idea of the sentence is similar to the writing is not the same, more to see, more practice will adapt.
3, if the output of the command is given a negative value to a variable, the syntax is $ (shell command)   Example: $ (PWD) displays the current directory
4, The Intercept of the variable, the comparison between the variables, etc., the above example is very simple, but includes a lot of knowledge points.

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.