views:

1014

answers:

3

I have an automator script that I'd like to run on a folder. I want the script to take each file in the folder and run my shell command on it. Automator is set to pass input to stdin, but I don't think I'm using stdin correctly below, can you help please?

for f in "$@" 
do
    java -Xmx1000m -jar /Users/myprog/myprog.jar $f 
done
A: 

Sorry, I just realized it worked as pass input as argument instead of as stdin, I'm not sure why though? If you can explain that would be great. Thanks.

John
Instead of posting this follow-up as an answer, you should have added it to your question by editing it or posted a comment on your question.
Dennis Williamson
+1  A: 

The special variable $@ represents all the command-line arguments provided to the script.

The for loop steps through each one and executes the jar file each time. You could shorten the script to:

for f
do
    java -Xmx1000m -jar /Users/myprog/myprog.jar "$f"
done

since the default behavior of for is to use $@.

You should put quotes around $f at the end of the java command in case there are spaces in the arguments.

Dennis Williamson
+1  A: 

I'm not sure I understand your question since your script does not appear to be using stdin. The $@ special parameter expands to the positional parameters passed as arguments in argv. It works the same way as if you called the script directly from the shell this way:

$ cat >test
for f in "$@"
do
  echo $f
done
$ chmod 755 test
$ ./test a b c
a
b
c

If you wanted to get the arguments from stdin, you could do something like this:

$ cat >test2
for f in $(cat)
do
  echo $f
done
$ chmod 755 test2
$ ./test2 <<EOF
> a b c
> EOF
a
b
c
Ned Deily