check if contents of an array is empty


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting check if contents of an array is empty
# 1  
Old 02-16-2012
check if contents of an array is empty

hi,

I have this array

Code:
a=("" "" "")

Is there a command that I can use to check if all members of the are empty?
I really don't like to use for or while loop.

thanks!

---------- Post updated at 02:23 PM ---------- Previous update was at 02:15 PM ----------

wait, I got it already

Code:
i=echo ${a[@]} | wc -w
if [ $i -eq 0] ; then
    echo empty
fi

anyway, any other solutions will be appreciated
# 2  
Old 02-16-2012
Another way :
Code:
set -- ${a[*]}
if [ $# -eq 0 ] ; then
    echo empty
fi

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Looping through the contents of array for output name

Hi all, I am trying to loop through the string contents of an array, to add it during the saving of the output files. I am trying this code to print each column and save it to unique file name, but it doesn't work. Thanks for any help. fnam=(japan usa uk) alldata.dat contained sample data... (1 Reply)
Discussion started by: ida1215
1 Replies

2. Shell Programming and Scripting

Display array contents on a new line

ksh eg arrayname=(1 2 3 4 5) I'm trying to display the individual contents of an array on a new line without using a loop, using one line of code. output 1 2 3 4 5 (3 Replies)
Discussion started by: squrcles
3 Replies

3. UNIX for Dummies Questions & Answers

[ksh93+] Array fed by function is empty when used in main.

I feel that i am missing something obvious but i can't find what is wrong. I have a script that is launching some functions with "&" and each call is feeding the array with a value. When all calls are finished I just want to retrieve the values of that array. It is looking like that : ... (5 Replies)
Discussion started by: bibou25
5 Replies

4. Shell Programming and Scripting

Write array contents to file

Hi, I have a bash script that currently holds some data. I am trying to write all the contents to a file called temp.txt. I am using echo ${array} > temp.txt The problem that I am experiencing is that the elements are being written horizontally in the file. I want them written... (5 Replies)
Discussion started by: Filter500
5 Replies

5. Shell Programming and Scripting

Empty Multiple files contents

I would like to empty multiple files contents (without delete the file) which have similar name to begin with. e.g. log1.txt log2.txt log3.txt log4.txt I know cat /dev/null will do the job but that only apply to a single file. But when i tried cat /dev/null > {} \; that doesnt do... (7 Replies)
Discussion started by: kin
7 Replies

6. Shell Programming and Scripting

Empty Directory Check

Hi All, I have a requirement to check all the files in a directory and mail non empty files Files are in csv format , i need to skip header while checking pls help Thanks (12 Replies)
Discussion started by: gwrm
12 Replies

7. UNIX for Dummies Questions & Answers

Display the contents of an array

Hi i have just registered So i am at university studying forensic computing and we have to learn c++ i have never done anything with c++ before and i am abit stuck i need to create a programme to display the contents of an array of characters forwards and in reverse Can anyone help me... (1 Reply)
Discussion started by: RossMc
1 Replies

8. UNIX for Dummies Questions & Answers

compare array contents with file

I have an array "arrA" with the following contents: A0012 Paint Shop.doc ES001 Contract Signature.doc Budget Plan.pdf TS PWS.pdf My data file "Files.dat" has the same contents: A0012 Paint Shop.doc ES001 Contract Signature.doc Budget Plan.pdf TS PWS.pdf I have a script that compares... (0 Replies)
Discussion started by: orahi001
0 Replies

9. Shell Programming and Scripting

How to empty file contents w/o rm and touch

Hi all, Can any one tell me how to empty the contents of a file in unix without using the following sequence : rm <file> ; touch <file> is there any command in unix?? is it possible through shell scripting? If so, how? -krishna (7 Replies)
Discussion started by: kris_kris
7 Replies

10. Shell Programming and Scripting

Can I assign the contents of file into an array?

I am trying to assign the contents of file e.g ls "$HOME" into an array. If it is possible then please guide me without using the concept of awk,sed, and perl16 Thanks (10 Replies)
Discussion started by: murtaza
10 Replies
Login or Register to Ask a Question