LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-14-2013, 01:23 PM   #1
rraina
LQ Newbie
 
Registered: Mar 2013
Posts: 16

Rep: Reputation: Disabled
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?
 
Old 03-14-2013, 01:32 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,811

Rep: Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190
Quote:
Originally Posted by rraina View Post
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.
 
Old 03-14-2013, 01:32 PM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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
 
Old 03-14-2013, 01:44 PM   #4
rraina
LQ Newbie
 
Registered: Mar 2013
Posts: 16

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-14-2013, 02:00 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,811

Rep: Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190
Quote:
Originally Posted by rraina View Post
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.
 
Old 03-14-2013, 02:08 PM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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.
 
Old 03-14-2013, 02:18 PM   #7
rraina
LQ Newbie
 
Registered: Mar 2013
Posts: 16

Original Poster
Rep: Reputation: Disabled
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
 
Old 03-14-2013, 02:26 PM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,811

Rep: Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190
Quote:
Originally Posted by rraina View Post
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).
 
Old 03-14-2013, 02:34 PM   #9
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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
 
Old 03-15-2013, 10:03 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
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.
 
Old 03-15-2013, 10:19 AM   #11
rraina
LQ Newbie
 
Registered: Mar 2013
Posts: 16

Original Poster
Rep: Reputation: Disabled
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
***
 
Old 03-15-2013, 10:28 AM   #12
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Just try it out:
Code:
DN=abc_dev
env=$(echo $DN | gawk 'BEGIN{FS="_"}; {if(NF>1) print $2; else print $1}')
echo $env
 
Old 03-15-2013, 10:59 AM   #13
rraina
LQ Newbie
 
Registered: Mar 2013
Posts: 16

Original Poster
Rep: Reputation: Disabled
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";
 
Old 03-15-2013, 12:10 PM   #14
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,811

Rep: Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190
Quote:
Originally Posted by rraina View Post
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

Last edited by TB0ne; 03-15-2013 at 12:11 PM.
 
Old 03-15-2013, 12:12 PM   #15
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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}'

Last edited by shivaa; 03-15-2013 at 12:13 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Shell script/Perl Script to remove the string until it finds special character '_' pooppp Programming 10 07-17-2012 09:36 AM
Shell script/Perl Script to remove the string until it finds special character '_' pooppp Programming 1 07-13-2012 01:03 AM
Shell script, Perl script, command or utility to convert Binary to text Perseus Programming 26 07-12-2012 06:00 AM
[SOLVED] bash and xterm: how make apps started by and for a script persist when script terminates porphyry5 Linux - General 4 06-15-2011 01:27 PM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 08:40 AM


All times are GMT -5. The time now is 10:04 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration