shell例子

(1)函数调用:
shell例子_第1张图片
(2)[root@localhost ~]# netstat -antup |grep rsync
[root@localhost ~]# rsync --daemon
[root@localhost ~]# netstat -antup |grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 1304/rsync
tcp6 0 0 :::873 ::? LISTEN 1304/rsync
[root@localhost ~]# pkill rsync
[root@localhost ~]# netstat -antup |grep rsync

shell例子_第2张图片

[root@localhost ~]# sh rsync.sh start
rsync 已启动
[root@localhost ~]# sh rsync.sh stop
rsync 已停止
[root@localhost ~]# sh rsync.sh restart
rsync 停止失败
rsync 已停止
rsync 已启动

在/etc/profile 中加入export PATH=$PATH:/root
(由于脚本名和命令名重复,执行时有问题,因此将脚本名改为RSYNC,避免冲突)

(3)
shell例子_第3张图片
(4)从“标准输入”读入n次字符串,每次输入的字符串保存在数组array里,,执行后输入数组元素
shell例子_第4张图片
(5)将字符串里的字母逐个放入数组,并输出到“标准输出”
shell例子_第5张图片
(6)把1-3 3个数字存到数组里 分别乘以8 然后依次输出。
shell例子_第6张图片
[root@localhost ~]# sh test8.sh
8
16
24

shell例子_第7张图片
(7)
shell例子_第8张图片

[root@localhost test]# sh test1.sh
请输入您要测试的主机ip:192.168.1.46
success!
[root@localhost test]# sh test1.sh
请输入您要测试的主机ip:192.168.26.66
fith

(8)
如果此用户不存在,则自动创建
shell例子_第9张图片
[root@localhost test]# sh test2.sh
请输入要查看的用户名name:aa
This user is exit

(9)
shell例子_第10张图片[root@localhost test]# sh test3.sh
version number is 3
version number more than the 10

(10)
shell例子_第11张图片
shell例子_第12张图片
[root@localhost test]# sh test4.sh
httpd is sleeping
[root@localhost test]# systemctl start httpd
[root@localhost test]# sh test4.sh
httpd is running

(11)
shell例子_第13张图片

shell例子_第14张图片
[root@localhost test]# rpm -qa vsftpd
[root@localhost test]# echo $?
0
[root@localhost test]# vim test5.sh
[root@localhost test]# rpm -qa |grep vsftpd
[root@localhost test]# echo $?
1

你可能感兴趣的:(shell例子)