Shell
胡辣汤油条丶
本人发布的文章只为学习之用,无其他用途
展开
-
Centos7开机自启shell脚本
本次以Apache为例:vim /etc/rc.d/rc.local在最下行添加一下内容/home/httpd.shhttpd.sh内容如下#!/bin/bashsystemctl start httpd最后重启服务器测试reboot原创 2021-04-07 10:07:34 · 500 阅读 · 1 评论 -
shell-sed流编辑器
选项含义:-n 静默输出,屏蔽自动打印。-i 直接修改源文件-r 在脚本指令中使用扩展正则表达式-e 允许多个脚本指令被执行-f 执行脚本a,append 追加i,insert 插入d,delete 删除s,substitution 替换p,printf 打印如:$ sed “2a xxxx” ./te...原创 2020-04-02 23:10:02 · 321 阅读 · 0 评论 -
shell-except(自动交互)
1.安装expect yum install -y expect2.expect原理[root@localhost ~]# which expect/usr/bin/expect#!/usr/bin/expect#使用expect脚本执行set timeout 30#设置超时时间spawn ssh root@192.168.2.105#传递指令expect {"yes/...原创 2020-03-19 14:18:31 · 2289 阅读 · 0 评论 -
shell-cut文本处理
-b 代表字节byte-c 代表字符,适用中文-d 设定间隔的字符-f 设定范围[root@localhost ~]# whoroot pts/0 2020-03-15 18:10 (192.168.2.106)root pts/1 2020-03-15 18:11 (192.168.2.106)[root@localhost ~]# w...原创 2020-03-15 18:36:09 · 151 阅读 · 0 评论 -
shell-函数
[root@localhost ~]# cat def.sh #!/bin/bash#定义函数hello(){ echo "hello world"}echo "start running hello define"hello #调用函数[root@localhost ~]# sh def.sh start running hello definehello world...原创 2020-03-06 17:11:51 · 82 阅读 · 0 评论 -
shell-shift使用
[root@localhost ~]# cat shift.sh #!/bin/bashwhile [ ! $# -eq 0 ]do echo "var1:$1 sum:$#" shift #默认左移1个参数,第一个参数被丢弃done[root@localhost ~]# sh shift.sh 1 2 3 4 5var1:1 sum:5var1:2 sum:4va...原创 2020-03-06 17:04:45 · 126 阅读 · 0 评论 -
shell-exit程序控制
[root@localhost ~]# cat exit.sh #!/bin/bashecho "please enter a string : "read str1if [ -z $str1 ] then echo "you enter is null" exit 222fi[root@localhost ~]# sh exit.sh please enter a ...原创 2020-03-06 16:46:09 · 78 阅读 · 0 评论 -
shell-continue
[root@localhost ~]# cat continue.sh #!/bin/bashfor i in 1 2 3do for j in 4 5 6 do if [ $i -eq 2 -a $j -eq 5 ] then continue 1 else echo "$i $j" fi donedone[root@localhost ~]# ...原创 2020-03-04 19:40:45 · 175 阅读 · 0 评论 -
shell-break
[root@localhost ~]# cat break.sh #!/bin/bashfor i in 1 2 3do for j in 4 5 6 do if [ $i -eq 2 -a $j -eq 5 ] then break 2 else echo "$i $j" fi donedone[root@localhost ~]# sh bre...原创 2020-03-04 19:34:37 · 113 阅读 · 0 评论 -
shell-与或非
整数对比-eq : =-ne : !=-gt : >-lt : <-ge : >=-le : <=字符对比!=-n-z与或非-a :与 a=1 [ $a = 1 -a $a != 2 ]-o : 或!: 非命令中的与或非&& : command1 && command2 命令1成...原创 2020-03-04 19:26:17 · 1436 阅读 · 0 评论 -
shell-服务启动脚本
[root@localhost ~]# cat nginx.sh #!/bin/bash#this is a nginx script!case $1 in start) netstat -anlpt | grep nginx if [ $? = 0 ] then echo "nginx server is running" else echo "start ...原创 2020-03-04 18:52:28 · 187 阅读 · 0 评论 -
shell-case语句
[root@localhost user]# cat untar.sh #!/bin/bashcase $1 in *.tar) tar -xvf $1 if [ $? = 0 ] then echo "untar seccess" else echo "untar failed" fi ;; *) echo "null"esac[root@l...原创 2020-03-03 16:30:11 · 93 阅读 · 0 评论 -
shell-while循环
[root@localhost user]# cat while.sh #!/bin/bashi=1while [ $i -le 20 ]do useradd stu$i echo "123456" | passwd --stdin stu$i >>users.txt echo "stu$i" let i++done[root@localhost user]# s...原创 2020-03-02 20:18:14 · 74 阅读 · 0 评论 -
shell-for循环
[root@localhost user]# cat num 12345[root@localhost user]# vim for.sh[root@localhost user]# lsfor.sh num 公共 模板 视频 图片 文档 下载 音乐 桌面[root@localhost user]# sh for.sh 12345[root@loca...原创 2020-03-02 18:56:33 · 123 阅读 · 0 评论 -
shell-检测Apache服务
#!/bin/bashsystemctl status httpdif [ $? = 0 ]then echo "httpd is running"else echo "httpd do not running" read -p "yes or no to restart httpd ?" DO echo $DO ...原创 2020-02-27 20:56:41 · 212 阅读 · 0 评论 -
shell-测试服务器是否宕机
[root@192 media]# ./targethost.sh connect: 网络不可达targethost is down[root@192 media]# ./targethost.sh PING 192.168.1.4 (192.168.1.4) 56(84) bytes of data.64 bytes from 192.168.1.4: icmp_seq=1 ttl=6...原创 2020-02-27 19:46:09 · 549 阅读 · 0 评论 -
条件测试语句
#test命令 用于测试语句[root@centos7 media]# touch 1.txt[root@centos7 media]# ls1.txt[root@centos7 media]# [ -d /media/1.txt ][root@centos7 media]# echo $?1[root@centos7 media]# [ -f /media/1.txt ][r...原创 2020-02-24 22:32:00 · 125 阅读 · 0 评论 -
read-交互式赋值
[user@centos7 ~]$ read yournameeric[user@centos7 ~]$ echo $yournameeric[user@centos7 ~]$ read -p "please enter your passwd:" passwdplease enter your passwd:123456[user@centos7 ~]$ echo $passwd...原创 2020-02-24 21:35:34 · 260 阅读 · 0 评论 -
05-Shell 传递参数
#!/bin/bashecho "shell chuan di can shu"echo "$0" #当前文件的文件名echo "$1" #脚本附带的第1个参数echo "$2" #脚本附带的第2个参数echo "$3" #脚本附带的第3个参数echo "$#" #脚本共附带几个参数echo "$*" #脚本附带的所有参数echo "$@" #脚本附带的所有参数echo "$$...原创 2020-02-14 21:08:41 · 91 阅读 · 1 评论 -
04-shell 数组
#!/bin/bash#定义数组array_name=(0 1 2 3 4 5)#读取数组echo ${array_name[2]}#获取数组长度的方法与获取字符串长度的方法相同#!/bin/basharray_name=(0 1 2 3 4 5)echo ${array_name[2]}# 取得数组元素的个数echo ${#array_name[*]} ${#array_...原创 2020-02-14 20:05:05 · 81 阅读 · 0 评论 -
03-shell 拼接字符串
#!/bin/bashyour_name="runoob"# 使用双引号拼接greeting="hello, \"$your_name\" !"#这里的\"表示转义输出为:"greeting_1="hello, ${your_name} !"echo $greeting echo $greeting_1原创 2020-02-14 19:39:05 · 298 阅读 · 0 评论 -
02-shell 定义变量
#!/bin/bashyour_name='eric' #定义变量echo $your_name #输出变量的两种方式,推荐使用${},好习惯不解释echo ${your_name}#定义只读变量readonly your_nameecho ${your_name}#删除变量,但是不能删除只读变量unset your_nameecho ${your_name}下图输出结...原创 2020-02-14 19:28:16 · 82 阅读 · 0 评论 -
01-初学shell(这里是指bash版本的shell,也是最常见的shell)
1.首先,创建一个shell的可执行文件vim ./test.sh这里用的vim编辑器,./表示当前目录,文件名为test,后缀为.sh2.写入文件内容#!/bin/bashecho 'hello world !'#! 是一个约定的标记,它告诉系统这个脚本需要什么解释器来执行,即使用哪一种 Shell。echo为输入的意思,相当于python3中的print()然后退出编辑器的...原创 2020-02-14 19:06:31 · 109 阅读 · 0 评论