shell中如何比较 路径 字符串?

hzgdiyer 2012-02-25 11:46:09
比如A字符串:/users/ricky/0220/app1
我获取了另外一个路径字符串B,如何比较A B 是否相同?

if [[ "$A" -eq "$B" ]];then
echo "same path"
else
echo "diff path"
fi

报错: arithmetic syntax error, 应该是路径中有特殊字符/的原因,各位有什么好办法吗?
...全文
200 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wowRicky 2012-02-26
  • 打赏
  • 举报
回复
多谢。

For Mathematics, use following operator in Shell Script
Mathematical Operator in Shell Script Meaning Normal Arithmetical/ Mathematical Statements But in Shell
For test statement with if command For [ expr ] statement with if command
-eq is equal to 5 == 6 if test 5 -eq 6 if [ 5 -eq 6 ]
-ne is not equal to 5 != 6 if test 5 -ne 6 if [ 5 -ne 6 ]
-lt is less than 5 < 6 if test 5 -lt 6 if [ 5 -lt 6 ]
-le is less than or equal to 5 <= 6 if test 5 -le 6 if [ 5 -le 6 ]
-gt is greater than 5 > 6 if test 5 -gt 6 if [ 5 -gt 6 ]
-ge is greater than or equal to 5 >= 6 if test 5 -ge 6 if [ 5 -ge 6 ]


For string Comparisons use
Operator Meaning
string1 = string2 string1 is equal to string2
string1 != string2 string1 is NOT equal to string2
string1 string1 is NOT NULL or not defined
-n string1 string1 is NOT NULL and does exist
-z string1 string1 is NULL and does exist

Shell also test for file and directory types
Test Meaning
-s file Non empty file
-f file Is File exist or normal file and not a directory
-d dir Is Directory exist and not a file
-w file Is writeable file
-r file Is read-only file
-x file Is file is executable
qq120848369 2012-02-26
  • 打赏
  • 举报
回复
字符串比较用=即可,数字才用-eq
lylm 2012-02-26
  • 打赏
  • 举报
回复
A="/users/ricky/0220/app1";B="/users/ricky/0220/app1";if [[ "$A" == "$B" ]];then echo "same path"; else echo "diff path"; fi

不是-eq,是==

23,114

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