0

I'm currently using this code:

target_directory_greater="/root/*/"

if [ "${target_directory}" != "${target_directory%%\***}" ]; then
    base_target_directory="${target_directory%%\***}"
    prefix_target_directory="${target_directory##**\*}"

    for t in $base_target_directory/* ; do
        if [ -d "${t}${prefix_target_directory}" ]; then
            # Found, replace target_directory with real path
            target_directory="${t}${prefix_target_directory}"
            break
        else
            continue
        fi
    done
fi

if [ ! -d "${target_directory}" ]; then
    printf "${target_directory} directory wasn't found.\\n\\n"
    exit 1
fi

My script will move two folders if grep finds the correct folder which it does. Once grep finds the correct folder for the files to move to, that code above kicks in to process the * character in my declared variable and it simply just uses the first folder instead of the one grep pointed to. Does anyone see my mistake there?

/root folder has a few other randomly named folders inside there and I use grep to grab a specific string and then move the files solely to that folder. But since there's a *, it's causing issues.

SLePort
  • 15,211
  • 3
  • 34
  • 44
Anthony
  • 1
  • 4
  • You define `target_directory_greater`, but then use `target_directory`. Is that intentional? – Gordon Davisson Apr 24 '16 at 04:48
  • Yes, it is, unless you spot a mistake with it..? – Anthony Apr 24 '16 at 11:50
  • It's hard to tell without more information -- what's `target_directory` set to, and what is the script doing that's different from what you expect. Also, try putting `set -x` before the problematic section (and `set +x` after it) to see what's actually going on. See [How to debug a bash script](http://stackoverflow.com/questions/951336/how-to-debug-a-bash-script). – Gordon Davisson Apr 24 '16 at 21:08
  • target_directory is not specified completely, we use grep to find the directory we want and grep does find the right directory, but then that part of the code messes up and just uses another directory instead. :p Shall I pastebin the full source code? – Anthony Apr 24 '16 at 22:17
  • That shouldn't be necessary, just include what `target_directory` is set to before this snippet executes, and what it's set to afterward (i.e. what other directory is it finding). Including an execution trace set `set -x` would also help a great deal. – Gordon Davisson Apr 24 '16 at 22:29
  • Here's the set -x output: https://ghostbin.com/paste/a93ek It finds the string in my target folder which is 2FD412E0* and then the code above runs and switches it completely and just picks the first folder it finds. – Anthony Apr 25 '16 at 00:55
  • According to the trace, the folder that `grep` finds a match in (/var/root/2FD412E0-...) never gets recorded anywhere. Is it supposed to be? Is `target_directory` supposed to be set to it? If so, the problem is that it's not being set after `grep` matches, not that this code is switching it. – Gordon Davisson Apr 25 '16 at 06:02
  • Any idea how we can set to it so that when the files move, they move to the 2FD412E0- folder (the one grep finds a match in) and not the others? – Anthony Apr 25 '16 at 10:03

0 Answers0