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