Setting the correct parameter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Setting the correct parameter
# 1  
Old 07-02-2010
Question Setting the correct parameter

Hi Gurus,

Here is the issue that I've encountered.

Code:
VAR1=`hostname | tr '[a-z]' '[A-Z]'`
-> convert hostname to uppercase

VAR2=`grep $VAR1 /etc/hosts | awk '{ print $1 }'`
-> Since hostname is already in uppercase, grep is unable to find it in /etc/hosts. How to handle VAR2?

Is there a better way to extract the hostname and IP on separate variable?
Please kindly advice on how to extract the IP address.

Thank you.



Regards,
Peter
# 2  
Old 07-02-2010
Peter

Do a case-insensitive search in that case:

Code:
VAR2=`grep -i $VAR1 /etc/hosts | awk '{ print $1 }'`

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 07-02-2010
Here's my script for printing the local IP.

I prefer to get the actual address from the network device instead of the address it's supposed to be in /etc/hosts. But some of my machines have the IP address assigned to br0 device instead eth0, so I check both.
Code:
#!/bin/sh
IP="$(/sbin/ifconfig br0 2>/dev/null|grep 'inet ')"
test -z "$IP"  &&  IP="$(/sbin/ifconfig eth0|grep 'inet ')"
echo $IP | sed 's/.*addr:\([0-9.]*\).*/\1/'

Of course, If you want the result in a variable, you could capture the last line back into IP:
IP="$(echo $IP | sed 's/.*addr:\([0-9.]*\).*/\1/')"
This User Gave Thanks to KenJackson For This Post:
# 4  
Old 07-02-2010
No need for grep if you are using gawk. You can turn case insensitivity on using IGNORECASE.
Code:
$ cat /etc/hosts
127.0.0.1       localhost
::1             localhost
....
192.168.0.115   admiralty
192.168.0.116   central
192.168.1.230   quarry
....
$ VAR1=Central
$ VAR2=$(gawk 'BEGIN {IGNORECASE=1} /'$VAR1'/ {print $1}' /etc/hosts)
$ echo $VAR2
192.168.0.116

This User Gave Thanks to fpmurphy For This Post:
# 5  
Old 07-02-2010
Code:
# grep `hostname|tr '[:lower:]' '[:upper:]'` /etc/hosts

This User Gave Thanks to ygemici For This Post:
# 6  
Old 07-02-2010
Hi Gurus,

Thanks a lot for your help.

I’ve managed to solve my issue.


Regards,
Peter
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Ubuntu

Network Manager not setting correct DNS servers

Since a few weeks i use Ubuntu 16 on my laptop: # uname -a Linux xxxx 4.8.0-52-generic #55~16.04.1-Ubuntu SMP Fri Apr 28 14:36:29 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux Because i want to use a custom name server i set the properties in the "Edit Connections" dialogue to the following: ... (2 Replies)
Discussion started by: bakunin
2 Replies

2. Shell Programming and Scripting

Call Script with Parameter (that has another parameter)

Hi. How do I achieve this sh /EDWH-DMT02/script/MISC/exec_sql.sh "@/EDWH-DMT02/script/others/CSM_CKC/Complete_List.sql ${file_name}" Complete_List.txt The /EDWH-DMT02/script/MISC/exec_sql.sh has two parameters and it's working fine with this sh /EDWH-DMT02/script/MISC/exec_sql.sh... (7 Replies)
Discussion started by: aimy
7 Replies

3. Shell Programming and Scripting

Resolving a parameter which is passed as parameter

Hi, I have the following files. ->cat scr.sh export TMP_DIR=/home/user/folder1 export TMP_DIR_2=/home/user/folder2 while read line do cat "$line" done<file_list.dat ------------------------ -> cat file_list.dat $TMP_DIR/file1.txt $TMP_DIR_2/file2.txt --------------------------- -> cat... (6 Replies)
Discussion started by: barath
6 Replies

4. Solaris

Is there a difference between setting a user as nologin and setting it as a role?

Trying to figure out the best method of security for oracle user accounts. In Solaris 10 they are set as regular users but have nologin set forcing the dev's to login as themselves and then su to the oracle users. In Solaris11 we have the option of making it a role because RBAC is enabled but... (1 Reply)
Discussion started by: os2mac
1 Replies

5. Shell Programming and Scripting

How to get the parameter value from the parameter file in perl?

hi all, i have a parameter file of following format, i want a method which can get the value of specific parameter. parameter file format: <Parameter Name="FileLocationWindows"> <Description> The directory location of the logger file. ... (1 Reply)
Discussion started by: laxmikant.hcl
1 Replies

6. Shell Programming and Scripting

Passing parameter to script, and split the parameter

i am passing input parameter 'one_two' to the script , the script output should display the result as below one_1two one_2two one_3two if then echo " Usage : <$0> <DATABASE> " exit 0 else for DB in 1 2 3 do DBname=`$DATABASE | awk -F "_" '{print $1_${DB}_$2}` done fi (5 Replies)
Discussion started by: only4satish
5 Replies

7. Shell Programming and Scripting

Command that takes one parameter and then searches for the passed in parameter

Hi I am looking for a unix command or a small shell script which can takes one parameter and then searches for the passed in the parameter in any or all files under say /home/dev/ Can anyone please help me on this? (3 Replies)
Discussion started by: pankaj80
3 Replies

8. UNIX for Advanced & Expert Users

OS level setting parameter for Application Hanging

Hi We are facing Application hanging issue from users who log in to applications from the server. But through other server , all the transactions are working fine. Now how can i can compare the OS level parameter setting on both servers. Or please suggest me your ideas to debug thes... (1 Reply)
Discussion started by: susa_dgl
1 Replies

9. Solaris

kernal parameter setting

hi, can anybody tell me how to increase the parameters like project.max-shm-ids on solaris10. i have used prctl, but got reset while server reboot. thnks and regards Ajay (1 Reply)
Discussion started by: ajaysahoo
1 Replies

10. Shell Programming and Scripting

how do I make dynamic parameter names? Or get the value of a parameter evaluated twi

Say I write something like the following: var1=1 var2=2 for int in 1 2 do echo "\$var$int" done I want the output to be: 1 2 Instead I get something like: $var1 $var2 (2 Replies)
Discussion started by: Awanka
2 Replies
Login or Register to Ask a Question