This topic has been archived. It cannot be replied.
-
工作学习 / IT技术讨论 / 请问UNIX大侠,用sh script 怎么比较两个变量的值是否相同呢?问题是变量的值中包含空格。谢谢了。例如,VAR1=‘ABC DEF’, VAR2=‘DBC DEF’
用 IF [$VAR1 = $VAR2] 不WORK啊。
-daffodil(我家宝宝1岁了!);
2004-5-7
{75}
(#1714892@0)
-
if [ $VER1 -eq $VER2 ]
到/sbin/ or /usr/bin 下, grep 一把;
-eagle_no1(瞎起哄);
2004-5-7
(#1714899@0)
-
请问为什么要去sbin/ or /usr/bin 下 grep 呢? 当前目录不行吗?谢谢.另外,请问 -eq 和 =的区别是什么呢?
-daffodil(我家宝宝1岁了!);
2004-5-7
(#1714908@0)
-
我是指去找个样板:cd /sbin ; grep if *; 你会发先一堆这样的script, 照着修修改改就行;
-eagle_no1(瞎起哄);
2004-5-7
(#1714911@0)
-
只是语法不一样,没实际意义;
-eagle_no1(瞎起哄);
2004-5-7
(#1714913@0)
-
谢谢你,不过刚试了一下,好像不行阿?要是变量的值有空格,即使两个变量的值一样,也被认为不一样。
-daffodil(我家宝宝1岁了!);
2004-5-7
(#1714914@0)
-
是的,是的,我也试了一下,不支持空格,语法应该是 if [ $var == $var ] then ...fi;
但在sh下总会有办法。。。
-eagle_no1(瞎起哄);
2004-5-7
(#1714923@0)
-
谢谢瞎起哄,请问还有什么script可以用呢?唉,sh script 不熟悉,只是偶尔用一下,哪里可以找到帮助信息呢?其实我是在WINDOWS NT的环境下用sh script.
-daffodil(我家宝宝1岁了!);
2004-5-7
(#1714931@0)
-
用sed 把空格填起来; 下面是运行结果和script;
-eagle_no1(瞎起哄);
2004-5-7
(#1714938@0)
-
here we go:[Computer:~/aaa] % ./try
this-is-try
ok!
end
[Computer:~/aaa] % cat try
var1="this is try" ;
var3="this is try";
var11=`echo $var1 | sed s/\ /-/g`
var33=`echo $var3 | sed s/\ /-/g`
echo $var11;
if [ $var11 == $var33 ]
then
echo "ok!";
fi
echo "end" ;
-eagle_no1(瞎起哄);
2004-5-7
{290}
(#1714934@0)
-
什么叫“要是变量的值有空格,即使两个变量的值一样,也被认为不一样”?你是不是在做2个数值比较啊?如果是字符串比较,多个空格当然认为不一样了。如果是做数值比较,一个偷懒方法就是2个数字各加一,然后比较
-oceandeep(北极熊·戟兵);
2004-5-7
(#1714946@0)
-
should be IF ["$VAR1" = "$VAR2"]; otherwise shell will consider $VAR1 and $VAR2 as intergar.
-ican(apple);
2004-5-7
(#1715001@0)
-
Thanks to everybody! I've tried, and apple's way works. One more question, what's the function in sh to remove the front and back space, just like the Trim() function in database? Many thanks again!
-daffodil(我家宝宝1岁了!);
2004-5-11
(#1718728@0)
-
tr ?
-canadiantire(Crappy Tire);
2004-5-11
(#1718743@0)
-
not work. :( . anyone can help?
-daffodil(我家宝宝1岁了!);
2004-5-11
(#1718886@0)
-
up.^=^
-daffodil(我家宝宝1岁了!);
2004-5-11
(#1719072@0)
-
费这么大劲,你用Perl算了。容易点。
-canadiantire(Crappy Tire);
2004-5-11
(#1719084@0)
-
I can not decide. :(
-daffodil(我家宝宝1岁了!);
2004-5-11
(#1719087@0)
-
sed ; read this example again: #1714934
-eagle_no1(瞎起哄);
2004-5-11
(#1719095@0)
-
I'm not familiar with shell. Could you please give me an example? If I want to remove the front and back space for variable $text, but keep the space inside the variable, what's the command? many thanks.
-daffodil(我家宝宝1岁了!);
2004-5-11
(#1719103@0)
-
thousands of solution, just try this one: text1 = `echo $text` ; if i understand your question.
-eagle_no1(瞎起哄);
2004-5-11
(#1719114@0)
-
You are really good!! It works! many many thanks.
-daffodil(我家宝宝1岁了!);
2004-5-11
(#1719123@0)