How to call program with array values as arguments? - arrays

I would like to call a program by filling in the argument values with varying amounts of array values as in:
myarray=("val1" "val2" val3" ... "valn")
program -t "${myarray[0]}" -t "${myarray[1]}" -t "${myarray[2]}" ... -t "${myarray[n-1]}"
I am essentially missing some way to expand the string "-t ${myarray[i]}" n times, where n is ${#myarray[#]}.
Thanks for showing me the light!
Background:
I extract base names of files from a tab-separated file using awk filtering and store them in an array. Now I would like to run a program using these specific filenames as individual arguments of the same option.

Related

Using array from an output file in bash shell

am in a need of using an array to set the variable value for further manipulations from the output file.
scenario:
> 1. fetch the list from database
> 2. trim the column using sed to a file named x.txt (got specific value as that is required)
> 3. this file x.txt has the below output as
10000
20000
30000
> 4. I need to set a variable and assign the above values to it.
A=10000
B=20000
C=30000
> 5. I can invoke this variable A,B,C for further manipulations.
Please let me know how to define an array assigning to its variable from the output file.
Thanks.
In bash (starting from version 4.x) and you can use the mapfile command:
mapfile -t myArray < file.txt
see https://stackoverflow.com/a/30988704/10622916
or another answer for older bash versions: https://stackoverflow.com/a/46225812/10622916
I am not a big proponent of using arrays in bash (if your code is complex enough to need an array, it's complex enough to need a more robust language), but you can do:
$ unset a
$ unset i
$ declare -a a
$ while read line; do a[$((i++))]="$line"; done < x.txt
(I've left the interactive prompt in place. Remove the leading $
if you want to put this in a script.)

How to pass one argument into multiple arrays

I want my bash script to process one or multiple attachments. They're passed to the script by the following parameter:
--attachment "filename(1),pathtofile(1),fileextension(1);[...];filename(n),pathtofile(n),fileextesion(n)"
--attachment "hello,/dir/hello.pdf,pdf;image,/dir/image.png,png"
This argument is stored in inputAttachments.
How can I store them in the following array?
attachmentFilename=("hello" "image")
attachmentPath=("/dir/hello.pdf" "/dir/image.png")
attachmentType=("pdf" "png")
My thought: I need to seperate inputAttachments at every semicolon and split those parts again at the comma. But how is this done and passed into the array.
I'm not exactly sure, how you are capturing the string after --attachment argument. But once you have it in a shell variable, all you need is a two-pass split which you can accomplish in the bash shell using read and a custom de-limiter to split on by setting the IFS value
IFS=\; read -r -a attachments <<<"hello,/dir/hello.pdf,pdf;image,/dir/image.png,png"
With this the string is split on ; and stored in the array attachments as individual elements which you can visualize by doing declare -p on the array name
declare -p attachments
declare -a attachments='([0]="hello,/dir/hello.pdf,pdf" [1]="image,/dir/image.png,png")'
Now loop over the array and re-split the elements on , and append to individual arrays
for attachment in "${attachments[#]}"; do
IFS=, read -r name path type <<<"$attachment"
attachmentFilename+=( "$name" )
attachmentPath+=( "$path" )
attachmentType+=( "$type" )
done

How can I qsub a bash script in a job array varying its parameters stored in a array in bash

I have a script which I want to execute with several different arguments, I've got an array that contains all the parameter(argument) combinations ${array[i]}.
I want to be able to submit a job array using all the different argument stored in the array:
arr_length=${#submittions[#]}
qsub -t 1-$arr_length myscript <*>
*Here, I want to use the values of -t to go through my array and use the different parameters stored in it here, I don't know is it is possible.
I have read that there is a built in variable $SGE_TASK_ID.
The array contain from two to seven file paths separated by one space and an amount of arr_length elements in the array. which will be the arguments for the python script.
${!array[#]} never contains the values of the elements in the array. The contain only the indices. For the array elements use "${array[#]}" in your script as
qsub -t 1-${arr_length} myscript "${array[#]}"
E.g.
array=('foo' 'bar' 'dude')
printf '%s\n' "${!array[#]}"
0
1
2
and see the output of
printf '%s\n' "${array[#]}"

grep results in array

I have a document which contains several names of files over which I want to use grep to gather all files with the xsd extension. When I use grep with my regex pattern, I get the correct results, about 18 of them. Now I want to store these results in an array. I used the following bash code :
targets=($(grep -i "AppointmentManagementService[\.]" AppointmentManagementService\?wsdl))
Then I print the array size :
echo ${#targets[#]}
which turns out to be 80 instead of 18 since it stored only a part of one result into an array cell. How do I make sure only one result goes into one array cell?
The results probably get split over multiple cells because a character (most likely space) is interpreted as an internal field separator.
Try executing it like this:
IFS=$'\n' targets=($(grep -i "AppointmentManagementService[\.]" AppointmentManagementService\?wsdl))

(bash) How to access chunks of command output as discrete array elements?

nmcli -t -f STATE,WIFI,WWAN
gives the output
connected:enabled:disabled
which I'd like to convert to something like
Networking: connected, Wifi: enabled, WWAN: disabled
The logical solution to me is to turn this into an array. Being quite new to bash scripting, I have read that arrays are just regular variables and the elements are separated by whitespace. Currently my code is
declare -a NMOUT=$(nmcli -t -f STATE,WIFI,WWAN nm | tr ":" "\n")
which seems to sort of work for a for loop, but not if i want to ask for a specific element, as in ${NMOUT[]}. Clearly I am missing out on some key concept here. How do I access specific elements in this array?
IFS=: read -a NMOUT < <(nmcli -t -f STATE,WIFI,WWAN)
Ignacio Vazquez-Abrams provided a much better solution for creating the array. I will address the posted question.
Array's in bash are indexed by integers starting at 0.
"${NMOUT[0]}" # first element of the array
"${NMOUT[2]}" # third element of the array
"${NMOUT[#]}" # All array elements
"${NMOUT[*]}" # All array elements as a string
The following provides good information on using arrays in bash: http://mywiki.wooledge.org/BashFAQ/005

Resources