LinuxQuestions.org
Visit Jeremy's Blog.
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 09-28-2012, 08:16 PM   #1
minty33
Member
 
Registered: Aug 2012
Location: earth
Distribution: Mint Xfce, Korora Gnome3, Ubuntu Server NoGui,
Posts: 136

Rep: Reputation: 1
simple script won't work


i am making a little script to find all mp3 file from any external drive with a music directory print them to a file the use that file to paly with mpg123.
the mpg123 part not the issue so i'll leave that out. what i can't see is why the followingdoesn't work.
-----------------------------------------------------------------------------------------------------------------------

find /media/*/music/* | grep *mp3 > playlist

#the find command gives the desired output but the pipe to grep gives no result even if to stdout not file.

-----------------------------------------------------------------------------------------------------------------------
i know this is so easy but from what iv'e read and know this should work so can somebody point out what must be my stupid mistake here.

Last edited by minty33; 09-28-2012 at 08:27 PM.
 
Old 09-28-2012, 09:11 PM   #2
lexwoodrum
LQ Newbie
 
Registered: Nov 2006
Location: Poughkeepsie NY USA
Distribution: centos 6.5, ubuntu 14.04.
Posts: 15

Rep: Reputation: 0
I don't think grep likes the asterisk. Try grep mp3 without it.
 
Old 09-28-2012, 09:15 PM   #3
minty33
Member
 
Registered: Aug 2012
Location: earth
Distribution: Mint Xfce, Korora Gnome3, Ubuntu Server NoGui,
Posts: 136

Original Poster
Rep: Reputation: 1
thank you lexwoodrum

I knew it had to be something obvious I wasn't doing right. I forgot grep doesn't need wildcards like find. It works now, and thanx again.
 
Old 09-30-2012, 10:02 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Not only does grep use regular expressions, rather than globbing, but you also forgot that the shell does perform globbing on all unquoted expressions. grep was being executed with a list of all mp3s in your current directory as arguments.

Get into the habit of always quoting your expressions in grep/sed/awk/whatever. Single quotes are generally best, unless you're using variables.

Finally, since you're using find anyway, why not just let it do all the work?
Code:
find /media/*/music/* -type f -name '*.mp3' -print > playlist
The -name options use globbing patterns. And again, you have to quote them.

Here are a couple of good links about using find:
http://mywiki.wooledge.org/UsingFind
http://www.grymoire.com/Unix/Find.html


(PS: As I asked in your other thread, please don't use unbroken lines of characters outside of code tags.)

Last edited by David the H.; 09-30-2012 at 10:09 AM. Reason: fixed link
 
1 members found this post helpful.
Old 09-30-2012, 11:01 AM   #5
minty33
Member
 
Registered: Aug 2012
Location: earth
Distribution: Mint Xfce, Korora Gnome3, Ubuntu Server NoGui,
Posts: 136

Original Poster
Rep: Reputation: 1
thanx for advice

I did read several examples and some used quotes and some didn't i couldn't see any distinction. Thank you for that info and now I know the best habit to have regarding quotes. As for using find for everything I figured I could do that actually,
but we had to write five simple scripts for Unix/Linux class and utilize things we covered like pipes so I did it that way just to demonstrate a pipe.
 
Old 09-30-2012, 01:13 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quoting serves a couple of related purposes.

First, it's used to determine the initial line "tokens" (a.k.a. "words"). The first thing the shell does when it parses the line is break it up into individual command arguments. The first one becomes the command name, and all others are options, filenames, etc. Quotes and backslash escapes tell it that certain spaces aren't to be treated as token delimiters, allowing strings with whitespace in them to be grouped as single elements.

The second purpose is to protect shell-reserved characters in subsequent parsing steps. Anything that the shell would normally treat as special is ignored if it was initially escaped (and non-special characters don't change). This is where the difference between single and double quotes becomes important. A single quote will escape everything following it, up to the next single quote, but double-quotes allow a few characters to retain their special meanings, particularly "$". This allows parameter expansions and substitutions to continue to occur, while still protecting everything else.

They also prevent these substitutions from being subsequently word-split and glob expanded. Yes, unquoted tokens can be sub-divided into more tokens in these steps, but the output of any parameter that was originally quoted will remain as a single token.

It's often useful to think of quotemarks as toggle switches, rather than containers. The first instance on the line turns off shell parsing, and the next one turns it on again, in a linear fashion. (Exceptions occur in certain nested constructs such as command substitutions, which are treated as separate quoting environments.)

If you see someone using an unquoted expression in a grep command, say, then it's likely because he knows that the string contains nothing that needs to be protected. Simple one-word searches are usually safe, for example. But most regex and globbing characters are treated specially by both the shell and many other programs, so you need to be careful to pass them to the correct one.

This page lists pretty much everything that might need to be escaped.
http://www.tldp.org/LDP/abs/html/special-chars.html

In any case, it doesn't hurt to quote, even when unnecessary. So if in doubt, quote 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
How does this simple awk script work? ziggy25 Linux - Newbie 1 12-11-2009 07:14 AM
Very simple script does not work....? M$ISBS Programming 7 01-28-2008 04:18 PM
why this simple cgi script doesnot work ? . adam_blackice Programming 4 04-04-2007 11:50 AM
Simple script won't work compused Linux - Newbie 4 11-22-2006 07:52 AM
Script in /etc/profile.d/ won't won't work!? BlueSpirit Slackware 1 10-15-2006 12:21 PM


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

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