gpt4 book ai didi

linux - 在 Ubuntu Bash 中,如何将变量与标准输出值进行比较?

转载 作者:太空宇宙 更新时间:2023-11-04 09:43:46 25 4
gpt4 key购买 nike

<分区>

I attempted to follow the answer on How do I compare two string variables in an 'if' statement in Bash?, but the accepted solution did not work. As you can see from the script below, my syntax follows the solutions on that question which gives me the error found here Bash syntax error: "[[: not found". And yes, I tried their solution too.

我有以下脚本,我试图从目录中删除所有数据。在删除所有数据之前,我想将一个变量与标准输出值进行比较,以验证我的目录是否正确。

为避免从错误的目录中删除所有数据,我试图将脚本中的变量与存储在 *.ini.php 文件中的数据进行比较。

这是脚本:

    #!/bin/bash
#--- script variables ---
#base path of the timetrex web folder ending with a / character
timetrex_path=/var/www/timetrex/
timetrex_cache=/tmp/timetrex/

#--- initialize script---
#location of the base path of the current version
ttrexVer_path=$(ls -d ${timetrex_path}*.*.*)/
#the timetrex cache folder
ttrexCache_path=$(sed -n 's/[cache]*dir =*\([^ ]*\)/\1/p' < ${ttrexVer_path}timetrex.ini.php)/
echo $timetrex_cache
echo $ttrexCache_path



#clear the timetrex cache
if [[ "$ttrexCache_path" = "$timetrex_cache" ]]
then
#path is valid, OK to do mass delete
#rm -R $ttrexCache_path*
echo "Success: TimeTrex cache has been cleared."
else
#path could be root - don't delete the whole server
echo "Error: TimeTrex cache was NOT cleared."
fi

脚本的输出显示如下:

/tmp/timetrex/
/tmp/timetrex/
Error: Timetrex cache was NOT cleared.

从输出中可以看出,两个值相同。但是,当脚本比较这两个变量时,它认为它们是不同的值。

这是因为值的类型不同吗?我是否在 if 语句 中使用了错误的比较运算符?提前致谢。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com