0

My logic is to check a variable contains Floating point or an Integer and then post that if the variable if floating then have to round the number to the nearest higher number and if it is integer print as it is.

if echo "$FS" | grep "^[0-9]*$" > /dev/null
then
echo "Integer"
elif echo "FS" | grep "^[0-9]*[.][0-9]*$" > /dev/null
then
echo "Floating"
fi

This works perfectly but the problem comes when I integrate this with in the case statement.

#!/bin/bash
IP_DIR=$1
ACTUAL=$2
typeset -l ACTUAL
RETURNSIZE=$3
typeset -l RETURNSIZE

if [ -d "$IP_DIR" ]; then
for OUTPUT in $(find $IP_DIR -maxdepth 1 | awk 'NR>1')
do
        if [ "$ACTUAL" == "true" ]; then
                case $RETURNSIZE in
                "gb") FS=`du -b $OUTPUT | awk {'print $1'}`
                      FS=$(echo "scale=12; $FS  / 1073741824" | bc)
                      echo $OUTPUT "|" $FS "GB";;
                "mb") FS=`du -b $OUTPUT | awk {'print $1'}`
                      FS=`echo $FS | awk '{ byte =$1 /1024/1024 ; print byte " MB" }'`
                      echo $OUTPUT "|" $FS;;
                "kb") FS=`du -b $OUTPUT | awk {'print $1'}`
                      FS=`echo $FS | awk '{ byte =$1 /1024 ; print byte " KB" }'`
                      echo $OUTPUT "|" $FS;;
                "b")  FS=`du -b $OUTPUT | awk {'print $1'}`
                      echo $OUTPUT "|" $FS "B";;
                "all")FS=`du -h $OUTPUT | awk {'print $1'}`
                      echo $OUTPUT "|" $FS;;
                esac
        elif [ "$ACTUAL" == "false" ]; then
               case $RETURNSIZE in
                "gb") FS=`du -b $OUTPUT | awk {'print $1'}`
                      FS=$(echo "scale=12; $FS  / 1073741824" | bc)
                      if [[ $FS == ^[0-9]*$ ]]; then echo $OUTPUT "|" $FS "GB" ;elif [[ $FS == ^[0-9]*[.][0-9]*$ ]]; then echo "$OUTPUT "|" $FS "GB round"; fi
                      ;;#echo $OUTPUT "|" $FS "GB Needed to be rounded";;
                "mb") FS=`du -m $OUTPUT | awk {'print $1'}`
                      echo $OUTPUT "|" $FS "MB";;
                "kb") FS=`du -k $OUTPUT | awk {'print $1'}`
                      echo $OUTPUT "|" $FS "KB";;
                "b")  FS=`du -b $OUTPUT | awk {'print $1'}`
                      echo $OUTPUT "|" $FS "B";;
                "all")FS=`du -h $OUTPUT | awk {'print $1'}`
                      echo $OUTPUT "|" $FS;;
                esac


        fi
done
else
echo "Directory Not Found"
fi

The error messages

.sh: line 48: unexpected EOF while looking for matching `"' .sh: line 50: syntax error: unexpected end of file

2

You have multiple issues in this script.

The missing " is on line 31:

echo "$OUTPUT "|" $FS "GB round"

You also use awk {'print $1'} rather than awk '{print $1}'.

For a full list of syntactical issues, past your script into ShellCheck.

There are also a number of things that could be done better, like stripping down the multiple calls to du -b $OUTPUT | awk {'print $1'} to just one call (makes it easier to maintain).

You also lack several double quotes around variable expansions.

2

I'll plug shellcheck.net, as it helped answer this. It complained:

^-- SC1079: This is actually an end quote, but due to next char it looks suspect.

Indeed, on line 31, you have:

if [[ $FS == ^[0-9]*$ ]]; then echo $OUTPUT "|" $FS "GB" ;elif [[ $FS == ^[0-9]*[.][0-9]*$ ]]; then echo "$OUTPUT "|" $FS "GB round"; fi

if we break that up for readability, it's:

if [[ $FS == ^[0-9]*$ ]]
then 
  echo $OUTPUT "|" $FS "GB"
elif [[ $FS == ^[0-9]*[.][0-9]*$ ]]
then 
  echo "$OUTPUT "|" $FS "GB round"
fi

... at which point it's easier to see that the very last echo "$OUTPUT is has unbalanced quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.