Solved

How to add unique folder name to a array in Bash

Posted on 2014-01-27
2
418 Views
Last Modified: 2014-02-01
Following is the details of my program:
containsElement: function which will decide if the 2nd arg contains the 1st arg or not

#!/bin/bash
containsElement () {
  local e
  for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
  return 1
}

    CL_HOME=${CL_HOME:=/var/lib/data/omfs/certvs/crls/current/crl}
       ARRAY=()
        for i in `find ${CL_HOME} -iname "*.crl"`
          do
            mydir=`dirname $i`
            containsElement "${mydir}" "${Array[@]}"
            if [ $? -eq 1 ]; then
                ARRAY+=("${Array[@]}" "{mydir}")
            fi
        done

        for mydir in `echo ${Unix[@]}`
        do
        #do something with teh variable $mydir
             echo ${mydir}
        done
But all the folders are getting added and i am left with a array with duplicate entries.
0
Comment
Question by:pvinodp
2 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39811967
ARRAY=(`for i in ${ARRAY[@]} ; do echo $i ; done | sort -u`)
0
 

Author Closing Comment

by:pvinodp
ID: 39827307
thats a crisp solution.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question