Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash IFS给出不正确的输出_Bash_Ifs - Fatal编程技术网

Bash IFS给出不正确的输出

Bash IFS给出不正确的输出,bash,ifs,Bash,Ifs,我有一个端口号文件map.ini: 50051=1 50052=1 50053=1 50054=1 50055=1 50056=1 以及脚本sample.sh,其内容为: #!/bin/bash file=map.ini while IFS='=' read -r port varPortStatus do if [[ $varPortStatus -eq "1" ]]; then printf "Available port is %s" $

我有一个端口号文件map.ini:

50051=1
50052=1
50053=1
50054=1
50055=1
50056=1
以及脚本sample.sh,其内容为:

#!/bin/bash
file=map.ini

while IFS='=' read -r port varPortStatus
do
        if [[ $varPortStatus -eq "1" ]]; then
                printf "Available port is %s" $port
                printf "Status is %d." $varPortStatus.
                return 0
        fi
done < "$file"
echo "No port is available"
我可以使用
sed
cut
命令操作来执行此操作。但我需要理解这里的IFS。我得到的输出是:

")syntax error: invalid arithmetic operator (error token is "
")syntax error: invalid arithmetic operator (error token is "
")syntax error: invalid arithmetic operator (error token is "
")syntax error: invalid arithmetic operator (error token is "
")syntax error: invalid arithmetic operator (error token is "
")syntax error: invalid arithmetic operator (error token is "
No port is available

请告诉我哪里做错了?

从输出中可以看出,map.ini或sample.sh中可能有dos行结尾,\r请尝试dos2unix map.ini或cat-ve map.ini进行验证

如果无法修改输入文件,可以在bash中删除
\r
字符

while IFS='=' read -r port varPortStatus
do
    varPortStatus=${varPortStatus%$'\r'}
    if [[ $varPortStatus -eq "1" ]]; then
            printf "Available port is %s\n" $port
            printf "Status is %d.\n" $varPortStatus
    fi
done < "$file"
当IFS='='读取-r端口varPortStatus时
做
varPortStatus=${varPortStatus%$'\r'}
如果[$varPortStatus-等式“1”];然后
printf“可用端口为%s\n”$port
printf“状态为%d。\n”$varPortStatus
fi
完成<“$file”

注意:还修复了添加到格式中的
\n
$varPortStatus
return
后被删除的问题,这些问题仅在函数或源代码脚本中有效。

从输出来看,map.ini或sample.sh中可能有dos行结尾
\r
,请尝试
dos2unix map.ini
,或
cat-ve map.ini
验证
$varPortStatus
后的点也必须删除,否则它对于
%d
无效,并且当我执行
cat-ve map.ini
时,
\n
在打印中丢失,我在每行的末尾得到
^M$
^M
是插入符号,表示chr 13 ascii,因为M是字母表中的第13个字母,与
\r
cariage return的转义符号相同抱歉,我忘了删除
return
。我不理解
varPortStatus%$'\r'
部分,虽然它是'\r'字符,但它通常从哪里来?
%
用于删除后缀模式,有关更多详细信息,请参阅bash手册中的
${parameter%word}
'
用于ANSI-C引用当我打开编辑器时,它没有显示任何内容,在gedit或vim编辑器中!这是因为文本文件格式是由vim中的文本编辑器处理的。您应该在状态栏中看到
[dos]
[unix]
,您指的是此文档吗?
while IFS='=' read -r port varPortStatus
do
    varPortStatus=${varPortStatus%$'\r'}
    if [[ $varPortStatus -eq "1" ]]; then
            printf "Available port is %s\n" $port
            printf "Status is %d.\n" $varPortStatus
    fi
done < "$file"