当前位置: 首页 > news >正文

扬中市建设局网站引擎搜索优化

扬中市建设局网站,引擎搜索优化,做网站公司那家好,直播视频网站建设概念 shell的条件测试目的是得出真和假。 shell 提供的条件测试语法 test 命令 [] 中括号命令 语法*: test条件测试 test命令用来评估一个表达式,他的结果是真,还是假,如果条件为真,那么命令执行状态结果就为0&…

概念

shell的条件测试目的是得出真和假。

shell 提供的条件测试语法
test 命令
[] 中括号命令

语法*:
条件测试

test条件测试

test命令用来评估一个表达式,他的结果是真,还是假,如果条件为真,那么命令执行状态结果就为0,否则就是不为0,通过$?取值。

test命令参数
============检测给定文件名是否存在
-e 判断该文件是否存在,(普通文件,目录),存在就为真,否则为假
============检测给定文件名的类型
-f 判断该文件名是否为文件
-d 判断该文件名是否为目录
-b 判断该文件名是否为块设备
-c 判断该文件名是否为字符设备
-s 判断该文件名是否为socket
-p 判断该文件名是否为管道FIFO
-L 判断该文件名是否为连接
============检测文件的权限
-r 判断该文件名是否可读
-w 判断该文件名是否可写
-x 判断该文件名是否可运行
-u 判断该文件名是否有SUID属性
-g 判断该文件名是否有SGID属性
-k 判断该文件名是否有Sticky bit属性
-s 判断该文件名是否为空白文件
============文件比较 如test file1 -nt file2
-nt (newer than)判断file1是否比file2新
-ot(older than)判断file1是否比file2旧
-ef 判断file1与file2是否为同一个文件,可用在判断hard link的判定上,主要意义在判定两个文件是否均指向同一个inode
============整数之间判定
-eq 两数值相等
-ne 两数值不等
-gt n1大于n2 (greater than)
-lt n1小于n2(less than)
-ge n1大于等于n2 (greater than or equal)
-le n1小于等于n2(less than or equal)
============判定字符串数据
-z 判断字符串是否为0,若字符串为空,则为true
-n 判断字符串是否为非0,若字符串为非空,则为true
= 判断str1与str2是否相等,相等则为true
!= 判断str1与str2是否不想等,不相等则为true
============多重条件判断
-a (and)两状态同时成立,则返回true
-o (or)两状态任意一个成立,则返回true
! 反向状态

test命令实践

-e演示

xiao123@xiao123:~/Downloads/shscripts$ test -e del_data.sh
xiao123@xiao123:~/Downloads/shscripts$ echo $?
0
xiao123@xiao123:~/Downloads/shscripts$ test -e del_data.sh12
xiao123@xiao123:~/Downloads/shscripts$ echo $?
1
xiao123@xiao123:~/Downloads/shscripts$
xiao123@xiao123:~/Downloads/shscripts$ test -e del_data.sh && echo 文件已存在
文件已存在
xiao123@xiao123:~/Downloads/shscripts$ test -e del_data.sh12 && echo 文件已存在
xiao123@xiao123:~/Downloads/shscripts$ test -e del_data.sh12 || echo 文件不存在
文件不存在
xiao123@xiao123:~/Downloads/shscripts$
xiao123@xiao123:~/Downloads/shscripts$ test -e hello && echo 该文件/目录已存在,不再执行创建动作 || mkdir hello
xiao123@xiao123:~/Downloads/shscripts$ test -e hello && echo 该文件/目录已存在,不再执行创建动作 || mkdir hello
该文件/目录已存在,不再执行创建动作
xiao123@xiao123:~/Downloads/shscripts$

-f演示

