LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-06-2018, 02:57 PM   #1
Davno
Member
 
Registered: Mar 2004
Location: Montreal, Canada
Distribution: Linux MX 23 KDE "Libretto"
Posts: 217

Rep: Reputation: 25
bash script modification help relating to linking html files for download


Hi.
I have a bash script that create a .html file that list all the files from a specific folder showing the date, the quantity of files in that folder, their titles, order numbers, ect...
Then that script copies that .html file to my Apache server.
I want to modify that script or may be the resulting .html file in order to link those files for download.
I know how to link a line to a file (one by one is not an option) or i could make the directory content downloadable but i rather modify the script instead, is it possible and how should i do it? Thank You.

Here is a sample of that script:
Code:
#!/bin/bash
# y
cd /media/norm/83644822B578E/x/y/
date > ty.html
echo "<hr>" >> ty.html
echo "y<br />" >> ty.html
number=`ls *.txt | wc -l`
echo "Their is $number y in this folder.<br />" >> ty.html
echo "<hr>" >> ty.html
count=0
for i in *txt ; do
#echo item: $i
((count+=1))
echo "# $count<br />" >> ty.html
echo $i "<br>" >> ty.html
done

cp /media/norm/83644822B578E/x/y/ty.html /var/www/html/list

Last edited by Davno; 09-06-2018 at 03:06 PM.
 
Old 09-06-2018, 03:03 PM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,732

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Isn't it just changing
Code:
echo $i "<br>" >> ty.html
to
Code:
echo "<a href=$i>$i</a> <br>" >> ty.html
Or am I missing something?
 
Old 09-06-2018, 03:09 PM   #3
Davno
Member
 
Registered: Mar 2004
Location: Montreal, Canada
Distribution: Linux MX 23 KDE "Libretto"
Posts: 217

Original Poster
Rep: Reputation: 25
Smile Thank you for the reply

Ill try that, Thxs
 
Old 09-06-2018, 03:19 PM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,732

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Sorry...the href needs to be quoted...try

Code:
echo '<a href="$i">$i</a> <br>' >> ty.html
 
Old 09-06-2018, 03:23 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
have you thought about maybe tar'ing all of the files then attach that to your download link to save time having to click each and every link to get the entire contents?

your loop in the script should be controlled by your total count or EOF. or a while loop even.

Code:
#!/bin/bash

script_dir=/holding_dir_for_file
search_dir=/where_ever
count=0

#header code and date time stamp echo'ed in here, 
#then add links here

while read f ; do
{
    echo "<a href="\"$f\"">$f</a>" >> ty.html
    ((count++))
}
done <<<"$(find "$search_dir" -type f )"

echo " total files : $count " >> ty.html

#then finalizing code to end your html file suitable for hosting here

cp or mv $script_dir/ty.html /var/www/html/list
then just run the script from a cental location

Last edited by BW-userx; 09-06-2018 at 04:15 PM.
 
1 members found this post helpful.
Old 09-06-2018, 03:29 PM   #6
Davno
Member
 
Registered: Mar 2004
Location: Montreal, Canada
Distribution: Linux MX 23 KDE "Libretto"
Posts: 217

Original Poster
Rep: Reputation: 25
Part it good

ty.html is being created with the folder content listed in it, and all lines show a link in "blue" but all the line point to where the new ty.html is located (in var/www/html/list) not where the the .txt files are located
(/media/norm/83644822B578E/x/y)
 
Old 09-06-2018, 03:50 PM   #7
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,732

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by Davno View Post
ty.html is being created with the folder content listed in it, and all lines show a link in "blue" but all the line point to where the new ty.html is located (in var/www/html/list) not where the the .txt files are located
(/media/norm/83644822B578E/x/y)
You need to construct the href value to be correct...pre-pend the path to the contents of the href= string:
Code:
echo '<a href="/media/norm/83644822B578E/x/y/$i">$i</a> <br>' >> ty.html
If that's where the files are located.

NOTE: Linking to the files will still require a visitor to right-click and select the appropriate item to download the file (On Firefox it's "Save link as...")
I'll need to remember how to make the link be a "download link"...I know I've done that...
Ahh (DDG is my friend)
Code:
echo '<a href="/media/norm/83644822B578E/x/y/$i" download="$i">$i</a> <br>' >> ty.html

Last edited by scasey; 09-06-2018 at 03:59 PM.
 
1 members found this post helpful.
Old 09-06-2018, 03:56 PM   #8
Davno
Member
 
Registered: Mar 2004
Location: Montreal, Canada
Distribution: Linux MX 23 KDE "Libretto"
Posts: 217

Original Poster
Rep: Reputation: 25
nope

Quote:
Originally Posted by scasey View Post
Sorry...the href needs to be quoted...try

Code:
echo '<a href="$i">$i</a> <br>' >> ty.html
As per your first reply it listed the name of the .txt properly but they link where the ty.html is located.
And as per your second reply all the .txt are called "$1" in the ty.html and still link to the ty.html location.
 
Old 09-06-2018, 03:59 PM   #9
Davno
Member
 
Registered: Mar 2004
Location: Montreal, Canada
Distribution: Linux MX 23 KDE "Libretto"
Posts: 217

Original Poster
Rep: Reputation: 25
OK :)

Quote:
Originally Posted by scasey View Post
You need to construct the href value to be correct...pre-pend the path to the contents of the href= string:
Code:
echo '<a href="/media/norm/83644822B578E/x/y/$i">$i</a> <br>' >> ty.html
If that's where the files are located.

NOTE: Linking to the files will still require a visitor to right-click and select the appropriate item to download the file (On Firefox it's "Save link as...")
I'll need to remember how to make the link be a "download link"...I know I've done that...
Thank YOu. I will try that.
 
Old 09-06-2018, 04:08 PM   #10
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,732

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
And as per your second reply all the .txt are called "$1" in the ty.html ...
Yes. You'll need to do it the way BW-userx posted...
Code:
"<a href=\"...\" download=\"$i\">$i</a>"
...that is, escape the internal quotes.

See w3shools
 
Old 09-06-2018, 04:13 PM   #11
Davno
Member
 
Registered: Mar 2004
Location: Montreal, Canada
Distribution: Linux MX 23 KDE "Libretto"
Posts: 217

Original Poster
Rep: Reputation: 25
Problem solved

It work now.
Thank you very much.
 
Old 09-06-2018, 04:17 PM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,740

Rep: Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923Reputation: 5923
Just for a FYI you can create dynamic web pages using something like php or even convert your existing script to a CGI for a more automatic process.
 
Old 09-06-2018, 06:54 PM   #13
Davno
Member
 
Registered: Mar 2004
Location: Montreal, Canada
Distribution: Linux MX 23 KDE "Libretto"
Posts: 217

Original Poster
Rep: Reputation: 25
still a little glitch

Quote:
Originally Posted by scasey View Post
You need to construct the href value to be correct...pre-pend the path to the contents of the href= string:
Code:
echo '<a href="/media/norm/83644822B578E/x/y/$i">$i</a> <br>' >> ty.html
If that's where the files are located.

NOTE: Linking to the files will still require a visitor to right-click and select the appropriate item to download the file (On Firefox it's "Save link as...")
I'll need to remember how to make the link be a "download link"...I know I've done that...
Ahh (DDG is my friend)
Code:
echo '<a href="/media/norm/83644822B578E/x/y/$i" download="$i">$i</a> <br>' >> ty.html
I thought it work perfect then i realised that some of these files have blank space in there name, so the link does not work.
Is there some line i can add to the script for it to disregarded the blank space. Otherwise it works nice.
Thank You
 
Old 09-06-2018, 07:14 PM   #14
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
single quoting it might help
Code:
userx@manjaroo:~
$ i="go ba"
$ echo \'$i\'
'go ba'
 
Old 09-06-2018, 08:07 PM   #15
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,732

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by BW-userx View Post
single quoting it might help
Code:
userx@manjaroo:~
$ i="go ba"
$ echo \'$i\'
'go ba'
or double quoting the assignment to i
Code:
for i in "*txt"
Please let us know which worked...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
A bash script med sed to extract every thing efter <body> and before </body> tags from html or htm files PetterS Linux - Newbie 4 01-21-2017 04:11 AM
Get file modification date/time in Bash script cmfarley19 Programming 16 04-15-2015 06:25 PM
problem linking bash-cgi script with HTML front-end using Ajax dreamqq Programming 2 07-31-2014 06:08 PM
[SOLVED] How can I purge files logarithmically by modification date (BASH)? gunnarflax Programming 31 03-10-2013 02:45 PM
Bash script: symbolic linking all files with same extension in a directory Katachi Programming 1 12-25-2006 09:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration