LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-28-2018, 10:34 AM   #1
ans1
LQ Newbie
 
Registered: Mar 2018
Posts: 12

Rep: Reputation: Disabled
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?
 
Old 03-28-2018, 10:58 AM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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

Last edited by BW-userx; 03-28-2018 at 11:10 AM.
 
Old 03-28-2018, 11:12 AM   #3
ans1
LQ Newbie
 
Registered: Mar 2018
Posts: 12

Original Poster
Rep: Reputation: Disabled
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?
 
Old 03-28-2018, 12:01 PM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
assignment inside of a conditional bash variable
https://stackoverflow.com/questions/...tion-in-bash-4
 
Old 03-28-2018, 01:12 PM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by ans1 View Post
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.

Last edited by ondoho; 03-29-2018 at 01:45 AM. Reason: remove ambiguity
 
Old 03-28-2018, 02:39 PM   #6
ans1
LQ Newbie
 
Registered: Mar 2018
Posts: 12

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-28-2018, 10:20 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,363

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
 
Old 03-29-2018, 01:47 AM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by ans1 View Post
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.
 
Old 03-29-2018, 07:21 AM   #9
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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

Last edited by keefaz; 03-29-2018 at 07:30 AM.
 
Old 03-29-2018, 10:05 AM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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
 
  


Reply

Tags
bash



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash: test for substring inside string ali_bush Linux - General 3 04-26-2018 07:31 PM
re:bash - how would I test for a string from a file and do something based on $string slacker_ Programming 2 06-09-2014 03:35 AM
[SOLVED] Bash script: How to test string value? KonfuseKitty Programming 1 07-10-2010 05:52 AM
bash script to test string complexity (like password complexity) robertjinx Linux - Server 2 05-12-2010 02:58 PM
bash string replacement t4sked Linux - General 5 05-14-2009 12:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration