bash with cut得到一个带回车符的值\ r

时间:2017-05-28 19:42:58

标签: bash

考虑以下bash脚本

NP=`grep nprocshared $GF | cut -d '=' -f2`
echo $NP
if [ $N -ne $NP ]; then
  echo "Error"

请注意$N是一个整数,很好!该文件包含

%nprocshared=6
%mem=12GB
...

使用set -x选项,我在输出

中看到了这一点
++ grep nprocshared file.gjf
++ cut -d = -f2
+ NP=$'6\r'
+ echo $'6\r' 
 6
+ '[' 4 -ne $'6\r' ']'

所以,我希望4ne6,但我没有看到。它将6描述为6\r

1 个答案:

答案 0 :(得分:1)

NP=`grep nprocshared $GF | cut -d '=' -f2 | tr -d \\r`
echo $NP
if [ $N -ne $NP ]; then
  echo "Error"

瞧。

或者更好,因为NP始终是一个数字:

NP=`grep nprocshared $GF | tr -dc 0-9`
echo $NP
if [ $N -ne $NP ]; then
  echo "Error"
相关问题