Score:-3

Separating list of file and directory names

jp flag

Can one simplify or make clearer the following loop. I loop through a sequence of file names and directories stored in parg, separating the valid names from the invalid ones. I also remove duplicates. fdir contains the valid entries, whereas fda and dra contain the valid files and valid directories respectively.

declare -A tag       # Prescribe an Associative Array
for fda in "${parg[@]}"; do
  [[ -f "$fda" ]] || [[ -d "$fda" ]] || continue  # Invalid Entry
  [[ ${tag[comint:${fda}]+E} ]] && continue       # Existing Entry
  tag[comint:${fda}]=1
  fdir+=( "$fda" )
  [[ -f "$fda" ]] && fla+=( "$fda" )
  [[ -d "$fda" ]] && dra+=( "$fda" )
done
ru flag
This sounds like a pure programming question - pure programming questions usually are better suited for StackOverflow.
muru avatar
us flag
Just using better variable names will make this much clearer, but what constitutes "clearer" is a matter of opinion.
Artur Meinild avatar
vn flag
To me, it seems like you're asking for QA on your code - and in this case (as muru said), that's a matter of opinion.
Score:0
jp flag

Rephrasing your question:

I have filenames and directory names (some of which are duplicates and some others are nonexistent) stored as elements in an array(original) ... In bash, how can I test and process the elements in the original array into a new array refined excluding duplicate and nonexistent filenames/directory names ... And at the same time creating two new arrays files(containing only refined filenames) and directories(containing only refined directory names)?

Issue reproduction:

# Create some test files
$ touch file{1..3}

# Create some test directories
$ mkdir dir{1..3}

# List current working directory contents
$ printf '%s\n' *
dir1
dir2
dir3
file1
file2
file3

# Add contents to an array
$ for i in *
  do
  original+=( "$i" )
  done

# Add contents to the same array again to generate duplicates
$ for i in *
  do
  original+=( "$i" )
  done

# Add some invalid entries
$ original+=( "file4" "dir4" "file5" "dir5" )

# Print the array
$ printf '%s\n' "${original[@]}"
dir1
dir2
dir3
file1
file2
file3
dir1
dir2
dir3
file1
file2
file3
file4
dir4
file5
dir5

Possible solution:

# Test, refine and classify array items
$ for i in "${original[@]}"
  do
  if [[ ! "${refined[*]}" =~ "$i" ]]
    then
    [ -f "$i" ] && refined+=( "$i" ) && files+=( "$i" )
    [ -d "$i" ] && refined+=( "$i" ) && directories+=( "$i" )
    fi
  done

# Print the "refined" array
$ printf '%s\n' "${refined[@]}"
dir1
dir2
dir3
file1
file2
file3

# Print the "files" array
$ printf '%s\n' "${files[@]}"
file1
file2
file3

# Print the "directories" array
$ printf '%s\n' "${directories[@]}"
dir1
dir2
dir3
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.