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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

过客不予秋风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值