need help for script
Hi there,
I need to write include the below logic in my script: if $DN=X_Y, then ENV=Y X_Y would be like abc_dev , so I want the value to be assigned to ENV=dev How can I achieve this? |
Quote:
|
Explain it little more, and let's know what exaclty your requirement is?
Anyway, you can try this: Code:
#!/bin/bash |
This code block is a part of a bigger script.
Basically DN is a variable which can have many values like abc_dev, abc_fgh, per_fgh What I want to achieve is that the code block should read the value of DN and there will be 2 cases further: case1: if there is an underscore in DN value(eg.abc_dev) , then ENV= dev case2: if there is no underscore(eg. abc) , it will assign the value with a different logic. i.e run nslookup for a number of VIPs. I need help for implementing the case1 right now. Also, I am thinking of using if else statements for both cases. I hope the requirement is more clear now. |
Quote:
|
As TBOne pointed, providing you a ready-made script isn't at all a good idea, because it will give you nothing to learn.
However, just look at what I said above in post#3, that will give you basic idea about your script. Let's know if you face any problem. |
I attempted the below:
DN=abc_dev env= echo $DN | cut -d '_' -f 2 >> if I write this,it gives the output as dev, whereas I need to store this value in env variable env= awk {'print $DN'} | cut -d '_' -f 2 >> this gives no output |
Quote:
Since you're wanting env to be the RESULT of other commands, you have to run those commands first. So, enclose them in backticks (the ` character). |
Just try:
Code:
#!/bin/bash Code:
# OR directly save value in env as |
0) Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.
1) To test a string for multiple possible globbing values, the case construct is usually better than an if test. 2) To trim out parts of strings in variables, don't use cut or awk or whatnot when you can use parameter substitution or other built-in string manipulations. 3) Pretty much all the basics you need to know can be found in the http://mywiki.wooledge.org/BashGuide. |
First of all, I cant post the entire script since it derives its variables from another script and sharing two scripts here is not possible. the scripts were already created by somebody else.I have to tweak them for new requirements.
shivaa, I tried the below as you suggested. *** Code:
$ DN=abc_dev |
Just try it out:
Code:
DN=abc_dev |
Shivaa, it gives the same results.
Let me try to explain the situation more in depth. This function basically needs to check if the server is run on the correct server. I have various domain names(DN) like app_dev, ws_prd, wcc, app_prd where the letters after the underscore denotes the environment(i.e. env), when it doesnt have an undersore, its the per environment. That is what I want to do as I have outlined in the code below.I want the script to check explicitly for the underscore, if its there in the DN value then the value of environment will set equal to the letters after the underscore.If the underscore is not there, it does the nslookup for the VIP which has the environment name suffixed to it and assign the value to env. Earlier I had though of doing nsloopkup for all cases, but that would not be a good idea to have so many nslookups. For the DN value, the number of letters before and after the underscore should be variable. I hope this makes more sense to you. Thanks again for your help. Code:
|
Quote:
And did you bother to read/try what I posted above??? Quote:
Again, as said before: enclose it in backticks, and it works fine. Code:
DN=abc_dev DN=abc_dev env= echo $DN | cut -d '_' -f 2 >> if I write this,it gives the output as dev, whereas I need to store this value in env variable env= awk {'print $DN'} | cut -d '_' -f 2 >> this gives no output |
As already told you, that without knowing the complete scnerio, it's not possible to suggest a correct answer. Also your code is not correct. So just try this:
Code:
#!/bin/bash |
All times are GMT -5. The time now is 04:47 AM. |