This question already has answers here:
Multi-dimensional arrays in Bash
(13 answers)
Closed 9 years ago.
i have a script which get datas from a file, but some parameters have more values than just one.
How can i make that my bash script can read as example:
user[3][1]=test1
user[3][2]=test2
Actually i make:
for i in $(seq ${#lala[#]}); do
${user[$i]}
done
which works. I have try it with ${user[$i][$i]} and seq ${#lala[#][#]}) but i cant get it running.
The script must then work with just [$i] and [$i][$i].
Can please somebody help me out, thanks a lot!
Well you're using wrong language/script, since BASH doesn't support multi-dimensional arrays. Having said that you can store a delimited text in each element of array that you can further break into array inside a loop.
Related
This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 2 years ago.
I need to setup an array and use that to loop through and list the files. I started with this setup and it works.
file = (x1 x2 x3 x4 x5)
for var in ${file[#]}; do
echo $var
done
I have a requirement to add the timestamp on these variables, I tried a few ways and none of the seem to work. Here is one way I tried to define the array:
today=$(date +"%Y%m%d")
file = (x1_"$today" x2_"$today" x3_"$today" x4_"$today" x5_"$today")
What would be the best way to define the array, where the array element has a variable?
The way you're using "$today" is correct, no problem there.
Don't put spaces around the equal sign. It needs to be file=(...) in both cases. Spaces are not allowed in assignments.
You can use Shell Check to catch these kinds of errors. Here's what it prints:
file = (x1_"$today" x2_"$today" x3_"$today" x4_"$today" x5_"$today")
^-- SC2283: Remove spaces around = to assign (or use [ ] to compare, or quote '=' if literal).
^-- SC1036: '(' is invalid here. Did you forget to escape it?
^-- SC1088: Parsing stopped here. Invalid use of parentheses?
This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Bash arbitrary glob pattern (with spaces) in for loop
(2 answers)
Closed 2 years ago.
I'm trying to read a bash array file by file but for files with spaces in them, it comes out as two separate files. How would I solve this?
file names:
file A
fileB
file_C
the output Im getting is like this
File
A
FileB
File_C
What Im trying to do is to get File A on one line
I know I can read the array at once but for now we need to do it with a for loop since we're gonna be doing operations to it later in the loop.
for i in "${array[#]}"; do
echo "$i"
done
This question already has answers here:
How can I join elements of an array in Bash?
(34 answers)
Closed 3 years ago.
How can I control how an array separates its elements? If you do:
echo ${array[*]}
it displays:
element1 element2 element3
and I would like it to be:
element1:element2:element3
Elements are joined by the first character of $IFS (internal field separator).
(IFS=':'; echo "${array[*]}")
Modifying $IFS has a lot of side effects. I recommend only changing it for a short duration inside a subshell so the rest of your script isn't affected.
This question already has answers here:
Printing lines from a text file in reverse order
(2 answers)
Closed 5 years ago.
Im trying to read a file not just from the last line but from the end of the line to the beginning too. Example:
Im trying
to read a file
How do I want to read:
elif a daer to
gniyrt mI
How do I do that?
You read each line, put all in a buffer ..reverse it and then parse.
The thing is most of the time it is not needed to read the file in reversed manner..But you can try this.
Some nice organized manner of doing this will be..using a LIFO data structure.
You can use a simple array and make it behave like a stack. Push everything then pop from top.
This question already has answers here:
How can I run an external program from C and parse its output?
(8 answers)
Closed 7 years ago.
I want to save the output from a bash script which is invoked from a C program to a variable declared in the C program. I searched and tried successfully calling a script using system, and I tried this, but it didn't work:
char* a;
system("a=`ls`");
printf("%s",a);
Use popen() system call. You can pass the cmd as the parameter. You will get the command output as text when the function returns. Hope this helps.