Linux bash shell learning notes

Source: Internet
Author: User

1. Bash script parameter Processing

Bash parameters can be accessed by adding a numerical number to $, where:

$ # Indicates the number of Script Parameters

$1 indicates the script's 1st parameters.

$2 represents the 2nd parameters of the script.

Similarly, $ n indicates the nth parameter of the script. However, $10 cannot be used to access the tenth parameter.

If there are more than 10 parameters, you must process or save the first parameter $1, then use the shift command to delete parameter 1 and move all the remaining parameters to one place.

$10 is changed to $9, and the value of $ # is updated to reflect the remaining amount of the parameter. The following example is used to traverse the parameter list of the script:

#!/bin/bashi = 1;while (($#> 0))do    echo $i, $1    shift    let i=i+1done

Save as test. Sh, add executable permissions, and then execute./Test. Sh 1 2 3 4 5 6 7 8 9 10The result is as follows:

111, 12, 23, 34, 45, 56, 67, 78, 89, 910, 1011, 11

 

 

2. The shell script automatically modifies the user password.

OZ: I want to write a script to restore the default settings. One of them is to restore it to the default password. Before that, I only know that the password is passwd, the new password is a form of interaction with the user. However, the default password of the Prime Minister is actually fixed and can be implemented without interaction. It can be implemented using scripts.

Most of the first checks on the internet showed that they used reverse CT to process user interaction. However, this was a little tricky, so I called my senior brother and I was not clear about it. So I checked it together, finally, it is feasible to practice a command, which is very simple.

Echo Username: Password | chpasswd

In fact, I do not know what it means when I use it. Because I am not familiar with shell commands, it is not clear about pipelines. As explained, chpasswd changes the passwords of a group of users by entering the User name: password. ECHO is equivalent to using echo output as the input of chpasswd.

The command is very simple, and the truth is very simple. I'm afraid I don't know it!

 

 

3. replace a line of content in the file

Replace the second behavior in the.txt file with AAAA:

Sed-e '2s/*/AAAA: G' a.txt
 

 

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.