为什么这个bash脚本不运行?

时间:2018-03-24 19:38:45

标签: bash shell unix scripting scripting-language

当我尝试运行它时,不确定为什么这个脚本会抛出随机错误。主要在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

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:-3)

更改代码行

if (( "$#" > 3 ))

if [[ $# > 3 ]]

顺便说一下:在一个始终包含数字的变量周围不需要引号。

以这种方式纠正其余的代码,改变v.gr. elif [ $# -gt 1 ] elif [[ $# > 1 ]]为了保持一致性。

相关问题