LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-08-2010, 07:52 PM   #1
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Rep: Reputation: 16
script filter: pls help me understand: do $(flac -cd "$file" | lame -h - ...


on this page is a great quick and dirty trick:

http://www.linuxtutorialblog.com/pos...ng-flac-to-mp3

The point of my post here is to better understand this "script"

Code:
for file in *.flac; 
do 
 $(flac -cd "$file" | lame -h - "${file%.flac}.mp3"); 
done
I understand this is saying for each *.flac file do the following to the file: decode and write output to stdout, pipe that to lame and do a substitution (%.flac), removing the .flac extension. What I don't understand is why that line is in $() --not even sure how to say that in english. I've tried placing different commands, such as $(echo $file) but that results in various errors.

Can you tell me what's up with this $()

Thank you.
 
Old 11-08-2010, 08:11 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
What you describe here is called "command substitution", and you can read about it here (and other fine locations):
http://tldp.org/LDP/abs/html/commandsub.html

However, in the code you show, if I had written that, I would not have put the command pipeline inside $() unless I were also going to fork the $() into the background, and in which case I would have used () to create a subshell instead of $(). The result of this would be that my parent script would perhaps terminate quicker, while allowing the individual conversion commands, each in their own subshell in the background, to run their course (maybe there are LOADS of files co convert, so convert them in the background and move on in the foreground).

I am not sure why the author of that code chose to put it inside $() but maybe a more experienced scripter around here will have some possible explanation.
 
Old 11-08-2010, 08:29 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Quote:
I am not sure why the author of that code chose to put it inside $() but maybe a more experienced scripter around here will have some possible explanation.
I wouldn't say I'm a more experienced scripter, but I'll take a stab at it.

It could be a very rudimentary/poor success-check. The entire line will be replaced with the stdout/stderr of the lame process. I don't use lame much, but I would bet it displays nothing when successful. So if it does output something it's probably because things went wrong. So when the script replaces the pipe-sequence with the output, it's unlikely that the output will be a recognizable command for the shell to execute. So it would generate an error. For instance, a script named simple.bash:
Code:
#!/bin/bash

for attempt in $( seq 1 3 ) ; do
  $( echo "bad command");
done

exit 0
Running that script gives:
Code:
$ ./simple.bash 
./simple.bash: line 4: bad: command not found
./simple.bash: line 4: bad: command not found
./simple.bash: line 4: bad: command not found
Not a very informative error.

If the lame command was successful, the output will be an empty string, making the original script execute an empty command, and continue with the loop.

All things being equal, I don't imagine the original author was trying to do anything tricky or fancy. I think the author just wasn't a bash expert and thought using $() was the only way to execute a command.

Last edited by Dark_Helmet; 11-08-2010 at 08:32 PM.
 
Old 11-09-2010, 04:14 AM   #4
LinuxTutBlog
LQ Newbie
 
Registered: Mar 2010
Location: On the web
Posts: 6

Rep: Reputation: 0
Ugh, would you believe that I actually never spotted this scripting mistake on my blog until I read this thread? I don't really know why I put it in there 3.5 years ago, but I'll just call it a "youthful indiscretion". Has been fixed .
 
Old 11-09-2010, 12:56 PM   #5
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Original Poster
Rep: Reputation: 16
thanks a bunch to everyone who responded here -- I understand this better now -- appreciate it!
 
  


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
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 02:03 AM
Error: "cannot set groups" by using "su -", pls help nelsonyuen Linux - General 14 07-31-2010 01:24 PM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 10:00 AM
"Changes&Hints" file hard to understand... jrch Slackware 2 02-14-2007 05:53 PM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 03:32 PM


All times are GMT -5. The time now is 08:38 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration