How to find variable is empty in shell script
2013-06-19 14:27
323 查看
(1).
var=""
if [ -n "$var" ]; then
echo "not empty"
else
echo "empty"
fi
(2).
function empty
{
local var="$1"
# Return true if:
# 1. var is a null string ("" as empty string)
# 2. a non set variable is passed
# 3. a declared variable or array but without a value is passed
# 4. an empty array is passed
if test -z "$var"
then
[[ $( echo "1" ) ]]
return
# Return true if var is zero (0 as an integer or "0" as a string)
elif [ "$var" == 0 2> /dev/null ]
then
[[ $( echo "1" ) ]]
return
# Return true if var is 0.0 (0 as a float)
elif [ "$var" == 0.0 2> /dev/null ]
then
[[ $( echo "1" ) ]]
return
fi
[[ $( echo "" ) ]]
}
usage:
if empty "${var}"
then
echo "empty"
else
echo "not empty"
fi
(3).
#!/bin/bash
vars=(
""
0
0.0
"0"
1
"string"
" "
)
for (( i=0; i<${#vars[@]}; i++ ))
do
var="${vars[$i]}"
if empty "${var}"
then
what="empty"
else
what="not empty"
fi
echo "VAR \"$var\" is $what"
done
exit
output:
VAR "" is empty
VAR "0" is empty
VAR "0.0" is empty
VAR "0" is empty
VAR "1" is not empty
VAR "string" is not empty
VAR " " is not empty
var=""
if [ -n "$var" ]; then
echo "not empty"
else
echo "empty"
fi
(2).
function empty
{
local var="$1"
# Return true if:
# 1. var is a null string ("" as empty string)
# 2. a non set variable is passed
# 3. a declared variable or array but without a value is passed
# 4. an empty array is passed
if test -z "$var"
then
[[ $( echo "1" ) ]]
return
# Return true if var is zero (0 as an integer or "0" as a string)
elif [ "$var" == 0 2> /dev/null ]
then
[[ $( echo "1" ) ]]
return
# Return true if var is 0.0 (0 as a float)
elif [ "$var" == 0.0 2> /dev/null ]
then
[[ $( echo "1" ) ]]
return
fi
[[ $( echo "" ) ]]
}
usage:
if empty "${var}"
then
echo "empty"
else
echo "not empty"
fi
(3).
#!/bin/bash
vars=(
""
0
0.0
"0"
1
"string"
" "
)
for (( i=0; i<${#vars[@]}; i++ ))
do
var="${vars[$i]}"
if empty "${var}"
then
what="empty"
else
what="not empty"
fi
echo "VAR \"$var\" is $what"
done
exit
output:
VAR "" is empty
VAR "0" is empty
VAR "0.0" is empty
VAR "0" is empty
VAR "1" is not empty
VAR "string" is not empty
VAR " " is not empty
相关文章推荐
- How to find variable is empty in shell script
- [quote] How to comment multi-lines in bash shell script.
- How to find and fix Bash Shell-shock vulnerability CVE-2014-6271 in unix like system
- [quote]HowTo Format Date For Display or Use In a Shell Script
- how to run 2 or more commands in a shell script [for Linux & Windows]
- How to format date for display or to use in a shell script
- How to execute shell script in Java?
- 【转】How to append current date and timestamp to filename in shell script
- How to execute shell script in Java?
- How to use ftp in a shell script
- [linux]How to set PATH in shell script, and keep it avaiable even after it exits
- how to find shell type in linux
- How to create a zip file in NetSuite SuiteScript 2.0 如何在现有SuiteScript中创建和下载ZIP压缩文档
- How to find report by report name in Windchill
- In C# how to find the mime type of a file
- How to Delete [This is no longer located in Directory] Files
- How to run a script in ubuntu?
- 数据源没发现! Name [jdbc/mysql5] is not bound in this Context. Unable to find [jdbc].
- What is reflection and how to use it in CSharp ?
- How to find a file in MS-DOS