Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to find if a file is available on the particular website. Typically it is an .mp3.
If it is available, I wish to action a command, likewise if not available - either exit or action a command.
I have had a go, but not sure where I am going wrong... I am hoping to write this in BASH as the other files I am running are in bash - keeping it simple (if possible)...
Code:
file=“music.mp3"
url="http://www.some.site/here”
curl -s $url/$file | grep 404
if [ -f $file ] ; then
echo " File -> $file <- FOUND!"
Run_download_script_here
else
echo " File -> $file <- Not found!"
fi
exit 0
Above is not displaying the desired result. I am simply wanting to check that the file is available on the website (for downloading).
I am getting lost with the grep section, possibly the If statement checking for the file showing on the website and whether or not I am required to download the file or is there a command to make sure that the file is there (rather than downloading it).
Also, curl sends the fetched data to stdout. So the output from -w will be appended there unless you do -w '%{stderr} %{http_code}' to redirect to stderr. However, I don't know how to juggle that so you'd get the file redirected from stdin at the same time stderr gets piped to grep.
Another option would be to use wget which will return an error code in the event of a 404 HTTP status code or similar failure.
Code:
#!/bin/sh
file="music.mp3"
url="http://www.some.site/here”
if wget $url/$file; then
echo " File -> $file <- FOUND!"
Run_download_script_here
else
echo " File -> $file <- Not found!"
fi
exit 0
I am aiming to run the script to see if the file is present on the webpage, not necessarily downloading the file to trigger a command.
With the stdout, assume that would be after the curl line (no output file)?
Let say the file is found (mp3), will curl download the file or simply output some data which we can use a command to say Yes/No in the IF statement?
I don’t understand what the code in #5 is doing and where it should go to try and make it happen?
It looks like it’s doing something when it sees a 404 error?
^^ Isn't this testing for the file name contained in the variable (music.mp3) exists on the local disk in the directory in which the script is running? Has nothing to do with whether a file by that name is found by curl...
I am aiming to run the script to see if the file is present on the webpage, not necessarily downloading the file to trigger a command.
Then perhaps curl -f would suffice?
Quote:
Originally Posted by orangepeel190
I don’t understand what the code in #5 is doing and where it should go to try and make it happen?
It looks like it’s doing something when it sees a 404 error?
It separately evaluates standard output and standard error. Using your code from #1 it would be something like this
I was simply having an attempt at some scripting, rather than be “one of those people” that simply asks someone else to do all the work for them.
Yes, I am aware that -f will see if the file is available on the local disc, maybe it was not the best to use that “-f” statement given the request to see if the file is simply available on the website. I was interested to see where the scripting in #5 would best go to give it a go.
The issue that I am seeing is that the script could download a error message imposed as $file which the system will see as a Pass.
Classic example was this morning, I ran a script thinking it was downloading the file, yet when I explored deeper, the file looked like the mp3 file, but the message below: cat $file
Quote:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>509 Bandwidth Limit Exceeded</TITLE>
</HEAD><BODY>
<H1>Bandwidth Limit Exceeded</H1>
The server is temporarily unable to service your
request due to the site owner reaching his/her
bandwidth limit. Please try again later.
</BODY></HTML>
The -f resulted in a “Yes, the file was downloaded”, when it clearly was not the audio file. I am now having to add an external conditional statement in the script to ensure the file is larger than, say, 1Mb. If the file is downloaded and larger than 1Mb, then “Success, the file was downloaded”, if not, delete the small file and Error out - try again later.
It would be good to not have to download the file IF is not the correct file or not even available..... kinda gotten a little bigger problem than a simple download script.
Happy to try as many options as possible to get a functioning script and learn in the process.
Appreciate the feedback and assistance to my steep learning curve....
Last edited by orangepeel190; 07-17-2020 at 09:16 PM.
I’ll try and pop that into the script as well as develop a IF/THEN statement for the size of the file to try and filter the file correctly.
The curve ball presented this morning with the bandwidth message masquerading as the .mp3 file. I am thinking that a size comparison filter would be the best to help tighten the filter on the correct file download., as typically the error files are smaller than 1Mb and audio files on this site are larger than 3Mb.
I was hoping to have not gone through the download process and filter locally, rather have some funky script that looks at the size of the remote file to make sure that it “appears” to be the correct file (by availability, name and size) and if so, run a command (either download or send email) that the correct remote file appears to be available.
I was thinking either dump the HTML code and use grep to pull out the file name or 404 error, but that technique appears to be flawed with the file masquerading as the audio file but contains the “This server is unavailable....” message this morning - hence now also having to compare file sizes
I am hoping that something would be possible with some crafty but robust scripting and commands in Linux? I’m running it on a Raspberry Pi.
I was thinking either dump the HTML code and use grep to pull out the file name or 404 error, but that technique appears to be flawed with the file masquerading as the audio file but contains the “This server is unavailable....” message this morning - hence now also having to compare file sizes
Is that information visible in the response headers?
Thanks for your response....
The website issue have since resolved, as it was a hosting issue this morning.... so I cannot answer your question when the server was experiencing issues. Don’t know where there was a bandwidth issue but I sent an email, they confirmed they were having issues and it appears the issues have resolved (more bandwidth?)
I’ve checked the curl output and it would appear to be best running a check based on a header for 404 or returning 200?
How can I grep the header (either 200 or 404) to provide me with an IF/THEN/ELSE/FI option to either run the download if “200” is returned or error out if “404” returned?
It seems like the webserver has a fit this morning which added to my additional question relating to file size
Would something like this potentially work?
Code:
enquiry=$(curl -sLo --head /dev/null -w "%{$url/$file}\n" ${1})
if [[ $enquiry != 200 ]]; then
echo "Success ${enquiry} on ${1}"
echo “Sending notification email the file is available....”
else
echo “File is not available.... try again later”
fi
Would it be —head or -I (capital i (eye))
Or alternate for enquiry != 404 (equating to error or not available)
Close for a bash script?
Last edited by orangepeel190; 07-18-2020 at 01:34 AM.
Or alternate for enquiry != 404 (equating to error or not available)
Close for a bash script?
Usually there are long options and short options, so with curl it would be matter of style and not substance whether to use -I or --head. Mind the type of dashes though. You need two single dashes (n-dashes) not one m-dash in this particular case.
The conditional statements in shell scripting work on the exit codes of programs or piped chains of programs. So you could do,
Code:
if /usr/bin/true; then
echo OK
else
echo Not OK
fi
if /usr/bin/false; then
echo OK
else
echo Not OK
fi
And then
Code:
if curl --silent --head $url/$file | grep -q -c 1 -P '^HTTP/\w\.\w\s200\sOK'; then
echo OK
else
echo Try later
fi
pan64 = that appears to download the file rather than checking if it’s available... is there an alternative to downloading and check via a curl command or result (potentially in the header)?
Last edited by orangepeel190; 07-18-2020 at 05:05 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.