码迷,mamicode.com
首页 > 系统相关 > 详细

shell编程

时间:2019-12-29 17:01:42      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:外部   res   bash   bsp   world   hello   字符串   文件   test   

一、文件数据操作

(一)向文件中追加数据

1、向a.txt文件末尾写入“what are you doing?”

方法一:sed命令

sed -i ‘$awhat are you doing?‘ a.txt

方法二:重定向方法

echo "what are you doing?" >> a.txt

(二)修改文件中的数据

1、向a.txt文件中“what”所在行修改为“how do you do?”

[root@localhost ~]# cat a.txt 
hello
hello world
what are you doing?
[root@localhost ~]# sed -i ‘/what/chow‘ a.txt
[root@localhost ~]# cat a.txt 
hello
hello world
how

 

二、文件检查

1、检查ifcfg-ens33文件是否存在于/etc/sysconfig/network-scripts目录中

test -f /etc/sysconfig/network-scripts/ifcfg-ens33

2、检查ifcfg-ens33文件是否存在于/etc/sysconfig/network-scripts目录中,并且是否为空

test -s /etc/sysconfig/network-scripts/ifcfg-ens33

 

三、变量操作

1、变量拼接

变量与变量拼接

[root@localhost ~]# a="hello"
[root@localhost ~]# b=" world"
[root@localhost ~]# c=$a$b
[root@localhost ~]# echo $c
hello world

变量与字符串拼接

[root@localhost ~]# a="hello"
[root@localhost ~]# b=$a" world"
[root@localhost ~]# echo $b
hello world

2、变量赋值

把运行的结果赋值给变量

[root@localhost ~]# result=`ls`
[root@localhost ~]# echo $result
anaconda-ks.cfg a.txt hey ifcfg- network_cfg.sh

3、sed命令引用外部变量(把a.txt文件中hello所在行修改为res变量的值)

[root@localhost ~]# vim b.txt 
[root@localhost ~]# cat b.txt 
are you sure?
hello
[root@localhost ~]# res="yeah"
[root@localhost ~]# sed -i ‘/hello/c‘"$res"‘‘ b.txt 
[root@localhost ~]# cat b.txt 
are you sure?
yeah

shell编程

标签:外部   res   bash   bsp   world   hello   字符串   文件   test   

原文地址:https://www.cnblogs.com/tom-blogs/p/12115236.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!