我有两个变量 $ 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
答案 0 :(得分:4)
if [ ${a/Source/''} == ${b/Destination/''} ]
then
# do your job
fi
答案 1 :(得分:1)
if [ `echo $a | sed 's/Source\///'` == `echo $b | sed 's/Destination\///'` ]
then
# Do something
fi
答案 2 :(得分:1)
使用case/esac
case "${a#*/}" in
${b#*/} ) echo "ok";;
esac
答案 3 :(得分:0)
或使用awk
#!/bin/bash
a="Source/dir1/dir11"
b="Destination/dir1/dir11"
SAVEIFS=$IFS
IFS="\/"
basea=$(echo $a | awk '{print $1}')
baseb=$(echo $b | awk '{print $1}')
if [ $basea == $baseb ]; then
echo "Equal strings"
fi
IFS=$SAVEIFS