0

I'm trying to write a bash script that search and replace a specific user input saved in config.sh using sed. This does work; however it only works partially as shown below.

config.sh

#!/bin/bash
#
#UserName to be deleted
delUserName=""
#Source
delUserSrc=/Users/"$delUserName"
#Destination
delUserDest=/Users/John/BackUp/"$delUserName"/"$delUserName".zip

main.sh

#!/bin/bash
#
source scripts/config.sh

echo -e "\nEnter user you wish to delete: \c" 
read -r UserName
sed -i '' -e "s/delUserName=.*/delUserName=$UserName/g" scripts/config.sh
echo -e "delUserName: $delUserName"
echo -e "delUserSrc: $delUserSrc"
echo -e "delUserDest: $delUserDest"

output1:

Enter user you wish to delete: Test
delUserName: 
delUserSrc:/Users/
delUserDest:/Users/John/BackUp/ / .zip

output2:

Enter user you wish to delete: Test1
delUserName:Test
delUserSrc:/Users/Test
delUserDest:/Users/John/BackUp/Test/Test.zip

output3:

Enter user you wish to delete: Test1
delUserName:Test1
delUserSrc:/Users/Test1
delUserDest:/Users/John/BackUp/Test1/Test1.zip

expected output1:

Enter user you wish to delete: Test
delUserName:Test
delUserSrc:/Users/Test
delUserDest:/Users/John/BackUp/Test/Test.zip

expected output2:

Enter user you wish to delete: Test1
delUserName:Test1
delUserSrc:/Users/Test1
delUserDest:/Users/John/BackUp/Test1/Test1.zip

The script lags. sed instantaneously changed the value for $delUserName BUT The proper values for $delUserName, $delUserSrc, and $delUserDest only echo on the 2nd run. The scripts run well when all variables are in main.sh except i have to do it this way. Save the user input into $UserName. Any idea why the values don't show when run the 1st time? Thanks

Community
  • 1
  • 1

1 Answers1

1

Here is what I think is happening.

The sed command replaces text in files. It does not modify the value of variables in memory. These values are assigned when you source config.sh.

So right after your sed line, you need to put this line :

source scripts/config.sh

It is the same line as above in your script. This is required there also so that your newly replaced values will be loaded in the variables so that you can display them. Once the new values are loaded in memory, then the echo statements will be able to expand the variables to that new value.

Fred
  • 6,590
  • 9
  • 20
  • it didn't work. Even tried these: http://stackoverflow.com/questions/5171901/sed-command-find-and-replace-in-file-and-overwrite-file-doesnt-work-it-empties – user3112318 Jan 28 '17 at 16:44
  • Do you know how/when your `config.sh` script is called? – Fred Jan 28 '17 at 16:47
  • @user3112318 Did you try including the dot `.` for really sourcing the file? Please show your complete code including the `source config.sh` in an update of your question.. – Walter A Jan 28 '17 at 17:24
  • @WalterA, I had source scripts/config.sh in main the whole time. I deleted by mistake when i posted it here. And without "source scripts/config.sh" $delUserName, $delUserSrc, and $delUserDest values would have been blank; however in my case, they display but only on the second run. The most surprising part is that the correct value for delUserName (i.e.: `delUserName=Test1` for 1st run and delUserName=Test2 for 2nd run) shows in config.sh but not in the terminal output (delUserName= for 1st run & delUserName=Test1 for 2nd run) – user3112318 Jan 29 '17 at 04:28
  • 1
    The problem is exactly as I suspected, you need to `source` your `config.sh` script after performing the replacement. Please see my updated anser. – Fred Jan 29 '17 at 04:39
  • I'm lost! It works but i'm lost! how come? You are a savior but i need some documentation, explanation. I NEED to understand/know why. Please. – user3112318 Jan 29 '17 at 04:54
  • 1
    Please read my answer over and over until it makes sense. When you run the `sed` command you are replacing text in a file, which does not affect at all the values that are assigned to the variables in memory. You need to re-source the config file so that the variables will be re-assigned, and their in-memory value (which is the one used when your `echo` statements are performed) will be the value you just put in the file. – Fred Jan 29 '17 at 05:04