AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1653224
Accepted
Hashim Aziz
Hashim Aziz
Asked: 2021-06-03 06:29:12 +0800 CST2021-06-03 06:29:12 +0800 CST 2021-06-03 06:29:12 +0800 CST

如何重写 if-elif 语句的墙

  • 772

对于cigs.sh - 完整的脚本可以在这里找到- 我编写了以下丑陋(但完美工作)逻辑来打印和格式化脚本的输出,部分只是为了找出所有边缘情况,但也因为我没有看到任何其他选择。

...

if [[ $W -le 0 && $D -le 0 && $H -eq 1 ]]; then string="$H hour"
elif [[ $W -le 0 && $D -le 0 && $H -gt 1 ]]; then string="$H hours"
elif [[ $W -le 0 && $D -eq 1 && $H -le 0 ]]; then string="$D day" 
elif [[ $W -le 0 && $D -eq 1 && $H -eq 1 ]]; then string="$D day and $H hour"
elif [[ $W -le 0 && $D -eq 1 && $H -gt 1 ]]; then string="$D day and $H hours"   
elif [[ $W -le 0 && $D -gt 1 && $H -le 0 ]]; then string="$D days"
elif [[ $W -le 0 && $D -gt 1 && $H -eq 1 ]]; then string="$D days and $H hour"
elif [[ $W -le 0 && $D -gt 1 && $H -gt 1 ]]; then string="$D days and $H hours"
elif [[ $W -eq 1 && $D -le 0 && $H -le 0 ]]; then string="$W week"
elif [[ $W -eq 1 && $D -le 0 && $H -eq 1 ]]; then string="$W week and $H hour"
elif [[ $W -eq 1 && $D -le 0 && $H -gt 1 ]]; then string="$W week and $H hours"
elif [[ $W -eq 1 && $D -eq 1 && $H -le 0 ]]; then string="$W week and $D day"
elif [[ $W -eq 1 && $D -gt 1 && $H -le 0 ]]; then string="$W week and $D days"
elif [[ $W -eq 1 && $D -eq 1 && $H -eq 1 ]]; then string="$W week, $D day and $H hour"
elif [[ $W -eq 1 && $D -eq 1 && $H -gt 1 ]]; then string="$W week, $D day and $H hours"
elif [[ $W -eq 1 && $D -gt 1 && $H -eq 1 ]]; then string="$W week, $D days and $H hour"
elif [[ $W -eq 1 && $D -gt 1 && $H -gt 1 ]]; then string="$W week, $D days and $H hours"
elif [[ $W -gt 1 && $D -le 0 && $H -le 0 ]]; then string="$W weeks"
elif [[ $W -gt 1 && $D -le 0 && $H -eq 1 ]]; then string="$W weeks and $H hour"
elif [[ $W -gt 1 && $D -le 0 && $H -gt 1 ]]; then string="$W weeks and $H hours"
elif [[ $W -gt 1 && $D -eq 1 && $H -le 0 ]]; then string="$W weeks and $D day"
elif [[ $W -gt 1 && $D -gt 1 && $H -le 0 ]]; then string="$W weeks and $D days"
elif [[ $W -gt 1 && $D -eq 1 && $H -eq 1 ]]; then string="$W weeks, $D day and $H hour"
elif [[ $W -gt 1 && $D -eq 1 && $H -gt 1 ]]; then string="$W weeks, $D day and $H hours"
elif [[ $W -gt 1 && $D -gt 1 && $H -eq 1 ]]; then string="$W weeks, $D days and $H hour"
elif [[ $W -gt 1 && $D -gt 1 && $H -gt 1 ]]; then string="$W weeks, $D days and $H hours"
fi

colour1='\033[0;31m'
colour2='\033[0;32m'
if (($elapsed < threshold))
then echo -e "${colour1}It's been $string since you last bought a $item."
else
echo -e "${colour2}It's been $string since you last bought a $item."
fi

