Ravenwolf
Ravenwolf

Reputation: 27

Assign HTML tags to a variable

Just a quick question guys. I would like to assign html tags to a variable in BASH. How do i do this?

Or is there a better way to do this?

This is the actual portion of the code:

function storage_util_1942
{
  if [ $SETFLAG_1942 = "YES" ];
  then

  for i in {1..5}
  do
  TMPSTOR=$(cat $HCREPORT1942 | grep -A$i DISK | tail -1)
  RULESET=$(cat $HCREPORT1942 | grep -A$i DISK | tail -1 | awk '{print $6}' | cut -d'%' -f1)

  if [ $RULESET -gt 90 ]
  then
    STORAGE$i="<font color=\"red\"><b>${TMPSTOR}</b></font>"
  else
    STORAGE$i="<font color=\"green\"><b>${TMPSTOR}</b></font>"
  fi
  done

fi

echo "  <tr align="left">"
echo "    <td><font face="verdana,helvetica" size="1" color="red"><b>Storage Capacity <font color="green">[Must be less than 90%]</b></font></td>"
echo "  </tr>"
echo "  <tr align="left" bgcolor="white" height="80">"
echo "    <td><font face="courier new" size="1" color="black">"
echo "      <br>"
echo "      <pre>"
echo "       $STORAGE1"
echo "       $STORAGE2"
echo "       $STORAGE3"
echo "       $STORAGE4"
echo "       $STORAGE5"
echo "      </pre></font>"
echo "    </td>"
echo "  </tr>"

}

This is part of an HTML base reporting that I am creating. The objective is to display the value of $STORAGE(#) base on the condition set above. RED if it is above 90 and GREEN otherwise.

When I tried to run the script it still give me the "No such file or directory" error:

+++ storage_util_1942
+++ '[' YES = YES ']'
+++ for i in '{1..5}'
++++ cat /apps/data/support_bin/monitoring/healthcheck/healthcheck.report
++++ grep -A1 DISK
++++ tail -1
+++ TMPSTOR='/dev/mapper/rootdg-rootdgvol2   ext3   1.9G  1.7G  133M   93%   /var'
++++ cat /apps/data/support_bin/monitoring/healthcheck/czchols1942.healthcheck.report
++++ grep -A1 DISK
++++ tail -1
++++ awk '{print $6}'
++++ cut -d% -f1
+++ RULESET=93
+++ '[' 93 -gt 90 ']'
+++ 'STORAGE1=<font color="red"><b>/dev/mapper/rootdg-rootdgvol2   ext3   1.9G  1.7G  133M   93%   /var</b></font>'

./script.sh: line 166: STORAGE1=<font color="red"><b>/dev/mapper/rootdg-rootdgvol2   ext3   1.9G  1.7G  133M   93%   /var</b></font>: No such file or directory

The evaluation before the last line is already correct but how come it evaluates the value as if it is executed? I should only stay as a value and not a command.

Thanks,

Upvotes: 1

Views: 2125

Answers (2)

Brian Stephens
Brian Stephens

Reputation: 5271

You need quotes around the variable assignments, and since your values have double-quotes (and no other variables referenced), I would use single quotes:

var='<font color="red">HELLO WORLD</font>'

If you need to interpret other variables within the assignment, you need to use double-quotes, escaping any literal ones:

var="<font color=\"red\">${TMPSTOR}</font>"

You also need to use arrays for your "dynamically-named" variables:

STORAGE[$i]="..."

echo "$STORAGE[1]" ...

The bigger question is what you're really trying to do with this. Why are you using a <font> tag, why aren't you just setting a CSS class as the variable (since that's the only difference in the HTML), etc.

Upvotes: 1

ghoti
ghoti

Reputation: 46876

There are lots of things in the script segment you've posted that could be done in "a better way". Here's a sampling of them.

  1. Don't use unnecessary pipes. awk will do everything that grep, tail and cut will do, without launching extra processes for each step through the pipeline.

    TMPSTOR=$(awk -vi="$i" '/DISK/{n=i+1} {n--} n==0 {print;exit}' $HCREPORT1942)
    
  2. Use quotes around variables. You've included if [ $SETFLAG_1942 = "YES" ]; which is backwards. The thing you want to protect in quotes is the variable, not the text that is hard-coded into the script.

  3. You're using dynamic variable naming. This is a Bad Thing, as it usually requires use of eval, which is evil.

    eval STORAGE$i="'<font color=\"red\"><b>\${TMPSTOR}</b></font>'"
    

    So instead of STORAGE$i=..., use an array.

    declare -a STORAGE
    if [ -n "${RULESET//[0-9]/}" ]; then
      # RULESET contains non-digts, which it should not.
      echo "ERROR: invalid data, please investigate." >&2
      exit 1
    elif [ "$RULESET" -gt 90 ]; then
      STORAGE[$i]="<span class='warn'>${TMPSTOR}</span>"
    else
      STORAGE[$i]="<span class='good'>${TMPSTOR}</span>"
    fi
    

    Then walk through the array with something like:

    for item in ${STORAGE[@]}; do
      echo "$item"
    done
    
  4. And finally, your HTML needs to be improved, but that's off-topic for this question.

Upvotes: 1

Related Questions