Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. /(PHP + Mysql Developer)+/
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2007
    Location
    UK
    Posts
    195
    Rep Power
    37

    Get Basename without extension not working


    Trying to get the basename of a file without the extension,
    I found this snippet but it just returns command not found twice:

    Code:
    #!/bin/bash
    # Convert to 
    file=$1
    # get extension; everything after last '.'
    ext=${file##*.}
    
    # basename
    basename=`basename "$file"`
    # everything after last '/'
    basename=${file##*/}
    
    # dirname
    dirname=`dirname "$file"`
    # everything before last '/'
    basename=${file%/*}
    Also tried (with and without the quotes on input and file_ext):
    Code:
    input=$1
    file_ext=.${input##*.}
    base=`basename "$input" $file_ext`
    executed: convert.sh /path/music.wav
    But it echoes base as music.wav

    Can anyone help? Thanks
  2. #2
  3. Moderator Emeritus
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2002
    Location
    Austin, TX
    Posts
    7,186
    Rep Power
    2265
    Moved to Linux
    DrGroove, Devshed Moderator | New to Devshed? Read the User Guide | Connect with me on LinkedIn
  4. #3
  5. /(PHP + Mysql Developer)+/
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2007
    Location
    UK
    Posts
    195
    Rep Power
    37
    Thanks, hopefully I'll get a response here.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,449
    Rep Power
    1751
    'Physically' your code works perfectly!

    Logically, however ... you do know that the last line of your code over-writes the basename variable. As a passing point I have never liked using variable names with the same name as *nix commands, I tend to get very confused!
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  8. #5
  9. /(PHP + Mysql Developer)+/
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2007
    Location
    UK
    Posts
    195
    Rep Power
    37
    Is that the first one or the second one?
    Thanks
  10. #6
  11. /(PHP + Mysql Developer)+/
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2007
    Location
    UK
    Posts
    195
    Rep Power
    37
    I thought I had this down but it keeps messing up, is there something wrong with my syntax on the last line?

    I get this error:
    Code:
    : No such file or directorymusic/download/3/14/snuffbox-stick.mp3
    Seems like the first bit got clipped off?

    Here it is:
    Code:
    #!/bin/bash
    file=$1
    base=`basename ${file%.*}`
    dir=`dirname $1`
    echo $file
    echo $base
    echo $dir
    ffmpeg -i ${file} -acodec libfaac -ab 320k ${dir}/${base}.aac
    I tried surrounding the last line in backticks (`) too, but it didn't make any difference.
    Am I supposed to be putting quotes around certain parts of the arguments?

    Thanks
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,449
    Rep Power
    1751
    My usual trick with stuff like this is to echo out the line to see what it looks like. To do it for real you won't want back ticks around the command as you are calling the ffmpeg command to convert your file to a different format.
    Change the last line to:

    Code:
    echo "ffmpeg -i ${file} -acodec libfaac -ab 320k ${dir}/${base}.aac"
    and see what appears.
    As you are creating a .aac output (using the libfaac codec) I am guessing that the file not found relates to your input file. From the directory you are in when you run the script, what does
    Code:
    ls -l music/download/3/14/snuffbox-stick.mp3
    show? Without a leading / the pathname is relative from current working directory, so you may want to give a full, absolute, path.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  14. #8
  15. /(PHP + Mysql Developer)+/
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2007
    Location
    UK
    Posts
    195
    Rep Power
    37
    Hi I tried echoing too, it's get all mismatched somehow and I have no idea how:
    Code:
    /home/theunsig/public_html/music/download/3/14/snuffbox-stick.mp3
    snuffbox-stick
    /home/theunsig/public_html/music/download/3/14
    .aacffbox-stickc -ab 320k /home/theunsig/public_html/music/download/3/14mp3
    The ls command says no such file or directory but that makes sense anyway, music is in another directory.

    The script: /home/unsig/public_html/py/convert.sh
    The file: /home/unsig/public_html/downloads/3/14/snuffbox-stick.mp3

    Any idea why it's garbled?

    Thanks!
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,449
    Rep Power
    1751
    It looks like something is using 'cursor control' and sending the output location back to start of the line - but that is quite possibly a red herring.
    I note you say the files are under /home/unsig but your output suggests it is /home/theunsig (i.e., theunsig not unsig). There's a couple of other pathname issues for the .mp3 file too.
    Comment out the ffmpeg call and just double check that what you enter as a parameter is produces (with the echo commands you have) the right results - if need be put a quick:
    Code:
    if [ ! -f "${file}" ]
    then
      echo "Input file \"${file}\" does not exist"
    fi
    if [ -f "${dir}/${base}.aac" ]
    then
      echo "Output file \"${dir}/${base}.aac\" already exists"
    fi
    to check for the input and output files existing.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  18. #10
  19. /(PHP + Mysql Developer)+/
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2007
    Location
    UK
    Posts
    195
    Rep Power
    37
    What's that cursor control issue?
    You were right about it being theunsig but it didn't make a difference.
    I tried your if block of code and it just says that it doesn't expect 'fi'. I can't see why, looks perfectly valid.
    Does it matter if the lines are indented or not? Or can you only use tabs and not spaces or vice versa?

    Thanks!

    edit:
    A carriage return after the last 'fi' causes unexpected EOF, without it returns the unexpected 'fi' error above.
    Doesn't work with just one of the if block either.
    Last edited by alexgeek; March 9th, 2011 at 12:03 PM.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,449
    Rep Power
    1751
    There are ways you can control the position of the cursor, but I have to admit to not being fully aware of how you do it!
    It is very possible there's a typo in my code as that was typed 'blind' without trying it out first. The errors could imply a mis-matched quote of some sort, so the interpreter hits the fi after having treated the matching if as being part of a string inside quotes.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,449
    Rep Power
    1751
    Just tried it on a Ubuntu netbook and it all works fine ... (with and without those if tests).
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,449
    Rep Power
    1751
    The script I have tried out is:
    Code:
    #!/bin/bash
    # Convert to 
    set -x
    file=$1
    base=`basename ${file%.*}`
    dir=`dirname $1`
    echo $file
    echo $base
    echo $dir
    if [ ! -f "${file}" ]
    then
      echo "Input file \"${file}\" does not exist"
    fi
    if [ -f "${dir}/${base}.aac" ]
    then
      echo "Output file \"${dir}/${base}.aac\" slready exists"
    fi
    echo ffmpeg -i ${file} -acodec libfaac -ab 320k ${dir}/${base}.aac
    set +x
    The set -x and set +x are for debug purposes, to see the results of operations.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  26. #14
  27. /(PHP + Mysql Developer)+/
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2007
    Location
    UK
    Posts
    195
    Rep Power
    37
    Using yours:
    Code:
    : invalid option 3: set: -
    set: usage: set [--abefhkmnptuvxBCHP] [-o option] [arg ...]
    /home/theunsig/public_html/music/download/3/14/snuffbox-stick.mp3
    snuffbox-stick
    /home/theunsig/public_html/music/download/3/14
    convert.sh: line 20: syntax error: unexpected end of file
    Somethings wrong here, there isn't even a 20th line.
    I'll contact the host support and see if they know I guess.
    Thanks for helping though
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,449
    Rep Power
    1751
    It almost smacks of your script being wrapped up in something else
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo