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 Code:
Changed: Not_Changed How do I test if bash performed a string replacement? |
Code:
#!/bin/bash Code:
if [[ "$string" != "$string2" ]]; |
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? |
assignment inside of a conditional bash variable
https://stackoverflow.com/questions/...tion-in-bash-4 |
Quote:
unless: if you wrap it in a loop, it then might detect a change on the next iteration. |
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. |
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 |
Quote:
PS: my previous post was ambiguous, i edited it, hopefully clarifying what i wrote. |
You could test if string contains the substring you want to replace, then replace it?
Code:
#!/bin/bash Code:
#!/bin/bash |
I just trust my coding.
Code:
#!/bin/bash |
All times are GMT -5. The time now is 02:13 PM. |