当前位置: 首页 > Linux

LinuxShell编程(七)——流程控制语句(一)条件判断语句

时间:2023-04-06 19:39:33 Linux

1.两种判断格式test判断公式【判断公式】(常用)2.根据文件类型判断测试选项函数-bfile判断文件是否存在,是块设备文件-cfile判断文件存在,且为字符设备文件-dfile判断文件是否存在,为目录-efile判断文件是否存在-ffile判断文件是否存在,为普通文件-Lfile判断文件是否存在,是符号链接文件-pfile判断文件是否存在,是管道文件-sfile判断文件是否存在,不为空-Sfile判断文件是否存在,是socket文件1.使用$?判断文件是否存在[root/tmp]#[-e/etc/passwd][root/tmp]#echo$?0[root/tmp]#[-e/etc/passwds][root/tmp]#回声$?12。使用&&和||判断文件是否为目录[root/tmp]#[-d/etc/passwd]&&echo'yes'||echo'no'no[root/tmp]#[-d/etc/]&&echo'yes'||echo'no'yes3.根据文件权限判断测试选项功能-rfile判断文件是否存在,当前用户有读权限-wfile判断文件是否存在,当前用户有写权限-xfile判断文件是否存在,当前用户有执行权限-ufile判断文件是否存在,有SUID权限-gfile判断文件是否存在,有SGID权限-kfile判断文件是否存在,并将SBit权限实例切换回普通用户,创建文件rwx.md,默认权限为rw-rw-r--[vagrant~/rwx]$touchrwx.md[vagrant~/rwx]$lltotalusage0-rw-rw-r--1vagrantvagrant0June104:23rwx.md判断文件权限,有读写权限,但没有执行权限[vagrant~/rwx]$[-rrwx.md]&&echo是||echonoyes[vagrant~/rwx]$[-wrwx.md]&&echoyes||echonoyes[vagrant~/rwx]$[-xrwx.md]&&echoyes||echonono修改文件拥有者为root,修改组权限为可读不可写可执行[vagrant~/rwx]$sudochownrootrwx.md[vagrant~/rwx]$sudochmod654rwx.md[vagrant~/rwx]$lltotalusage0-rw-r-xr--1rootvagrant0Jun104:23rwx.md*这时候用户申请组权限,可以读,不能写,并且可以执行[vagrant~/rwx]$[-wrwx.md]&&echoyes||echonono[vagrant~/rwx]$[-xrwx.md]&&echoyes||echonoyes将文件的所有者改回vagrant[vagrant~/rwx]$sudochownvagrantrwx.md[vagrant~/rwx]$lltotalusage0-rw-r-xr--1vagrantvagrant0June104:23rwx.md*此时用户申请了owner权限,即可读、可写、不可执行[vagrant~/rwx]$[-wrwx.md]&&回显是||echonoyes[vagrant~/rwx]$[-xrwx.md]&&echoyes||echonono[vagrant~/rwx]$./rwx.md-bash:./rwx.md:Insufficientpermissions4.比较测试两个文件的选项File1-ntFile2判断文件1的修改时间是否较新thanfile2file1-otfile2判断文件1的修改时间是否比文件2新oldfile1-effile2判断文件1和文件2的inode号是否一致,可以理解为两个文件是同一个文件。这是判断硬链接的好方法1、间隔1秒比较两个文件的最后修改时间,创建两个文件file1和file2[root/tmp/tmp]#touchfile1;睡觉1;触摸文件2[root/tmp/tmp]#lltotalusage0-rw-r--r--1rootroot0June106:52file1-rw-r--r--1rootroot0June106:52file2比较两个文件的最后修改时间[root/tmp/tmp]#[file1-otfile2]&&echoyes||echonoyes[root/tmp/tmp]#[file1-ntfile2]&&echoyes||回声诺诺2。判断两个文件的inode号是否一致创建file和file2,并创建硬链接file_ln[root/tmp/tmp]#touchfile[root/tmp/tmp]#touchfile2[root/tmp/tmp]#lnfilefile_ln从ll-i第一列可以看出file和file_ln的i节点是一致的[root/tmp/tmp]#ll-itotalusage034117777-rw-r--r--2rootroot0Jun106:57file34117781-rw-r--r--1rootrootroot0Jun106:58file234117777-rw-r--r--2rootroot0Jun106:57file_lnpassed[file-effile_ln]判断只有file和file_ln的i节点一致[root/tmp/tmp]#[file-effile_ln]&&echoyes||echonoyes[root/tmp/tmp]#[file2-effile_ln]&&echoyes||echonono[root/tmp/tmp]#[file-effile2]&&echoyes||echonono5.两个整数比较测试选项函数integer1-eqinteger2判断整数1是否等于整数2Integer1-neInteger2判断Integer1是否不等于Integer2Integer1-gtInteger2判断整数1是不大于整数2整数1-lt整数2判断整数1是否小于整数2整数1-ge整数2判断整数1是否大于等于整数2整数1-le整数2判断整数1是否小于大于等于整数21。整数1是否等于整数2[root~]#[1-eq1]&&echoyes||echonoyes[root~]#[1-eq2]&&echoyes||echonono[root~]#[2-eq1]&&echoyes||回声诺诺2。-ne:判断整数1是否不等于整数2[root~]#[1-ne1]&&echoyes||echonono[root~]#[1-ne2]&&echoyes||echonoyes[root~]#[2-ne1]&&echoyes||echonoyes3.-gt:判断整数1是否大于整数2[root~]#[1-gt1]&&echoyes||echonono[root~]#[1-gt2]&&echoyes||echonono[root~]#[2-gt1]&&echoyes||回声noyes4。-lt:判断整数1是否小于整数2[root~]#[1-lt1]&&echoyes||echonono[root~]#[1-lt2]&&echoyes||echonoyes[root~]#[2-lt1]&&echoyes||呼应诺诺5。-ge:判断整数1是否大于等于整数2[root~]#[1-ge1]&&echoyes||echonoyes[root~]#[1-ge2]&&echoyes||echonono[root~]#[2-ge1]&&echo是的||回声noyes6。-le:判断整数1是否小于等于整数2[root~]#[1-le1]&&echoyes||echonoyes[root~]#[1-le2]&&echoyes||echonoyes[root~]#[2-le1]&&echoyes||echonono6.字符串的判断测试选项函数-zstring判断字符串是否为空-nstring判断字符串是否为Non-emptystring1==string2判断string1和string2是否相等string1!=string2判断string1和string2是否不等于1-zstring:判断字符串是否为Empty[root~]#name=''[root~]#[-z"$name"]&&回声是||echonoyes[root~]#name=Wang[root~]#[-z"$name""]&&echoyes||echonono[root~]#unsetname[root~]#[-z"$name"]&&echoyes||echonoyes2.-nstring:判断字符串是否不为空[root~]#name=''[root~]#[-n"$name"]&&echoyes||echonono[root~]#name=Wang[root~]#[-n"$name"]&&echoyes||echonoyes[root~]#unsetname[root~]#[-n"$name"]&&echoyes||echonono3.string1==string2:判断string1和string2是否相等[root~]#a=123[root~]#b=123[root~]#c=1234[root~]#["$a"=="$c"]&&echoyes||echonono[root~]#["$a"=="$b"]&&echoy是||回声noyes4。String1!=String2:判断字符串1和字符串2是否不相等[root~]#a=123[root~]#b=123[root~]#c=1234[root~]#["$a"!="$b"]&&echo是||echonono[root~]#["$a"!="$c"]&&echoyes||echonoyesVII.判断1-a判断2逻辑与,判断1和判断2都为真,最终结果为真判断1-o判断2逻辑或,判断1和判断2其中之一为真,最终结果为真!判断逻辑非,将原来的判断公式1反转。判断1-a判断2:逻辑与,判断1和判断2都为真,最终结果为真[root~]#[a==a-ab==b]&&回显是||echonoyes[root~]#[a==a-aa==b]&&echoyes||echonono[root~]#[a==b-ab==b]&&echoyes||echonono[root~]#[a==b-ab==a]&&echoyes||回声诺诺2。判断1-o判断2:逻辑或,判断1和判断2有一个为真,则最终结果为真[root~]#[a==a-ob==b]&&echoyes||echonoyes[root~]#[a==a-oa==b]&&echoyes||echonoyes[root~]#[a==b-ob==b]&&echoyes||echonoyes[root~]#[a==b-ob==a]&&echoyes||回声诺诺3。!判断:逻辑非,将原判断取反[root~]#[!a==a]&&echo是||回声没有没有[root~]#[!a==b]&&echo是||回声