ubuntu中使用==比较字符串报错解决方法

ubuntu12.04版本

以下脚本,总是报错

#!/bin/bash

while read line
do
testing=$(echo $line | grep -ic '<servername>')
if [ "$testing" == "1" ] ; then
#here [==] is reason
	echo $line
fi
done < dbinfo.xml

 报错的提示为

dbinfo2.sh: 6: [: 0: unexpected operator
dbinfo2.sh: 6: [: 1: unexpected operator
dbinfo2.sh: 6: [: 0: unexpected operator
dbinfo2.sh: 6: [: 0: unexpected operator
dbinfo2.sh: 6: [: 0: unexpected operator
dbinfo2.sh: 6: [: 0: unexpected operator
dbinfo2.sh: 6: [: 0: unexpected operator

查找原因

1.ubuntu默认的shell版本为dash,改为鸟哥小菜中的bash

2.使用=(一个等于号)比较字符串的

其实,鸟哥推荐使用==(两个等于号)比较字符串,话说一个有赋值的意思

 

那么怎么把sh改为指向bash呢?
最暴力的方法当然是直接把/bin/sh的软链接改到bash中,
如:ln -s /bin/bash /bin/sh
但是,有优雅一些的方法,
sudo dpkg-reconfigure dash
出现菜单问你是否要dash的时候,选no就可以了。
再次检查一下, ls /bin/sh -al 发现软链接指向/bin/bash就可以了。

 

linux 软连接怎么查看原始地址

 

只要ls -l 一下这个文件 ->后面就是原始地址
ls -l sh
lrwxrwxrwx 1 root root 4  4月 26 16:46 sh -> bash

你可能感兴趣的:(ubuntu)