LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How do I test if bash performed a string replacement? (https://www.linuxquestions.org/questions/linux-newbie-8/how-do-i-test-if-bash-performed-a-string-replacement-4175626531/)

ans1 03-28-2018 10:34 AM

How do I test if bash performed a string replacement?
 
I'm trying to test if a string replacement occurred.

Unfortunately, my "if" statement always tests "TRUE".
Code:

#!/bin/bash
string=Not_Changed
if [ string=${string/Not_/Was_} ]; then #Did a replacement occur?
 echo "Changed: $string"                #Yes
else
 echo "Not changed: $string"            #No
fi

Giving:
Code:

Changed: Not_Changed
My online searching yields how to do replacements, but not how to test if they occurred.

How do I test if bash performed a string replacement?

BW-userx 03-28-2018 10:58 AM

Code:

#!/bin/bash
string=Not_Changed
#first change the string then check it
string2=${string/Not_/Was_}

if [[ "$string" == "$string2" ]];
then #Did a replacement occur?
 echo  "Not changed: $string" #no
else
 echo  "changed: $string2"  #yes
fi

If statements ALWAYS test (for) true, else you'll throw an error.
Code:

if [[ "$string" != "$string2" ]];
then #Did a replacement occur?
 echo  "changed: $string2" #yes
else
 echo  "Not changed: $string"  #no
fi


ans1 03-28-2018 11:12 AM

Hello BW-userx,
Thank you for your reply.

I was unclear about my goal in my post. I apologize.

I'm trying to understand what capabilities bash offers with its conditionals (what operations can the "if" test).
Can the "if" detect the execution of a string operation which occurs within the "if" statement?

BW-userx 03-28-2018 12:01 PM

assignment inside of a conditional bash variable
https://stackoverflow.com/questions/...tion-in-bash-4

ondoho 03-28-2018 01:12 PM

Quote:

Originally Posted by ans1 (Post 5836523)
Can the "if" detect the execution of a string operation which occurs within the "if" statement?

no.

unless: if you wrap it in a loop, it then might detect a change on the next iteration.

ans1 03-28-2018 02:39 PM

Hello BW-userx and ondoho,
Thank you for the responses.
I read your link, did more searches, and did more tests.
It appears that you are correct, ondoho. I can't execute a command within a bash conditional. (Pity. I was hoping bash would be similar to perl.)
That explains why I couldn't find references in my online searches (trying to find something that you can't do).

Thanks for the help, and the answer.

chrism01 03-28-2018 10:20 PM

You can definitely run cmds inside an if [[ ]] statement, but in this case you are testing the result of the cmd (ie if it failed or not), NOT the whether the data was changed.
IOW, some cmds only rtn other-than-zero if they actually failed ie crashed or equiv.
If they 'ran ok', it doesn't actually mean they did what you expected.
Remember a cmd is just a program.

Here is a page with cmds inside an 'if' https://stackoverflow.com/questions/...google_rich_qa & here is the page on 'Parameter Expansions' http://wiki.bash-hackers.org/syntax/pe

HTH

ondoho 03-29-2018 01:47 AM

Quote:

Originally Posted by ans1 (Post 5836625)
It appears that you are correct, ondoho. I can't execute a command within a bash conditional.

this is NOT true and i never said that.

PS:
my previous post was ambiguous, i edited it, hopefully clarifying what i wrote.

keefaz 03-29-2018 07:21 AM

You could test if string contains the substring you want to replace, then replace it?

Code:

#!/bin/bash
string=Not_Changed
if [[ $string = *Not_* ]]; then
 string=${string/Not_/Was_}
 echo "Changed: $string"                #Yes
else
 echo "Not changed: $string"            #No
fi

Putting test and replacement in same line
Code:

#!/bin/bash
string=Not_Changed
if [[ $string = *Not_* ]] && string=${string/Not_/Was_}; then #Did a replacement occur?
 echo "Changed: $string"                #Yes
else
 echo "Not changed: $string"            #No
fi


BW-userx 03-29-2018 10:05 AM

I just trust my coding.
Code:

#!/bin/bash

substring="and"

while read f ;
do
 if [[ "$f" =~ "$substring" ]] ;
then
    newstring="${f/$substring/GO}"
fi
 
done< somefile



All times are GMT -5. The time now is 02:13 PM.