llcong 发表于 2018-8-25 10:22:57

Shell理论学习(二)

  30.tee:读取标准输入,然后由标准输出显示,并把这些数据存储在指定的文件中
  执行本命令,test.txt若已经存在,会被清空,若不存在则会建立一个新文件,结束操作ctrl+D
# tee test.txt  
hello my world!
  
hello my world!
  
# cat test.txt
  
hello my world!
  -a以文件追加的方式,把输入的数据接在test.txt的文件尾,并不会把test.txt清空
# tee -a test.txt  
This is a zhuijia!
  
This is a zhuijia!
  
# cat test.txt
  
hello my world!
  
This is a zhuijia!
  31.diff:比较两个文件的差异
# diff file4.txt ../test.txt  
1,2c1,2
  
< haha
  
< hello myworld
  
---
  
> hello my world!
  
> This is a zhuijia!
  32.xargs:由标准输入,安排要执行的命令和参数
  寻找.txt的文件,然后给xargs处理,-n 2 表示执行命令的参数至多两个,也就是说:把找到的.txt文件,两个一组的方式交给diff去比较
# find /root/test/ -name "*.txt"| xargs -n 2 diff  
1,2d0
  
< haha
  
< hello myworld
  33.可以把多个命令弄成一组,然后整组去执行
  方法一:(命令1;命令2;命令3)
  ()会开启一个子shell环境,来执行此括号中的命令组
# (cat test.txt;find /root/test/ -name "*.txt";date +%F)  
hello my world!
  
This is a zhuijia!
  
/root/test/file5.txt
  
/root/test/file2.txt
  
/root/test/file4.txt
  
/root/test/file3.txt
  
2015-01-09
  方法二:{ 命令1;命令2;命令3; }
  和第一种方法不同的是,此法是吧这些命令组在现行的shell中去执行,而非在子shell中执行
# { cat test.txt;find /root/test/ -name "*.txt";date +%F; }  
hello my world!
  
This is a zhuijia!
  
/root/test/file5.txt
  
/root/test/file2.txt
  
/root/test/file4.txt
  
/root/test/file3.txt
  
2015-01-09
  34.记录命令的执行过程
  有时候,我们需要把执行命令所产生的信息记录,以作为排错参考,数据保存之用
  script[日志文件]
  如果不提供自定义的日志文件,默认会把数据存储到typescript这个文件
  执行script指令后,就可以开始操作各种命令,script会忠实的记录下所有的输出信息.如果想要结束操作,执行exit,便可以离开script
root@kaibin ~]# script /tmp/test.txt  
Script started, file is /tmp/test.txt
  
# ls /tmp/test.txt
  
/tmp/test.txt
  
# { cat test.txt;find /root/test/ -name "*.txt";date +%F; }
  
hello my world!
  
This is a zhuijia!
  
/root/test/file5.txt
  
/root/test/file2.txt
  
/root/test/file4.txt
  
/root/test/file3.txt
  
2015-01-09
  
# find /root/test/ -name "*.txt"| xargs -n 2 diff
  
1,2d0
  
< haha
  
< hello myworld
  
# exit
  
exit
  
Script done, file is /tmp/test.txt
  
#查看日志
  
root@kaibin ~]# head /tmp/test.txt
  
Script started on 2015年01月09日 星期五 17时53分07秒
  
# { cat test.txt;find /root/test/ -name "*.txt";date +%F; }
  
hello my world!
  
This is a zhuijia!
  
/root/test/file5.txt
  
/root/test/file2.txt
  
/root/test/file4.txt
  
/root/test/file3.txt
  
2015-01-09
  
# find /root/test/ -name "*.txt"| xargs -n 2 diff
  35.unset:
  unset -v 变量名称
  选项-v表示要取消的是变量
  unset -f 函数名称
  选项-f表示要取消的是函数
  36.常用环境变量

[*]  PS1:主要提示符


[*]  SECONDS
  目前Bash Shell运行的时间(以秒计算)
# echo $SECONDS  
22

[*]  TMOUT
  如果TMOUT大于0,则bash会等待TMOUT秒后,自动结束目前的bash shell
  以下的设定,让bash shell在3秒后自行结束
  TMOUT=3

[*]  $1~$n
  位置参数,若位置参数n超过9以上,则要用${n}来表示,例如${10}是第10个参数

[*]  $0:执行程序的名称
[*]  $*:代表所有的位置参数,并且视为一个字符串
  例如:test.sh abc 123 xyz,则$*的内容为字符串"abc 123 xyz"
[*]  $@:代表所有的位置参数.但$@代表各位置参数组成的串行
  例如:test.sh abc 234 xyz,则$@的内容为"abc","234","xyz"这三个字符串
[*]  $#:位置参数的个数
[*]  $?:上一条命令的执行结果,如果为0为成功,不为0都有问题
[*]  $$:当前进程PID
[*]  $!:上一个后台程序的进程编号
[*]  $_:

[*]  script执行时,bash的绝对路径,例如:/bin/bash
[*]  上一条命令执行时,最后一个位置参数,如上一条命令是:test.sh abc 123 xyz.则$_的值是"xyz"
[*]  检查邮件时,$_的值为邮件文件名



页: [1]
查看完整版本: Shell理论学习(二)