bash测试test详解
概述
- 任何相对完整的计算机语言都能够测试某个条件,然后根据测试的结果采取不同的动作。对于测试条件,
Bash使用test命令、各种方括号和圆括号、if/then结构等来测试条件。
7.1. Test Constructs
-
一个if/then语句结构测试一个或多个命令的退出状态是否为0(因为在unix系统中0表示’执行成功’),
如果为0,就执行语句后面的命令。 -
Bash中有个专用的命令叫[(左中括号,bash特殊字符之一)。它是内置命令test别名,为提升效率
其同时也是bash的内置命令。该命令视其接受的参数为比较表达式或文件测试(测试文件是否存在、
文件类型、文件权限等)并且返回一个对应于比较结果的退出状态(如果比较或测试结果为真则返回0,否则返回1)。 -
在bash2.02版本中,bash新增了[[ … ]]叫扩展的test测试命令,其进行比较时更贴合其他编程语言
的风格。需要注意的是[[是一个bash关键字而非命令。bash视[[ $a -lt $b ]]为单个元素,返回一个退出状态。
[root@centos8 ~]#type [[
[[ is a shell keyword
[root@centos8 ~]#type [
[ is a shell builtin
[root@centos8 ~]#type test
test is a shell builtin
[root@centos8 ~]#which [
/usr/bin/[
[root@centos8 ~]#which test
/usr/bin/test[root@centos8 ~]#a=3
[root@centos8 ~]#b=4
[root@centos8 ~]#[[ $a -lt $b ]]
[root@centos8 ~]#echo $?
0
[root@centos8 ~]#a=5
[root@centos8 ~]#[[ $a -lt $b ]]
[root@centos8 ~]#echo $?
1
- ‘(( … ))’ 和 ‘let …’ 结构用来进行简单的数学运算,也会返回一个退出状态,退出状态决定于其里面的
算术表达式展开后的结果是否是非0值。这些算术运算展开结构可能会被用来进行算术比较。
(( 0 && 1 )) # 逻辑与
echo $? # 1 ***
# And so ...
let "num = (( 0 && 1 ))"
echo $num # 0
# But ...
let "num = (( 0 && 1 ))"
echo $? # 1 ***
(( 200 || 11 )) # 逻辑或
echo $? # 0 ***
# ...
let "num = (( 200 || 11 ))"
echo $num # 1
let "num = (( 200 || 11 ))"
echo $? # 0 ***
(( 200 | 11 )) # 逐位或
echo $? # 0 ***
# ...
let "num = (( 200 | 11 ))"
echo $num # 203
let "num = (( 200 | 11 ))"
echo $? # 0 ***
# "let" 结构和双圆括号的返回状态相同。
-
“let” 结构和双圆括号的返回状态相同。
-
注意:某个算术表达式的退出状态不是该算术表达式计算错误的值。
var=-2 && (( var+=2 )) # 此处算术表达式值为0,退出状态为1
echo $? # 1
var=-2 && (( var+=2 )) && echo $var # 此处由于算术表达式为0,退出状态为1;bash认为非0的退出状态是命令未执行成功,导致$$后面的echo命令不在执行# Will not echo $var!
- if不仅仅可以测试中括号中的条件,还可以测试任何命令
if cmp a b &> /dev/null # 压缩标准输出和错误输出.
then echo "Files a and b are identical."
else echo "Files a and b differ."
fi
# The very useful "if-grep" construct:
# -----------------------------------
if grep -q Bash filethen echo "File contains at least one occurrence of Bash."
fi
word=Linux
letter_sequence=inu
if echo "$word" | grep -q "$letter_sequence"
# The "-q" option to grep suppresses output.
# -q 选项不输出任何信息到标准输出
thenecho "$letter_sequence found in $word"
elseecho "$letter_sequence not found in $word"
fi
if COMMAND_WHOSE_EXIT_STATUS_IS_0_UNLESS_ERROR_OCCURREDthen echo "Command succeeded."else echo "Command failed."
fi
例7-1. 什么才是真?
#!/bin/bash
# Tip:
# 如果你不确定某个条件的结果,那最好在if测试结构中测试其。
echo
echo "Testing \"0\""
if [ 0 ] # zero
thenecho "0 is true."
else # Or else ...echo "0 is false."
fi # 0 为真.
echo
echo "Testing \"1\""
if [ 1 ] # one
thenecho "1 is true."
elseecho "1 is false."
fi # 1 为真.
echo
echo "Testing \"-1\""
if [ -1 ] # -1
thenecho "-1 is true."
elseecho "-1 is false."
fi # -1 为真.
echo
echo "Testing \"NULL\""
if [ ] # NULL (空条件)
thenecho "NULL is true."
elseecho "NULL is false."
fi # NULL 空为假。
echo
echo "Testing \"xyz\""
if [ xyz ] # 随机字符串
thenecho "Random string is true."
elseecho "Random string is false."
fi # 随机字符串为真.
echo
echo "Testing \"\$xyz\""
if [ $xyz ] # 测试变量$xyz是否为空, 但是...# $xyz只是一个未初始化的变量.
thenecho "Uninitialized variable is true."
elseecho "Uninitialized variable is false."
fi # 未初始化的字符串为假.
echo
echo "Testing \"-n \$xyz\""
if [ -n "$xyz" ] # 有点卖弄学问的做法.
thenecho "Uninitialized variable is true."
elseecho "Uninitialized variable is false."
fi # 同样未初始化的字符串为假.
echo
xyz= # 初始化了,但是值为空.
echo "Testing \"-n \$xyz\""
if [ -n "$xyz" ]
thenecho "Null variable is true."
elseecho "Null variable is false."
fi # 空变量为假.
echo
# 什么时候'假'为真呢?(When is "false" true?)
echo "Testing \"false\""
if [ "false" ] # 此处 "false" 只是一个字符串而已...
thenecho "\"false\" is true." #+ 结果为真啦啦啦.
elseecho "\"false\" is false."
fi # 这时候'假'为真.
echo
echo "Testing \"\$false\"" # 再次测试'假',此时的'假'为未初始化的变量.
if [ "$false" ]
thenecho "\"\$false\" is true."
elseecho "\"\$false\" is false."
fi # 此时测试结构为假.
# What would happen if we tested the uninitialized variable "$true"?
echo
exit 0
if [ condition-true ]
thencommand 1command 2...
else # 或者Or else ...# 在下面写测试条件为假时的代码.command 3command 4...
fi
- 条件测试结构中当if和then在同一行时,必须使用分号结束if语句。if和then都为bash关键字。
关键字(或者命令)所开始的语句,必须在同一行的下一个语句前使用分号结束。
if [ -x "$filename" ]; then
- Else if and elif
- elif 是 else if 的简写.用来实现嵌套语句.
if [ condition1 ]
thencommand1command2command3
elif [ condition2 ]
# Same as else if
thencommand4command5
elsedefault-command
fi
- 结构’if test condition-true’和结构’if [ condition-true ]‘完全等价。当使用后者时,左中括号’['会调用test命令。
所以右中括号在if/test结构中不是严格需要的,然而新版本的bash要求必须跟上。 - 特别指出:test命令时bash内置命令。其用来测试文件类型和比较字符串。因此,
在bash脚本中test命令不会调用外部二进制命令/usr/bin/test,此时的test命令是sh-utils包的一部分。
bash$ type test
test is a shell builtin
bash$ type '['
[ is a shell builtin
bash$ type '[['
[[ is a shell keyword
bash$ type ']]'
]] is a shell keyword
bash$ type ']'
bash: type: ]: not found
- 如果由于某些原因,你希望在脚本中使用/usr/bin/test,那可以使用完整的路径名指明.
例7-2.test,/usr/bin/test,[ ],和/usr/bin/[
#!/bin/bash
echo
if test -z "$1"
thenecho "No command-line arguments."
elseecho "First command-line argument is $1."fi
echo
if /usr/bin/test -z "$1" # 和内置命令"test"等价.
# ^^^^^^^^^^^^^ # 指明了完整的路径.
thenecho "No command-line arguments."
elseecho "First command-line argument is $1."
fi
echo
if [ -z "$1" ] # 和上面的代码块功能相同.
# if [ -z "$1" # 该代码应该可以正常工作,但是...
#+ Bash说后面的右中括号必须带,哎.
thenecho "No command-line arguments."
elseecho "First command-line argument is $1."
fi
echo
if /usr/bin/[ -z "$1" ] # 和上面的代码块功能相同.
thenecho "No command-line arguments."
elseecho "First command-line argument is $1."
fi
echo
exit 0
- 相比’[ ]‘,’[[ ]]'测试结构更加健壮。后者是扩展的test命令,从ksh88版本中借鉴而来。
- 在’[[ ]]’ 结构中不允许文件名展开或者单词分割,但是允许参数展开和命令替换。
file=/etc/passwd
if [[ -e $file ]]
thenecho "Password file exists."
fi
- 使用’[[ … ]]‘测试结构,而不使用’[ … ]‘可以避免脚本中很多逻辑错误。
比如:&&,||,<,>操作符
在’[[ ]]‘结构中适用,但是在’[ ]'结构中报错。 - 对于八进制/十六进制的算术运算在’[[ ]]'结构中亦支持。
# [[ 八进制/十六进制运算 ]]
# Thank you, Moritz Gronbach, for pointing this out.
decimal=15
octal=017 # = 15 (decimal)
hex=0x0f # = 15 (decimal)
if [ "$decimal" -eq "$octal" ]
thenecho "$decimal equals $octal"
elseecho "$decimal is not equal to $octal" # 结果是15不等于017
fi # 在单中括号结构中不计算 [ single brackets ]!
if [[ "$decimal" -eq "$octal" ]]
thenecho "$decimal equals $octal" # 15 等于 017
elseecho "$decimal is not equal to $octal"
fi # 双中括号中计算 [[ double brackets ]]!
if [[ "$decimal" -eq "$hex" ]]
thenecho "$decimal equals $hex" # 15 等于 0x0f
elseecho "$decimal is not equal to $hex"
fi # [[ $hexadecimal ]] 单独引用一个十六进制数,也会自动计算为十进制!
- 在if后,要么test命令要么测试中括号都是必须存在的([] [[ ]])。
dir=/home/bozo
if cd "$dir" 2>/dev/null; then # "2>/dev/null"将会重定向标准错误.echo "Now in $dir."
elseecho "Can't change to $dir."
fi
- 如上面例子,"if 命令"结构会返回命令的退出状态。
- 同样,下面在多个结构的组合用法中,一个处于中括号内的测试条件可以不需要if。
var1=20
var2=22
[ "$var1" -ne "$var2" ] && echo "$var1 is not equal to $var2"
home=/home/bozo
[ -d "$home" ] || echo "$home directory does not exist."
- 双圆括号结构(( ))展开并计算数学运算表达式。如果表达式运算结果为0,则其返回一个为1的退出状态,或者假"false"。一个非0的计算值则返回一个退出状态0,或者真"true"。
例7-3.使用(( ))结构测试算术运算结果
#!/bin/bash
# arith-tests.sh
# Arithmetic tests.
# (( ... ))结构计算并测试数学运算表达
# (( ... ))结构对于数学运算表达式的测试结果退出状态与[ ... ]结构相反!
# (( ... ))结构中运算结果非0为真,运算结果为0时退出状态为假。
(( 0 ))
echo "Exit status of \"(( 0 ))\" is $?." # 1
(( 1 ))
echo "Exit status of \"(( 1 ))\" is $?." # 0
(( 5 > 4 )) # true
echo "Exit status of \"(( 5 > 4 ))\" is $?." # 0
(( 5 > 9 )) # false
echo "Exit status of \"(( 5 > 9 ))\" is $?." # 1
(( 5 == 5 )) # true
echo "Exit status of \"(( 5 == 5 ))\" is $?." # 0
# (( 5 = 5 )) gives an error message.
(( 5 - 5 )) # 0
echo "Exit status of \"(( 5 - 5 ))\" is $?." # 1
(( 5 / 4 )) # Division o.k.
echo "Exit status of \"(( 5 / 4 ))\" is $?." # 0
(( 1 / 2 )) # 除法结果小于1.
echo "Exit status of \"(( 1 / 2 ))\" is $?." # 小于1的结果被圆整为0.# 1
(( 1 / 0 )) 2>/dev/null # 使用0作为除数非法.
# ^^^^^^^^^^^
echo "Exit status of \"(( 1 / 0 ))\" is $?." # 1
# ======================================= #
# (( ... )) 该结构也常常被用在 if-then 测试结构中.
var1=5
var2=4
if (( var1 > var2 ))
then #^ ^ Note: Not $var1, $var2. Why?echo "$var1 is greater than $var2"
fi # 5 is greater than 4
exit 0
7.2. 文件测试操作(File test operators)
- if [ … ] 如果测试条件…为真则返回退出状态值0
-e 测试文件是否存在
-a 同上,已经被弃用,不推荐使用
-f 测试文件是否为普通文件(不是文件夹或者设备文件)
-s 测试文件是否非空(大小不是0)
-d 测试文件是否是一个文件夹
-b 测试文件是否是一个块设备
-c 测试文件是否是一个字符设备
device0="/dev/sda2"
if [ -b "$device0" ]
thenecho "$device0 is a block device."
fi
# /dev/sda2 是一个块设备.device1="/dev/ttyS1"
if [ -c "$device1" ]
thenecho "$device1 is a character device."
fi
# /dev/ttyS1 是一个字符设备.
-p 测试文件是否为管道文件.
function show_input_type()
{[ -p /dev/fd/0 ] && echo PIPE || echo STDIN # 此处/dev/fd/0表示标准输入
}
show_input_type "Input" # STDIN 标准输入
echo "Input" | show_input_type # PIPE 管道
-h 测试文件是否是一个符号链接
-L 测试文件是否是一个符号
-S 测试文件是否是一个socket文
-t 测试文件(或者文件描述符)是否与某个终端关联
该测试可以测试脚本中的标准输入[ -t 0 ]或者标准输出[ -t 1 ]是否是一个终端。
-r 运行本测试的用户是否对文件有读权限
-w 写权限
-x 执行权限
-g 文件或者文件夹是否设置SGID标志位
如果某个文件或者文件夹上设置有SGID权限,那么在该文件夹下创建的文件属主为该文件夹的属主。
-u 文件或者文件夹是否设置suid
如果某个属于root的二进制文件上被root设置了SUID标志位,则不管是谁运行,该可执行文件都以root权限运行;
单某个可执行文件必须要访问系统硬件时此功能非常有用。如果缺失SUID标志位,这些二进制文件不能被非root用户运行。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BLdVwEBX-1691025062052)(png/2019-10-21-12-40-31.png)]
如上图:有SUID标志位的文件权限模式的执行权限位标识为s而不再是x(rwx --> rws).
-k 测试sticky位是否设置
如果一个文件设置了sticky位,那么该问价会被保存在cache内便于访问
如果某个文件夹被设置了sticky位,那么该文件夹的写权限将会被限制。
设置sticky位后,其他用户的执行权限模式不在表现为x而是t(rwx --> rwt)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-noSbHD9e-1691025062054)(png/2019-10-21-12-56-29.png)]
也就是说:如果一个用户对某个设置有sticky位的目录有读权限但不是该目录的属主,那么他只能删除该目录下其拥有
的文件。这样做可以防止用户在公共文件夹意外删除别人的文件,例如/tmp文件夹。(当然,root用户是可以删除和更改的)
-O 测试自己是否是文件属主
-G 测试文件的GID是否和自己相同
-N 测试文件自最后一次读以来是否被修改过
f1 -nt f2 测试文件f1是否是比f2新
f1 -ot f2 测试文件f1是否是比f2旧
f1 -ef f2 测试文件f1和f2是否都是同一个文件的硬链接
! 对上面所列的条件取反。(如果条件为空,返回真,如下面例子)
[root@centos8 ~]#if [[ ! ]]; then echo true; fi
true
例7-4. 测试失效的链接(Testing for broken links)
#!/bin/bash
# broken-link.sh
# Written by Lee bigelow <ligelowbee@yahoo.com>
# Used in ABS Guide with permission.
# 该脚本找出失效的符号链接并引用后输出。以便于给xargs处理。
#+ 例如. sh broken-link.sh /somedir /someotherdir|xargs rm
# 只不过更加推荐下面的方法:
# find "somedir" -type l -print0|\
# xargs -r0 file|\
# grep "broken symbolic"|
# sed -e 's/^\|: *broken symbolic.*$/"/g'
#
# 注意: 谨慎对待 /proc 文件系统和任何循环链接
################################################################
# 下面的语句表示如果没有指定目录参数传给脚本就将路径设置为当前的工作
# 目录。否则使用指定的目录参数进行搜索。
######################
[ $# -eq 0 ] && directorys=`pwd` || directorys=$@# 下面的函数检查传给脚本的目录中为符号链接并且不存在的文件,检查到后引用起来并打印。
# 如果是目录中的子目录,则将该目录再次传给函数检查。
##########
linkchk () {for element in $1/*; do[ -h "$element" -a ! -e "$element" ] && echo \"$element\"[ -d "$element" ] && linkchk $element# Of course, '-h' tests for symbolic link, '-d' for directory.done
}# 下面将每个传给脚本的合法目录参数传给linkchk()函数,如果不是合法目录,则打印错误信息和用法。
##################
for directory in $directorys; doif [ -d $directory ]then linkchk $directoryelseecho "$directory is not a directory"echo "Usage: $0 dir1 dir2 ..."fi
done
exit $?
7.3. 其它的比较操作
-
一个二进制比较操作符号比较两个变量或者比较数量。整数或者字符串的比较使用特定的符号集合。
-
整数比较
-eq 是否相等
if [ "$a" -eq "$b" ]-ne 是否不等
if [ "$a" -ne "$b" ]-gt $a是否大于$b
if [ "$a" -gt "$b" ]-ge $a是否大于或等于$b
if [ "$a" -ge "$b" ]-lt $a是否小于$b
if [ "$a" -lt "$b" ]-le $a是否小于或等于$b
if [ "$a" -le "$b" ]< 小于(必须在双圆括号结构中)
(("$a" < "$b"))<= 小于或等于(必须在双圆括号结构中)
(("$a" <= "$b"))> 大于(必须在双圆括号结构中)
(("$a" > "$b"))>= 大于或等于(必须在双圆括号结构中)
(("$a" >= "$b"))
- 字符串比较
= is equal to
if [ "$a" = "$b" ]注意=两边的空格.
if [ "$a"="$b" ] # 注意:该写法与上面的不等价.
== is equal to
if [ "$a" == "$b" ]
该写法是 = 的近似写法.
[[ $a == z* ]] # 如果 $a 以字母"z"开头则为真(模式匹配).
[[ $a == "z*" ]] # 将"z*"视为普通字符串.
[ $a == z* ] # 此处会视为通配和单词分割.
[ "$a" == "z*" ] # 将"z*"视为普通字符串.
# Thanks, Stéphane Chazelas
!= 是否不等
if [ "$a" != "$b" ]
该操作符也可以在[[ ... ]]结构中参与模式匹配操作.
< 是否小于,以ASCII字符的顺序比较
if [[ "$a" < "$b" ]]
if [ "$a" \< "$b" ]
注意: "<" 在 [ ] 结构中需要被转义.
> 是否大于,以ASCII字符的顺序比较
if [[ "$a" > "$b" ]]
if [ "$a" \> "$b" ]
-z 判断某字符串是否为空,也就是长度为0
String='' # Zero-length ("null") string variable.
if [ -z "$String" ]
thenecho "\$String is null."
elseecho "\$String is NOT null."
fi # $String is null.
-n 判断某字符串是否非空.
- 注意:-n 测试选项需要被测试的字符串被双引号引用起来,一般总是将被测试的字符串引用起来。
例 7-5. 算术运算和比较
#!/bin/bash
a=4
b=5
# Here "a" and "b" can be treated either as integers or strings.
# There is some blurring between the arithmetic and string comparisons,
#+ since Bash variables are not strongly typed.
# Bash permits integer operations and comparisons on variables
#+ whose value consists of all-integer characters.
# Caution advised, however.
echo
if [ "$a" -ne "$b" ] # 数学运算比较
thenecho "$a is not equal to $b"echo "(arithmetic comparison)"
fi
echo
if [ "$a" != "$b" ] # 字符串比较
thenecho "$a is not equal to $b."echo "(string comparison)"# "4" != "5"# ASCII 52 != ASCII 53
fi
# 在该例子中 "-ne" 和 "!=" 都可以.
echo
exit 0
例 7-6. 测试某字符串是否为空
#!/bin/bash
# str-test.sh: Testing null strings and unquoted strings,
#+ but not strings and sealing wax, not to mention cabbages and kings . . .
# Using if [ ... ]
# If a string has not been initialized, it has no defined value.
# 如果一个字符串没有被定义,其没有被定义的值,这种状态叫'null'
if [ -n $string1 ] # string1 没有被初始化或定义过.
thenecho "String \"string1\" is not null."
else echo "String \"string1\" is null."
fi
# 此处认为$string1非空,即使它没有被初始化.
echo
# 再试试?
if [ -n "$string1" ] # 这次 $string1 被双引号引用起来.
thenecho "String \"string1\" is not null."
else echo "String \"string1\" is null."
fi # 为空!
echo
if [ $string1 ] # 此处变量string未引起
thenecho "String \"string1\" is not null."
else echo "String \"string1\" is null."
fi # 为空,正确.
# 最好还是引用起来需要测试的字符串,不管是变量还是字符串本身.
#
# As Stephane Chazelas points out,
# if [ $string1 ] has one argument, "]"#该测试结果只有一个参数"]"
# if [ "$string1" ] has two arguments, the empty "$string1" and "]"#该测试结果有两个参数空字符串"$string1"和"]"
echo
string1=initialized
if [ $string1 ] # 赋值后, $string1 .
thenecho "String \"string1\" is not null."
else echo "String \"string1\" is null."
fi # 非空.
# 然而,最好还是引起来,因为...
string1="a = b"
if [ $string1 ]
thenecho "String \"string1\" is not null."
else echo "String \"string1\" is null."
fi # 此处不引用导致错误!
exit 0 # Thank you, also, Florian Wisser, for the "heads-up".
例 7-7. zmore
#!/bin/bash
# zmore
# View gzipped files with 'more' filter.
E_NOARGS=85
E_NOTFOUND=86
E_NOTGZIP=87
if [ $# -eq 0 ] # same effect as: if [ -z "$1" ]
# $1 can exist, but be empty: zmore "" arg2 arg3
thenecho "Usage: `basename $0` filename" >&2# Error message to stderr.exit $E_NOARGS# Returns 85 as exit status of script (error code).
fi
filename=$1
if [ ! -f "$filename" ] # 使用引号可以避免带空格的文件名出错.
thenecho "File $filename not found!" >&2 # Error message to stderr.exit $E_NOTFOUND
fi
if [ ${filename##*.} != "gz" ]
# Using bracket in variable substitution.
thenecho "File $1 is not a gzipped file!"exit $E_NOTGZIP
fi
zcat $1 | more
# Uses the 'more' filter.
# May substitute 'less' if desired.
exit $? # Script returns exit status of pipe.
# Actually "exit $?" is unnecessary, as the script will, in any case,
#+ return the exit status of the last command executed.
- compound comparison
-a
logical and
exp1 -a exp2 returns true if both exp1 and exp2 are true.
-o
logical or
exp1 -o exp2 returns true if either exp1 or exp2 is true.
- These are similar to the Bash comparison operators && and ||, used within double brackets.
[[ condition1 && condition2 ]]
- The -o and -a operators work with the test command or occur within single test brackets.
if [ "$expr1" -a "$expr2" ]
thenecho "Both expr1 and expr2 are true."
elseecho "Either expr1 or expr2 is false."
fi
- But, as rihad points out:
[ 1 -eq 1 ] && [ -n "ècho true 1>&2`" ] # true
[ 1 -eq 2 ] && [ -n "ècho true 1>&2`" ] # (no output)
# ^^^^^^^ False condition. So far, everything as expected.
# However ...
[ 1 -eq 2 -a -n "ècho true 1>&2`" ] # true
# ^^^^^^^ False condition. So, why "true" output?
# Is it because both condition clauses within brackets evaluate?
[[ 1 -eq 2 && -n "ècho true 1>&2`" ]] # (no output)
# No, that's not it.
# Apparently && and || "short-circuit" while -a and -o do not.
7.4. Nested if/then Condition Tests
- Condition tests using the if/then construct may be nested. The net result is equivalent to using the &&
compound comparison operator.
a=3
if [ "$a" -gt 0 ]
thenif [ "$a" -lt 5 ]thenecho "The value of \"a\" lies somewhere between 0 and 5."fi
fi
# Same result as:
if [ "$a" -gt 0 ] && [ "$a" -lt 5 ]
thenecho "The value of \"a\" lies somewhere between 0 and 5."
fi
7.5. Testing Your Knowledge of Tests
- The systemwide xinitrc file can be used to launch the X server. This file contains quite a number of if/then
tests. The following is excerpted from an “ancient” version of xinitrc (Red Hat 7.1, or thereabouts).
if [ -f $HOME/.Xclients ]; thenexec $HOME/.Xclients
elif [ -f /etc/X11/xinit/Xclients ]; thenexec /etc/X11/xinit/Xclients
else# failsafe settings. Although we should never get here# (we provide fallbacks in Xclients as well) it can't hurt.xclock -geometry 100x100-5+5 &xterm -geometry 80x50-50+150 &if [ -f /usr/bin/netscape -a -f /usr/share/doc/HTML/index.html ]; thennetscape /usr/share/doc/HTML/index.html &fi
fi
- Explain the test constructs in the above snippet, then examine an updated version of the file,
/etc/X11/xinit/xinitrc, and analyze the if/then test constructs there. You may need to refer ahead to
the discussions of grep, sed, and regular expressions.
相关文章:
bash测试test详解
bash测试test详解 概述 任何相对完整的计算机语言都能够测试某个条件,然后根据测试的结果采取不同的动作。对于测试条件, Bash使用test命令、各种方括号和圆括号、if/then结构等来测试条件。 7.1. Test Constructs 一个if/then语句结构测试一个或多个命…...
你来问我来答,ChatGPT对话软件测试!主题互动
你来问我来答,ChatGPT对话软件测试! 大家好,我是聪明而有趣的ChatGPT。作为IT专家,我将竭尽全力为你解答技术问题,并提供适合各个级别人群理解的解决方案。无论你是初学者还是专业人士,我都会用智能、简单…...

无人机巢的作用及应用领域解析
无人机巢作为无人机领域的创新设备,不仅可以实现无人机的自主充电和电池交换,还为无人机提供安全便捷的存放空间。为了帮助大家更好地了解无人机巢,本文将着重解析无人机巢的作用和应用领域。 一、无人机巢的作用 无人机巢作为无人机技术的重…...

面试热题(环形链表II)
给定一个链表,返回链表开始入环的第一个节点。 从链表的头节点开始沿着 next 指针进入环的第一个节点为环的入口节点。如果链表无环,则返回 null。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引…...

策略模式:优雅地实现可扩展的设计
策略模式:优雅地实现可扩展的设计 摘要: 策略模式是一种常用的设计模式,它可以帮助我们实现可扩展的、灵活的代码结构。本文将通过一个计算器案例来介绍策略模式的概念、使用场景以及如何在实际项目中应用策略模式来提高代码的可维护性和可扩…...

从8个新 NFT AMM,聊聊能如何为 NFT 提供流动性
DeFi 的出现,开启了数字金融民主化的革命。其中,通过 AMM 自由创建流动性池极大地增加了 ERC-20 Token 的流动性,并为一些长尾 Token 解锁了价值的发现,因而今天在链上可以看到各种丰富的交易、借贷和杠杆等活动。 而另一方面&am…...

习题1.27
先写代码 (defn square [x] (* x x)) (defn expmod[base exp m](cond ( exp 0) 1(even? exp) (mod (square (expmod base (/ exp 2) m)) m):else (mod (* base (expmod base (- exp 1) m)) m)))(defn fermat-test[n](defn try-it [a](cond ( a n) (println "test end&qu…...

简单游戏截图_可控截取内容2
一个需求 我需要在场景中截取不同层级的截图(如只截模型或只截UI或只截外部相加看到的画面 或全都截或和Shader配合呈现人眼夜视仪热成像的画面切换) 将截图排到列表中,在场景UI中展示出来 如何做 相机要能够看到不同的画面 将当前帧画面存储下来 将存储的画面展示出…...

跨域+四种解决方法
文章目录 一、跨域二、JSONP实现跨域请求三、前端代理实现跨域请求四、后端设置请求头实现跨域请求五、Nginx代理实现跨域请求5.1 安装Nginx软件5.2 使用Ubuntu安装nginx 本文是在学习课程满神yyds后记录的笔记,强烈推荐读者去看此课程。 一、跨域 出于浏览器的同…...
RW-Everything的RwDrv.sys驱动调用
RW-Everything的RwDrv.sys驱动调用 一、RwDrv.sys二、示例代码三、总结 一、RwDrv.sys RW-Everything是一个硬件底层的工具,可用于物理内存、BIOS、PCI和IO端口的查看和修改,其基于驱动RwDrv.sys来实现,利用这个驱动可以实现系统的侵入。 二…...

0101docker mysql8镜像主从复制-运维-mysql
1 概述 主从复制是指将主数据库的DDL和DML操作通过二进制日志传到从库服务器,然后在从库上对这些日志重新执行(也叫重做),从而使得从库和主库的数据保持同步。 Mysql支持一台主库同时向多台从库进行复制,从库同时可以…...

uC-OS2 V2.93 STM32L476 移植:系统启动篇
前言 前两篇已经 通过 STM32CubeMX 搭建了 NUCLEO-L476RG 的 STM32L476RG 的 裸机工程,下载了 uC-OS2 V2.93 的源码,并把 uC-OS2 的源文件加入 Keil MDK5 工程 本篇适配 uC-OS2 的 系统定时器(Systick)与 PendSV_Handler…...

redis 集群 1:李代桃僵 —— Sentinel
目前我们讲的 Redis 还只是主从方案,最终一致性。读者们可思考过,如果主节点凌晨 3 点突发宕机怎么办?就坐等运维从床上爬起来,然后手工进行从主切换,再通知所有的程序把地址统统改一遍重新上线么?毫无疑问…...
重置 Macbook 中MySQL 的 root 用户密码
Mac上好久前安装测试用的MySQL的Root密码忘记,猜了些常用密码都不对,只能重置密码。 重置密码 1、关闭MySQL服务,可以直接在系统偏好里关闭 sudo /usr/local/mysql/support-files/mysql.server stop 2、进入安装目录,启动安全…...
2308C++搞笑的概念化
前一篇的概念化,太搞笑 元<类 T>概念 可画要求(T&t,整 i){t.画(i);}; 构 方形{空 画(整 i){打印(2*i);} };元<可画 T>空 f(T&t,整 i){t.画(i);t.画(i); }空 主(){方形 e;f(e,33); }用得着那么复杂吗?...
修改node_modules里的源码
最近在工作中使用到一款生成二维码的依赖(以vue项目为例讲解):vue-qr,安装的4.0.9版本的,在启动工程的时候报错: You may need an appropriate loader to handle this file type后我查阅各种资料发现 1&a…...
【每日一题Day287】LC24 两两交换链表中的节点 | 模拟 递归
两两交换链表中的节点【LC24】 给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。 周赛暂停一周啦 思路:模拟 记录前驱…...
Java ~ Collection/Executor ~ PriorityBlockingQueue【源码】
前言 相关系列 《Java ~ Collection【目录】》(持续更新)《Java ~ Executor【目录】》(持续更新)《Java ~ Collection/Executor ~ PriorityBlockingQueue【源码】》(学习过程/多有漏误/仅作参考/不再更新)…...
Java后台生成微信小程序码并以流的形式返回给前端
后端代码 获取access_token import net.sf.json.JSONObject;public class WeChatUtil {/*** 获取token*/private static String ACCESSTOKENURL "https://api.weixin.qq.com/cgi-bin/token?grant_typeclient_credential&appid{appId}&secret{appSecret}"…...

AtcoderABC226场
A - Round decimalsA - Round decimals 题目大意 给定一个实数X,它最多可以使用三位小数表示,而且X的小数点后有三位小数。将X四舍五入到最接近的整数并打印结果。 思路分析 可以使用round函数进行四舍五入 知识点 round(x) 是一个用来对数字进行四…...
【杂谈】-递归进化:人工智能的自我改进与监管挑战
递归进化:人工智能的自我改进与监管挑战 文章目录 递归进化:人工智能的自我改进与监管挑战1、自我改进型人工智能的崛起2、人工智能如何挑战人类监管?3、确保人工智能受控的策略4、人类在人工智能发展中的角色5、平衡自主性与控制力6、总结与…...
多场景 OkHttpClient 管理器 - Android 网络通信解决方案
下面是一个完整的 Android 实现,展示如何创建和管理多个 OkHttpClient 实例,分别用于长连接、普通 HTTP 请求和文件下载场景。 <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas…...
测试markdown--肇兴
day1: 1、去程:7:04 --11:32高铁 高铁右转上售票大厅2楼,穿过候车厅下一楼,上大巴车 ¥10/人 **2、到达:**12点多到达寨子,买门票,美团/抖音:¥78人 3、中饭&a…...
稳定币的深度剖析与展望
一、引言 在当今数字化浪潮席卷全球的时代,加密货币作为一种新兴的金融现象,正以前所未有的速度改变着我们对传统货币和金融体系的认知。然而,加密货币市场的高度波动性却成为了其广泛应用和普及的一大障碍。在这样的背景下,稳定…...
【Go语言基础【13】】函数、闭包、方法
文章目录 零、概述一、函数基础1、函数基础概念2、参数传递机制3、返回值特性3.1. 多返回值3.2. 命名返回值3.3. 错误处理 二、函数类型与高阶函数1. 函数类型定义2. 高阶函数(函数作为参数、返回值) 三、匿名函数与闭包1. 匿名函数(Lambda函…...

HubSpot推出与ChatGPT的深度集成引发兴奋与担忧
上周三,HubSpot宣布已构建与ChatGPT的深度集成,这一消息在HubSpot用户和营销技术观察者中引发了极大的兴奋,但同时也存在一些关于数据安全的担忧。 许多网络声音声称,这对SaaS应用程序和人工智能而言是一场范式转变。 但向任何技…...
ubuntu22.04 安装docker 和docker-compose
首先你要确保没有docker环境或者使用命令删掉docker sudo apt-get remove docker docker-engine docker.io containerd runc安装docker 更新软件环境 sudo apt update sudo apt upgrade下载docker依赖和GPG 密钥 # 依赖 apt-get install ca-certificates curl gnupg lsb-rel…...
《Offer来了:Java面试核心知识点精讲》大纲
文章目录 一、《Offer来了:Java面试核心知识点精讲》的典型大纲框架Java基础并发编程JVM原理数据库与缓存分布式架构系统设计二、《Offer来了:Java面试核心知识点精讲(原理篇)》技术文章大纲核心主题:Java基础原理与面试高频考点Java虚拟机(JVM)原理Java并发编程原理Jav…...

【大模型】RankRAG:基于大模型的上下文排序与检索增强生成的统一框架
文章目录 A 论文出处B 背景B.1 背景介绍B.2 问题提出B.3 创新点 C 模型结构C.1 指令微调阶段C.2 排名与生成的总和指令微调阶段C.3 RankRAG推理:检索-重排-生成 D 实验设计E 个人总结 A 论文出处 论文题目:RankRAG:Unifying Context Ranking…...

Springboot 高校报修与互助平台小程序
一、前言 随着我国经济迅速发展,人们对手机的需求越来越大,各种手机软件也都在被广泛应用,但是对于手机进行数据信息管理,对于手机的各种软件也是备受用户的喜爱,高校报修与互助平台小程序被用户普遍使用,为…...