也许我只是很笨,但是像上面的代码一样尴尬,我看不出有更好的方法可以重写它。是否存在,如果存在,它是什么?

bash automation
  • 4 4 个回答
  • 142 Views

4 个回答

  • Voted
  1. meuh
    2021-06-03T09:33:55+08:002021-06-03T09:33:55+08:00

    另一种方法是从冗长的详细版本开始,并使用匹配模式将其修剪以删除复数等。我不知道它是否更受欢迎,因此可能更难维护。

    string=" $W weeks, $D days and $H hours" # leading space to simplify matching
    string=${string/ 1 weeks/1 week}
    string=${string/ 1 days/ 1 day}
    string=${string/ 1 hours/ 1 hour}
    string=${string# 0 weeks,}
    string=${string/ 0 days/}
    string=${string%and 0 hours}
    string=${string/, and/ and}
    string=${string# }
    string=${string% }
    string=${string%,}
    string=${string#and }
    [[ $string =~ and ]] || string=${string/,/ and}
    

    这使用 bash 的参数扩展语法,其中${parameter/pattern/replacement}尝试匹配 glob 模式,如果找到则替换它。这用于“修复”“1 周”之类的“错误”,使其变为“1 周”。为了避免也匹配21 weeks,初始字符串在开头有一个额外的空格,所以空格可以在模式中以确保只有“1”匹配。

    ${parameter#pattern}如果在参数的开头找到匹配项,则语法将删除该匹配项。这用于删除“0 周”。

    ${parameter%pattern}如果在参数末尾找到匹配项,语法将删除该匹配项。这用于删除“和 0 小时”。其他修复如果在开头或结尾留下空格,则删除空格,如果在结尾留下逗号,如果在开头留下空格,则删除“and”和空格。

    如果字符串中没有“and”,则最后一行将逗号替换为“and”。例如,这对应于将“2 周 3 天”更改为“2 周 3 天”,我们已经删除了“0 小时”。

    • 3
  2. Gordon Davisson
    2021-06-04T00:47:29+08:002021-06-04T00:47:29+08:00

    如果您在 bash 下运行脚本,这个怎么样(基于 Jeff Zeitlin 的评论):

    # Start with an empty list of units
    units=()
    
    # Add the non-zero units to the list
    if (( W == 1 )); then units+=("1 week")
    elif (( W > 1 )); then units+=("$W weeks")
    fi
    
    if (( D == 1 )); then units+=("1 day")
    elif (( D > 1 )); then units+=("$D days")
    fi
    
    if (( H == 1 )); then units+=("1 hour")
    elif (( H > 1 )); then units+=("$H hours")
    fi
    
    # Based on the number of non-zero units, add separators appropriately
    case ${#units[@]} in
            3) string="${units[0]}, ${units[1]} and ${units[2]}" ;;
            2) string="${units[0]} and ${units[1]}" ;;
            1) string="${units[0]}" ;;
            0) string="less than an hour" ;;
    esac
    

    警告:这几乎需要 bash。zsh 也有数组,但是它对条目进行不同的编号(从 1 而不是 0 开始),所以最后一部分在 zsh 下会奇怪地失败。

    • 3
  3. Best Answer
    meuh
    2021-06-04T04:42:22+08:002021-06-04T04:42:22+08:00

    在 bash 有[[ ]]表达式之前的日子里,case语句被大量用于模式匹配。if 的列表可以转换为单个 case 语句(+符号只是一个任意分隔符):

    case $W+$D+$H in
    0+0+0 ) string="" ;;
    0+0+1 ) string="$H hour" ;;
    0+1+0 ) string="$D day"  ;;
    0+1+1 ) string="$D day and $H hour" ;;
    1+0+0 ) string="$W week" ;;
    1+0+1 ) string="$W week and $H hour" ;;
    1+1+0 ) string="$W week and $D day" ;;
    1+1+1 ) string="$W week, $D day and $H hour" ;;
    
    0+0+* ) string="$H hours" ;;
    0+*+0 ) string="$D days"  ;;
    *+0+0 ) string="$W weeks" ;;
    0+1+* ) string="$D day and $H hours" ;;
    0+*+1 ) string="$D days and $H hour" ;;
    *+0+1 ) string="$W weeks and $H hour" ;;
    *+1+0 ) string="$W weeks and $D day" ;;
    1+0+* ) string="$W week and $H hours" ;;
    1+*+0 ) string="$W week and $D days" ;;
    1+1+* ) string="$W week, $D day and $H hours" ;;
    1+*+1 ) string="$W week, $D days and $H hour" ;;
    *+1+1 ) string="$W weeks, $D day and $H hour" ;;
    
    0+*+* ) string="$D days and $H hours" ;;
    1+*+* ) string="$W week, $D days and $H hours" ;;
    *+0+* ) string="$W weeks and $H hours" ;;
    *+*+0 ) string="$W weeks and $D days"  ;;
    *+1+* ) string="$W weeks, $D day and $H hours" ;;
    *+*+1 ) string="$W weeks, $D days and $H hour" ;;
    *+*+* ) string="$W weeks, $D days and $H hours" ;;
    esac
    

    这仍然很难消化,但是如果我们将复数单词与变量保持为s或不分开,则该case语句要简单得多:

    ws=s; [ $W = 1 ] && ws=
    ds=s; [ $D = 1 ] && ds=
    hs=s; [ $H = 1 ] && hs=
    case $W+$D+$H in
    0+0+0 ) string="" ;;
    0+0+* ) string="$H hour$hs" ;;
    0+*+0 ) string="$D day$ds"  ;;
    0+*+* ) string="$D day$ds and $H hour$hs" ;;
    *+0+0 ) string="$W week$ws" ;;
    *+0+* ) string="$W week$ws and $H hour$hs" ;;
    *+*+0 ) string="$W week$ws and $D day$ds" ;;
    *+*+* ) string="$W week$ws, $D day$ds and $H hour$hs" ;;
    esac
    
    • 3
  4. Yorik
    2021-06-03T08:15:04+08:002021-06-03T08:15:04+08:00

    Bash支持函数,所以不妨试试函数。我(用伪代码)勾勒出一个可能的策略。

    "s" 很容易处理,因为只有当值为 1 时它才应该是单数。逗号和“and”是基于计数的决定,因此一个函数返回一个空字符串或“正确复数”的字符串,下一个函数用于根据字符串的空性质增加一个计数器。

    然后使用计数器来操作两个分隔符字符串,然后整个事情就被盲目地组装起来。

    伪代码:

    function foo ($int, $singular, $suffix, $ignoreZero) {
        $rval = "";
        if ( $ignoreZero && $int == 0 )  { return $rval; }
        if ( $int == 1 ) { $rval = $int + " " + $singular; }
        else { $rval = $int + " " + $singular + $suffix; }
        return $rval;
    }
    function bar($int, $str) {
       $rval = $int;
       if ( $str !="" ) { $rval = $int + 1; }
       return $rval;
    }
    
    $count = 0
    
    $strW = foo($W, "week", "s", true);
    $count = bar($count, $strW);
    
    $strD = foo($D, "day", "s", true);
    $count = bar($count, $strD);
    
    $strH = foo($H, "hour", "s", true);
    $count = bar($count, $strH);
    
    $sep1 = "";
    $sep2 = " and ";
    
    if ($count == 3) { $sep1=", ";}
    if ($count < 2) { $sep2=""; }
    
    $emit = $strW + $sep1 + $strD + $sep2 + $strH
    
    • 0

相关问题

  • 在非 root 用户上用 bash 替换 zsh

  • 在 macOS High Sierra 的终端中设置环境变量时遇到问题

  • 对于 cp 或 mv,是否有等同于 cd - 的东西?

  • Notify-发送窗口下出现的通知

  • 如何从 WSL 打开 office 文件

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve