Hi all
I have a little problem with bourne shell. I've just started writing that kind of scripts
I have to write a script that search in folder for header.txt, footer. and content.txt. If these files exists i have to copy the content from them (ignoring the lines with #) and generate HTML file with this content where the content from file footer.txt goes between footer tags, the text from header.txt in header tags and content between them. After that I have to check date of the files. If the tree files are newer than the HTML i have to generate a new one , if not to exit the script. In the three files (header.txt, footer. and content.txt) i can have all kind of text withoth head and body tags.

 #!/bin/sh
 echo "Enter folder path: "
read path
cd
cd $path
 file1="header.txt" 
file2="footer.txt"
file3="content.txt"
file4="project.html" 
 if [ -e $file1 ]
    then echo "Header.txt exists"
    else echo "Header.txt doesn't exists"
fi
 if [ -e $file2 ]
    then echo "Footer.txt exists"
    else echo "Footer.txt doesn't exists"
 fi
 if [ -e $file3 ]
    then echo "Content.txt exists"
    else echo "Content.txt doesn't exists"
fi
if [ -e $file4 ]
    then //if file exists i have to check the modify dates and if the HTML file is older than the other 3 to I have to create a new HTML else to exit
    else //if the file doesn't exist to create one
 fi

Can anyone help me , please
Thank you in advance

Member Avatar for b1izzard

If file exists i have to check the modify dates and if the HTML file is older than the other 3 to I have to create a new HTML else to exit

You can sort the file based on the Modification time by using

    ls -rt1 $file1 $file2 $file3 $file4

Now check the firstline of the output, if it contains "project.html" then create a new HTML file.

thank you for that advise ,but my problem is how to create the html file (file4) and copy the data from the other three file in that one . I use this code

cat > $file4 <<- _EOF_
<HTML>
 <HEAD>
   <TITLE>
   </TITLE>
 </HEAD>
 <BODY>
 </BODY>
</HTML>
_EOF_
echo "Project.html has been created"

I put this code in line 24 after "else" but this doesn't help me . I can not figure out how to read the three files and put the data from them in the body tag. For example the data from header.txt should be copy between header tags in the body.

backquotes with cat?

"> filename
Connect standard output to the file filename. This allows you to save the output of a command to a file. If the file does not exist, it is created. If it does exist, it is emptied before anything happens."
this is what i've read in website for bourne shell programing. I can't find anything about HTLM problem that i have

hi,

if file exists i have to check the modify dates and if the HTML file is older than the other 3 to I have to create a new HTML

this tests £file4 is older than the other files

test $file4 -ot $file1 -a $file4 -ot $file2 -a ...

if previous is true; then

look at what this does

cat > finalFile<<eof
header
$(<header.txt)
body
$(body.txt)
foot
$(<footer.txt)
end
eof

and adapt to your case.

ah, your using SH :(
this won't work then, sorry.

you need to echo tags and cat file between each echo, using >> to append.

I get to here with the code

#!/bin/sh
echo "Enter folder path: "
read path
cd
cd $path
file1="header.txt" 
file2="footer.txt"
file3="content.txt"
file4="project.html" 
if [ -e $file1 ]
  then echo "Header.txt exists"
  else echo "Header.txt doesn't exists"
fi
if [ -e $file2 ]
  then echo "Footer.txt exists"
  else echo "Footer.txt doesn't exists"
fi
if [ -e $file3 ]
  then echo "Content.txt exists"
  else echo "Content.txt doesn't exists"
fi
if [ -e $file4 ]
  then
        if  [ $file4 -ot $file1 ] && [ $file4 -ot $file2 ] && [ $file4 -ot $file3];
        then
        (cat <<_EOF
<body>
<header>
$(cat < $file1 )
</header>
$(cat < $file3)
<footer>
$(cat < $file2)
</footer>
</body>
_EOF
)> project.html
        echo "Project.html has been updated"
        else exit(0) 
        fi
  else
(cat <<_EOF
<body>
<header>
$(cat < $file1 )
</header>
$(cat < $file3)
<footer>
$(cat < $file2)
</footer>
</body>
_EOF
)> project.html
echo "Project.html has been created"
fi  

but when i start it and i have on error : ./project.sh: 39: Syntax error: word unexpected (expecting ")") can you help me with that.
And what have i to write to cat only the lines which don't start with # from the files 1,2 and 3 ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.