1753642 Members
5090 Online
108798 Solutions
New Discussion юеВ

Re: substituting

 
SOLVED
Go to solution
andi_1
Frequent Advisor

substituting

Hi,

Sometime, I will have the following string:
"vgcreate $opstr -p $maxPV -l $maxLV -s $PESize -e $maxPEperPV $vg $dskName"

Also, in some cases, I will need to remove some of the options specified in the above string and have the following format:
vgcreate $opstr -p $maxPV -l $maxLV $vg $dskNam

(no -e $maxPEperPV, -s $PESize)

Do you know how do it?

Thanks!
8 REPLIES 8
Peter Kloetgen
Esteemed Contributor

Re: substituting

Hi Andi,

what about putting the two commands into a script and give a hard link to that script. ( a second name, same inode number):
vi name1

case $0 in
name1) first_possible_command_line ;;
name2) second_possible_command_line ;;
esac


After this the following command line:

ln name1 name2

That should do it for you!

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Shahul
Esteemed Contributor

Re: substituting

Hi

U can use sed command to remove unwanted string. It is like this

#sed 's/-s $PEsize -e $maxPEperPV//g' file

I hope this will do for U.

Best of luck
Shahul
James R. Ferguson
Acclaimed Contributor
Solution

Re: substituting

Hi:

This would remove the portion you don't want:

# sed -e 's/-s $PESize -e $maxPEperPV //'

For instance:

# X='vgcreate $opstr -p $maxPV -l $maxLV -s $PESize -e $maxPEperPV $vg $dskName'

# Y=`echo $X|sed -e 's/-s $PESize -e $maxPEperPV //'`

# echo $Y

Regards!

...JRF...
Peter Kloetgen
Esteemed Contributor

Re: substituting

Hi again Andi,

sorry, i forgot to mention why this works....

$0 stores the name of the command to execute, and you simply check, which name was used when the script was called. If you used name1, the first command line is executed, in your case with all options... If the used name was name2, the alternative command line is executed.

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
andi_1
Frequent Advisor

Re: substituting

Thank you very much for your input!

One las thing:

how can I remove -e -s but without specifying $PESize, or $MAXPEperPV?

e.g.
remove -s and the next value?

Thank you!
James R. Ferguson
Acclaimed Contributor

Re: substituting

Hi (again)!

OK, here's a small script which will do what you want:

#!/usr/bin/sh
echo $1 | awk '{K=split($0,a)
N=1;while (N <= K)
{ if (a[N]=="-s") N=N+2
if (a[N]=="-e") N=N+2
printf "%s ",a[N];N++
}
printf "\n"
}'
exit 0

...pass your command string to this script. If the string contains the '-e' or '-s' option, the next argument is removed, otherwise the command is left intact and re-echoed. If we call the script 'my.sh' you could do this:

# X=`./my.sh 'vgcreate $opstr -p $maxPV -l $maxLV -s $PESize -e $maxPEperPV $vg $dskName'`

# echo $X

...yields:

vgcreate $opstr -p $maxPV -l $maxLV $vg $dskName

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: substituting

Hi (again):

The script I posted above has one dumb flaw. It assumes that the arguments passed to it are quoted. Replace "$1" with "$@", or use the attachment here (...the Forum mechanics make mince-meat of formatted scripts!).

Thus:

# ./my.sh vgcreate -e 4096 -l 10 -p 50 -s 8 /dev/vg01 /dev/dsk/cXtYdZ

...returns:

vgcreate -l 10 -p 50 /dev/vg01 /dev/dsk/cXtYdZ

Regards!

...JRF...


David Totsch
Valued Contributor

Re: substituting

When you set one of your parameters, add the appropriate switch then...an empty named parameter that returns nothing will not interfere with the operation of the command line (as opposed to a hard-coded switch that requires a value). For example:

maxPEperPV=$(...whatever you use now...)
if ( ${maxPEperPV} > 0 )
then
maxPEperPV="-e ${maxPEperPV}"
else
maxPEperPV=""
fi
# do similar ofr PESize, or even leverage
# the above test
.
.
.
vgcreate $opstr -p $maxPV -l $maxLV $PESize $maxPEperPV $vg $dskName

If PESize and maxPEperPV were specified, they will be used, if not, they interpret to nothing.

-dlt-