• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Pipes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Pipes


  • Subject: Re: Pipes
  • From: Graff <email@hidden>
  • Date: Sun, 11 Jan 2004 15:32:25 -0500

Those look fine to me but I would remove the quotes and the curly braces, they are not needed. For example, instead of:
ls -la | grep -i "${1}"
use
ls -la | grep -i $1

- Ken

On Jan 11, 2004, at 2:27 PM, Marc K. Myers wrote:

Thanks for the tip on how to debug shell scripts using the sh shell and the "-x" option. That's what I was looking for.

The scripts I'm using look like this:

____________________________
#!/bin/sh

###############################################
## Find a string in "ls" output
###############################################

if [ $# -ne 1 ] ; then
echo "This script requires one argument"
else
ls -la | grep -i "${1}"
fi
____________________________

and

____________________________
#!/bin/sh

###############################################
## Find a string in "ps" output
###############################################

if [ $# -ne 1 ] ; then
echo "This script requires one argument"
else
ps -acx | grep -i "${1}"
fi
____________________________

My original question was why the first one required the curly brackets around the "1" to work and the second one didn't. Now I can't reproduce the situation. It works with or without the curly brackets. There must have been something different about the way the shell was configured when it wouldn't work without the curly brackets, but I have no idea what that might be.

Marc [01/11/04 2:25:58 PM]

From: Graff <email@hidden>
Date: Sun Jan 11, 2004 3:02:48 AM America/Detroit
To: "Marc K. Myers" <email@hidden>
Cc: email@hidden
Subject: Re: Pipes

I still don't see exactly what you are trying to do, are you calling a script named cprc which looks for a process? Something like this:
cprc processname

If so then you would probably want a script similar to this:

##########
#!/bin/sh

# make sure there are arguments
# $# represents the number of arguments
if [ $# -ge 1 ]; then
ps -acx | grep -i $1
fi
##########



For cfle this should work:

##########
#!/bin/sh

# make sure there are arguments
# $# represents the number of arguments
if [ $# -ge 1 ]; then
ls -la | grep -i $1
fi
##########


If you want to watch a shell script run (to debug it) when you working with it in the terminal then run it like this:
/bin/sh -x cprc 'mail'

Remember that for ls you need to be in the directory where the file is located when you run the command. If you want to do a more global find then you should use the find or locate commands. So if you want a listing of everything named txt on the desktop you should do something like this:
cd ~/Desktop;/bin/sh -x cfle 'txt'

- Ken

On Jan 11, 2004, at 1:39 AM, Marc K. Myers wrote:

These are being used in shell scripts that I use to find matching processes in "ps" listings and matching files in "ls" listings. The first is "cprc" ("see processes") and the second is "cfle" ("see files"). Each is invoked with a single argument which is the string to be matched in the listing.

There's probably a way to debug what's going on inside a shell script but I haven't figured it out yet.

Marc [01/11/04 1:24:25 AM]

Date: Sun Jan 11, 2004 12:56:05 AM America/Detroit
To: "Marc K. Myers" <email@hidden>
Cc: email@hidden
Subject: Re: Pipes

First of all, "$1" resolves to a variable named 1. Doing [2] gives me a listing of everything in the working directory, to find out why I checked what $1 was set to by doing:
echo "$1"

It turned out that the variable $1 was set to a linefeed and was thus matching every line from the ls command.

If you want to search for a literal $1 you should do this:
ls -la | grep -i '\$1'

You need the \ before the $ because a $ is a metacharacter that represents the end of the line in a regular expression and so the \ escapes that metacharacter and makes it into a normal dollar sign.

So basically things are most likely acting screwy because you are running into some murky waters where things are a bit undefined. What is it that you are trying to grep here? If you explain what pattern you are trying to match then we can see what is best to use.

- Ken

On Jan 10, 2004, at 11:44 PM, Marc K. Myers wrote:

Could someone tell me why this works:

[1] ps -acx | grep -i "$1"

and this doesn't?

[2] ls -la | grep -i "$1"

This *will* work:

[3] ls -la | grep -i "${1}"

and this will work

[4] ps -acx | grep -i "${1}"

but I can't figure out why [2] won't.

Marc [01/10/04 11:43:56 PM]
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Pipes
      • From: Christopher Nebel <email@hidden>
References: 
 >Re: Pipes (From: "Marc K. Myers" <email@hidden>)

  • Prev by Date: Re: Elementary fie naming question
  • Next by Date: Re: Unvirtual ambient photon quotient
  • Previous by thread: Re: Pipes
  • Next by thread: Re: Pipes
  • Index(es):
    • Date
    • Thread