xiao123@xiao123:~/Downloads/shscripts$ ls
calculation.sh           chaochao_5_finished.png  index.html.11  index.html.2   index.html.28  index.html.6
chaochao_1_finished.png  chaochao_5.jpg           index.html.12  index.html.20  index.html.29  index.html.7
chaochao_1.jpg           del_data.sh              index.html.13  index.html.21  index.html.3   index.html.8
chaochao_2_finished.png  expr_test1.sh            index.html.14  index.html.22  index.html.30  index.html.9
chaochao_2.jpg           expr_test.sh             index.html.15  index.html.23  index.html.31  let_test.sh
chaochao_3_finished.png  hello                    index.html.16  index.html.24  index.html.32
chaochao_3.jpg           index.html               index.html.17  index.html.25  index.html.33
chaochao_4_finished.png  index.html.1             index.html.18  index.html.26  index.html.4
chaochao_4.jpg           index.html.10            index.html.19  index.html.27  index.html.5
xiao123@xiao123:~/Downloads/shscripts$ test -f hello && echo ok || echo no
no
xiao123@xiao123:~/Downloads/shscripts$

-d演示

xiao123@xiao123:~/Downloads/shscripts$ ls
calculation.sh           chaochao_5_finished.png  index.html.11  index.html.2   index.html.28  index.html.6
chaochao_1_finished.png  chaochao_5.jpg           index.html.12  index.html.20  index.html.29  index.html.7
chaochao_1.jpg           del_data.sh              index.html.13  index.html.21  index.html.3   index.html.8
chaochao_2_finished.png  expr_test1.sh            index.html.14  index.html.22  index.html.30  index.html.9
chaochao_2.jpg           expr_test.sh             index.html.15  index.html.23  index.html.31  let_test.sh
chaochao_3_finished.png  hello                    index.html.16  index.html.24  index.html.32
chaochao_3.jpg           index.html               index.html.17  index.html.25  index.html.33
chaochao_4_finished.png  index.html.1             index.html.18  index.html.26  index.html.4
chaochao_4.jpg           index.html.10            index.html.19  index.html.27  index.html.5
xiao123@xiao123:~/Downloads/shscripts$ test -d hello && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$

-z/-n演示

xiao123@xiao123:~/Downloads/shscripts$ test -d hello && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$ test -z "" && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$ test -z " " && echo ok || echo no
no
xiao123@xiao123:~/Downloads/shscripts$ test -n "" && echo ok || echo no
no
xiao123@xiao123:~/Downloads/shscripts$ test -n " " && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$

中括号条件测试

脚本中经常进行条件测试,用的最多的都是中括号。
test和[]的作用是一样的。
注意点:中括号前后都要有空格[ ]

[ -n “${filename}” ]
注意在条件测试中,使用变量必须要添加双引号""

xiao123@xiao123:~/Downloads/shscripts$ file=del_data.sh
xiao123@xiao123:~/Downloads/shscripts$ [ -f "${file}" ] && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$ file=del_data.sh12
xiao123@xiao123:~/Downloads/shscripts$ [ -f "${file}" ] && echo ok || echo no
no
xiao123@xiao123:~/Downloads/shscripts$

双中括号条件测试

双中括号增加了正则表达式的支持,其他与中括号相同。

变量测试

把字符串写入变量中
对变量测试,必须要加双引号

xiao123@xiao123:~/Downloads/shscripts$ file1="鸡你太美.jpg"
xiao123@xiao123:~/Downloads/shscripts$ [[ -f "${file1}" ]] && echo "我是两年半的练习生,鸡你太美" || echo "我是个好人"
我是个好人
xiao123@xiao123:~/Downloads/shscripts$ [[ -f "${file1}" ]] && echo "我是两年半的练习生,鸡你太美" || echo "我是个好人"
我是个好人
xiao123@xiao123:~/Downloads/shscripts$ touch "鸡你太美.jpg"
xiao123@xiao123:~/Downloads/shscripts$ [[ -f "${file1}" ]] && echo "我是两年半的练习生,鸡你太美" || echo "我是个好人"
我是两年半的练习生,鸡你太美
xiao123@xiao123:~/Downloads/shscripts$
字符串测试

比较两个字符串变量的值,是否相等,不等的情况。

= 判断是否相等
!= 判断不相等
! 取结果的反义,颠倒黑白

注意:
对于字符串变量的比较一定要给变量添加双引号
使用等于号判断,左右两边必须有空格

演示

xiao123@xiao123:~/Downloads/shscripts$ [ ! -f "糖果超甜组合.txt" ] && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$ [ -f "糖果超甜组合.txt" ] && echo ok || echo no
no
xiao123@xiao123:~/Downloads/shscripts$
数值比较测试

