上海专业微信网站建设,网站建设去哪里,工业和信息化部直属大学,php网站开发现状都用什么打开模板涉及到比较和判断的时候#xff0c;要注意整数比较使用-lt#xff0c;-gt#xff0c;ge等比较运算符#xff0c;详情参考#xff1a;整数比较文件测试使用 -d, -f, -x等运算发#xff0c;详情参考#xff1a;文件测试逻辑判断使用 (且)、||(或)、#xff…涉及到比较和判断的时候要注意整数比较使用-lt-gtge等比较运算符详情参考整数比较文件测试使用 -d, -f, -x等运算发详情参考文件测试逻辑判断使用 (且)、||(或)、(取反)字符串比较实用字符串的比较使用以下三个比较运算符 或者()、! 、 、 -n表示判断后面的值是否为空不为空则返回true为空则返回false。下面的一个例子#!/bin/bash#文件名:test.shread -p please input name: nameread -p please input password: pwdif [ -z $name ] || [ -z $pwd ]thenecho hackerelseif [ $name root ] [ $pwd admin ]thenecho welcomeelseecho hackerfifi运行测试ubuntuubuntu:~$ ./test.shplease input name:rootplease input password:adminwelcomeubuntuubuntu:~$ ./test.shplease input name:rootplease input password:hackerubuntuubuntu:~$ ./test.shplease input name:rootplease input password:beyondhackerubuntuubuntu:~$注意比较运算符的两边都有空格分隔同时要注意比较运算符两边的变量是否可能为空比如下面这个例子#!/bin/bash#文件名:test.shif [ $1 hello ];thenecho yeselif [ $1 no ];thenecho nofi运行ubuntuubuntu:~$ ./test.sh./test.sh: line 4: [: : unary operator expected./test.sh: line 7: [: : unary operator expectedubuntuubuntu:~$ ./test.sh helloyesubuntuubuntu:~$ ./test.sh nonoubuntuubuntu:~$ ./test.sh testubuntuubuntu:~$可以看到在代码中想要判断shell命令的第二个参数是否为hello或者no但是在测试的时候如果没有第二个参数那么就变成了 if [ hello ]这个命令肯定是错误的了所以会报错比较好的做法是在判断之前加一个判断变量是否为空 或者使用双引号将其括起来注意必须使用双引号因为变量在双引号中才会被解析。#!/bin/bash#文件名:test.shif [ $1 yes ]; thenecho yeselif [ $1 no ]; thenecho noelseecho nothingfi运行ubuntuubuntu:~$ ./test.shnothingubuntuubuntu:~$ ./test.sh yesyesubuntuubuntu:~$ ./test.sh nonoubuntuubuntu:~$ ./test.sh demonothing这样的话就不会报错了。