如何使用 apt-get 在 Ubuntu Linux 上安装 ssh
2024年3月29日操作方法:在 Unix/Linux 中 Bash 提取文件名和扩展名
2024年3月29日我是 Unix/Linux 新用户。我正在学习 shell 脚本。如何在 bash shell 中比较数字?
您需要使用 test 命令使用以下运算符来执行各种数字比较:
- INTEGER1 -eq INTEGER2– INTEGER1 等于 INTEGER2
- INTEGER1 -ge INTEGER2– INTEGER1 大于或等于 INTEGER2
- INTEGER1 -gt INTEGER2– INTEGER1 大于 INTEGER2
- INTEGER1 -le INTEGER2– INTEGER1 小于或等于 INTEGER2
- INTEGER1 -lt INTEGER2– INTEGER1 小于 INTEGER2
- INTEGER1 -ne INTEGER2– INTEGER1 不等于 INTEGER2
[请勿打印]
教程详情 | |
---|---|
难度级别 | 简单的 |
根权限 | 不 |
要求 | 没有任何 |
美东时间。阅读时间 | 4分钟 |
[/不打印]
向测试实用程序问好
test 命令计算表达式,如果计算结果为 true,则返回零 (true) 退出状态;否则返回 1(假)。语法是:
测试条件 测试条件&& do_something || do_nothing_due_to_false [条件] && do_something ||由于错误而什么都不做
if 命令的语法如下:
如果 [条件];然后 do_run_this_due_to_true_condition else do_run_this_due_to_false_condition fi
示例:在 bash 中比较数字
判断5是否大于10,输入(在终端输入命令):
x = 5 y = 10 [ $x -gt $y ] 回显 $?
输出示例:
1
在 bash shell 中,非零输出意味着错误结果,即 $x 不大于 $y。尝试以下示例(在终端键入命令):
x = 51 y = 10 [ $x -gt $y ] 回显 $?
0
零值表示真实结果,即 $x 大于 $y。让我们使用以下语法使输出更具可读性:
[条件] && 真命令||错误命令
判断5是否大于10,输入(在终端输入命令):
x = 5 y = 10 [ $x -gt $y ] && echo "是$ x > $ y " || echo "否$ x 不 > $ y"
否 $x 不 > $y
您可以使用 if 语句,如下所示:
#!/bin/bash read -p "输入一个数字(必须大于 20): " n if test $n -gt 20 then echo " $n大于 20." else echo “你没有遵循我的指示。” 菲
或者
#!/bin/bash read -p "输入一个数字(必须大于20):" n if [ $n -gt 20 ] ;然后 echo " $n大于 20." else echo “你没有遵循我的指示。” 菲
运行如下:
./script.sh 输入一个数字(必须大于 20):22 22 大于 20。./script.sh 输入一个数字(必须大于 20):8 您没有遵循我的指示。
算术测试选项
您可以通过键入以下命令查看所有支持的选项的列表:
$ help test
示例输出:
File operators:
-a FILE True if file exists.
-b FILE True if file is block special.
-c FILE True if file is character special.
-d FILE True if file is a directory.
-e FILE True if file exists.
-f FILE True if file exists and is a regular file.
-g FILE True if file is set-group-id.
-h FILE True if file is a symbolic link.
-L FILE True if file is a symbolic link.
-k FILE True if file has its `sticky' bit set.
-p FILE True if file is a named pipe.
-r FILE True if file is readable by you.
-s FILE True if file exists and is not empty.
-S FILE True if file is a socket.
-t FD True if FD is opened on a terminal.
-u FILE True if the file is set-user-id.
-w FILE True if the file is writable by you.
-x FILE True if the file is executable by you.
-O FILE True if the file is effectively owned by you.
-G FILE True if the file is effectively owned by your group.
-N FILE True if the file has been modified since it was last read.
FILE1 -nt FILE2 True if file1 is newer than file2 (according to
modification date).
FILE1 -ot FILE2 True if file1 is older than file2.
FILE1 -ef FILE2 True if file1 is a hard link to file2.
String operators:
-z STRING True if string is empty.
-n STRING
STRING True if string is not empty.
STRING1 = STRING2
True if the strings are equal.
STRING1 != STRING2
True if the strings are not equal.
STRING1 < STRING2
True if STRING1 sorts before STRING2 lexicographically.
STRING1 > STRING2
True if STRING1 sorts after STRING2 lexicographically.
Other operators:
-o OPTION True if the shell option OPTION is enabled.
! EXPR True if expr is false.
EXPR1 -a EXPR2 True if both expr1 AND expr2 are true.
EXPR1 -o EXPR2 True if either expr1 OR expr2 is true.
arg1 OP arg2 Arithmetic tests. OP is one of -eq, -ne,
-lt, -le, -gt, or -ge.
如果 ARG1 等于、不等于、
小于、小于或等于、大于或大于或等于
ARG2,则算术二元运算符返回 true。
也可以看看
- 第 4 章:条件执行(决策)
- 请参阅 获取更多信息。