cancel
Showing results for 
Search instead for 
Did you mean: 

Shell Scripting Question

 
KapilRaj
Honored Contributor

Shell Scripting Question

After a long time, back to the ITRC. How are you guys doing ? ..

Coming to the question ..

I am looking for a command which will substitute the arguments given to a command.

i.e.
If I execute that command this way,

....

It should produce an output by replacing references to $1 as , $2 as and so on ...

I can think of an "sed" solution. But I am looking for something smaller and simpler.

Regards,

Kaps
Nothing is impossible
10 REPLIES
TwoProc
Honored Contributor

Re: Shell Scripting Question

That's a confusing request, as it doesn't sound like much of anything, so I think I'm misunderstanding.

But "echo" will do that

$> echo one two three four five six
one two three four five six

We are the people our parents warned us about --Jimmy Buffett
KapilRaj
Honored Contributor

Re: Shell Scripting Question

script1 is ,

if [ $1 -eq 100 ]
then
echo "It was hundred "
fi

And you execute

script1 10

The output should be

if [ 10 -eq 100 ]
then
echo "It was hundred "
fi

I am looking for a script to replace "command"

Regards,

Kaps
Nothing is impossible
TwoProc
Honored Contributor

Re: Shell Scripting Question

OK, It looks to me you want to see what your commands would evaluate to while running.

While it's not a script (its just a shell command option), this will get you close.

$> set -x

and then run your script.

It will show you what you are looking for.

An example to see if process id of the current shell is equal to 37.

$> echo [ $$ -eq 37 ]|| echo yes && echo no
+ echo '[' 7670 -eq 37 ']'
[ 7670 -eq 37 ]
+ echo no
no

An easy way to do this is just to put a
"set -x" command near the top of your script, and comment it out when you don't need it any longer.
We are the people our parents warned us about --Jimmy Buffett
KapilRaj
Honored Contributor

Re: Shell Scripting Question

There is the catch. I do not want this shell script to be executed. I mean, it should resolve the variables inside w/o executing the shell script.

Regards,

Kaps
Nothing is impossible
KapilRaj
Honored Contributor

Re: Shell Scripting Question

I am also going to believe that my question is really a stupid one. $1 , $2 kind of variables makes sense only when I execute it ..
Nothing is impossible
James R. Ferguson
Acclaimed Contributor

Re: Shell Scripting Question

Hi Kaps:

> I am also going to believe that my question is really a stupid one. $1 , $2 kind of variables makes sense only when I execute it ..

From that perspective, make your script self-documenting:

# cat somescript
#!/usr/bin/sh
typeset WHO=$1
typeset WHERE=$2
echo "I said hello ${WHERE} ${WHO}"

# ./somescript Kaps there
I said hello there Kaps

Regards!

...JRF...
KapilRaj
Honored Contributor

Re: Shell Scripting Question

Thanks everybody.

Hi JRF,

In the example you gave, I was expecting an output like ,

# cat somescript
#!/usr/bin/sh
typeset WHO=kapil
typeset WHERE=
echo "I said hello ${WHERE} ${WHO}"

I think it is an sed question i.e. ,

cat myscript | sed "s;$1;arg1;g" .. .. , I think I have to do it that way.

Regards,

Kaps
Nothing is impossible
James R. Ferguson
Acclaimed Contributor

Re: Shell Scripting Question

HI (again) Kaps:

> I think it is an sed question i.e. ...cat myscript | sed "s;$1;arg1;g" .. .. , I think I have to do it that way

Well, if that's what you really want, at least don't run the useless 'cat' process and fix the replacement:

# sed 's;$1;arg1;g' file

(or)

# sed "s;\$1;arg1;g" file

...but the use of a semicolon as a delimiter is limiting since you might want to make one pass and do:

# sed "s/\$1/arg1/g;s/\$2/arg2/g" file

(or):

# sed 's/$1/arg1/g;s/$2/arg2/g' file

Please note that I am still unsure as to what is your real objective.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Shell Scripting Question

>I am looking for a command which will substitute the arguments given to a command.

I'm still not sure what you want??

>i.e. If I execute that command this way,
....

>It should produce an output by replacing references to $1 as , $2 as and so on

If you want to change $1, etc, you can use set:
set
KapilRaj
Honored Contributor

Re: Shell Scripting Question

I think I had too much beer that day. Today when I read my question, I can make neither head nor tail !

Closing the thread ..
Nothing is impossible