操作方式
数值计算

  1. 在中括号以及test中数值比较的用法

在中括号中,使用数学比较符号,请添加转义符号

xiao123@xiao123:~/Downloads/shscripts$ [ 2 > 1 ] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [ 1 > 2 ] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [ 1 \> 2 ] && echo yes || echo no
no
xiao123@xiao123:~/Downloads/shscripts$
xiao123@xiao123:~/Downloads/shscripts$ [ 2 \= 2 ] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [ 2 != 2 ] && echo yes || echo no
no   #不等于可以不用添加转义
xiao123@xiao123:~/Downloads/shscripts$ [ 3 != 2 ] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [ 3 \!= 2 ] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [ 3 \!\= 2 ] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [ 3 \!\= 3 ] && echo yes || echo no
no
xiao123@xiao123:~/Downloads/shscripts$
  1. 双中括号
    对中括号的补充,双中括号还支持正则处理。
    在双中括号中不要转义字符。
xiao123@xiao123:~/Downloads/shscripts$ [[ 5 > 6 ]] && echo yes || echo no
no
xiao123@xiao123:~/Downloads/shscripts$ [[ 5 < 6 ]] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [[ 5 = 6 ]] && echo yes || echo no
no
xiao123@xiao123:~/Downloads/shscripts$ [[ 5 != 6 ]] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [[ 5 -ge 6 ]] && echo yes || echo no
no
xiao123@xiao123:~/Downloads/shscripts$ [[ 5 -le 6 ]] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [[ 5 -gt 6 ]] && echo yes || echo no
no
xiao123@xiao123:~/Downloads/shscripts$ [[ 5 -lt 6 ]] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$
逻辑操作符号测试

&& -a 与运算, 两边都为真,结果才为真
|| -o 或运算,两边有一个为真,结果为真
逻辑操作

中括号逻辑运算比较

xiao123@xiao123:~/Downloads/shscripts$ file1=/etc/init.d/rsync
xiao123@xiao123:~/Downloads/shscripts$ file2=/etc/hostname
xiao123@xiao123:~/Downloads/shscripts$ [ -f "${file1}" -a -f "${file2}" ] && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$ file1=/tmp/sqqqq
xiao123@xiao123:~/Downloads/shscripts$ [ -f "${file1}" -a -f "${file2}" ] && echo ok || echo no
no
xiao123@xiao123:~/Downloads/shscripts$ [ -f "${file1}" -o -f "${file2}" ] && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$

双中括号运算比较

xiao123@xiao123:~/Downloads/shscripts$ str1=""
xiao123@xiao123:~/Downloads/shscripts$ str2="yuyu"
xiao123@xiao123:~/Downloads/shscripts$ [[ -n "${str1}" && -n "${str2}" ]] && echo ok || echo no
no
xiao123@xiao123:~/Downloads/shscripts$ [[ -n "${str1}" || -n "${str2}" ]] && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$

脚本开发

要求:接收用户输入,判断它是否等于某个数字。

xiao123@xiao123:~/Downloads/shscripts$ bash ./test_input.sh
Please input a char: 3
脚本出错,必须输入1和2
xiao123@xiao123:~/Downloads/shscripts$ bash ./test_input.sh
Please input a char: 1
1
xiao123@xiao123:~/Downloads/shscripts$ bash ./test_input.sh
Please input a char: 2
2
xiao123@xiao123:~/Downloads/shscripts$ cat ./test_input.sh
#! /bin/bashread -p "Please input a char: " var1[ "${var1}" -eq "1" ] && {echo ${var1}exit 0
}[[ "${var1}" = "2" ]] && {echo ${var1}exit 0
}[ "${var1}" != 2 -a "${var1}" != 1 ] && {echo "脚本出错,必须输入1和2"exit 1
}
xiao123@xiao123:~/Downloads/shscripts$

要求:安装lnmp/lamp脚本开发。

