jeudi 5 août 2021

Shell - Control flow function, Multiples variables

I'm pretty new to programming and shell and I'm not having an issue with the script below, it's working perfectly but I want to know if I can do better.

I'm inserting the content of some CSV files (value1, value2, value3) into an html table. I'm using a while loop (easier for me), but I think there might be a simpler way to do it.

Is it possible to have the below script in one single while loop? I want to echo different text depending on the value of the variable. Thanks

echo "<!DOCTYPE html>
<html>
<style>
tr, td {
  border: 1px solid black;
  border-collapse: collapse;
  text-align: center;
}

table {
  width: 75%;
}

h2 {
  color: navy;
  text-transform: uppercase;
}

h3 {
  color: navy;
  text-transform: uppercase;
  padding-top:50px;
}

</style>"

var1 ="value1"
for a1 in $var1; do
  echo "<h2>text1</h2>
<table>"
  print_header=true
  while read INPUT ; do
    if $print_header;then
      echo "<tr><span style="text-transform:uppercase"><td><b>${INPUT//,/</b></td><td><b>}</b></td></span></tr>";
      print_header=false
    else
      echo "<tr><td>${INPUT//,/</td><td>}</td></tr>" ;
    fi
  done < $a1;
  echo "</table>"
  done
 
var2="value2 value3"
for a2 in $var2; do
  echo "<h2>text2</h2>
<table>"
  print_header=true
  while read INPUT ; do
    if $print_header;then
      echo "<tr><span style="text-transform:uppercase"><td><b>${INPUT//,/</b></td><td><b>}</b></td></span></tr>";
      print_header=false
    else
      echo "<tr><td>${INPUT//,/</td><td>}</td></tr>" ;
    fi
  done < $a2 ;
  echo "</table>"
  done 

Aucun commentaire:

Enregistrer un commentaire