Shell script implementation automatically generates files and changes their permissions

Source: Internet
Author: User
Tags touch touch command

When you learn the shell, you create new files and then write shell scripts in the new file to learn. So there are a few steps:

1. Create a new shell script using the touch command

2. Writing shell scripts

3, use chmod more shell script permissions, make it executable permissions, and then execute

When you write a few more scripts, you find this process cumbersome, and the new shell scripts are created in ascending order of numbers. Why not write a script to implement all three of these processes. So it started, it took a long time to get out, true shame, or not familiar with the shell.

First talk about the realization of the idea:

1, first get the latest script name in the directory, extract the numbers

2, add the number 1, and then splicing with. Sh to get a new toe name

3. Create a new script with touch

4. Use chmod to change permissions

For the first step:

Use the Ls–t parameter to sort the display in time.


To extract the first row of data, you need to use Head–n 1, so use a pipe to stitch two commands together.

650) this.width=650; "title=" clip_image004 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image004" src= "http://s3.51cto.com/wyfs02/M00/79/32/wKiom1aLUj_D6u4NAAASxG3bXVY795.jpg" height= "/>"

The number to extract. Use TR–CD ' [0-9] ' to remove all characters except 0-9 in the string, or to use pins to stitch the commands between them.

650) this.width=650; "title=" clip_image006 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image006" src= "http://s3.51cto.com/wyfs02/M00/79/30/wKioL1aLUmXgvA6LAAASvznPWt0882.jpg" height= "Notoginseng"/>

This is followed by Echo just to make the numbers appear in one line.

So in the script, the following line of code is done. Save the extracted numbers in the variable number.

650) this.width=650; "title=" clip_image008 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image008" src= "http://s3.51cto.com/wyfs02/M01/79/32/wKiom1aLUkHB4clIAAAaAnRSXQI678.jpg" height= "/>"

Step Two:

Use $ ((expression)) for mathematical operations.

650) this.width=650; "title=" clip_image009 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image009" src= "http://s3.51cto.com/wyfs02/M02/79/32/wKiom1aLUkKw0uh6AAAPdhHTEbI498.png" height= "/>"

Stitching, directly ligatures the string together.

650) this.width=650; "title=" clip_image010 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image010" src= "http://s3.51cto.com/wyfs02/M02/79/32/wKiom1aLUkOwVlizAAAS00eaM78000.png" height= "/>"


Step Three:

Use bash's ' commands ' to execute shell commands.

650) this.width=650; "title=" clip_image011 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px; "border=" 0 "alt = "clip_image011" src= "http://s3.51cto.com/wyfs02/M02/79/30/wKioL1aLUmmyXG_1AAATueapI2I853.png" height= "/>"


Fourth Step:

Or use Bash's ' commands ' to execute shell commands

650) this.width=650; "title=" clip_image012 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px, "border=" 0 "alt=" clip_ image012 "src=" Http://s3.51cto.com/wyfs02/M01/79/30/wKioL1aLUmvykdZiAAAQnpOVE3w662.png "height=" "/>"

So, just a few lines of code are required to achieve the desired function.

Number= ' Ls-t | Head-n 1 | TR-CD ' [0-9] ' number=$ (($number + 1)) new_sh_file= "$number" ". Sh" ' Touch $new _sh_file ' chmod a+x./-R '

You can also assign the script to the/usr/bin directory so that the script can be executed directly anywhere in the future.



This article is from the "Beidou Star" blog, please be sure to keep this source http://weiqi7777.blog.51cto.com/1866266/1731671

Shell script implementation automatically generates files and changes their permissions

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.