LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-17-2011, 11:31 AM   #1
dugan
Senior Member
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 4,766

Rep: Reputation: 1467Reputation: 1467Reputation: 1467Reputation: 1467Reputation: 1467Reputation: 1467Reputation: 1467Reputation: 1467Reputation: 1467Reputation: 1467
basename with command substitions and xargs


I get this behavior on Slackware 13.37, which includes BASH 4.1.010. Yes, BASH is my shell.

I have a file called a.flac and I'm in the directory that contains it.

The output of the ls command is expected:

Code:
ls *.flac
gives:

Code:
a.flac
Removing the extension with basename works as expected:

Code:
basename a.flac .flac
gives:

Code:
a
Putting the above command in a variable substitution works as expected:

Code:
echo `basename a.flac .flac`
gives:

Code:
a
Using xargs with ls and a variable substitution works as expected:

Code:
ls *.flac | xargs -i echo `echo {}`
gives:

Code:
a.flac
However, when I try to add the basename command to the above command, it stops working.

Code:
ls *.flac | xargs -i echo `basename {} .flac`
gives:

Code:
a.flac
Whereas the result I expect is:

Code:
a
Why is it not working, and how do I make it work?

Last edited by dugan; 06-17-2011 at 11:33 AM.
 
Old 06-17-2011, 11:43 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
The command substitution is performed before the xargs command and the basename command is not aware of what {} represents. Actually you don't need the command substitution in this case: you can just use the basename command as argument to xargs:
Code:
ls *.flac | xargs -I"{}" basename {} .flac
Also note that the -I option requires an argument and the -i options is deprecated.

Moreover, it's useful in these cases to use the debugging option of bash. Just put the command line into a script and execute with the -x option:
Code:
$ cat > test.sh
ls *.flac | xargs -I"{}" echo $(basename {} .flac)
$ bash -x test.sh
+ ls a.flac
++ basename '{}' .flac
+ xargs '-I{}' echo '{}'
a.flac
In this way you can see that the basename command interprets the {} string as literal and results in a literal {}, that is no change made. Then the echo command just prints out the passed argument.

Last edited by colucix; 06-17-2011 at 11:47 AM.
 
Old 06-17-2011, 01:23 PM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Also, as long as you're using bash, then it's really a good idea to use $(command) instead of `command`.

It nests easily and can't be confused with commas.
 
Old 06-21-2011, 09:58 AM   #4
tange
LQ Newbie
 
Registered: Jul 2010
Posts: 13

Rep: Reputation: 9
If you have GNU Parallel installed:
parallel echo {.} ::: *.flac
or:
ls *.flac | parallel echo {.}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
“basename $0” returns basename: -k: unknown option jaideepca Linux - Newbie 6 11-09-2009 05:30 AM
Bash script - how to get 'basename' command to do what I want..? craftybytes Linux - Software 8 02-14-2008 03:07 AM
How to get the 'basename' (or what other.?) command to do what I want..? craftybytes Programming 1 02-13-2008 06:27 AM


All times are GMT -5. The time now is 08:00 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration