Re: Adding to the command line via variables

From: Chris F.A. Johnson (cfajohnson_at_gmail.com)
Date: 07/31/05


Date: Sat, 30 Jul 2005 19:06:53 -0400

On 2005-07-30, Jeff Kloek wrote:
> Hello. I'm trying to figure out how I can build a command line using the
> output of the ls |grep command. The problem is the number of files returned
> will vary.
> As an example, one time might find output1, output2 and output3, and another
> time it might also include output4 and output5.
>
> How can I use the output of "ls |grep output" to build a command line that
> does the following:
> lp -d printername outputX outputX outputX ?
> I don't expect to ever exceed the allowable count of command line
> parameters.
> Any ideas would be greatly appreciated!!

   Unless you need a regular expression to select the files, there is
   no need for ls or grep. To print all the files beginning with
   "output", just use:

lp -d printername output*

   If you do need to use a regular expression, there's still no need
   for ls:

lp -d $(printf "%s\n" * | grep REGEX)

   Using anything other than wildcard expansion will present problems
   if the filenames are badly formed (e.g., if they contain spaces).

-- 
    Chris F.A. Johnson                     <http://cfaj.freeshell.org>
    ==================================================================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
    <http://www.torfree.net/~chris/books/cfaj/ssr.html>


Relevant Pages

  • Re: MacTeX 2007 and documentation
    ... may be the most useful command available when doing searches at the ... will find every file on your system that has the text "memoir" on it, ... If locate generates 30 lines of text, where each line is a file, grep ... I need a Unix guru to chat to for half an hour or so to put me straight. ...
    (comp.text.tex)
  • Re: piping multiple selections to mv or cp and exclusion
    ... pipe to shell to execute each command statement ... Unix is indeed lifelong learning :-) ... and the regexp. ... back into the grep statement. ...
    (comp.unix.shell)
  • Re: MacTeX 2007 and documentation
    ... means you need expertise in your host operating system as well. ... locate memoir | grep pdf ... I'm not someone who lives at the command line, ...
    (comp.text.tex)
  • Re: Program to scan directories
    ... Is there a UNIX command which scans directories and execute any other ... the regex you specify here is /not/ given to grep to ... entries (files and subdirectories). ... tell grep to recurse through the kpathsea directory (the *.h regex ...
    (comp.unix.programmer)
  • Re: Using the grep command to filter
    ... I am a college student currently enrolled in a Unix class. ... that ubuntu Ubuntu UBUNTU ubunTU whatever in them. ... grep ubuntu /etc/* of course. ... Now look at the output of that grep command will give you all the ...
    (comp.unix.shell)