2

I'm trying to upload certificates(just created) to some storage.

So I can read all certificates in my folder and want to use content of each of this file to a variable in a loop.

#!/bin/bash
dir="${0%/*}"
#for f in $(cat $dir"/"*.crt)
#  do
#    data='{"certificate_data":'"$f"'}'
#done 
url="localhost:50183/api/v0.1/Certificates"
data='{"certificate_data":'$(cat $dir"/"*.crt)'}'
echo "$data"

So I got all certificates in one time but I need to get in $data each of content of files in a loop with correct form something like:

{"certificate_data":"<certificate_data_from_file>"}
{"certificate_data":"<certificate_data_from_file>"}
......
and so on

I know that I should use another one loop but don't know how.

Be grateful for any tips!

4
  • 1
    Side note: $(cat file.txt) can be replaced with $(< file.txt). Commented Dec 17, 2018 at 11:42
  • It's not clear what you're doing with the information since your simple example keeps overwriting data. But perhaps within your same for loop you would have data_file='{"certificate_data":'"$f"'}' then data_cert='{"certificate_data":'$(< $dir"/"*.crt)'}' followed by whatever logic you want to do with $data_file and $data_cert. Commented Dec 17, 2018 at 11:46
  • ok, just try to delete for f in $(cat $dir"/"*.crt) do data='{"certificate_data":'"$f"'}' done and it's work without it, but filled all certificates in 1 "certificate_data" and I need to put 1 certificate to 1 "certificate_data" Commented Dec 17, 2018 at 12:02
  • Does it help when you change both data assignments? data="${data}{\"certificate_data\":....}" Commented Dec 17, 2018 at 12:04

1 Answer 1

1

This should do the work:

#!/bin/bash
for f in ./dir/*.crt
do
  data='{"certificate_data":"'"$(< "${f}")"'"}'
  echo "${data}"
done

Test:

$ ls ./dir/*
./dir/cert1.crt  ./dir/cert2.crt
$ cat ./dir/*
I am certificate1.
I am certificate2.
$ ./cert.sh
{"certificate_data":"I am certificate1."}
{"certificate_data":"I am certificate2."}
Sign up to request clarification or add additional context in comments.

6 Comments

No need per se to pollute memory with a variable. Just put the magic sauce directly in the echo argument.
I think that questioner wants to have it in variable, so he can do something? with it. echo here is only test of doing something with variable.
Thanks, it should be work, but not for me. I try to set current directory cwd=$(pwd) then for f in $cwd"/"*.crt and get only 1 certificate data. I need to separate *.crt files from other files because my 1st script generate different types of certificates and keys for them, so I need paste only certificate data without data from *.key files.
I edited anser to take only .crt, if You wanna all crts in current dir, use following: for f in "$(pwd)/"*.crt anyway for safer solution I would use rather find -exec or xargs -0 combo
Can I ask why when I used echo "${data}" after loop it shows me only 2nd (of 2) certificates. But when use it in loop it shows me 2 of 2?
|

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.