Linux shell
Originated byHCQ, 20161119
Back to upper pages Local folder
目录
1.1. Linux命令行与shell脚本编程大全(第2版)完整.中文版.pdf
正文
1 书籍阅读
1.1. Linux命令行与shell脚本编程大全(第2版)完整.中文版.pdf
1.1.1. 第5章 使用Linux环境变量
全局变量在命令行输入 printenv 可以显示全局环境变量
局部环境变量输入 set,显示所有的全局环境变量和局部环境变量
显示环境变量值 $echo $HOME
所有的(全局)系统环境变量大写,局部环境变量小写
1.1.1.1. 设置环境变量
1.设置临时环境变量
$exporttest=’Testing
删除临时环境变量
$unset test
2.设置对所有用户永久生效的环境变量
用VI在文件/etc/profile文件中增加变量,该变量将会对Linux下所有用户有效,并且是“永久的”。
例如:编辑/etc/profile文件,添加CLASSPATH变量
1 2 | # vi /etc/profile export CLASSPATH=./JAVA_HOME/lib;$JAVA_HOME/jre/lib |
注:修改文件后要想马上生效还要运行# source /etc/profile不然只能在下次重进此用户时生效
3.设置对单一用户永久生效的环境变量
用VI在用户目录下的.bash_profile文件中增加变量,改变量仅会对当前用户有效,并且是“永久的”。
例如:编辑guok用户目录(/home/guok)下的.bash_profile
2 3 4 | $ vi /home/guok/.bash.profile 添加如下内容: export CLASSPATH=./JAVA_HOME/lib;$JAVA_HOME/jre/lib |
注:修改文件后要想马上生效还要运行$ source /home/guok/.bash_profile不然只能在下次重进此用户时生效
1.1.1.2. 登录shell的启动文件顺序
/etc/profile
$HOME/.bash_profile
$HOME/.bash_login
$HOME/.profile
默认启动shell时将首先执行/etc/profile文件。
当前发行版本只执行$HOME/.bash_profile、$HOME/.bash_login、$HOME/.profile三个文件中的一个。
1.1.2. 第10章 构建基本脚本
在同一行执行多个命令,用冒号隔开命令。
1.1.2.1. Shell脚本文件的创建和运行
Shell文件的格式:
#!bin/bash #表示用bash shell来执行此脚本文件.
将脚本文件存储为Test.sh。进入该文档的目录,修改执行权限:
chmod u+x
执行该脚本
./Test.sh
1.1.3. 第11章 使用结构化命令
if command #若条件成立,才执行then语句
then
commands
fi#if语句的结束标志
1.1.4. 第18章 初识sed和gawk
1.1.4.1. sed:stream editor
sed命令格式 sed optionsscript file
1.1.4.2. options的3种设置
-e script Add commands specified inthe script to the commands run while processing the input.
-f file Add the commands specifiedin the file to the commands run while processing the input.
-n Don't produce output for each command, but waitfor the print command.
-i 直接在原文件上操作,操作将改写原文件
sed ‘s/dog/cat/’ data1# s为替换命令,用/隔开替换和被替换的字符串,第一个是要替入的,第二个是被替换的,data1位文件名称
sed -e ‘s/brown/green/; s/dog/cat/’data1 #-e选项,一行执行多个命令,用分号隔开多个命令,用引号包含多个命令,被处理文件为data1
sed -f script1 data1#-f选项,执行文件script1中的sed命令行,目标文件为data1
1.1.4.3. 替换命令格式
s/pattern/replacement/flags
Thereare four types of substitution flags available:
Anumber, indicating the pattern occurrence for which new text should besubstituted.
g—Indicates that new text should be substituted for alloccurrences of the existing text.
p—Indicates that the contents of the original line shouldbe printed.
w file—Write the results of the substitution to a file.
Inthe first type of substitution, you can specify which occurrence of thematching pattern the sed editor should substitute new text for:
1.1.4.4. 地址
[address]command
address {
command1
command2
command3
}
sed ‘2s/dog/cat/’ data1 #替换第二行的cat
sed ‘2,3s/dog/cat/’ data1 #替换第2、3行的cat
sed ‘2,$s/dog/cat/’ data1#替换第2到最后一行的cat
1.1.4.5. 删除
sed ‘3d’ data7#删除第3行
sed ‘/number 1/d’ data7#采用匹配模式,删除含有number 1字符的行
sed ‘/1/,/3/d’ data7#采用匹配模式,删除1~3行
1.1.4.6. 插入文本 i,a
The insert command (i) adds a new linebefore the specified line. 插入到指定位置的前面
The append command (a) adds a new lineafter the specified line. 插入到指定位置的后面
sed ‘$a\ This is a new line of text.’ data7 #插入到最后一行的末尾
1.1.4.7. 修改行 c
sed ‘3c\
> This is a changed line of text.’ data7
1.1.4.8. 转换 y
sed ‘y/123/789/’ data8 #一一对应的替换,将1替换成7,2替换成8,3替换成9,不管1,2,3出现的位置