Find Out If a Variable Is Set or Not(转载)
来源:互联网 发布:会sql好找工作吗 编辑:程序博客网 时间:2024/06/05 14:10
Bash Shell: Find Out If a Variable Is Set or Not
How do I check if a bash shell variable called $input is defined or not under BSD / Apple OS X / Unix / Linux like operating systems?
Method #1: Bash Variable Existence Check
The syntax is as follows to determine if $input is defined or not:
${Variable?Error \$Variable is not defined}
OR
${Variable:?Error \$Variable is not defined}
In this example your script will stop executing if the variable $input is not defined:
input="Foo bar"echo ${input?Error \$input is not defined.}unset inputecho ${input?Error \$input is not defined.}
Sample outputs:
Foo barbash: input: Error $input is not defined.
In this example, make sure $input is defined and is not empty, enter:
[[ $input && ${input-x} ]]input="Foo"[[ $input && ${input-x} ]] && echo "Found" || echo "Not found"unset input[[ $input && ${input-x} ]] && echo "Found" || echo "Not found"
Here is an example that make sure $_php_map_extension is defined:
# read config data loadConfigData "${_t_domain_php_conf}" $LINENO "${FUNCNAME[0]}" # make sure it is defined and not empty if [[ $_php_map_extension && ${_php_map_extension-_} ]] then at=${#_php_map_extension[*]} # get total elements in an array s="" echo '## Map extension to .php? ##' echo 'fastcgi.map-extensions = (' for (( i=0; i<${at}; i++ )); do [ $i -lt $(( $at - 1 )) ] && s="," || s="" # remove , for last item in an array echo " \".${_php_map_extension[i]}\" => \".php\"${s}" done echo ')'elseecho "Skiping php map extension as \$_php_map_extension is not defined in /usr/local/etc/nixcraft/conf/php.conf." fi
Sample outputs:
Skiping php map extension as $_php_map_extension is not defined in /usr/local/etc/nixcraft/conf/php.conf
OR
## Map extension to .php? ##fastcgi.map-extensions = ( ".html" => ".php", ".htm" => ".php", ".phtml" => ".php", ".php3" => ".php", ".php4" => ".php")
Method #2: isvarset() function
The above examples are useful for a sanity checking. Finally, you can use the following code:
isvarset(){ local v="$1" [[ ! ${!v} && ${!v-unset} ]] && echo "Variable not found." || echo "Variable found."} # find out if $vech defined or notvech="Bus" && isvarset vechvech="" && isvarset vechunset vech && isvarset vech
Method 3: Using the Length of STRING
The -z option to test command returns TRUE of the Length of STRING is zero. You can use the following syntax:
### set or not???input="Foo"[ -z "${input+x}" ] && echo "\$input is not set" || echo "\$input found and is set to \"$input\"." ### Not set at ALLunset input[ -z "${input+x}" ] && echo "\$input is not set" || echo "\$input found and is set to \"$input\"." ### 'set but empty' or not?input=""[ -z "$input" -a "${input+x}" = "x" ] && echo "\$input variable is set with empty value." || echo "\$input found and is set to "\$input\""
The above syntax will tell if a variable is defined or not defined or defined with a empty value in a bash shell script.
- Find Out If a Variable Is Set or Not(转载)
- Linux / Unix Script: Simple Process Checker To Find Out If A Service Is Running or Not
- 1414 - OUT or INOUT argument 3 for routine nenghao.xxx is not a variable or NEW pseudo-variable in B
- Error Code: 1414. OUT or INOUT argument 2 for routine company.new_procedure is not a variable or NEW
- PHP Check If Variable Is Set
- if there is a error "SSSE3 instruction set not enabled"
- Linux / UNIX: Find out or determine if process pid is running
- [VB.NET]Object variable or With block variable not set
- ErrorList:Object variable or With block variable not set error
- -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable a
- -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable a
- Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable a
- -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable a
- -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable a
- -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable a
- -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable a
- -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable a
- -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable a
- 优秀程序员不得不知道的20个位运算技巧
- Linux 进程间通信 - 共享内存shmget方式
- C#高仿腾讯QQ截图程序
- 命令行收邮件
- cin、cin.get()、cin.getline()、getline()、gets()函数的用法
- Find Out If a Variable Is Set or Not(转载)
- form 中Enctype=multipart/form-data 的作用
- OpenCV的几个小技巧
- 网络游戏解密
- 手机、火车飞机时刻、Email 电子邮件地址验证等 WEB 服务
- JavaScript数组常用操作
- C# 通讯中ReceiveTimeout和SendTimeout的作用
- C# 网络连接中异常断线的处理:ReceiveTimeout, SendTimeout 及 KeepAliveValues(设置心跳)
- 一个合格的程序员应该读过哪些书 .