LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   string replace in bash script (https://www.linuxquestions.org/questions/linux-newbie-8/string-replace-in-bash-script-878111/)

xombboxer 05-01-2011 05:15 AM

string replace in bash script
 
i want to find something in a string and replace with other string in bash and ksh how do i do that

ex:
Code:

mystring="asdf-)"
i want to replace ')' with '.'
like this
Code:

replace(mystring, match, newstring)
finally I want mystring as
Code:

mystring="asdf-."

druuna 05-01-2011 05:22 AM

Hi,

Using bash internals:
Code:

mystring=${mystring/)/.}
Man bash for the details and other neat tricks.

Hope this helps.

macemoneta 05-01-2011 05:24 AM

From 'man bash':

Quote:

${parameter/pattern/string}

Pattern substitution. The pattern is expanded to produce a pattern just as in pathname expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. If pattern begins with /, all matches of pattern are replaced with string. Normally only the first match is replaced. If pattern begins with #, it must match at the beginning of the expanded value of parameter. If pattern begins with %, it must match at the end of the expanded value of parameter. If string is null, matches of pattern are deleted and the / following pattern may be omitted. If parameter is @ or *, the substitution operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the substitution operation is applied to each member of the array in turn, and the expansion is the resultant list.
For your example:

Code:

mystring="asdf-)"
echo ${mystring/)/.}


xombboxer 05-01-2011 06:54 AM

yes this is perfect but in my case i have pass this for a sh file from prompt as
Code:

./sh pass="asdf-)"
this will be treated as asdf-) in bash. Then i am calling another sh file, where i am using expect (password). there i cannot change value fro ')' to '.'

how do i do that...

it says bad parameter

MTK358 05-01-2011 10:18 AM

Could you explain your sutuation better? And where exactly do you want to do substitution?

xombboxer 05-01-2011 12:24 PM

ok

i had enough trouble with with expect and special character ')'.

i will replace ')' with say 'n' while passing the parameter and in the expect utility i will replace back 'n' with ')'

my requirement now is

how do i replace 'n' with ')' withing expect utility.
Code:

${mystring/)/.}
Wont work within expect



please help me :(

MTK358 05-01-2011 12:26 PM

What is this "expect" you keep talking abut?

Nylex 05-01-2011 12:27 PM

Quote:

Originally Posted by MTK358 (Post 4342898)
What is this "expect" you keep talking abut?

http://www.nist.gov/el/msid/expect.cfm

xombboxer 05-01-2011 12:29 PM

hi MTK358

its an utility used to automate things like automatic password entry etc . i too dont know much about it. i think its sort of TCL

druuna 05-01-2011 12:43 PM

@xombboxer: You talk about two scripts, input from the prompt and expect. Do you mind posting what you have and telling which line(s) are not working?

xombboxer 05-02-2011 12:51 AM

first im calling
Code:

./main.sh pass="p:-\)10"
note that i am escaping ')' as '\)'

from main.sh again im calling one more sh file

Code:

../_exp.sh $PASS
were $PASS contains
Code:

"p:-\)10"
_exp.sh looks like this

Code:

#!/usr/bin/expect -f
.
.
.
set p [lrange $argv 0 0]
.
.
.

send -- "echo correct script \r"

send -- "echo valu  passwrd 0 argument = $p \r"

set send_human {.1 .25 2 .05 1.5}
set breaktheloop 0

while {$breaktheloop == 0} {
        expect "*Password:*"
        send -h "$p\r"
        expect -timeout -1 
        sleep 10
        set breaktheloop 1

the main idea is to pass the password as a parameter.

the problem is because of the special character ')'. it is correctly interpreted in main.sh. But when it goes to second shell file which is to handle expect utility, it does not recognize '\' as escape character. it treats "p:-\)10" as 7 character string but actually my password is 6 character string "p:-)10".

druuna 05-02-2011 03:23 AM

Hi,
Quote:

Originally Posted by xombboxer (Post 4343480)
first im calling
Code:

./main.sh pass="p:-\)10"
note that i am escaping ')' as '\)'

Why this construct? It seems you need to split the pass part from the "p:-\)10" part inside your script.

Wouldn't ./main.sh "p:-\)10" (or ./main.sh 'p:-)10') be easier? Inside the script you can do: PASS="$1"

If you put single quotes around the input instead of double quotes, you do not need to escape the ) when starting the main script.

Quote:

Originally Posted by xombboxer
from main.sh again im calling one more sh file
Code:

../_exp.sh $PASS

Do you need to call a second script? Can't you incorporate the expect part into your first script? This way you can use the vars that are used in both the bash and the expect part.
Code:

#/bin/bash
.
bash stuff goes here
.

expect -c "
.
expect stuff goes here
.
"

more bash stuff

This way you won't need the set p [lrange $argv 0 0] part and can use $PASS instead of $p (send -h "$PASS\r")

Hope this helps.


All times are GMT -5. The time now is 08:39 PM.