bash 中的对话框未正确获取变量

2024-01-07

我对该脚本的一致性有些疑问。您可以运行一次,没有任何问题。但是,如果您立即重新加载它并再次运行它,它就无法正确获取变量,因为输出没有以正确的方式写入文件。

前任: 该实用程序主要用于更新 4 个文件。 .temp、.tiers、.version 和 .sync 在各个阶段,通过对话框提示,它会更新文件。我的问题是,有时它不会更新文件,而且我找不到任何原因,因为只有在您运行过一次之后才出现这种情况。我在启动时擦除了文件,所以我不确定这是怎么回事。就好像它只是从记忆中抓取的一样?

无论如何,要测试它,您需要 /test 中的以下文件

感谢任何能给我一些指导的人。

cat .tiers
Stable=1
Release=2
Beta=3

CODE:

#!/usr/bin/env bash
touch .version
touch .temp
VERSION=`cat .version`
DIR=/test/
STORED=`cat ${DIR}/.temp`
################################
#     REARRANGE TIERS          #
################################
rearrange()
{
start
end
}


################################
#     SYNC FILE EXISTS         #
################################
sync_exists()
{
dialog --msgbox ".sync exists in the directory $(echo ${VERSION}).\n\n Use Tier Move                 instead." 10 40
clean_up
exit 1;
}


################################
#         CLEAN UP             #
################################
clean_up()
{
rm -rf .version
rm -rf .update
rm -rf .temp
}

################################
#     OPTOUT & CLEAN UP        #
################################
opt_out()
{
dialog --msgbox "You've opted out of the tier update." 5 40
rm -rf ${DIR}/.version ${DIR}/.update ${DIR}/.temp 
}


################################
#     UPDATE .TIERS FILE       #
################################
tier_updater()
{
dialog --msgbox "\n\n
$(cat ${DIR}/.temp) is now $VERSION." 8 27
sed -i s/${STORED}=.*/${STORED}=${VERSION}/g ${DIR}/.tiers
clean_up
}



################################
#     UPDATE FILE LIST         #
################################
file_updater()
{

rm -rf ${VERSION}/.sync
        for x in `find $VERSION -type d \( ! -iname "." \)|sed s/${VERSION}//g`; do echo     "d===$x===755" >> ${DIR}/${VERSION}/.sync ; done
        for y in `find $VERSION -type f \( ! -iname ".*" \)|sed s/${VERSION}//g`; do     echo "f===$y===644===`md5sum ${VERSION}"$y"|awk '{print $1}'`" >>     "${DIR}"/"$VERSION"/.sync;done
        find "${DIR}"/"${VERSION}" -type f -exec gzip -f '{}' \; > /dev/null 2>&1      |xargs gzip -d "${DIR}"/${VERSION}/.sync
}



################################
#     TIER UPDATE PROMPT       #
################################
tier_update_prompt()
{
if [ -f ${VERSION}/.sync ]; then  sync_exists
else file_updater
fi
dialog --menu "\n
Directory List Built.\n
File List Built.\n
Files Compressed.\n
Sync File in place.\n\n
Would you like to update the tier for $(cat ${DIR}/.temp)?" \ 15 60 2  "Yes" "This will     apply all changes for $(echo $VERSION) to $(cat ${DIR}/.temp)." "No" "This will revert all     changes." 2>&1 > ${DIR}/.update
if [ "$?" = "0" ] ; then
        _update=$(cat ${DIR}/.update)
if [ "$_update" = "Yes" ] ; then
tier_updater
fi
if [ "$_update" = "No" ] ; then
opt_out ;
fi
else
        echo "You have now exited the application"
    clean_up;
fi
}

################################
#     NEW VERSION INPUT        #
################################ 
stable()
{
dialog --inputbox "Enter the new version for $(cat ${DIR}/.temp)" 8 30 2>     "${DIR}"/.version
if [ -d `cat .version` ] ; then tier_update_prompt;
        else
            dialog --msgbox "WARNING!!!!\n\n The folder $(${VERSION}) does not     exist!\nPlease upload this folder before you proceed!" 8 50 ;
        clean_up
    fi
}

################################
#        TIER SELECTION        #
################################
startup()
{
dialog --menu "Tiers are currently set as the following. Which would you like to update?    \n" 12 78 5 \
"Stable" "$(cat ${DIR}/.tiers|grep Stable|sed 's/Stable=//g')" "Release" "$(cat     ${DIR}/.tiers|grep Release|sed 's/Release=//g')" "Beta" "$(cat ${DIR}/.tiers|grep Beta|sed     's/Beta=//g')"  2> "${DIR}"/.temp
# OK is pressed
if [ "$?" = "0" ] ; then
        _return=$(cat ${DIR}/.temp)
fi
if [ "$_return" = "Stable" ] ; then
stable
fi
if [ "$_return" = "Release" ] ; then
stable
fi
if [ "$_return" = "Beta" ] ; then
stable
    else 
            echo "You have now exited the application"      
    clean_up;
fi
}

您可以使用该方法将对话框输出直接捕获到变量中:

exec 3>&1 
result=$(dialog  --menu head 15 20 6 $(for ((i=1;i<30;i++));do echo  tag$i item$i;done)  2>&1 1>&3);
exitcode=$?;
exec 3>&-;
echo $result $exitcode

http://mywiki.wooledge.org/BashFAQ/002 http://mywiki.wooledge.org/BashFAQ/002

所以按照我的方法startup看起来像:

startup()
{
  exec 3>&1 
  _return=$(dialog --menu "Tiers are currently set as the following. Which would you like to update?    \n" 12 78 5 \
  "Stable" "$(cat ${DIR}/.tiers|grep Stable|sed 's/Stable=//g')" "Release" "$(cat     ${DIR}/.tiers|grep Release|sed 's/Release=//g')" "Beta" "$(cat ${DIR}/.tiers|grep Beta|sed     's/Beta=//g')"  2>&1 1>&3)
   exitcode=$?
   exec 3>&-;
    # OK is pressed
  if [ "$exitcode" == "0" ] ; then
    if [[ "$_return" == "Stable" || "$_return" == "Release" || "$_return" == "Beta" ]] 
    then
       stable
    fi
  fi
  echo "You have now exited the application"      
  clean_up;
}

另外还有很多不干净的东西,比如 $(${VERSION}) 这里:

dialog --msgbox "WARNING!!!!\n\n The folder $(${VERSION}) does not

据我从代码中了解到,它将运行空命令,因为 .version 在启动时为空,并且

 VERSION=`cat .version`

在 tier_update_prompt() 中 $VERSION 仍然是空字符串,

我尝试重构你的代码,但结果却变得很混乱,所以建议:

  • 获取如上所示的对话框结果
  • 运行带参数的函数:


#declaration
function_name() { 
  parameter=$1;
  echo "$parameter"
}
var=value
#run function
function_name var     

  
  • 不要使 ${VAR} 过于复杂,
  • 请记住双引号会扩展,所以echo "here is $var"代替echo "here is $(echo ${var})"
  • 尽可能少地使用临时文件,例如:diff <(ls dir1) <(ls dir2)
  • 不要将工作文件保留在根目录中(例如/test/)
  • 不以 root 身份工作
  • 学习perl
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

bash 中的对话框未正确获取变量 的相关文章

随机推荐