由于windows下出错,从grep中提取并处理linenumber(因为提取的数字是字符串)

由于windows下出错,从grep中提取并处理linenumber(因为提取的数字是字符串)



我从 grep 结果中提取行号时遇到了问题。Windows (gitBash) 无法比较它们,因为数字是字符串而不是数字(我的 macOS 可以毫无问题地做到这一点)。

我想要做的是:如果行号小于 10,则在行号前添加一个零

以下是代码片段:
local number="" local command="" # grep complete list and itereate over this list grep -n --color=always "${1}" "${2}" | while read -r greppedList ; do for ln in "${greppedList}" ; do # split the line to number and command number=$(echo ${ln} | cut -d ':' -f 1) if (( ${number} < 10 )) ; then command="${ln:2:${#ln}-1}" else command="${ln:3:${#ln}-1}" fi printWithFormattedLineNumbers "${number}" "${command}" done done
Windows 上的问题是行if (( ${number} < 10 )) ; then。Windows
无法比较它,因为它不是数字。

你能帮我改变获取行号的方式吗?

答案1

Windows 上的问题是那行 if (( ${number} < 10 )) ; then。Windows 无法比较它,因为它不是数字。

然后将其视为字符串,并使用语句case代替if

case "${number}" in
[0-9])
        command="${ln:2:${#ln}-1}"
        ;;
*)
        command="${ln:3:${#ln}-1}"
esac

相关内容