Bash question: working with an array of previously set variable strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash question: working with an array of previously set variable strings
# 1  
Old 12-01-2014
Display Bash question: working with an array of previously set variable strings

while i've used arrays to work with variables, i've never used them to loop through a set of strings and wanted to ask the community for some feedback or assistance.

let me be specific. here's my code:

Code:
# URL port Variables
port2195=`nc -z $url2195 2195`
port2196=`nc -z $url2196 2196`
port5223=`nc -z $url5223 5223`
port443=`nc -z $url443 443`

# Set array
range=( port2195 port2196 port5223 port443 )

echo ""
echo "Now checking the ports..."
echo ""

for i in "${range[@]}"
do
	if [[ "$i" = *succeeded* ]];
	then
		echo "SUCCESS: $i is open/working."
	else
		echo "FAILURE: $i is NOT open/working."
	fi	
done

this code works just fine.... just not in the way that i want!

the script is passing the members of the array exactly as i've typed them, one at a time: port2195, port2196, port5223, etc.

what i'm trying to do and failing to achieve is to pass each member of the array as a previously established string: $port2195, $port2196, $port5223, etc.

changing the names of each member of the array to include a "$", doesn't work, neither does adding a second "$" into the if statement.

i'm not even sure if you can pass strings via an array but, if it is possible, much obliged for someone to drop me a few pointers.

cheers,
# 2  
Old 12-01-2014
There's at least two workarounds for that assignment statement:
Code:
- eval range=( $port2195 $port2196 $port5223 $port443 ) # deprecated as eval can be dangerous (see many a post in these fora)
- range=( $(echo $port2195 $port2196 $port5223 $port443 ))

# 3  
Old 12-01-2014
How about something like this:
Code:
url[2195]=foo
url[2196]=bar
url[5223]=baz
url[443]=bay

range="2195 2196 5223 443"
echo ""
echo "Now checking the ports..."
echo ""
for i in $range
do
  if [[ $(nc -z "${url[i]}" $i) == *succeeded* ]];
  then
    echo "SUCCESS: $i is open/working."
  else
    echo "FAILURE: $i is NOT open/working."
  fi	
done


Last edited by Scrutinizer; 12-01-2014 at 06:39 PM..
# 4  
Old 12-01-2014
i appreciate the quick response!

if the range command (which i'd never heard of) is more effective at passing a list of values than the array command, why bother using the array command? it certainly seems like range is able to accomplish what array can do and then some.

the script works as i'd intended now, so to both of you, again: thank you.
# 5  
Old 12-02-2014
There is no range command.
Code:
range="2195 2196 5223 443"

A string with spaces is assigned to a simple variable.
Code:
for i in $range

The unquoted $range lets the shell break the string into tokens, so there is a loop over all tokens.
Of course you can directly do
Code:
for i in 2195 2196 5223 443

---------- Post updated at 04:13 AM ---------- Previous update was at 04:03 AM ----------

An alternative technique:
Code:
while read url port
do
  if [[ $(nc -z "$url" "$port") == *succeeded* ]]
  then
    echo "SUCCESS: $port is open/working."
  else
    echo "FAILURE: $port is NOT open/working."
  fi
done << URL_PORT
foo 2195
bar 2196
baz 5223
bay 443
URL_PORT

# 6  
Old 12-02-2014
It looks like you have to bind stderror with stdout to get the output into a variable.
2>&1

# URL port Variables
Code:
port2195=`nc -z $url2195 2195 2>&1`
port2196=`nc -z $url2196 2196 2>&1`
port5223=`nc -z $url5223 5223 2>&1`
port443=`nc -z $url443 443 2>&1`
port25=`nc -z localhost 25 2>&1`

Use $var so they will get expanded by bash.
Code:
# Set array
range=( $port2195 $port2196 $port5223 $port443 $port25 )

If you want an array for record keeping or logging or whatever, building the array in a loop might be better...

Code:
url[2195]=localhost
url[2196]=localhost
url[5223]=localhost
url[443]=localhost
url[25]=localhost


port[2195]=""
port[2196]=""
port[5223]=""
port[443]=""
port[25]=""

echo
echo "Now checking the ports..."
echo

# fill port array in a loop
for i in ${!url[*]}  # !url returns the index of the array, not the value.
do
    port[$i]=$(nc -zv ${url[$i]} $i 2>&1)
    if [[ ${port[$i]} =~ *succeeded* ]]; then
        echo "SUCCESS: port $i is open/working."
    else
        echo "FAILURE: port $i is NOT open/working."
    fi
done


Last edited by Scrutinizer; 12-02-2014 at 02:54 PM.. Reason: Doh! the arrow goes > that away; mod: extra code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Unable to mount previously-working NFS share from NIM to LPAR

Right, now that I've finally worked out this website, I'll ask my question! I am having an absolute nightmare with NFS on AIX. I have used it many times, and I know what I'm doing, however I cannot fathom what is going on here. I have 2 LPARs, sitting on the same physical host. They are... (12 Replies)
Discussion started by: tmooredba
12 Replies

2. Shell Programming and Scripting

Adding an element to a bash array with a variable

Hello, I have a simple task and I am having some trouble with the syntax. I have a variable with an assigned value, CMD_STRING='-L 22 -s 0 -r -O -A i -N 100 -n' I would like to add that variable to an array. As far as I have been able to look up, the syntax should be something like, ... (4 Replies)
Discussion started by: LMHmedchem
4 Replies

3. Shell Programming and Scripting

Bash script set command to a variable

Hi, Will following set up work in bash script? I've got errors if assigning following binary command to a variable. But on the other hand, COMMAND="ls" works. Any explanation please? How can I assign binary command to a variable COMMAND then I can just call ${COMMAND}? COMMAND="rsync"... (3 Replies)
Discussion started by: hce
3 Replies

4. Shell Programming and Scripting

How to get value from array and set those values as a variable

I am new to ksh scripting, specially array. How do i get values from an array and set the value as variable and pass those variables to the different functions?? someone taught me how to get input from a file with have columns i need to read, but now i doesnt know how to set those value to be a... (7 Replies)
Discussion started by: gavin_L
7 Replies

5. Shell Programming and Scripting

Creating bash array name from variable

Hi gurus, I need to create arrays from variables, via a loop. The issue I have is with the array name creation. How do I use a variable to define an array? I want to do something like declare -a $H where $H is my loop variable. I then need to add items to each array I've created,... (3 Replies)
Discussion started by: melias
3 Replies

6. Shell Programming and Scripting

bash variable (set via awk+sed) not working as expected

Hi! Been working on a script and I've been having a problem. I've finally narrowed it down to this variable I'm setting: servername=$(awk -v FS=\/ '{ print $7 } blah.txt | sed 's\/./-/g' | awk -v FS=\- '{print $1}')" This will essentially pare down a line like this: ... (7 Replies)
Discussion started by: creativedynamo
7 Replies

7. Shell Programming and Scripting

BASH - set specific user variable via string operators

Apologies for the utter triviality of this question, but we all have to start somewhere! I've also tried searching but this question is pretty vague so I didn't (a) really know what to search for or (b) get many relevant hits to what I did search for. Anyway, I'm in the process of self-teaching... (1 Reply)
Discussion started by: u5j84
1 Replies

8. Shell Programming and Scripting

saving awk value in a bash array variable

hi all i am trying to save an awk value into an array in bash: total=`awk '{sum+=$3} END {print sum}' "$count".txt"` ((count++)) the above statement is in a while loop.. $count is to keep track of file numbers (1.txt,2.txt,3.txt,etc.) i get the following error: ./lines1:... (1 Reply)
Discussion started by: npatwardhan
1 Replies

9. Shell Programming and Scripting

How to check if two variable are empty strings at once? (bash)

I need to check if $1 or $2 are empty before continuing but I don't know if bash has any logic of the sort. This is what I'm looking for - except that "and" doesn't seem to work. if and ;then ... Thank you! :D (4 Replies)
Discussion started by: ph0enix
4 Replies

10. Shell Programming and Scripting

[bash] Check if variable is set or blank

Hello guys. In my script, i have the following code: echo "The tarfile contains these directorys" tar -tf file.tar > tarlist.txt cat tarlist | awk -F/ '{print $1 "/" $2}' | nl echo "Enter path of the directory you want to extract or just press enter to extract everything: " read path... (1 Reply)
Discussion started by: noratx
1 Replies
Login or Register to Ask a Question