1

I have a bash array comprised of names of files, say:

foo = "foo.file"
bar = "bar.file"
declare -a foobar=( $foo $bar )

foo and bar are organized in a directory structure as subdirectories:

/maindirectory/foo/foo.file
/maindirectory/bar/bar.file

I would like to loop through the array, generating link targets to the complete path, for example:

maindirectory="/maindirectory/"
target = $maindirectory(something referential to the name of the variable here)$foo
link = foo
ln -s target link

So the goal is to encode the path (and name of the link) as the variable name and the name of the file as the value of the variable.

currently I do this in a harder-to-keep-track-of-way in which I have two arrays declared: one with the path to the file and the other with the names of links. I can loop through:

 for ((i=0; i<${#foobarname[@]}; i++)); do
    link=$workingdirectory${foobarname[i]};
    target=$maindirectory${foobarpath[i]};
    ln -s "$target" "$link";
 done

but as I said before, I would rather do this with only one array.

Is there a way to encode an array of variable elements, and then spit out the name of the variable element instead of the value of the variable element?

7
  • Sounds impractical, why do you need this? Commented Oct 27, 2019 at 21:12
  • this approach reduces the need to update many fields by hand and reduces the rate of error. We're talking about a very large number of files. Commented Oct 27, 2019 at 21:18
  • declare -A foobar=( [foo.file]=/maindirectory/foo/foo.file ) ? Commented Oct 27, 2019 at 21:21
  • Or perhaps declare -A foobar=( [foo]=foo.file [bar]=bar.file ) Commented Oct 27, 2019 at 21:25
  • I don't understand. What is wrong in $maindirectory/foo/? Or just declare -a foobar=( foo/$foo bar/$bar ) ? Commented Oct 27, 2019 at 21:25

2 Answers 2

1

If every file X.file is in folder X, you can use ${X%.file}/$X to find the full path.

Sign up to request clarification or add additional context in comments.

Comments

0

First of all, even if we store the file paths to variables, you still need instruct bash which variables to loop through, right? Thus, you need a list of variables.

I am not quite sure this is what you want, but, for example:

#!/bin/bash

maindirectory="/MyFolder"

foo=file1
bar=file2
foobar=file3
barfoo=file4

# need a list so that we can loop thru
namelist="foo bar foobar barfoo"

for link in $namelist; do
    file=`eval echo \\$$link`
    target=$maindirectory/$link/$file
    echo ln -s $target $link
done

It's a test code. When you have done the test, remove echo, so that it does the real task to do a symbolic link.

Or, if your file name is exactly the same as its variable, except for the extension ".file", there is a more simpler version:

#!/bin/bash

maindirectory="/MyFolder"

for link in "foo bar foobar barfoo"; do
    target=$maindirectory/${link}/${link}.file
    echo ln -s $target $link
done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.