【发布时间】:2011-04-14 03:30:13
【问题描述】:
我有两个变量 $a 和 $b
和
$a=Source/dir1/dir11
$b=Destination/dir1/dir11
$a 和 $b 发生变化,但 Source/ 和 Destination/ 的首字母保持不变。
我想比较 $a 和 $b 而没有 Source/ 和 Destination/
我应该怎么做? 以下代码我正在使用
SOURCE_DIR_LIST=`find Source/ -type d`
DEST_DIR_LIST=`find Destination/ -type d`
for dir_s in $SOURCE_DIR_LIST
do
for dir_d in $DEST_DIR_LIST
do
if [ ${dir_s/Source\//''} == ${dir_d/Destination\//''} ]
then
echo " path match = ${dir_s/Source\//''}"
else
echo "path does not match source path = ${dir_s/Source\//''} "
echo " and destination path= ${dir_d/Destination\//''} "
fi
done
done
但输出如下所示
path match = ''
./compare.sh: line 9: [: ==: unary operator expected
path does not match source path = ''
and destination path= ''dir2
./compare.sh: line 9: [: ==: unary operator expected
more
【问题讨论】:
-
仅供参考,您实际上并没有在该表达式中分配任何内容。丢掉美元符号来做作业。
-
'Source' 和 'Destination' 的名称是固定的,还是它们的路径保存在变量中?这可能会影响最佳答案。
-
即使它像 $a=Source/dir1/dir11/mydir 一样发生变化,我也想比较。我总是想在没有 Source/ 和 Destination/ 的情况下进行比较
-
如果 [ ${a/Source\//''} == ${b/Destination\//''} ]