help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Putting escaped elements in an array


From: Jesper Nygårds
Subject: Re: [Help-bash] Putting escaped elements in an array
Date: Mon, 28 Apr 2014 19:05:58 +0200

Thank you so much, Eduardo, for the very detailed feedback. I really
appreciate it.

However, I now face three problems which I cannot resolve even after your
helpful suggestions:

1. I can't get the 'currentdir=$(readlink -e .; printf x)
currentdir=${currentdir%?}' line to work. It always leaves $currentdir as a
two-line value, i.e. $currentdir is set to the directory plus an empty
line. Am I missing something?

Apart from the problem above, your simplification of my code works very
well, but I now have two more things that I want to do:

2. I want to only keep unique values in my array $dirstack, but without
resorting it. In my original solution, this line works as I would like it
to:

eval dirstack=( $(printf "%q\n" "address@hidden" | awk '!x[$0]++') )

But I can't get it to work without the eval in there. Without it, quoted
spaces get lost in successive additions to the array. Is there a way to
simplify it?

3. I only want to keep a limited number of elements in array (say 20).
Again, I can't get it to work without an eval thrown in:

eval dirstack=( $(printf "%q " "address@hidden:0:20}") )

So for reference, this is what my function looks like after your
suggestions and my additions (but I have removed the trick referenced in 1.
above, since I couldn't get it to work):

_push_dir() {
  local currentdir
  currentdir=$(readlink -e .)
  if [[ -d $lastdir && $lastdir != "$currentdir" ]]; then
    dirstack=("$lastdir" "address@hidden")
    eval dirstack=( $(printf "%q\n" "address@hidden" | awk '!x[$0]++') )
    if (( address@hidden > 20 )); then
        eval dirstack=( $(printf "%q " "address@hidden:0:20}") )
    fi
  fi
  lastdir=$currentdir
}

I test it with a directory structure like the following:
mkdir -p /tmp/dir\ one/dir\ two
cd /tmp
cd dir\ one
cd dir\ two
cd


reply via email to

[Prev in Thread] Current Thread [Next in Thread]