Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I stored some information about the system in an arrays .. and I tried to store the length of an array in a variable then print the value of the variable on the terminal , but there is no output!

#!/bin/bash


###################################################################################

openFilesCount=$(lsof -Fn -u teeba| sort  | uniq | grep /home | wc -l);

openPortsCount=$(lsof -Fn -u teeba| sort  | uniq | grep /home | wc -l);

readingTime=$(date +%Y-%m-%d_%T);

usersArr=($(awk -F: '$3 >= 1000 && $1 != "nobody" {print $1}' /etc/passwd) );

pidsArr=($(ps axo pid));

lastIdex=${#usersArr[@]};

###################################################################################

echo "$lastIndex";

Where is the error ? How to store the array's length in a variable ?

share|improve this question
    
your syntax looks fine, what is the error showing in terminal? –  souravc Feb 15 at 14:33
    
@souravc the result must be 3 but i got nothing !!!! –  Akari Feb 15 at 14:49
2  
check the spellings, you stored in lastIdex but calling $lastIndex –  souravc Feb 15 at 14:51
    
@souravc thank u x______x –  Akari Feb 15 at 14:56
add comment

1 Answer 1

up vote 1 down vote accepted

Your syntax is basically correct. (You don't need any of the ; there but that's not a problem.)

It seems that somehow your script is not really executed, because:

echo ${#nonexistentarray[@]}

This will always print a number that's 0 (if the array doesn't exist) or higher (if the array or the variable exists).

So in your script, if these two lines are really executed:

lastIdex=${#usersArr[@]};
echo "$lastIndex";

....... ok... There's a typo there: you're setting the value in lastIdex but then printing lastIndex... So probably that's your problem.

share|improve this answer
    
No I didn't copy this script from anywhere .. I wrote it using vim editor ... when I typed echo "${#usersArr[@]}" directly i got the right answer which is 3 ... but when i stored the length in a variable then print its value i got nothing !!!! –  Akari Feb 15 at 14:47
    
@Akari ah.... I just noticed your typo... –  janos Feb 15 at 14:53
    
thank u x______x –  Akari Feb 15 at 14:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.