shell脚本一

第六章

1、编写脚本
编写脚本
[root@centos ~]# vim first.sh
在这里插入图片描述
2、脚本文件添加执行权限
[root@centos~]# chmod +x first.sh
[root@centos ~]# ./first.sh
[root@centos ~]# source first.sh
root@centos ~]# sh first.sh
1.重定向输出
不直接输出屏幕显示
[root@centos ~]# uname -p > kernel.txt
[root@centos ~]# cat kernel.txt
在这里插入图片描述
2.将数据追加到文件中
[root@centos ~]# uname -r >> kernel.txt
[root@centos ~]# cat kernel.txt
在这里插入图片描述
重定向输入
1、创建存放密码文件
[root@centos ~]# vim passwd.txt
在这里插入图片描述
2、创建用户
[root@centos ~]# useradd ggg
3、将passwd文件中的内容输入到jerry用户的密码
[root@centos ~]# passwd --stdin jerry < passwd.txt
错误重定向
[root@centos ~]# tar jcf /nonedir/etc.tgz /etc/ 2> error.log
[root@centos ~]# cat error.log
管道符操作
netstat -anptu | grep sshd
[root@centos ~]# grep “/bin/bashKaTeX parse error: Expected 'EOF', got '#' at position 94: …[root@centos ~]#̲ grep "/bin/bas… /etc/passwd | awk -F: '{print $1,$3}
自定义变量
创建自定义变量
[root@centos ~]# Product=Python
[root@centos ~]# Version=2.17.13
查看和引用变量的值
[root@centos ~]# echo $Product
在这里插入图片描述
[root@centos ~]# echo $Product $Version
在这里插入图片描述
添加{ }
[root@centos ~]# echo $Product2.5
在这里插入图片描述
双引号
将引用该变量值赋值给新的变量
[root@centos ~]# PYTHON=Python 2.7.13
在这里插入图片描述
[root@centos ~]# PYTHON=“Python 2.7.13”
[root@centos ~]# echo $PYTHON
在这里插入图片描述
[root@centos ~]# SQLServer=“SQLServer $Version”
[root@centos ~]# echo $SQLServer
在这里插入图片描述
单引号
1.不能赋值
[root@centos ~]# SQLServer=‘SQLServer $Version’
[root@centos ~]# echo $SQLServer
在这里插入图片描述
反撇
1、命令替换,提取命令执行的结果
[root@centos ~]# Denylist=‘grep -v “^#” /etc/vsftpd/ftpusers’
[root@centos ~]# echo $DenyList
在这里插入图片描述
[root@centos ~]# rpm -qc $(rpm -qf $(which useradd))
在这里插入图片描述
[root@centos ~]# read ToDirl
在这里插入图片描述
[root@centos ~]# read ToDirl
[root@centos ~]# echo $TODirl
输入变量赋值read 结合-p
[root@centos ~]# read -p “请指定备份存放目.:” ToDir2‘
在这里插入图片描述
[root@centos ~]# echo KaTeX parse error: Expected 'EOF', got '#' at position 97: …[root@centos ~]#̲ echo "Product KaTeX parse error: Expected 'EOF', got '#' at position 88: … root@centos ~]#̲ bash [root@cen…Product KaTeX parse error: Expected 'EOF', got '#' at position 25: …[root@centos ~]#̲ exit 全局 [root@…Product $Version
在这里插入图片描述
[root@centos ~]# exit
[root@centos ~]# export FQDN=“www.jb-aptech.com.cn”
[root@centos ~]# echo $FQDN
在这里插入图片描述
管道符 位置变量
1、查看信息
[root@centos01 ~]# df -hT
在这里插入图片描述
2数值变量的运算
[root@centos ~]# x=35
[root@centos ~]# y=16
[root@centos ~]# expr $x + $y
在这里插入图片描述
[root@centos ~]# Ycube=‘expr $Y * $Y * $Y’
[root@centos ~]# echo $Ycube
在这里插入图片描述
环境变量
[root@centos ~]# env
[root@centos ~]# ls -lh /root/first.sh
在这里插入图片描述
[root@centos ~]# echo KaTeX parse error: Expected 'EOF', got '#' at position 21: …[root@centos ~]#̲ first.sh [root…PATH:/root”
[root@centos ~]# echo $PATH
在这里插入图片描述
[root@centos ~]# first.sh
[root@centos ~]# history | wc -l
在这里插入图片描述
位置变量
[root@centos ~]# vim adder2num.sh
在这里插入图片描述
root@centos ~]# chmod +x adder2num.sh
[root@centos ~]# ./adder2num.sh 12 34
[root@centos ~]# ./adder2num.sh 56 78

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值