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
  • 749

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=&quot;${path_str}index.html#toc&quot;>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
  • 749