【问题标题】:Why isn't this bash script running?为什么这个 bash 脚本没有运行?
【发布时间】:2018-03-24 19:38:44
【问题描述】:

不确定为什么当我尝试运行该脚本时会抛出随机错误。主要在第 1-10 行。这主要是一个编译问题。它也会为空行抛出错误,但我已修复它。

我收到以下错误:

Main.sh: line 9: syntax error near unexpected token $'\r'
Main.sh: line 9: if (( "$#" > 3 ))

这是我的代码:

#!/bin/bash

#echo "All parameters: $@"
#echo "Parameter 1: $1"
#echo "Parameter 2: $2"
#echo "Parameter 3: $3"

tempCol="tempcolfile"
tempRow="temprowfile"
tempMean="tempmeanfile"

if (( "$#" > 3 ))
then
   echo "Matrix has too many arguments" 1>&2
   exit 1
fi


: <<'END'
elif [ $# -gt 1 ]
then
   fileOne=$2
   fileTwo=$3
elif [ $# -eq 1 ]
then
   fileOne=tmp
   cat > $fileOne
   #echo "Cat has finished"
fi
END


#function for dims
dims()
{
  lineNum=0

  while read myLine
  do
    lineNum=`expr $lineNum + 1`
  done <$fileOne

  numcol=$( head -n 1 $fileOne | wc -w) #count number of numbers in first line (-n 1) of input file provided from first argument
  echo -e "$lineNum $numcol"
}

#function for transpose
transpose()
{
  lineNum=0
  i=1

numcol=$( head -n 1 $fileOne | wc -w)

while [[ "$i" -le "$numcol" ]]
  do
     cut -f $i $fileOne > $tempCol
     cat $tempCol | tr -s '\n' '\t' > "$tempRow"
     rev "$tempRow" >"temp222"
     cat "temp222" | cut -c 2-  >"temp333"
     rev "temp333">$tempRow
     #echo >> "$tempRow"
     cat $tempRow
     #cat -A $tempRow
     i=`expr $i + 1`
  done
}

add()
{
 echo "Add function"
}

mean()
{
  i=1
  numcol=$( head -n 1 $fileOne | wc -w)
  echo "Number Col: $numcol"

  while [[ $i -le $numcol ]]
  do

  sum=0
  cut -f $i $fileOne > $tempCol

  while read num
  do
     sum=`expr $sum + $num`
     mean=`expr $sum / 2`
  done <$tempCol

  echo $mean > $tempRow
  cat $tempRow | tr -s '\t' > $tempMean
  echo >> "$tempMean"
  i=$((i+1))

  cat $tempMean

done


}

mulitply()
{
  echo "Multiply Function"
}


#Executing dims and mean
  if [ $1 = "dims" ] || [ $1 = "mean" ]
    then


      if (("$#" > 2 ))
      then
          echo "Invalid number of arguments" 1>&2
          exit 1
        fi

        if [ $# -gt 1 ]
        then
           fileOne=$2
           fileTwo=$3
         fi

        if [ $# -eq 1 ]
        then
           fileOne=tmp
           cat > $fileOne
           #echo "Cat has finished"
        fi

      if [ ! -f $2 ]
        then
          echo "Invalid file" 1>&2        #Redirects stdout to stderr 1>&2
          exit 1
      fi

      if [ $1 = "dims" ]
      then
        dims $fileOne
      fi

      if [ $1 = "mean" ]
      then
     mean $fileOne
      fi



    fi

#Executing transpose
    if [ $1 = "transpose" ]
    then


      if (( "$#" > 2 ))
      then
          echo "Invalid number of arguments" 1>&2
          exit 1

        elif [ $# -gt 1 ]
        then
           fileOne=$2
           fileTwo=$3


        elif [ $# -eq 1 ]
        then
           fileOne=tmp
           cat > $fileOne
           #echo "Cat has finished"
        fi


      if [ ! -r $2 ]
        then
          echo "Invalid file" 1>&2        #Redirects stdout to stderr 1>&2
          exit 1
      fi

      transpose $fileOne

    fi


#Executing add
    if [ $1 = "add" ]
    then
      echo "in Add"

      if [ $# -eq 0 ]
      then
        echo "No arguments to add" 1>&2
        exit 1
      fi

      if [ $# -ne 2 ]
      then
        echo "Invalid number of arguments" 1>&2
        exit 1
      fi

      if [ ! -r $2 ] || [ ! -r $3 ]
      then
        echo "Invalid file" 1>&2
        exit 1
      fi


       add $fileOne $fileTwo
    fi

【问题讨论】:

标签: bash shell unix scripting scripting-language


【解决方案1】:

正如 shellter 在他的评论中暗示的那样,问题在于您的文件包含换行样式的混合。例如。 Microsoft 编辑器(包括记事本)总是在每行末尾添加回车符(CR,\n换行符(LF,\r)。这对于发送到旧电传打印机的数据是有意义的,旧电传打印机有一个物理机制,必须返回最左边的位置向下移动一行。

因此,使用可以删除多余的 \r 字符的合理编辑器,或 dos2unix 等实用程序(也如 shellter 所述)将解决您的问题。

正如 Pierre François 指出的那样,您可以使用 bash 特定的 [[ conditional expression ]] 语法进行所有比较。它比使用 [ expression ] 更灵活且可能更快,并且改进的一致性可以提高可读性。

【讨论】:

    【解决方案2】:

    更改代码行

    if (( "$#" > 3 ))
    

    进入

    if [[ $# > 3 ]]
    

    顺便说一句:总是包含数字的变量不需要引号。

    以这种方式更正其余代码,更改 v.gr。为了连贯性,elif [ $# -gt 1 ] 变为 elif [[ $# &gt; 1 ]]

    【讨论】:

    • 在我看来,这两种变体在功能上是相同的。
    猜你喜欢
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多