-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

2

1 Answer 1

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}"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.