-1

i trying to write a bash script to get output 'pkg vim, size small, size medium, size large' which will be passed as argument to another command.

arr_size=(small medium large)
arr_pkg=(vim)
for j in "${arr_pkg[@]}"
      do
   echo  -n pkg "$j"
      done
    for i in "${arr_size[@]}"
  do
       echo -n , size $i
  done

executing the script gives output as pkg vim, size small, size medium, size large

i need some suggestion on how to pass this output to another command or variable so i can be used as argument for another command.

final command will be as shown below.

grun --file 123.txt --tt='pkg vim, size small, size medium, size large'

Any other suggestion will also be helpful

mebb
  • 51
  • 6
  • You will store the output of the first command to a variable and use this variable as argument, or in one line `-tt="$()"`, see: https://stackoverflow.com/questions/4651437/how-do-i-set-a-variable-to-the-output-of-a-command-in-bash https://stackoverflow.com/questions/2314750/how-to-assign-the-output-of-a-bash-command-to-a-variable https://stackoverflow.com/questions/9768228/bash-script-store-command-output-into-variable etc – thanasisp Sep 29 '20 at 13:18
  • Does this answer your question? [How do I set a variable to the output of a command in Bash?](https://stackoverflow.com/questions/4651437/how-do-i-set-a-variable-to-the-output-of-a-command-in-bash) – thanasisp Sep 29 '20 at 13:25

1 Answers1

0

You could append each string to a variable, eg:

pfx=""                              # start with a null prefix string for first element
arglist=""                          # start with empty arg list

arr_size=(small medium large)
arr_pkg=(vim)

for j in "${arr_pkg[@]}"
do
    arglist+=${pfx}"pkg ${j}"
    pfx=', '                        # use ', ' as prefix for rest of the args
done

for i in "${arr_size[@]}"
do
    arglist+=${pfx}"size ${i}"
    pfx=', '                        # use ', ' as prefix for rest of the args, in case arr_pkg was empty
done

With the final result being:

$ echo "${arglist}"
pkg vim, size small, size medium, size large

Which can be used in your follow-on command like such:

$ grun --file 123.txt --tt="${arglist}"
markp-fuso
  • 8,786
  • 2
  • 8
  • 24