Shell Scripting compilation

Source: Internet
Author: User

First, replace the 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 assume the value of the above Tracker_server in the source file is not fixed. Should be changed to the following wording:
echo "Enter tracker_ip:"
Read Tracker_ip
echo "$Tracker _ip"
Sed-i "/tracker_server/s/\ (. *=\). */\1$tracker_ip/"/etc/fdfs/storage.conf

# # This is not the correct way to do this:
Sed-i "s/tracker_server=192.168.209.121/tracker_server= $Tracker _ip/"/etc/fdfs/storage.conf


Second, insert the text:

The following command can insert a specified line or multiline of text after b_ipadress 192.168.1.188 the line
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 were to replace the middle A with I, I would insert multiple lines in front of the b_ipadress 192.168.1.188 line.


Three, single and double cited:

A single argument is a strong reference, what is the value in the argument, and what is the value of the variable;
The double argument is a weak reference. If the value in the argument is included again, all of these variables are replaced immediately when the value is assigned.


Like what:
echo "Who am I: $LOGNAME" results: Who am I:yeexun
echo "Who am I: ' $LOGNAME '" results: Who am I: ' Yeexun '
echo "Who am I:" $LOGNAME "results: Who am I:yeexun

Echo ' Who am I: $LOGNAME ' results: Who am I: $LOGNAME
Echo ' Who am I: ' $LOGNAME ' results: Who am I: ' $LOGNAME '
Echo ' Who am I: ' $LOGNAME ' results: Who am I:yeexun


Get the current line number from the string and assign it to a variable, and then insert the text based on the derived variable line number

1. Get the row number assignment variable
myline=$ (awk '/test123/{print NR} '/root/my/mybin/sbin/test.config)
2. Insert text according to the line 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


Five, the operation of the variable

The operation should be placed in square brackets
Known variable myline, the variable plus 2 should be written as: $[myline+2]


Vi. Replace the value of a string that appears several times

Unlimited:
Sed-e ': a;/\ (. *abc\) \{3\}/{s/abc/def/3;q}; N;b a '
ABC represents the pre-replacement data, DEF is the replacement data, 3 is the first occurrence

There are restrictions (such as: Title ABCDEFG. Cannot have =. Can only be a space)
Replaces the data for the specified row: sed ' 0,/title/s/\ (title \). */\1hello/' Urfile
0 indicates the first occurrence in the text


Vii. replacing data for a specified row

Sed-i ' 2c\storage_server_port=11111 '/ROOT/MY/MYBIN/SNBIN/TEMP2


Eight, the variable save the total number of files

Temp= ' Grep-c. /ROOT/MY/MYBIN/SNBIN/TEMP2 '


Nine, delete the last few lines of the file

Delete last line: Sed-e ' $d ' file
Delete last few lines: statement with a For loop plus polygon


X. Other
When the shell is assigned a value. Cannot have spaces before and after the equals sign
If condition should be like this: if space [space $tempvar0 space = = Space "N" space]
Basic knowledge: http://bbs.chinaunix.net/thread-452942-1-1.html

Shell Scripting compilation

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.