|
Home > Archive > Unix Shell > January 2007 > how do I pass on options as-is within a script?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
how do I pass on options as-is within a script?
|
|
| RolandRB 2007-01-12, 7:23 am |
| I want to write a bash script named "cpv" (copy with checksum
verification) that calls "cp" and passes on all the options that "cpv"
was invoked with through to "cp" without any change. Can you tell me
the best way to do this?
| |
| Stephane CHAZELAS 2007-01-12, 7:23 am |
| 2007-01-12, 04:08(-08), RolandRB:
> I want to write a bash script named "cpv" (copy with checksum
> verification) that calls "cp" and passes on all the options that "cpv"
> was invoked with through to "cp" without any change. Can you tell me
> the best way to do this?
You'd need to parse the options the same was as cp does.
Using ltrace or truss -u type of commands may help you find out
which call to getopt* type of function is made.
See the getopts shell builtin command or
http://stchaz.free.fr/getopts_long.sh to replicate the same.
For GNU tools, be sure to do a:
export POSIXLY_CORRECT=1
to make sure a correct option parsing is performed.
Then, you will be able to know where the options end and when
the args start.
This will not be the end of your journey. You'll then have to
find out which files where copied etc.
Also, that checksum verification doesn't make sense. cp will
make sure everything was transfered OK.
If a bit on disk gets corrupted for some reason, the next time
you read the file (to compute a checksum), you will probably not
notice it as you'll get the cached version rather than reading
it from disk.
--
Stéphane
| |
| Stephan Grein 2007-01-12, 7:23 am |
| cat cpv.sh
#!/bin/bash
set -x
echo "$*"
Is this appropriate?
--
Stephan Grein, <stephan at stephan minus rockt dot de>
https://stephan-rockt.de
GnuPG-Key-ID: 0xF8C275D4
FingerPrint: 5B6F 134A 189B A24D 342B 0961 8D4B 0230 F8C2 75D4
| |
| Ron Hardin 2007-01-12, 7:23 am |
| RolandRB wrote:
>
> I want to write a bash script named "cpv" (copy with checksum
> verification) that calls "cp" and passes on all the options that "cpv"
> was invoked with through to "cp" without any change. Can you tell me
> the best way to do this?
cp "$@"
--
Ron Hardin
rhhardin@mindspring.com
On the internet, nobody knows you're a jerk.
| |
| RolandRB 2007-01-12, 1:16 pm |
|
Ron Hardin wrote:
> RolandRB wrote:
>
> cp "$@"
Thanks for the responses. The above won't work for what I am trying to
do because I want to do single copies using "cp", given multiple input
files destines for a directory, and after each one do a checksum so I
need to pass all the options to the "cp" command each time. I have to
allow for multiple copies of files to a directory and indeed it will
nearly always be used this way, as well as a single copy.
| |
| Janis Papanagnou 2007-01-12, 7:20 pm |
| RolandRB wrote:
> Ron Hardin wrote:
>
> Thanks for the responses. The above won't work for what I am trying to
> do because I want to do single copies using "cp", given multiple input
> files destines for a directory, and after each one do a checksum so I
> need to pass all the options to the "cp" command each time. I have to
> allow for multiple copies of files to a directory and indeed it will
> nearly always be used this way, as well as a single copy.
As far as I understand, you don't know how many options your command
will get and you also don't know how many files. In this case you have
to parse the options, e.g. by using getopts as Stephane suggested; in
this case you have to provide the specification of all possible option
names for your original cp command. As far as I can see there's also
the possibility to just skip all arguments until you find an argument
that has no dash, because the cp commands will likely not have options
with option arguments (but you have to verify that for your cp command).
So what you could do is
c=0
for arg
do
case $arg in
-*) opts="$opts $arg"; c=$(($c + 1));;
*) break;;
esac
done
shift $c
echo "options: $opts"
echo "files: $@"
Janis
| |
| RolandRB 2007-01-20, 7:23 am |
|
Janis Papanagnou wrote:
> RolandRB wrote:
>
> As far as I understand, you don't know how many options your command
> will get and you also don't know how many files. In this case you have
> to parse the options, e.g. by using getopts as Stephane suggested; in
> this case you have to provide the specification of all possible option
> names for your original cp command. As far as I can see there's also
> the possibility to just skip all arguments until you find an argument
> that has no dash, because the cp commands will likely not have options
> with option arguments (but you have to verify that for your cp command).
> So what you could do is
>
> c=0
> for arg
> do
> case $arg in
> -*) opts="$opts $arg"; c=$(($c + 1));;
> *) break;;
> esac
> done
> shift $c
> echo "options: $opts"
> echo "files: $@"
>
>
> Janis
Thanks for that. I guess it could be improved by making it break if it
encounters "--" after adding it to "opts".
In the end I had to give up on this as the checksum will likely have
picked up the file in the disk cache rather than on disk. I
specifically wanted it to read the file off the disk so this checksum
process (now I am using "cmp") will have to wait until all the copies
have finished processing, so hopefully the first file is flushed out of
the cache.
|
|
|
|
|