 |
|
 |
|
|
 |
Question - reading command line parms inside a script. |
 |
 |
|
|
02-27-04 04:34 PM
I've been going through the FAQs and so far haven't found an answer for this
problem.
I'm trying to read a set of command line parms. The shells I have available
to me are SH, CSH, and KSH. The working environment is CSH. The command
line in question is:
reshape 90-94 -f=" Skip " 95-99 -f=" " 100-102 -f=" >>" 11-11 -f="< " 19-58
What I need to get from this is each of the parms exactly as written, i.e.,
90-94
-f=" Skip "
95-99
-f=" "
100-102
-f=" >>"
11-11
-f="< "
19-58
So far every attempt to check the $1, $2, $3... parms returns correct values
for the numeric ranges, but, of course, the quote characters have been
purged. I need a way for my script to get them exactly as written.
test script - reshape:
#! /bin/csh
echo "$*"
echo " "
echo "$1"
echo "$2"
echo "$3"
echo "$4"
echo " "
foreach Entry ( $* )
echo $Entry
end
Thanks for any suggestions you might have.
Ken Andrews
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Question - reading command line parms inside a script. |
 |
 |
|
|
02-27-04 04:34 PM
In article <tBJ%b.90650$Hy3.38640@edtnps89>,
"Ken Andrews" <gobble@degook.com> wrote:
> So far every attempt to check the $1, $2, $3... parms returns correct valu
es
> for the numeric ranges, but, of course, the quote characters have been
> purged. I need a way for my script to get them exactly as written.
You can't. Quotes are processed by the shell, but they're transparent
to the application. There's no way to tell which of the following the
user entered:
"foo bar"
'foo bar'
foo\ bar
foo" "bar
"f""o""o"' bar'
All the program ever sees is the resulting token.
Why do you think you need this?
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Question - reading command line parms inside a script. |
 |
 |
|
|
02-27-04 07:34 PM
2004-02-27, 15:44(+00), Ken Andrews:
[...]
> reshape 90-94 -f=" Skip " 95-99 -f=" " 100-102 -f=" >>" 11-11 -f="< " 19-5
8
>
> What I need to get from this is each of the parms exactly as written, i.e.,[/color
]
[...]
> So far every attempt to check the $1, $2, $3... parms returns correct valu
es
> for the numeric ranges, but, of course, the quote characters have been
> purged. I need a way for my script to get them exactly as written.
[...]
You could read the shell history file and do your parsing of the
last shell command line.
But why would you need to do such a thing?
--
Stéphane ["Stephane.Chazelas" at "free.fr"]
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Question - reading command line parms inside a script. |
 |
 |
|
|
02-27-04 10:34 PM
In article <slrnc3v489.43.stephane.chazelas@spam.is.invalid>,
Stephane CHAZELAS <this.address@is.invalid> wrote:
> 2004-02-27, 15:44(+00), Ken Andrews:
> [...]
> [...]
> [...]
>
> You could read the shell history file and do your parsing of the
> last shell command line.
That depends on the user using a particular shell and having history
enabled. Some shells that have history don't write it to a file
continuously, only when you run a command that saves it.
And of course, what does the OP expect to happen if his program wasn't
invoked by a shell at all. It could have been run by another program
using exec().
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Question - reading command line parms inside a script. |
 |
 |
|
|
03-01-04 05:34 PM
"Barry Margolin" <barmar@alum.mit.edu> wrote
> "Ken Andrews" <gobble@degook.com> wrote:
>
values
>
> You can't. Quotes are processed by the shell, but they're transparent
> to the application. There's no way to tell which of the following the
> user entered:
>
> "foo bar"
> 'foo bar'
> foo\ bar
> foo" "bar
> "f""o""o"' bar'
>
> All the program ever sees is the resulting token.
>
> Why do you think you need this?
We're in the process of switching languages (and associated support
programs). We need to build wrapper scripts to take in a set of parms (as
per the example I gave) and either pass them on to the original support
program or restructure the parms and pass them on to the new support
program.
Something along the lines of:
#! /bin/sh
if [ "$1" = "alpha" ] ; then
shift
reformat $*
else
(do some fiddlywork to convert the parms)
newform $a $b $c $d $e...
fi
The original example I gave was an actual example of command-line parms for
the reformat support program, indicating certain columns of data to extract
and certain string constants to install.
Ken Andrews
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Question - reading command line parms inside a script. |
 |
 |
|
|
03-01-04 09:36 PM
On Mon, 01 Mar 2004 at 16:39 GMT, Ken Andrews wrote:
> "Barry Margolin" <barmar@alum.mit.edu> wrote
> values
>
> We're in the process of switching languages (and associated support
> programs). We need to build wrapper scripts to take in a set of parms (as
> per the example I gave) and either pass them on to the original support
> program or restructure the parms and pass them on to the new support
> program.
>
> Something along the lines of:
>
> #! /bin/sh
> if [ "$1" = "alpha" ] ; then
> shift
> reformat $*
To preserve the original arguments use:
reformat "$@"
> else
> (do some fiddlywork to convert the parms)
> newform $a $b $c $d $e...
newform "$a" "$b" "$c" "$d" "$e" ....
> fi
>
> The original example I gave was an actual example of command-line parms fo
r
> the reformat support program, indicating certain columns of data to extrac
t
> and certain string constants to install.
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Question - reading command line parms inside a script. |
 |
 |
|
|
03-10-04 07:34 PM
Chris F.A. Johnson wrote:
>On Mon, 01 Mar 2004 at 16:39 GMT, Ken Andrews wrote:
>
> To preserve the original arguments use:
>
>reformat "$@"
>
>
>newform "$a" "$b" "$c" "$d" "$e" ....
Thank you for the suggestions.
Script y.y:
#! /bin/sh
echo "* gives --> $*"
echo "@ gives --> $@"
a="$1"
b="$2"
c="$3"
d="$4"
echo " -->" "$a" "$b" "$c" "$d"
Command line:
y.y a b "C" -f="d"
Output:
* gives --> a b C -f=d
@ gives --> a b C -f=d
--> a b C -f=d
Apparently I'm doing something wrong, as I see no difference between any of
the 3 outputs, and all are wrong. This has been tested both running in an
SH and a CSH environment.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Question - reading command line parms inside a script. |
 |
 |
|
|
03-10-04 07:34 PM
On Wed, 10 Mar 2004 at 18:46 GMT, Ken Andrews wrote:
> Chris F.A. Johnson wrote:
>
> Thank you for the suggestions.
>
> Script y.y:
>
> #! /bin/sh
> echo "* gives --> $*"
> echo "@ gives --> $@"
> a="$1"
> b="$2"
> c="$3"
> d="$4"
> echo " -->" "$a" "$b" "$c" "$d"
>
> Command line:
>
> y.y a b "C" -f="d"
>
> Output:
>
> * gives --> a b C -f=d
> @ gives --> a b C -f=d
> --> a b C -f=d
>
> Apparently I'm doing something wrong, as I see no difference between any o
f
> the 3 outputs, and all are wrong. This has been tested both running in an
> SH and a CSH environment.
Put this in your script:
echo "Using \$*:"
for x in $*
do
echo "$x"
done
echo
echo "Using \"\$@\":"
for x in "$@"
do
echo "$x"
done
And call it with:
y.y a "b c" d "e f" g
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Question - reading command line parms inside a script. |
 |
 |
|
|
03-10-04 07:34 PM
"Chris F.A. Johnson" <c.fa.johnson@rogers.com> wrote
> Put this in your script:
>
> echo "Using \$*:"
> for x in $*
> do
> echo "$x"
> done
> echo
> echo "Using \"\$@\":"
> for x in "$@"
> do
> echo "$x"
> done
>
>
> And call it with:
>
> y.y a "b c" d "e f" g
Output ($*)
a
b
c
d
e
f
g
Output ($@)
a
b c
d
e f
g
Yes, this groups the b/c and the e/f together, as expected. But the quote
marks are still gone. I need the quote marks. They're the entire thing
I've been after. Testing this with my test line of "y.y a b "C" -f="d" from
prior message gets:
Output ($*)
a
b
C
-f=d
Output ($@)
a
b
C
-f=d
The grouping isn't my problem. The missing quotes *inside* the script are.
Inside the script, I need the following parms (or any possible permutation,
sequence is variable):
1) a
2) b
3) "C"
4) -f="d"
The nearest solution I've found is using a line like:
y.y a b \"C\" -f=\"d\"
but it's a suboptimal result as it requires modifying several hundred
scripts. The objective is to use the original lines as written in the
scripts, and build a new script that intercepts the given command and
receives the command-line parms exactly as written.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Question - reading command line parms inside a script. |
 |
 |
|
|
03-10-04 09:35 PM
Ken Andrews <gobble@degook.com> wrote:
> Yes, this groups the b/c and the e/f together, as expected. But the quote
> marks are still gone. I need the quote marks. They're the entire thing
> I've been after.
> 1) a
> 2) b
> 3) "C"
> 4) -f="d"
Like others, I'm curious why you need quote marks. Your original
'reshape' never sees quote marks anyways. For example,
-f=" aa"
will be given to 'reshape' as
-f=<Space><Space>aa
where <Space> is the actual ASCII 0x20 (space) character.
--
William Park, Open Geometry Consulting, <opengeometry@yahoo.ca>
Linux solution for data processing and document management.
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
|
Sponsored Links |
 |
 |
|
|
 |
All times are GMT. The time now is 06:40 PM. |
 |
|
|
 |
|
 |
|
|
 |
|
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
|
|
|
|
Medical and Health forum | Computer Games Reviews | Graphics design forum
|
 |
|
 |
|