2

I have the following function but it is repetitive and not scalable if I have 5, 6, etc.

append_footnote(){
  file=$1
  if [ "$#" -ge 2 ];then 
    depth=$2
    if [ $depth -eq 1 ];then
      echo '<span style="float: footnote;"><a href="../index.html#toc">Go to TOC</a></span>' >> "$file"
    elif [ $depth -eq 2 ];then
      echo '<span style="float: footnote;"><a href="../../index.html#toc">Go to TOC</a></span>' >> "$file"
    elif [ $depth -eq 3 ];then
      echo '<span style="float: footnote;"><a href="../../../index.html#toc">Go to TOC</a></span>' >> "$file"
    elif [ $depth -eq 4 ];then
      echo '<span style="float: footnote;"><a href="../../../../index.html#toc">Go to TOC</a></span>' >> "$file"
    fi
  else
    echo '<span style="float: footnote;"><a href="./index.html#toc">Go to TOC</a></span>' >> "$file"
  fi
}

It just add ../ in front of index.html#toc depending on $2 arg. How can I make it better?

shin
  • 729
  • 5
  • 15
  • Related: [Writing a character N times using the printf command](https://unix.stackexchange.com/questions/188658/writing-a-character-n-times-using-the-printf-command) – steeldriver Nov 22 '22 at 00:57

1 Answers1

0

I came up this one. It works but please let me know if you have better solutions.

repeat(){
  END=$2
  for i in $(eval echo "{1..$END}")
  do
    echo -n "$1"
  done
}

append_footnote(){
  file=$1
  if [ "$#" -ge 2 ];then 
    depth=$2
    path_str=$(repeat '../' $depth)
    if [ $depth -gt 1 ];then
        echo "<span style='float: footnote;'><a href=\"${path_str}index.html#toc\">Go to TOC</a></span>" >> "$file"
    fi
  else
    echo '<span style="float: footnote;"><a href="./index.html#toc">Go to TOC</a></span>' >> "$file"
  fi
}
shin
  • 729
  • 5
  • 15