40427

Using a variable name to create an array bash, unix

Question:

First I should perhaps explain what I want to do...

<ul><li>I have 'n' amounts of files with 'n' amount of lines. All I know is that the line count will be even.</li> <li>The user selects the files that they want. This is saved into an array called ${selected_sets[@]}.</li> <li>The program will print to screen a randomly selected 'odd numbered' line from a randomly selected file. </li> <li>Once the line has been printed, I don't want it printed again...</li> </ul>

Most of it is fine, but I am having trouble creating arrays based on the contents of ${selected_sets[@]}... I think I have got my syntax all wrong :)

for i in ${selected_sets[@]} do x=1 linecount=$(cat $desired_path/$i | wc -l) #get line count of every set while [ $x -le $linecount ] do ${i}[${#${i}[@]}]=$x x=$(($x+2)) # only insert odd numbers up to max limit of linecount done done

The problem is ${i}[${#${i}[@]}]=$x I know that I can use array[${#array[@]}]=$x but I don't know how to use a variable name.

Any ideas would be most welcome (I am really stumped)!!!

Answer1:

In general, this type is question is solved with eval. If you want a a variable named "foo" and have a variable bar="foo", you simply do:

eval $bar=5

Bash (or any sh) treats that as if you had typed

foo=5

So you may just need to write:

eval ${i}[\${#${i}[@]}]=$x

with suitable escapes. (A useful technique is to replace 'eval' with 'echo', run the script and examine the output and make sure it looks like what you want to be evaluated.)

Answer2:

You can create named variables using the declare command

declare -a name=${#${i}[@]}

I'm just not sure how you would then reference those variables, I don't have time to investigate that now.

Using an array:

declare -a myArray for i in ${selected_sets[@]} do x=1 linecount=$(cat $desired_path/$i | wc -l) #get line count of every set while [ $x -le $linecount ] do $myArray[${#${i}[@]}]=$x let x=x+1 #This is a bit simpler! done done

Beware! I didn't test any of the above. HTH

Recommend

  • Arrays are null. Not getting information from while loop
  • a column that increments for every row where column A = number and resets to zero when A = another n
  • WPF TextBox ScrollToLine not updating if visible
  • Count wrapped lines in a textbox
  • Sending rails errors to rspec output
  • WP7 difficulties binding data to listbox itemssource - won't refresh
  • Accessing the variables from a PHP Anonymous Function
  • ASPNetCore MVC Routing Let Server Handle Specific Route
  • Ruby 1.8.6 Array#uniq not removing duplicate hashes
  • Why doesnt this Java loop in a thread work?
  • ZipList with Scalaz
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • In C what exactly happens if i use () to initialize a double dimension array instead of the {}?
  • What Makes These Two Array Adds Different?
  • Configure Spring's MappingJacksonHttpMessageConverter
  • Simple linked list-C
  • AppleScript : find open tab in safari by name and open it
  • Question about instantiating object
  • Laravel: Getting Session ID oddly truncates when using foreach
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Use of this Javascript
  • Email verification using google app script and google forms
  • Yii2: Config params vs. const/define
  • Meteor: Do Something On Email Verification Confirmation
  • C++ Partial template specialization - design simplification
  • Initializer list vs. initialization method
  • Control modification in presentation layer
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Fetching methods from BroadcastReceiver to update UI
  • Sony Xperia Z Tablet not found by adb
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Knitr HTML Loop - Some HTML output, some R output
  • Where to put my custom functions in Wordpress?
  • Large data - storage and query
  • How can I estimate amount of memory left with calling System.gc()?
  • How to get next/previous record number?
  • RestKit - RKRequestDelegate does not exist
  • Python: how to group similar lists together in a list of lists?
  • WPF Applying a trigger on binding failure
  • Hits per day in Google Big Query