Re: Terminal – ls-like command for http directories?

Johnny Rosenberg gurus.knugum at gmail.com
Sat May 25 16:54:20 UTC 2013


2013/5/25 Florian Diesch <diesch at spamfence.net>:
> Am Sat, 25 May 2013 14:20:57 +0200
> schrieb Johnny Rosenberg <gurus.knugum at gmail.com>:
>
>> Is this possible?
>>
>> Okay, an example:
>> I have a script that downloads and installs unetbootin to my system. I
>> run the script like this:
>>
>> ./Install.sh 583
>>
>> This downloads and ”installs” Unetbootin 5.83, which means it uses
>> wget to download the file, then moves the file to a proper place and
>> finally adds a link to it in ~/bin so I can run it by only typing:
>> unetbootin
>>
>> Now, for this to work, I first need to find out what is the latest
>> version, for instance by looking it up in my web browser. Then I can
>> run my install script.
>>
>> Of course it would be more convenient if the script could find the
>> latest version for me.
>>
>> The latest version (today, 2013-05-25) is:
>> http://tenet.dl.sourceforge.net/project/unetbootin/UNetbootin/583/unetbootin-linux-583
>>
>> So what I need is a command like:
>> some_ls-like_command
>> "http://tenet.dl.sourceforge.net/project/unetbootin/UNetbootin/"
>> Then I guess I would have a loop that determines the highest value
>> somehow, but I guess I will figure that part out.
>
> You can download the page and use awk and friends to the highest
> value, e.g.
>
> ----------------------------------------------------------------------
> URL='http://tenet.dl.sourceforge.net/project/unetbootin/UNetbootin/'
>
> wget -q -O- "$URL" | \
> awk -F '"|/' '/^<tr class/ && !/Custom/ {print $4 }' | \
> sort -n | tail -1
> ----------------------------------------------------------------------

This made my script a lot shorter, thanks! And while editing it, I
also found a bug that I corrected. Not an important one, but still…
The current script:
#!/bin/sh

# To do:
# Some error handling, perhaps?

# 1. Variables
UnetbootinPath="${HOME}/Eget/Nerladdat/Verktyg/Boot/Unetbootin"
OldVersionsPath="${UnetbootinPath}/Gamla versioner"
NewVersionPath="${UnetbootinPath}/Senaste"
RunPath="${HOME}/bin"
DownloadPath="http://tenet.dl.sourceforge.net/project/unetbootin/UNetbootin"

# 2. Just doing an unnecessary parameter check…
if [ $# -ne 0 ]; then
	echo "Alla parametrar ignoreras."
	echo
fi

# 3. Determine the latest version.
Version=$(wget -q -O- "${DownloadPath}" | \
	awk -F '"|/' '/^<tr class/ && !/Custom/ {print $4 }' | tail -1)
FileName="unetbootin-linux-${Version}"
rm -fr "${SourceForgePath}"

# 3.1. Maybe the latest version is already installed?
if [ -f "${NewVersionPath}/${FileName}" ]; then
	echo "Senaste versionen är redan installerad."
	exit 1
fi

# 4. Download the latest version.
wget "${DownloadPath}/${Version}/${FileName}"

# 5. Add the run flag to the downloaded file.
chmod +x "${UnetbootinPath}/${FileName}"

# 6. Move files to where they belong.
# 6.1. First, the file in ${NewVersionPath} isn't the newest one anymore.
#      Now, move that file to the path for older versions.
mv "${NewVersionPath}/"* "${OldVersionsPath}/"

# 6.2. Now, move the downloaded file to ${NewVersionPath}.
mv "${UnetbootinPath}/${FileName}" "${NewVersionPath}/"

# 6.3. Create a link to the new file in ${RunPath}.
ln -fs "${NewVersionPath}/${FileName}" "${RunPath}/unetbootin"

# End of script


I ran it a couple of times under different conditions, and it seems to
work fine (so did the old one but a few milliseconds slower…).


Johnny Rosenberg




More information about the ubuntu-users mailing list