The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Operating Systems
> Linux Help
|
Get Basename without extension not working
Discuss Get Basename without extension not working in the Linux Help forum on Dev Shed. Get Basename without extension not working Linux Help forum discussing topics including usage, troubleshooting, modules, and distributions. Linux is an open source OS, based on UNIX.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 4th, 2011, 05:52 AM
|
 |
/(PHP + Mysql Developer)+/
|
|
Join Date: Nov 2007
Location: UK
|
|
|
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
|

March 5th, 2011, 01:36 PM
|
 |
Moderator Emeritus
|
|
Join Date: Feb 2002
Location: Scottsdale, AZ
|
|
|
Moved to Linux
|

March 5th, 2011, 01:46 PM
|
 |
/(PHP + Mysql Developer)+/
|
|
Join Date: Nov 2007
Location: UK
|
|
|
Thanks, hopefully I'll get a response here.
|

March 6th, 2011, 07:21 AM
|
|
|
'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
|

March 6th, 2011, 02:45 PM
|
 |
/(PHP + Mysql Developer)+/
|
|
Join Date: Nov 2007
Location: UK
|
|
Is that the first one or the second one?
Thanks 
|

March 8th, 2011, 12:34 PM
|
 |
/(PHP + Mysql Developer)+/
|
|
Join Date: Nov 2007
Location: UK
|
|
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
|

March 8th, 2011, 01:16 PM
|
|
|
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.
|

March 8th, 2011, 01:24 PM
|
 |
/(PHP + Mysql Developer)+/
|
|
Join Date: Nov 2007
Location: UK
|
|
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!
|

March 9th, 2011, 01:34 AM
|
|
|
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.
|

March 9th, 2011, 11:59 AM
|
 |
/(PHP + Mysql Developer)+/
|
|
Join Date: Nov 2007
Location: UK
|
|
|
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.
|

March 9th, 2011, 05:55 PM
|
|
|
|
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.
|

March 9th, 2011, 06:12 PM
|
|
|
|
Just tried it on a Ubuntu netbook and it all works fine ... (with and without those if tests).
|

March 10th, 2011, 06:41 AM
|
|
|
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.
|

March 10th, 2011, 01:53 PM
|
 |
/(PHP + Mysql Developer)+/
|
|
Join Date: Nov 2007
Location: UK
|
|
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 
|

March 11th, 2011, 03:17 PM
|
|
|
|
It almost smacks of your script being wrapped up in something else
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|