开发者

Comparing two variable in shell

开发者 https://www.devze.com 2023-02-24 22:32 出处:网络
I have two variables $a and $b and $a=Source/dir1/dir11 $b=Destination/dir1/dir11 $a and $b changes but initialsSource/ and Destination/ remains same.

I have two variables $a and $b

and

$a=Source/dir1/dir11
$b=Destination/dir1/dir11

$a and $b changes but initials Source/ and Destination/ remains same.

I want to compare $a and $b without Source/ and Destination/

how should I do it? Following code I am using

   SOURCE_DIR_LIST=`find Source/ -type d`
   DEST_DIR_LIST=`find Destinat开发者_运维知识库ion/ -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

But output is coming like as follow

 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


if [ ${a/Source/''} == ${b/Destination/''} ]
then
  # do your job
fi


if [ `echo $a | sed 's/Source\///'` == `echo $b | sed 's/Destination\///'` ]
then
    # Do something
fi


using case/esac

case "${a#*/}" in
 ${b#*/} ) echo "ok";;
esac


or with 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
0

精彩评论

暂无评论...
验证码 换一张
取 消