0

I have an array of string foo bar baz lorem ipsum

I'm passing this array to another bash script by appending a random password from output of openssl rand -hex 3

Expected array output is like foo:a6f bar:0o0 baz:5!3 ....

How can I achieve this?

1
  • Loop on your array items, call your openssl for each element, append the array item, : and your newly created password to a string, call the script with that new string. Commented Aug 30, 2022 at 16:09

1 Answer 1

1

Here is one idea.

Expected array output is like foo:a6f bar:0o0 baz:5!3 ....

#!/usr/bin/env bash

array=(foo bar baz lorem ipsum)

for i in "${array[@]}"; do
  IFS= read -r rand < <(openssl rand -hex 3) &&
  new_array+=("$i":"$rand")
done

printf '%s\n' "${new_array[*]}"

I'm passing this array to another bash script by appending a random password from output of openssl rand -hex 3

Using a separate script and feeding the array.

The script named myscript

#!/usr/bin/env bash

new_array=()

for f ; do
  IFS= read -r rand < <(openssl rand -hex 3) &&
  new_array+=("$f":"$rand")
done

printf '%s\n' "${new_array[*]}"

At the command line.

initialize the array of strings.

array=(foo bar baz lorem ipsum)

Feed it against myscript.

myscript "${array[@]}"

Not sure how you would incorporate the code above in your script though, since we don't know where are the strings is coming from.

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.