您好,我正在尝试比较目录中的两个字符串。格式如下。
{sametext}差异{sametext}
。
注意:{sametext} 不是每个文件都是静态的
例如
myfile_1_exercise.txt
与 myfile_2_exercise.txt
你能告诉我如何在 if 语句中匹配上述字符串吗?
基本上我需要知道如何忽略两个字符串中的数字以使它们相同。
部分示例代码如下所示:
我的示例代码如下所示:
for g in `ls -d */`;
do
if [ -d $g ]; then
cd $g # down 1 directories
for h in `ls *root`;
do
printf "${Process[${count}]} = ${basedir}/${f}${g}${h}\n"
h1=${h}
if [ "${h1}" = "${h2}" ]; then # NEED to MATCH SOME HOW??????????
echo we have a match
fi
h2=${h1}
let count+=1
done
cd ../
#printf "\n\n\n\n"
fi
done
应该用什么测试来确定这一点而不是 "${h1}"= "${h2}"
?
干杯,
迈克
最佳答案
"myfile_1_exercise.txt" == "myfile_2_exercise.txt"
你的意思是上面的测试应该返回 true
(忽略数字)对吗?
这就是我会做的:
h1="myfile_1_exercise.txt"
h2="myfile_2_exercise.txt"
if [ $( echo ${h1} | sed 's/[0-9]*//g' ) == $( echo ${h2} | sed 's/[0-9]*//g' ) ] ; then
# do something here.
fi
关于linux - Bash 比较目录中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6295774/