Variable is not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable is not working
# 1  
Old 01-16-2014
Variable is not working

Hello Friends

I want to create a bash script to ping my server but its not working as per my below code

Code:
#!/bin/bash
AA=AA1xx.example.com
BB=BB1xx.example.com
ping -c 3 $`hostname|cut -c-2`

Error:
ping: unknown host $BB

Can any one help me to resolve this .

Thank you
# 2  
Old 01-16-2014
Quote:
Originally Posted by nith_anandan
Hello Friends

I want to create a bash script to ping my server but its not working as per my below code

Code:
#!/bin/bash
AA=AA1xx.example.com
BB=BB1xx.example.com
ping -c 3 $`hostname|cut -c-2`

Error:
ping: unknown host $BB

Can any one help me to resolve this .

Thank you
why exactly do you need a '$'?
# 3  
Old 01-16-2014
I presumed the $ is to prepend to the output of hostname and cut to act as a variable.

Code:
eval ping -c 3 \$$(hostname|cut -c-2)

Someone will be along in a minute to explain that "eval is dangerous" Smilie

You could just use an if or case statement for this?!
This User Gave Thanks to Scott For This Post:
# 4  
Old 01-16-2014
Quote:
Originally Posted by vgersh99
why exactly do you need a '$'?
"$" is used to execute variables which I set beginning of my script is it right ?

---------- Post updated at 08:49 PM ---------- Previous update was at 08:47 PM ----------

Quote:
Originally Posted by Scott
I presumed the $ is to prepend to the output of hostname and cut to act as a variable.

Code:
eval ping -c 3 \$$(hostname|cut -c-2)

Someone will be along in a minute to explain that "eval is dangerous" Smilie

You could just use an if or case statement for this?!
eval solution works good Thank you Scott
but Could you please help me to use if statement.

Last edited by nith_anandan; 01-16-2014 at 11:40 AM..
# 5  
Old 01-16-2014
Code:
eval ping -c 3 $(hostname|cut -c-2)

One $ too much...
# 6  
Old 01-16-2014
If I understand what you're trying to do, try:
Code:
#!/bin/bash
hosts[AA]=AA1xx.example.com
hosts[BB]=BB1xx.example.com
host=$(hostname)
ping -c 3 ${hosts[${host:0:2}]}


Last edited by Don Cragun; 01-16-2014 at 11:43 AM.. Reason: Fix typo: ${host:1:2} should be ${host:0:2}
# 7  
Old 01-16-2014
The first two letters form the name of the variable to look in for the hostname, therefore \$$(hostname ...) would be more apt if using eval. It's probably also possible to avoid hostname and cut if the HOSTNAME variable is set.
Code:
${HOSTNAME:0:2}


Last edited by Scott; 01-16-2014 at 12:03 PM.. Reason: Rewording to clarify my meaning
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep with variable not working

Hello, I am using below code : export ORAR=orp712z.int.thomsonreuters.com #echo $ORAR; if echo $ORAR|grep -i "_" then ORACLE_SID1= echo $ORAR|cut -f2 -d "_" echo $ORACLE_SID1 ORACLE_SID=fgrep "$ORACLE_SID1" /etc/oratab|cut -f1 -d ":" #echo $ORACLE_SID else ORACLE_SID1= echo $ORAR|cut... (1 Reply)
Discussion started by: admin_db
1 Replies

2. Shell Programming and Scripting

Variable not working in grep from script

Hi, If I hard code a value in the grep it works fine from script, when I use variable it doesn't work. On a seperate note, some lines (during testing) works fine from command line but not from scirpt. #!/bin/bash # Will fetch the (oldest - as ls will sort by name by default)Date in the... (7 Replies)
Discussion started by: krish.m
7 Replies

3. AIX

Shell variable is not working

One of my shell script, I'm using shell variable to substitute parameter value to a oracle related command "tnsping". without substitution same value is working fine. see below example. Below is the result is expected with command: > tnsping "(ADDRESS = (PROTOCOL = TCP)(HOST = uedcb031)(PORT... (4 Replies)
Discussion started by: sendtoshailesh
4 Replies

4. Shell Programming and Scripting

export variable not working after su

I have a requirement to change user inside a shell script and execute group of commands. I have done it many times earlier but never came across the issue with exporting variables. Strangely if a var is exported afetr su, it is not working where as if it is does outside su, it works. Another issue... (8 Replies)
Discussion started by: sasiharitha
8 Replies

5. Shell Programming and Scripting

export of a variable isn't working

Hi I want export input data ... echo "month: " read m export m=$m also export m is not working ? the month-variable should be exportet for the use in other scripts, but it is not working like this. What i'm doing wrong? Thanks in advance! IMPe (10 Replies)
Discussion started by: IMPe
10 Replies

6. Shell Programming and Scripting

A simple variable subst is not working

Hi what i want: listing files in a special range ls -lrt 20120601{05..06}* ... -rw-rw-r-- 1 imp imp 279 1. Jun 07:51 201206010550 -rw-rw-r-- 1 imp imp 279 1. Jun 07:01 201206010600 -rw-rw-r-- 1 imp imp 279 1. Jun 07:11 201206010610 -rw-rw-r-- 1 imp imp 279 1. Jun 07:21... (1 Reply)
Discussion started by: IMPe
1 Replies

7. UNIX for Dummies Questions & Answers

grep'ing a variable - why not working

Hi all, Am writing a ksh script where I am looking for processes that has gone defunct and all of which has the same PPID PID is the variable that I need to match as this is the process ID of the processes that has gone defunct Am just curious how come the following DOES NOT work? ps... (6 Replies)
Discussion started by: newbie_01
6 Replies

8. Shell Programming and Scripting

Variable not working correctly.

Hi, I have a script where I am trying to set a local variable using the following, MYVAR="$NAME"_"$NAME2".txt where say, NAME = one NAME2 = two so I want the output one_two.txt but what I am getting is, two.txt basically the $NAME2 is overwriting, what am I doing wrong? ... (3 Replies)
Discussion started by: walsh_j
3 Replies

9. Shell Programming and Scripting

Basic variable expansion not working...

#!/usr/bin/bash if then echo "Not valid arguments entered. Just username should be entered." else USER_NAME=$1 FILE_NAME=$USER_NAME.info UNN=STUDIN\\\\$1 echo $UNN last STUDIN\\\\$1 last UNN If I type `last STUDIN\\eip060` it works but if I try to expand it with variable it is... (5 Replies)
Discussion started by: Zammy_bg
5 Replies

10. Shell Programming and Scripting

assign value to variable is not working

Hi :) The next script campares two files File1-Line1 vs File2-Line1, File1-Line1 vs File2-Line2... only if line contains "AS-", if LineX is not in File2 writes in aux, but "valor" is allways=1 never changes! :confused: What is wrong? valor changes to 0 before break, after brake is again 1 ... (3 Replies)
Discussion started by: agustincm
3 Replies
Login or Register to Ask a Question