Results 1 to 3 of 3

Thread: How can I filter a saved file filename from a link? [shellscript]

  1. #1
    Join Date
    Nov 2008
    Beans
    48

    [SOLVED]How can I filter a saved file filename from a link? [shellscript]

    Hello, I'm trying to make a simple script which downloads a linked media file, then immediately plays it on mplayer. These are mostly for the podcasts I subscribe to.

    #!/bin/sh

    wget -O temp #1
    mplayer temp
    Instead of temp, I want to be able to preserve the original intended filename. So www.podcast.com/hello.mp3 > hello.mp3

    I think its some sort of sed/awk type filtering or something, but I'm not sure. Any of you know?

    Thanks,

    Kitt
    Last edited by kittkatt; January 23rd, 2011 at 10:04 PM. Reason: it got solved

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: How can I filter a saved file filename from a link? [shellscript]

    Code:
    file="a.com/stuff/hello.mp3"
    fileshort=${file##*/}
    echo $file "=>" $fileshort
    ${VARIABLE##*/}
    VARIABLE - variable to cut text from
    ## - ignore stuff matching the pattern, starting from the left (single # would be non-greedy, ## is greedy and it tries to match as much as it can, there are also % and %% that do similar thing at the end of the string)
    */ - pattern to throw away - in this case any sequence ended with /

    #*/ a/b/c
    ##*/ a/b/c
    %/* a/b/c
    %%/* a/b/c
    bolded is the returned value, the rest of the string is thrown away
    Last edited by Vaphell; January 23rd, 2011 at 07:45 PM.

  3. #3
    Join Date
    Nov 2008
    Beans
    48

    Re: How can I filter a saved file filename from a link? [shellscript]

    Sweet! Solved it, thanks!

    Here's the script

    #!/bin/sh

    echo $1
    fileTarget=$1
    fileshort=${fileTarget##*/}
    echo $fileTarget "=>" $fileshort

    wget -O $fileshort $fileTarget
    mplayer $fileshort

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •