I have multiple script files that I have created, that allow me to simply replace a few tokens at the top of the file, and then not have to go through the actual script and change anything. I have about 10 of them, but I was hoping to find a way to write a small script that would allow me to input what value I want those tokens to hold, and then run that script, and it would go through all the scripts I have and replace the tokens with the values I input in to the first script.
Not sure if I explained it very well, but here's an example.
when I run replaceScript.sh, I'd like to be able to input, say, 5 variables(var1-var2-...var5), then the replaceScript.sh will go through and replace those same variables that have been set in serverScript1-3, with the values I input in to the replaceScript.sh
Okay, I have about, 8 variables(now that I think about it, variable isn't the best name for them. More just tags that I have created in the following format to make it easy to replace them manually, which I am now trying to do with a script). All named in this format
@tag1@ @tag2@ @tag3@
etc
.
.
.
Now the text inside the @..@ tag doesn't follow the naming convention of tag1.2.3.4.... etc, but it was just easier to use that on here.
The file names that hold these tags are as follows:
startTestABC
stopTestABC
deleteABC
rotateABC
A little more information would be that those ABC scripts all set some variable equal to the tag, ie
TEST_NAME="@tag3@"
ANOT_VAR="@tag4@"
That way, when I run the script I can replace all the @...@ tags with what I need, and then the bottom of the script calls everything by the constant variable names.
I was thinking either the script has a header that I could vi it and change it b/c this script will be used over and over with different values for the tags each time. So, either a header section, or even an interactive script where it asks for the values, stores them, then uses those to run the replace command in each script.
Output is obviously the updated ABC script. It doesn't even need to create the new files, all it has to do is modify the existing script, b/c I have my directory which holds these scripts, and then when I use them I always have to move them somewhere else, and then run the replace script from the new directory.
##################################
#Header of script, place values in variable names#
##################################
VAR_MAN_NAME="@MAN_NAME@"
VAR_PORT_NUM="@PORT_NUM@"
VAR_TEST_NUM="@TEST_NUM@"
###################################
#---------------Script Logic---------------------#
###################################
.
.
.
.
This section uses all the variable names, and doesn't use any flags
.
.
.
.
I have multiple different scripts that use this same format and have the same variables and tags in each of them. Let's say I have hundreds of different things I need to run this script for. Right now it works fine, I just replace the @...@ tags manually each time I need a new set of scripts. That's the part I want to automate, I need a script where I can input the new tag values only once, and then that script will go through and replace the tags of my 10 scripts that use the same tags.
Looks like sed can do it, but I still haven't figured out the bst way to do it when I have 10 different strings I need to replace(that will be different everytime I run the replace.sh script) in 10 different files.
The replacement part itself ( replaceScript.sh script ) is not very hard. The point is that the *ABC scripts will be running multiple times, and after their first change through the replacement script, there won't be any more tags @..@ left in them ( or am I missing something here ? ), and you need to change those values back again in the future.
before after
VAR_MAN_NAME="@MAN_NAME@" VAR_MAN_NAME=other NO more tags @..@!
VAR_PORT_NUM="@PORT_NUM@" VAR_PORT_NUM=1001
VAR_TEST_NUM="@TEST_NUM@" VAR_TEST_NUM=20
So how are you planning to rechange the new values ( other,1001,20,...) to the old tag values @..@, for the next run ?
Are the variable names VAR_MAN_NAME, VAR_PORT_NUM,..., the same in all *ABC scripts ? If so, you can replace their values based on their unique names in all the files.
Another way, you might use their same record number ( NR ) in all the files, if they are in the same position.
I think you need to provide more details about the unique properties of these variables.
The ABC scripts I have in a separate directory. What I'm going to do is copy all those ABC scripts to a different directory along with the replace.sh script. Then I'll run the replace script. So I'll still have the originals untouched, and then the new ones are what I'll use for that. Then I can do the same thing with the original ABC scripts, whenever I need to replace the @...@ tokens with something else.
Edit:
to add to that, the name of the scripts are always going to be static, and won't change, that's why I just want to copy them to a temp directory, run the script, and then I can do what I need to with them. No need to rename the files or anything like that.
Assuming the same format and your initial conditions, try and adapt the following:
#!/bin/bash
#set -x
cd /full/path/to/your/ABC_scripts
read -p "enter 1st value: " v1
read -p "enter 2rd value: " v2
read -p "enter 3rd value: " v3
# ... Enter more variables here ( v4, v5,...), in the same way as above.
read -p "enter full path to temp dir: " path
#If you want the path hardcoded use,
# path="/full/path/to/temp-dir"
for file in *ABC
do
awk -F= '/@MAN_NAME@/{$0=$1"="v1}
/@PORT_NUM@/{$0=$1"="v2}
/@TEST_NUM@/{$0=$1"="v3}
#/@other_tags_here@/{ "preserve same format as above" }
1' v1="$v1" v2="$v2" v3="$v3" "$file" > "$path"/"$file"
done
Well, the situation has changed, your tokens have special characters in them, and you need to escape all of them. See the sample below and make the proper changes to your code.
awk -F= '/\$\{B_D\}\/user_projects\/@B_D_N@/{$0=$1"="v1}
/t3:\/\/@U_S@:@A_S_P@/ {$0=$1"="v2}
/@U_H@\/keys\/@B_D_N@\/@K_S_D@/ {$0=$1"="v3}
## repeat the same thing for all of your tokens, using their actual names, and escape special chars: - $ / + * ? . etc
1' v1="$v1" v2="$v2" v3="$v3" "$file" > "$path"/"$file"
That's the point, the structure looks fine, but it's the names of the tokens that are important, the script depends totally on their unique names and formation.
The code worked fine for me, look at a test using the above adjustments, with three random tokens given above:
# ./replace.sh
enter 1st value:
"t3://U_S_Tag_Value:A_S_P_Tag_Value"
enter 2nd value:
"other variable goes here"
enter 3rd value:
"000 000 000"
enter full path to temp dir:
/home/user/test
------
$ ll test
-rw-r--r-- 1 user sys 557 Oct 28 19:37 file_1ABC
-rw-r--r-- 1 user sys 557 Oct 28 19:37 file_2ABC
-rw-r--r-- 1 user sys 557 Oct 28 19:37 file_3ABC
-rw-r--r-- 1 user sys 557 Oct 28 19:37 file_4ABC
$ cat test/file_2ABC
##################################
#Header of script, place values in variable names#
##################################
S_N="@A_S_N@"
B_D="@B_I_D@"
D_B="t3://u_s_Tag_Value:a_s_p_Tag_Value"
D_D="${D_B}/@D_N@"
S_U="other variable goes here"
A_U="t3://@A_U_S@:@A_S_P@"
K_D="000 000 000"
VAR_MAN_NAME="@MAN_NAME@"
VAR_PORT_NUM="@PORT_NUM@"
VAR_TEST_NUM="@TEST_NUM@"
###################################
#---------------Script Logic---------------------#
###################################
.
.
.
This section uses all the variable names, and doesn't use any flags
Some of those aren't inside the tags and I'm only looking to replace the tags, not entire variable names. And as you can see some variables are made up of multiple tags b/c those shorter tags may be used in other parts of the script as well. But the variable is not needed, just part of it(the tag).
There are only two tags that will have something besides a number or letter, and that's the @BEA_INSTALL_DIR@ and the @USER_HOME@ directory.