-bash: a: unbound variable 条件判断 虽然你可以通过上面的测试和&&、||控制操作符来完成一个大量的程序设计,不过bash环境中还包含了更亲切的“if,then,else”和case语法结构。当你学会了这两种结构之后,你还会学到循环结构,这时你的知识结构才会真正拓展开来。 if-then-else语句 bash环境下的if命令是一个复合的...
if [ -z "$variable_name" ]; then echo "变量未设置或为空" else echo "变量已设置且不为空" fi -z 检查字符串长度是否为零,即变量是否为空。 -n 检查字符串长度是否非零,即变量是否已设置且不为空。 方法三:使用 declare 命令 你可以使用 declare 命令来检查变量是否存在。 代码语言:txt 复制 decla...
# set variable identifying the chroot you work in (used in the prompt below) if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then debian_chroot=$(cat /etc/debian_chroot) fi # set a fancy prompt (non-color, unless we know we "want" color) case "$TERM" in x...
- Testifa given variable is [eq]ual/[n]ot [e]qual/[g]reater [t]han/[l]ess [t]han/[g]reater than or [e]qual/[l]ess than or [e]qual to the specified number: [[$variable-eq|ne|gt|lt|ge|leinteger]] - Testifthe specified variable has a [n]on-empty value: [[ -n$vari...
VARIABLE=2 然后我们通过 $VARIABLE 引用该变量。这里有一点非常重要,也极容易忽视的就是:千万不要在等号两边加空格。虽然加上空格也不会引起语法错误,但很可能造成意想不到的结果。例如 VARIABLE= 2 这个语句,解释器很可能会将一个空字符串赋值给 VARIABLE,然后运行一个名字叫 2 的脚本。一般常用的 Bash ...
会返回错误 test: line 5: var: unbound variable var未绑定变量 -x 参数 【-o xtrace】 执行命令之前打印命令,用来判断结果来自于哪里 #!/usr/bash set -x echo "hello world" 执行上面脚本,结果如下 + echo 'hello world' hello world 如果只想对特定的代码进行查看命令,可以使用 +x ...
# 取消变量的值 unset my_variable 还可以使用环境变量为变量赋值。例如,可以使用$PATH环境变量: 代码语言:txt 复制 # 使用环境变量为变量赋值 current_path=$PATH echo "当前的PATH环境变量值为:$current_path" 参考链接: Bash变量赋值 相关搜索: linux bash 变量赋值 在bash中赋值位置变量 如何为bash中的布尔值...
if语法更短 case设置变量的简单语句 在:内置的可以用来避免重复variable=在一个case语句。该$_变量存储的最后一个命令的最后一个参数。:总是成功,所以它可以用来存储变量值。 其他 使用read作为替代的sleep命令 令人惊讶的是,sleep是一个外部命令而不是bash内置命令。
name: cannot unset:readonlyvariable[root@localhost ~]# name="jerry obame"[root@localhost ~]# echo $namejerry obame[root@localhost ~]# declare -r name[root@localhost ~]# name=hello-bash: name:readonlyvariable[root@localhost ~]# unset name-bash: unset: name: cannot unset:readonlyvariable...
set -e # 判断脚本输出,此处0为异常 if [ "$mysql_process_num" -ge 1 ]; then echo "$mysql_process_num|proc_name=mysql" else echo "0|proc_name=mysql" fi 脚本命令解析 执行器 #!/bin/bash 首行表示此脚本使用/bin/sh来解释执行,#!是特殊的标识符,后跟此脚本解释器的路径。