-2

I have a little project with HTML. My script to build my HTML is this:

#!/bin/bash

summe=$(find /home/info/PDF -type f | wc -l)
array=()
find /home/info/PDF -type f -print0 >tmpfile
while IFS=  read -r -d $'\0'; do
array+=("$REPLY")
done <tmpfile
if [ $summe>0 ]
    then
            rm -f /var/www/html/index.html
            cat header.txt > /var/www/html/index.html
            for (( i=0; i<$summe; i++))
            do
                    cat testcontent.txt >> /var/www/html/index.html
            done
            cat footer.txt >> /var/www/html/index.html
fi

With $summe I count how many PDFs are in my directory, in order to display them at a later stage on my Nginx. In the example, I have 3 PDFs then the content snippet will be set 3 times in my HTML. Now my problem is the name of the PDFs are different and I didn't know how I can change them with SED correctly.

The content snippet:

<div class="content">
    <object data="chungus.pdf" style="width:1920px;height:1080px"</object>
  </div>

I hope this is enough information and sorry for my bad english I'm still studying.

Biffen
  • 6,249
  • 6
  • 28
  • 36
  • The concept for your script is a bit unusual. You should describe in a more abstract level what you want to do. I understand that you want to create a HTML file that contains a constant(?) header `header.txt`, a part `testcontent.txt` for every file you found, modified to contain the file name (only the base name `eins.pdf` or with full path) and a footer. You re-create the HTML file only if you found at least one file. If you didn't find a file, the HTML remains unchanged. Do you need to display the number of files somewhere in the header or footer? – Bodo Mar 19 '19 at 10:52
  • Possible duplicate of [Comparing numbers in Bash](https://stackoverflow.com/q/18668556/608639), [Compare variable with integer in shell?](https://stackoverflow.com/q/18133161/608639), etc. Also see [How to use Shellcheck](http://github.com/koalaman/shellcheck), [How to debug a bash script?](http://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](http://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](http://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Mar 19 '19 at 10:52
  • @jww Obviously there are problems in the script, but the main question seems to be how to get the file names into the generated HTML output (using sed). – Bodo Mar 19 '19 at 10:55
  • @Bodo I dont need to display the number of files in the header or footer only in testcontent.txt – Big_Chungus Mar 19 '19 at 12:20
  • Please [edit] your question to add more information instead of answering in a comment only. I'm not sure I understand correct. Do you need `$summe` in the modified version of `testcontent.txt`? Your content snippet contains a file name `chungus.pdf`. I assume this should be replaced with the file names like `eins.pdf`, `zwei.pdf` etc. from the example that someone removed from the question. There is nothing that obviously should be replaced with the number of files. If you don't need the number in the content, you don't need to count the files first. – Bodo Mar 19 '19 at 12:46

1 Answers1

2

in bash from bash manual CONDITIONAL EXPRESSIONS, note the double quotes around variable and spaces around > are important

[ "$summe" > 0 ]

is a lexicographic test

string1 > string2

          True if string1 sorts after string2 lexicographically.

Single bracket test for posix compatibility

[ "$summe" -gt 0 ]

otherwise with bash double brackets allows to remove double quotes

[[ $summe -gt 0 ]]

otherwise there's arithmetic expression (double parentheses), here the spaces are not important and the $ can be ommited

((summe>0))
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36