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 05-30-2010, 09:28 PM   #1
Speedy2k
LQ Newbie
 
Registered: Nov 2008
Posts: 9

Rep: Reputation: 0
Bash Variable Naming Question?


I'm writing a script for asterisk to monitor trunk failure, i do a loop for every trunk it got nad would like to name variable like server1=, server2= naming the server upgoing as the trunk is. here is the scripts:

Code:
#!/bin/bash
#Checking number of SIP Registered trunk.
nmb_sip=`/usr/sbin/asterisk -rx "sip show registry" | gawk -F" " 'FNR>1' | wc -l`
sip_trunk=`/usr/sbin/asterisk -rx "sip show registry" | gawk -F" " 'FNR>1'`
echo Number of SIP Trunk: $nmb_sip
COUNT=1 # counting lines from 1 
echo "$sip_trunk" | while read line
do 
serveur=`echo $line | gawk -F" " '{print $1}' | gawk -F":" '{print $1}'`
username=`echo $line | gawk -F" " '{print $2}'`
status=`echo $line | gawk -F" " '{print $4}'`
echo Serveur: $serveur
echo Username: $username
echo Status: $status
let COUNT=$COUNT+1 
if [[ $COUNT -ge $nmb_sip ]]
	then
		break
fi 
done

#Checking number of IAX2 Registered trunk.
nmb_iax2=`/usr/sbin/asterisk -rx "iax2 show registry" | gawk -F" " 'FNR>1' | wc -l`
iax2_trunk=`/usr/sbin/asterisk -rx "iax2 show registry" | gawk -F" " 'FNR>1'`
echo Number of IAX2 Trunk: $nmb_iax2
COUNT="1" # counting lines from 1 
echo "$iax2_trunk" | while read line
do 
A=$(echo ${line} | gawk -F" " '{print ($1)}' | gawk -F":" '{print ($1)}')
echo $A
server"${COUNT}"="$A"
username=`echo $line | gawk -F" " '{print $3}'`
status=`echo $line | gawk -F" " '{print $6}'`
echo Serveur$COUNT: "$serveur"
echo Username: "$username"
echo Status: "$status"
let COUNT=$COUNT+1 
if [[ $COUNT -ge $nmb_iax2 ]]
	then
		break
fi 
done
what i would like to do is name the variable server, username and status with the count variable, like this server$COUNT to have server1 when on trunk one, bu as soon as i add the $COUNT after the server, it seems to try to make it a command, it says that:
Code:
./test.sh: line 45: server1=74.63.41.218: command not found
Can someone help me on this one? Thanx!
 
Old 05-31-2010, 12:07 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Code:
server"$COUNT"="$A"
This is no good. You can't directly use a variable to set the name of another variable, it needs to be a simple string. While there are ways to work around it using eval or indirect referencing to generate dynamic variables, an easier method may be to use an array instead.

Code:
server[$COUNT]="$A"
echo "${server[$COUNT]}"
Note that for bash v.3 or earlier, the $COUNT index must be an integer, but bash v.4 has associative arrays, which allow strings to be used.

BTW: I see two different spellings, server, and serveur. Are these supposed to be the same? Also, you should quote your echo strings.
Code:
echo "Serveur: $serveur"
Finally, $(...) is recommended over `...`.

Last edited by David the H.; 05-31-2010 at 12:16 AM. Reason: Added note about arrays, and final suggestion.
 
Old 05-31-2010, 04:28 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
ok ... while not easy it is actually possible to have dynamic variables but its fiddly and you may need to be careful on how you go about it.
The general scenario is this:

1. Create variable names using eval
2. Assign the created variable name to another variable
3. Access data using indirect references
Code:
#!/bin/bash

n=12

eval $(echo "var$n=ten")
vars="var$n"

echo $var12 # this is just to show you that using the full name works.
echo ${!vars}
 
Old 05-31-2010, 05:01 PM   #4
Speedy2k
LQ Newbie
 
Registered: Nov 2008
Posts: 9

Original Poster
Rep: Reputation: 0
Code:
Code:
server[$COUNT]="$A"
echo "${server[$COUNT]}"
This as worked perfeclty thanx a lot, but here is another problem i got:

Code:
#Checking number if IAX2 Peer marked as trunk.
nmb_iax2_peer=`/usr/sbin/asterisk -rx "iax2 show peers" | grep " (T) " | wc -l`
iax2_peer=`/usr/sbin/asterisk -rx "iax2 show peers" | grep " (T) "`
echo ""
echo "Number of IAX2 Peers marked as trunk: $nmb_iax2_peer"
COUNT="1" # counting lines from 1 
echo "$iax2_peer" | while read line
do 
IAXPEERSRV[$COUNT]=$(echo ${line} | gawk -F" " '{print $2}')
IAXPEERUSER[$COUNT]=$(echo ${line} | gawk -F" " '{print ($1)}' | gawk -F"/" '{print ($1)}')
IAXPEERSTAT[$COUNT]=$(echo ${line} | gawk -F" " '{print $7}')
echo "*****IAX2 Peer ${COUNT} Information*****"
echo ""
echo "IAX2 Peer ${COUNT} Server: ${IAXPEERSRV[$COUNT]}"
echo "IAX2 Peer ${COUNT} Username: ${IAXPEERUSER[$COUNT]}"
echo "IAX2 Peer ${COUNT} Status: ${IAXPEERSTAT[$COUNT]}"
if [[ ${IAXPEERSTAT[$COUNT]} == "OK" ]]
	then
		echo "All is perfect for this one."
	else 
		echo "Something wrong"
		iax_reload="1"
		
fi

let COUNT=$COUNT+1 
if [[ $COUNT > $nmb_iax2_peer ]]
	then
		echo "*****End IAX2 Peers INFORMATION*****"
fi 
done
echo "${sip_reload}"
echo "${iax_reload}"
if [[ $sip_reload == "1" ]]
	then
		echo "We are gona reload the SIP module to make those trunk come back online."
		/usr/sbin/asterisk -rx "module reload dnsmgr"
		/usr/sbin/asterisk -rx "module reload chan_sip.so"
		break
fi
if [[ $iax_reload == "1" ]]
	then
		echo "We are gona reload the IAX2 module to make those trunk come back online."
		/usr/sbin/asterisk -rx "module reload dnsmgr"
		/usr/sbin/asterisk -rx "module reload chan_iax2.so"
		break
fi
The problem i got you can see at the end is the iax_reload variable doesn't seems to be exported out of the while loop?? Is there anyway to do that ?? Thanx!
 
Old 05-31-2010, 07:25 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
That is because you used a pipe into the loop. This cause a subshell and once returned from any variables created inside are lost.
Dpepnding on what is stored in iax2_peer you could try a for in loop or maybe a here document on your while loop
 
Old 06-01-2010, 12:22 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
You can also use a here string instead of a pipe.

Code:
while read line; do 
     stuff	
done <<<"$iax2_peer"
 
Old 06-01-2010, 01:14 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Damn ... string not document ... that was what I was thinking of .. thanks David :0
 
Old 06-01-2010, 09:23 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Actually, it is possible to use a here document as well. It's just not as clean looking
Code:
while read line; do 
     stuff	
done <<FOO
$iax2_peer
FOO
 
  


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
question regarding bash script variable mrmnemo Linux - Desktop 4 03-22-2010 01:55 PM
Simple variable bash script help/question lilrazzzzz Programming 4 04-29-2009 06:50 PM
bash read files to variable question babag Programming 7 06-25-2007 06:04 AM
Bash Shell Scripting Dynamic Variable naming question ZuG Programming 2 02-07-2007 03:39 PM
variable naming conventions patpawlowski Programming 6 02-25-2004 02:49 PM


All times are GMT -5. The time now is 06:18 AM.

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