正如标题所述?可能吗?我使用下面的代码,但它无法确定变量是否有空格。希望有人能帮助我
for file in `ls *.[Pp][Dd][Ff]`
do
var1=`echo "$file" | sed -e "s/.*-\(.*\)-.*/\1/"`
var2="document.num=.*$var1"
var3=`grep -l ${var2} *xml`
case "$var3" in
*\ *)
echo $var3 >> haha
;;
*)
var4=`echo "$var3" | sed -e "s/-.*//"`
varName="$var4.$file"
echo ${var2}
echo $var3
mv $file $varName
;;
esac
答案 0 :(得分:1)
适合我:
var="ab"
#var="a b"
case "$var" in
*\ * ) echo "Has space" ;;
* ) echo "No space" ;;
esac
根据注释掉的变量,它会打印Has space
或No space
答案 1 :(得分:0)
如果您想要如何灵活定义空格,可以使用Bash的内置正则表达式测试。例如:
# Variable has a space.
foo=' '
[[ $foo =~ [[:space:]] ]]; echo $?
0
# Variable has a tab.
foo=$'\t'
[[ $foo =~ [[:space:]] ]]; echo $?
0
# Variable has no whitespace because it's empty.
foo=''
[[ $foo =~ [[:space:]] ]]; echo $?
1