xiao123@xiao123:~/Downloads/shscripts$ mkdir test
xiao123@xiao123:~/Downloads/shscripts$ echo "echo LAMP is installed" > ./test/lamp.sh
xiao123@xiao123:~/Downloads/shscripts$ echo "echo LNMP is installed" > ./test/lnmp.sh
xiao123@xiao123:~/Downloads/shscripts$ chmod +x ./test/lamp.sh
xiao123@xiao123:~/Downloads/shscripts$ chmod +x ./test/lnmp.sh
xiao123@xiao123:~/Downloads/shscripts$

源码

xiao123@xiao123:~/Downloads/shscripts$ cat ./test_install.sh
#! /bin/bashpath=./test[ ! -d  "${path}" ] && mkdir ${path} -pcat << END1. [install lamp]2. [install lnmp]3. [exit]please input the num you want:
ENDread numexpr ${num} + 1 &>/dev/null[ $? -ne 0 ] && {echo "input number must be {1|2|3}"exit 1
}[ ${num} -eq 1 ] && {echo "Strating installing lamp.....waiting..."sleep 2[ -x ${path}/lamp.sh ] || {echo "The file does not exist or can't be execut."exit 1}${path}/lamp.shexit $?
}[ ${num} -eq 2 ] && {echo "Strating installing lnmp.....waiting..."sleep 2[ -x ${path}/lamp.sh ] || {echo "The file does not exist or can't be execut."exit 1}${path}/lnmp.shexit $?
}[ ${num} -eq 3 ] && {echo "byebye"exit 3
}[[ ! ${num} =~ [1-3] ]] && {echo "The number input must be {1|2|3}"exit 4
}
xiao123@xiao123:~/Downloads/shscripts$

运行结果

xiao123@xiao123:~/Downloads/shscripts$ bash ./test_install.sh1. [install lamp]2. [install lnmp]3. [exit]please input the num you want:
e
input number must be {1|2|3}
xiao123@xiao123:~/Downloads/shscripts$ bash ./test_install.sh1. [install lamp]2. [install lnmp]3. [exit]please input the num you want:
3
byebye
xiao123@xiao123:~/Downloads/shscripts$ bash ./test_install.sh1. [install lamp]2. [install lnmp]3. [exit]please input the num you want:
1
Strating installing lamp.....waiting...
LAMP is installed
xiao123@xiao123:~/Downloads/shscripts$ bash ./test_install.sh1. [install lamp]2. [install lnmp]3. [exit]please input the num you want:
2
Strating installing lnmp.....waiting...
LNMP is installed
xiao123@xiao123:~/Downloads/shscripts$
http://www.shuangfujiaoyu.com/news/18453.html

相关文章:

  • 厦门网站设计哪家公司好营销公司排行
  • 微信小程序网站建设公司网站建设费用明细表
  • python能够做网站seo是哪里
  • 优化设计六年级下册语文答案百度seo优化收费标准
  • 遂宁市网站建设国内最新新闻热点事件
  • 纵横网站百度搜索网页版入口
  • 北京做网站建设的公司有哪些百度seo培训班
  • 即墨做网站的免费seo网站自动推广软件
  • 温州科技网站建设刚刚刚刚刚刚刚刚刚刚刚刚刚刚
  • 微信企业公众号开发便宜的seo官网优化
  • 合肥高端网站新产品推广方案范文
  • 58企业网站如何做手游推广个人合作平台
  • 微信网站建设报价襄阳seo优化排名
  • 商家产品展示网站源码如何网页优化
  • 重庆工程建设信息网站手机网站制作平台
  • 怎么看商标有没有注册优化模型有哪些
  • 网站内容做淘宝店铺链接影响排名吗想做一个网站
  • 吕梁网站建设公司简述优化搜索引擎的方法
  • 免费的作文网站建站软件可以不通过网络建设吗
  • 建设网站免费百度推广登录后台登录入口
  • 南京做中英文网站设计关键词网站排名查询
  • 做恋足的视频网站网站快速排名优化报价
  • 织梦做的网站如何上线宁波网站关键词优化代码
  • 专门做手工的网站网络推广员要怎么做
  • 中冶东北建设网站泰安百度推广电话
  • 如何做网站 优帮云长沙网站推广排名
  • 中国十大服装设计院校整站优化cms
  • 在线做任务的网站酒店如何进行网络营销
  • 什么网站都可以进入的浏览器东莞网站制作外包
  • 廊坊网站建设公司百度商务合作电话