Collection of shell scripts

Source: Internet
Author: User

I. Replace text:

#1
Sed-I's/disabled = true/disabled = false/'/etc/fdfs/storage. conf
#2
Sed-I's/base_path = \/home \/yuqing \/fastdfs/base_path = \/fdfs \/storage/'/etc/fdfs/storage. conf
#3
Echo "Enter Tracker_IP :"
Read Tracker_IP
Echo "$ Tracker_IP"
Sed-I "/tracker_server/s/192.168.209.121/$ Tracker_IP/"/etc/fdfs/storage. conf
#4 if the value of tracker_server in the source file is not fixed, change it to the following statement:
Echo "Enter Tracker_IP :"
Read Tracker_IP
Echo "$ Tracker_IP"
Sed-I "/tracker_server/s/\ (. * = \). */\ 1 $ Tracker_IP/"/etc/fdfs/storage. conf

# This statement is incorrect:
Sed-I "s/tracker_server = 192.168.209.121/tracker_server = $ Tracker_IP/"/etc/fdfs/storage. conf


Ii. Insert text:

The following command inserts a specified line or multiple lines of text after B _IPadress 192.168.1.188
Sed-I '/B _IPadress 192.168.1.188/a \ test123'/root/my/myBin/sbin/test. config
Sed-I '/B _IPadress 192.168.1.188/s/$/\ nline1 \ nline2/'/root/my/myBin/sbin/test. config
Sed-I '/B _IPadress 192.168.1.188/a \ test123 \ n123 \ n123 \ n123'/root/my/myBin/sbin/test. config

If I replace a with I, multiple rows are inserted before the B _IPadress 192.168.1.188 row.


3. single quotation marks and double quotation marks:

Single quotes are strongly referenced. What are the values in quotes and the values of variables;
Double quotation marks are weak references. If the values in the quotation marks contain variables, all these variables will be replaced immediately when values are assigned.
For example:
Echo "who am I: $ LOGNAME" Result: who am I: yeexun
Echo "who am I: '$ logname'" Result: who am I: 'yeexun'
Echo "who am I:" $ LOGNAME "" Result: who am I: yeexun

Echo 'Who am I: $ logname' result: who am I: $ LOGNAME
Echo 'Who am I: "$ LOGNAME" 'result: who am I: "$ LOGNAME"
Echo 'Who am I: '$ logname' result: who am I: yeexun


4. Obtain the current row number based on the string and assign it to a variable. Then, insert text based on the row number.

1. Get the value-assigned variable of the row number.
Myline = $ (awk '/test123/{print NR}'/root/my/myBin/sbin/test. config)
2. Insert text based on the row number variable
Sed-I '3s/$/& \ a123/'a.txt
Sed-I ''$ myline's/$/& \ a \ nupstream fdfs_group1 {\ n server '$ tempvar': 8080; /'/root/my/myBin/sbin/test. config
Sed '3a 123 '1.txt
Sed-I ''$ myline 'a \ nupstream fdfs_group1 {\ n server '$ tempvar': 8080; '/root/my/myBin/sbin/test. config


V. Variable Calculation

Computation should be placed in square brackets
Known variable myline. The variable plus 2 should be written as: $ [myline + 2]


6. Replace the value of a string that appears for the first time

Unlimited:
Sed-e': a;/\ (. * ABC \) \ {3 \}/{s/ABC/DEF/3; q}; N; B'
ABC indicates the data before replacement, DEF indicates the data after replacement, and 3 indicates the first occurrence

There are restrictions (for example, title abcdefg, cannot have =, can only be a space)
Replace the data of the specified row: sed '0,/title/s/\ (title \). */\ 1 hello/'urfile
0 indicates the first appearance in the text


7. Replace the data of a specified row

Sed-I '2c \ storage_server_port = 100'/root/my/myBin/snbin/temp2


8. Total number of rows for saving files with variables

Temp = 'grep-c./root/my/myBin/snbin/temp2'


9. Delete the last few lines of the file

Delete the last row: sed-e '$ d' file
Delete the last few rows: Add the following statement to the for Loop


10. Others
When a shell value is assigned, there cannot be spaces before and after the equal sign
If conditions should be like this: if space [space $ tempvar0 Space = space "N" Space]
Basic knowledge: http://bbs.chinaunix.net/thread-452942-1-1.html

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.