LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   need help for script (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-for-script-4175454085/)

rraina 03-14-2013 01:23 PM

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?

TB0ne 03-14-2013 01:32 PM

Quote:

Originally Posted by rraina (Post 4911623)
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?

Post what you've done/tried so far, and we can help. Otherwise, take a look at the cut command, if all you want to do is trim off the first few characters.

shivaa 03-14-2013 01:32 PM

Explain it little more, and let's know what exaclty your requirement is?

Anyway, you can try this:
Code:

#!/bin/bash
DN=X_Y
var=$(echo $DN | cut -d"_" -f2)
ENV=$var
echo $ENV


rraina 03-14-2013 01:44 PM

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.

TB0ne 03-14-2013 02:00 PM

Quote:

Originally Posted by rraina (Post 4911633)
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.

Yes...and again, you need to post what YOU have written so far. You've been pointed to the cut command, which will let you trim things. We will be glad to HELP you, but we're not going to write a script for you.

shivaa 03-14-2013 02:08 PM

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.

rraina 03-14-2013 02:18 PM

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

TB0ne 03-14-2013 02:26 PM

Quote:

Originally Posted by rraina (Post 4911659)
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

Ok, again, post your script. In my post signature, it has links to a bash scripting tutorial, and there are THOUSANDS of others you can easily find.

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).

shivaa 03-14-2013 02:34 PM

Just try:
Code:

#!/bin/bash
DN=abc_dev
var=$(echo $DN | cut -d"_" -f2)    # It will save 'dev' in var variable
# OR
var=$(echo $DN | awk -F"_" {print $2})
ENV=$var
echo $ENV

Code:

# OR directly save value in env as
ENV=$(echo $DN | cut -d"_" -f2)
# OR
ENV=$(echo $DN | awk -F"_" {print $2})
echo $ENV


David the H. 03-15-2013 10:03 AM

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.

rraina 03-15-2013 10:19 AM

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
$ env=$(echo $DN | cut -d '_' -f 2 )
$ echo $env
dev
#if I modify the value of DN as abc, which will be one of the case in my script, this still returns a value.I want this to fail since the underscore is missing
$ DN=abc
$ env=$(echo $DN | cut -d '_' -f 2 )
$ echo $env
abc

***

shivaa 03-15-2013 10:28 AM

Just try it out:
Code:

DN=abc_dev
env=$(echo $DN | gawk 'BEGIN{FS="_"}; {if(NF>1) print $2; else print $1}')
echo $env


rraina 03-15-2013 10:59 AM

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:


if $DN= *_*
 then env=$(echo $DN | cut -d '_' -f 2 )
else [`nslookup admper|grep Name | awk {'print $2'} | cut -c-6` == `hostname` ] ; then env="per";


TB0ne 03-15-2013 12:10 PM

Quote:

Originally Posted by rraina (Post 4912286)
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.

Yes, you can share two scripts here, and you can even post one, with a clear comment that says "This variable comes from the other script". Either works, and will give us some idea of what you're working with. If you're not going to post your script(s), then read through one of the VERY easily found bash scripting tutorials, and work through the errors on your own. We can't guess as to what's happening.

And did you bother to read/try what I posted above???
Quote:

Originally Posted by TB0ne
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).

There are THOUSANDS of bash scripting tutorials you can find with a quick Google search, that can tell you how to do this, and you've been given the tools/hints you need in this thread.

Again, as said before: enclose it in backticks, and it works fine.
Code:

DN=abc_dev
env=`echo $DN | cut -d '_' -f 2 `
echo "ENV = $env"

The above example prints out "dev"...just like you want it to. Can't be more plain.



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

shivaa 03-15-2013 12:12 PM

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
echo -n "Enter DN: "; read DN
env=$(echo $DN | gawk 'BEGIN{FS="_"}; {if(NF>1) print $2; else print $1}')  ## It will print what is after underscore
                                                                            ## If no underscore found, it will take whole string
echo $env
nslookup $env | awk '$1 ~ /Name/ {print $2}'



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