1

Below is a sample code:

arg[1]="shell"
arg_value[1]=10
agr[2]="script"
agr_value[2]=50

command skbit ${agr[1]}="${arg_value[1]}" ${agr[2]}="${arg_value[2]}"

skbit is a script that accepts arguments in above format.

Now if there are multiple arguments that depends on the run time. How to create above command dynamically.

I tried with,

command skbit $str

where str= ${agr[1]}="${arg_value[1]}" ${agr[2]}="${arg_value[2]}" and so on.

this structure is not working for command for multiple reason.

I want a code of below structure:

command skbit {A code which will generate arguments dynamically}
2
  • Why agr and arg? Why arg_value and agr_value? Commented Jan 7, 2019 at 12:21
  • BashFAQ #50 is a good place to start. Commented Jan 7, 2019 at 14:36

1 Answer 1

1

Are you maybe looking for something like this:

# define some arguments and values
arg[1]=shell
arg_value[1]=10
arg[2]=script
arg_value[2]=50

# joins the arguments and its values
for i in "${!arg[@]}"; do
  args+=( "${arg[i]}=${arg_value[i]}" )
done

# calls the command with the joined arguments
command skbit "${args[@]}"

You might also use associative array instead:

#!/bin/bash
declare -A arg

arg[shell]=10
arg[script]=50

for i in "${!arg[@]}"; do
  args+=( "$i=${arg[$i]}" )
done

command skbit "${args[@]}"
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.