Skip to main content
edited title
Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

If loopstatement inside a case statement

Source Link
ramp
  • 781
  • 4
  • 12
  • 22

If loop inside a case statement

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