findutils-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Findutils-patches] xargs problem


From: Dale R. Worley
Subject: Re: [Findutils-patches] xargs problem
Date: Tue, 20 Dec 2016 09:20:32 -0500

aws backup <address@hidden> writes:
[adding a line break and indentation:]
> fswatch -0 -Ie ".*\.*$" -i ".*\.mp4$" /path/to/folder |
>    xargs -0 -n 1 -I {} /path/to/shellscript.sh
>
> shellscript.sh:
>
> s3cmd put {} s3://bucket/ <s3://bucket/>
>
> I get following failure message:
>
> ERROR: Parameter problem: Nothing to upload.
>
> So I get stuck already on the first point. 
> Sorry I am a beginner in shell scripting.
> How do I get it into the script?

When you run a shell script, the arguments that follow the script name
are assigned to the variables $1, $2, etc. within the script.  So you
want to say:

 fswatch -0 -Ie ".*\.*$" -i ".*\.mp4$" /path/to/folder |
    xargs -0 -n 1 -I {} /path/to/shellscript.sh {}

(IIRC, you can exploit the defaults by saying
"xargs -0 -n 1 /path/to/shellscript.sh".)

And have shellscript.sh say:

 s3cmd put "$1" s3://bucket/ <s3://bucket/>

Then xargs runs commands like:

  /path/to/shellscript.sh /.../name-of-some-file

and when shellscript.sh runs, $1 gets replaced with
/.../name-of-some-file, effectively:

 s3cmd put /.../name-of-some-file s3://bucket/ <s3://bucket/>

There are a lot of rules regarding exactly how quoting works within
shell scripts, but in this case the essential rules are:

- xargs does not invoke the shell to assemble and run the commands it
  runs.  Hence, '/path/to/shellscript.sh {}' gets turned into the kernel
  call to run the program that you'd expect, the first item being
  /path/to/shellscript.sh and the second item being the filename that
  xargs dissects out -- regardless of any special characters that appear
  in the file name

- The shell picks up the second argument to the kernel call and assigns
  it to the variable $1 without any further processing.

- When the value of $1 is substituted into the shell line

 s3cmd put "$1" s3://bucket/ <s3://bucket/>

the presence of "..." around $1 causes the value of $1 to be used as the
first argument in the kernel call to run s3cmd, regardless of any
special characters that appear in the value of $1.  Without "...", if
the value of $1 contained whitespace characters, the value would be
divided into multiple arguments to the kernel call.  (And possibly other
transformations, I'd have to read the manual page carefully to get it
right.)

Dale



reply via email to

[Prev in Thread] Current Thread [Next in Thread]