所以,人们经常问为什么我不是 Linux 的粉丝,这就是为什么。在 Windows 编程中,\n 是 \n。时期。没有其他解释。但是在 linux bash 中,谁知道你会得到什么。
例如:我在网上查找的每件事都说: IFS=$'\n'
将在新行上拆分。所以有人请向我解释为什么会这样:
local OIFS="$IFS"
IFS=$'\n'
local status=$(git status --porcelain 2> /dev/null)
#I even added this just in case:
status=$( status )
#but no matter what length is always 1
local length=${#status[@]}
echo $length
IFS=$OIFS
当我确定此存储库中的 git status 返回 7 行数据时,回显 1 的值?我试过了,$(echo -en "\n\b"),我只试过 "\n"(这看起来很有趣,可以在新行上拆分,但也可以在字母 n 上拆分。)就像标准化没有在linux中不存在。我可以多次运行相同的脚本并获得不同的结果。
我想要的只是一个脚本,它获取 git status 的瓷器结果,在新行上拆分它,然后在每一行的空间上拆分它。这将创建一个数组数组,如下所示:
[0] "M","modfile.cs"
[1] "A","addfile.cs"
[2] "??","newfile.cs"
但 IFS 似乎并不想处理回车。在其他命令中,我能够解决此问题,因为它们具有 -printf 或 --pretty 参数,我可以更改拆分字符。但是获取状态我只有换行,到目前为止,bash 脚本不能在新行上拆分。
有任何想法吗?感谢 Jaeden "Sifo Dyas" al'Raec Ruiner
PS 告诉你的编辑,我决定什么是代码,而不是它。