Re: Terminal – ls-like command for http directories?
Johnny Rosenberg
gurus.knugum at gmail.com
Sat May 25 14:04:06 UTC 2013
2013/5/25 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.
>
> That directory currently looks like this:
> 180/ - 21-Jul-2009 00:18
> 189/ - 17-Jul-2009 23:46
> 191/ - 17-Jul-2009 23:46
> 201/ - 17-Jul-2009 23:46
> 214/ - 29-Aug-2012 00:37
> 215/ - 06-Sep-2012 00:38
> 216/ - 27-Aug-2012 00:37
> 225/ - 17-Jul-2009 23:46
> 229/ - 17-Jul-2009 23:46
> 235/ - 17-Jul-2009 23:46
> 241/ - 17-Jul-2009 23:46
> 243/ - 17-Jul-2009 23:46
> 248/ - 27-Nov-2012 01:18
> 262/ - 10-Jul-2012 05:27
> 265/ - 17-Jul-2009 23:46
> 272/ - 11-Jul-2012 03:42
> 274/ - 17-Jul-2009 23:46
> 275/ - 12-Jul-2012 00:43
> 281/ - 17-Jul-2009 23:46
> 282/ - 13-Nov-2012 07:35
> 299/ - 03-Oct-2012 00:35
> 304/ - 22-Nov-2012 00:43
> 306/ - 08-Nov-2012 00:39
> 307/ - 12-Aug-2012 00:37
> 310/ - 17-Jul-2009 23:46
> 312/ - 15-Aug-2012 00:41
> 319/ - 18-Aug-2012 01:04
> 323/ - 01-Nov-2012 00:21
> 344/ - 01-Sep-2012 00:37
> 356/ - 17-Jul-2009 23:46
> 372/ - 20-Sep-2009 21:22
> 377/ - 28-Oct-2009 23:21
> 391/ - 01-Feb-2010 06:07
> 393/ - 15-Oct-2012 02:13
> 408/ - 13-Feb-2010 22:20
> 419/ - 11-Oct-2012 00:40
> 424/ - 27-Mar-2010 21:10
> 429/ - 27-Jan-2013 03:04
> 433/ - 19-Oct-2012 00:35
> 436/ - 22-Oct-2012 00:30
> 442/ - 23-Oct-2012 00:29
> 466/ - 03-Jun-2010 07:11
> 471/ - 31-Oct-2012 00:22
> 485/ - 03-Nov-2012 00:25
> 490/ - 01-Dec-2012 00:25
> 491/ - 02-Dec-2012 00:56
> 494/ - 11-Oct-2010 01:04
> 502/ - 22-Dec-2012 06:28
> 506/ - 14-Jan-2013 02:11
> 527/ - 27-Mar-2011 03:29
> 549/ - 15-Jan-2013 00:23
> 555/ - 17-Mar-2013 00:48
> 563/ - 27-Apr-2013 00:18
> 568/ - 21-May-2013 00:26
> 575/ - 28-Apr-2012 08:02
> 577/ - 13-Jul-2012 04:06
> 578/ - 15-Jul-2012 02:25
> 581/ - 27-Aug-2012 07:07
> 583/ - 24-Dec-2012 08:45
> Custom/
>
>
> Johnny Rosenberg
Okay, I solved it, I think, see 3.1 in my 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"
# 1.1. These folders are created by the next wget next command below.
SourceForgePath="${UnetbootinPath}/tenet.dl.sourceforge.net"
ContentsPath="${SourceForgePath}/project/unetbootin"
# 2. Just doing an unnecessary parameter check…
if [ $# -ne 1 ]; then
echo "Alla parametrar ignoreras."
echo
fi
# 3. Determine the latest version.
# No error handling at the moment…
wget -r "${DownloadPath}"
# 3.1. The HTML file UNetbootin in ${ContentsPath} has been created. It contains
# the sub-diectories of ${DownloadPath}. The second to last line represents
# the sub-directory for the latest version.
Version=$(html2text "${ContentsPath}/UNetbootin" | \
tail -2 | head -1 | sed -r 's/([0-9]*).*/\1/')
FileName="unetbootin-linux-${Version}"
rm -fr "${SourceForgePath}"
# 3.2. 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 anymore.
# Now, move that file to the path for older versions.
cd "${NewVersionPath}/"
mv $(ls | grep "unetbootin-linux") "${OldVersionsPath}/"
# 6.2. Now, move the downloaded file to ${NewVersionPath}.
cd "${UnetbootinPath}"
mv $(ls | grep "unetbootin-linux") "${NewVersionPath}/"
# 6.3. Create a link to the new file in ${RunPath}.
cd "${NewVersionPath}/"
ln -fs $(find "${NewVersionPath}/" | \
grep "unetbootin-linux") "${RunPath}/unetbootin"
cd "${UnetbootinPath}"
Now, after running this script, the latest version is located in the
latest version directory and a link to it, called ”unetbootin”, is
avaliable in ~/bin.
So I tried to open it by typing ”unetbootin” in a terminal, but it
failed to open. A password is needed to run unetbootin, and that
dialogue came up as expected. I entered my password, clicked OK, the
dialogue disappeared, but then nothing more happened.
If I type ~/bin/unetbootin, the program runs as expected.
~/bin is included in $PATH as the first path, so I shouldn't really
need to type anything more than just unetbootin.
I am obviously missing something, right?
Johnny Rosenberg
More information about the ubuntu-users
mailing list