0

Not sure why this script is throwing up random errors when I try to run it. Mainly in lines 1-10. It's mostly a compilation issue. It's throwing up errors for empty lines as well but I fixed it.

I'm getting the following errors:

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

Here is my code:

#!/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
Ani
  • 1

2 Answers2

1

As shellter hints at in his comment, the problem is that your file contains a mix of newline styles. E.g. Microsoft editors (including notepad) always add both a carriage return (CR, \n) and a line feed (LF, \r) at the end of each line. This made sense for data sent to old teleprinter, which had a physical mechanism that had to return to the leftmost position and move down one line.

So using a sensible editor that can remove the superfluous \r characters, or a utility such as dos2unix (also, as noted by shellter) would fix your problem.

As Pierre François points out, you can use the bash specific [[ conditional expression ]] syntax for all your comparisons. It's more flexible and potentially faster than using [ expression ] and improved consistency can improve readability.

Morten
  • 634
  • 5
  • 13
-3

Change the line of code

if (( "$#" > 3 ))

into

if [[ $# > 3 ]]

By the way: quotes are not necessary around a variable containing always a number.

Correct also the rest of the code in this way, changing v.gr. elif [ $# -gt 1 ] into elif [[ $# > 1 ]] for the sake of coherence.

Pierre François
  • 5,850
  • 1
  • 17
  • 38