dynamically allocate variables in bash - bash

I've encountered a problem in my bash script.
I need to assign new variables according to files in my folder and assign them a number according to the amount of arguments the script gets (whether it's a script or not).
I'm trying to get a script written like this:
n_${array[*]}=`arg_count ${array[*]}`
while arg_count checks how many parameters a script gets.
for further use, I'm going to change those variables if there's a function with different arguments needed.
Thanks in advance!

In general, you can use the declare builtin to accomplish this, because it is a command whose argument is a string that resembles an assignment.
declare "n_${array[*]}=$(arg_count ${array[*]})"
However, note that unless you set IFS appropriately and the array contents are amenable, the expansion of ${array[*]} isn't going to be a string that forms part of a valid identifier.
You probably want to either use an associative array,
declare -A n
n[${array[*]}]=$(arg_count ${array[*]})
or write your code in a programming language that properly supports data structures.

Related

How to pass infomation to POV-Ray from a shell script

How do I pass a variable from a shell script to POV-Ray? My desire would the ability to pass a variable as a command-line argument and then use that value in the .ini file or .pov file
Something like
POV-Ray +pass var=$imageNumber file.pov
And then being able to use var in POV-Ray
I realize that I could edit the .ini and .pov files in the script or use modulus to use the single frame variable as two variables, but both those solutions seem awkward.
I want to generate 1000s of extremely similar scenes. Each scene is exactly the same except that a heightmap uses a different image file as its source. Normally, I would use the animation tools in POV-Ray to generate multiple frames. However, I am already using the animation tools to cycle over a different property in each scene.
For *nix systems, use POV-Ray's file handling system to open the standard-in file in your .pov file
#fopen STDIN "/dev/stdin" read
#read (STDIN, var1, var2)
This will read from the standard-in for a comma separated list of POV literals. However, POV-Ray doesn't handle reading from a pipe; Thus, use herestrings (or heredocuments if you must use only sh compatible syntax) to fill stdin for POV-Ray.
For example, if run in the shell (works for bash):
povray "example.pov" <<<'"hello","world"'
Will fill the variables var1 and var2 from above with the values "hello" and "world" respectively. Note that quotes must be included around each string value in the list. This is because POV wants POV literals in the 'file' we are passing.
If you want to use an .ini file instead, just call the .ini file in place of the .pov file and everything will work as expected.
If you want more or less variable to be passed to the POV file, add or remove variable names from the #read directive and extend or trim the number you are passing to the same length.'
You can also pass shell variables like this. If foo contains "hello" including the quotes, and "example.pov" is expecting one string in the herestring, then
povray "example.pov" <<<$foo
will pass hello to the variable in the #read directive.
Additionally, you can other POV literals than stings, in that case use the relevant POV syntax the that literal type. However, you can't put POV expressions into the herestring. See the wiki page for more information.
As of POV-Ray 3.7 you can now declare constants in the INI file, and therefore the command line, with Declare=MyValue=24. This would be the same as a #declare MyValue=24; in a scene file. The value on the right-hand side must be a constant float value.
see the relevant manual entry
As long as you don't pass fractional values (or use extremely large sequence numbers), you should be able to use this as a component in the file name.

How does the # work in Bash?

When I open BashBurn source I se:
mainmenu[7]="$bb_menu_8#check_path"
I don't know what does #check_path mean? And how does it work?
It doesn't mean any special in Bash alone: # is just a part of a string here. It could have a special meaning inside the BashBurn implementation, however.
It looks like it is a part of the translation framework used inside BashBurn. You can search it on your own on GitHub for example.
mainmenu is set in BashBurn.sh, which is then passed as the second argument to the bbmenu function. The function is defined in bbmenu.sh. There is a description of the syntax, including this information:
ALL ITEMS ARE SEPERATED BY AMPERSAND.
All you need to do now is to read the rest of the documentation in the bbmenu.sh file.

Why bash indirect expansion has to use temp variable?

From https://stackoverflow.com/a/10820494/1764881, I know that the standard way of doing it seems to be:
var="SAMPLE$i"
echo ${!var}
But, I can't seem to do any of these following forms. They all failed:
echo ${!SAMPLE$i}
echo ${!"SAMPLE$i"}
I read the bash man page, but I still couldn't understand. Is it true that the first form is the only form accepted?
Yes. The underlying logic is that all parameter expansions take a single, literal word as the name of the parameter to expand, and any additional operator does something to the result. ! is no exception; var is expanded as usual, but the result is expanded again.
(As an aside, even arrays follow this rule. It might seem that something like ${array[2]%foo} applies two operators to array, but really array[2] is treated as the name of a single parameter. There is a little difference, as the index is allowed to be an arbitrary arithmetic expression rather than a literal number.)
(And for completeness, I should mention the actual exceptions, ${!prefix*} and ${!name[*]}, which confusingly use the same operator ! for querying variables themselves. The first lists variable names starting with the same prefix; the second lists the keys of the named array.)

Bash string manipulation, extracting/removing parts

I'm modifying an old bash file and am having some trouble manipulating strings. The problem is that the strings can be anything random to the left of _<date>.<num>. For example, from ThisIsAString-Sub_tag_150827.1, I need to extract _150827.1. In bash, this seems very difficult to do. In any other language, I would split on _, and just grab the last element of the list. How do I do this in bash? I've tried a few different ways (including with awk), but cannot seem to get it right.
With bash's Parameter Expansion:
a="ThisIsAString-Sub_tag_150827.1"
echo "${a##*_}"
Output:
150827.1

How to parse a word in shell script

I want to parse the following word in shell script
VERSION=METER1.2.1
Here i want to split it as two words as
WORD1=METER
WORD2=1.2.1
Let me help how to parse it?
Far more efficient than using external tools such is sed is bash's built-in parameter expansion support. For instance, if you want the name variable to contain everything until the first number, and the numbers variable to contain everything after the last alpha character:
version=METER1.2.1
name=${version%%[0-9]*}
numbers=${version##*[[:alpha:]]}
To understand this, see the BashFAQ entry on string manipulation in general, or the BashFAQ entry on parameter expansion in particular.

Resources