简体   繁体   中英

get each line of a command in an array bash raspbian

I'm trying to improve my scripting skills. I know there's online converters that are doing it but i'd like to make a script that compare in a certain folder ( /opt/Citrix/ICAClient/keystore/cacerts/ ), if the.crt are already in the.pem format and if not convert them.

So i know this clearly isn't the best way to do so... But i've started like this:

`

#!/bin/bash
#remove both files if they already exists
rm crt.txt
rm pem.txt 
#certificate selection in .crt format
Certificate_crt=$(sudo ls /opt/Citrix/ICAClient/keystore/cacerts/ | grep .crt | sed 's/\(.*\)\..*/\1/')

#certificate selection in .pem format
Certificate_pem=$(sudo ls /opt/Citrix/ICAClient/keystore/cacerts/ | grep .pem | sed 's/\(.*\)\..*/\1/') 

#sending results in text files
echo "$Certificate_crt" >> crt.txt
echo "$Certificate_pem" >> pem.txt

#recovery of certificate names in .crt not having a .pem equivalent
Certificate_crt_WO_pem=$(cat crt.txt | grep -v "$Certificate_pem" | tr " " "\n")
#echo "$Certificate_crt_WO_pem"


#initialisation 

i=0

#Print the split string

for i in "${Certificate_crt_WO_pem[@]}"
do
    name_certificate=$(echo $i | tr " " "\n")  
    echo "${name_certificate[@]}"  
    echo "$i"
    i=i+1   
done

`

The thing is that when my "for" is launched, it stores all the result of "Certificate_crt_WO_pem" in the array $name_certificate[0] and then stop it self.

What i want is to store, line by line, the result of " cat crt.txt | grep -v "$Certificate_pem" | tr " " "\n" " into the array name_certificate.

This array will be use to launch something like this " openssl -in $name_certificate[$i].crt -out $name_certificate[$i].pem PEM " (in a for loop in the will to convert every namefile.crt in every namefile.pem).

If someone can help me i'll be more than gratefull... (And yes i've already tried to search on the.net, had followed some online courses but none of them was saying the same thing about the bash's arrays, so i'm a bit lost...)

What i want is to store, line by line, the result of

Then do that.

var=$(something)
#   ^^ - normal variable assignment

var=(      $(something)      )
#  ^^                        ^ - array assignment
# the unquoted result of expansions is split on IFS
#    default on tabs, newlines and spaces

so I guess you want:

Certificate_crt_WO_pem=($(grep -v "$Certificate_pem" crt.txt))

Doing cat file | grep cat file | grep is a useless use of cat . Use grep.. file or < file grep... .

Remember do not parse ls output . Don't ls | something ls | something . Prefer globulation or find instead.

Read how to read a stream line by line in bashfaq . Read how to use arrays in bashfaq .

Note that grep parses a regex, so grep.pem matches any character followed by pem . I guess you wanted grep '\.pem' . I do not think grep -v "$Certificate_pem" does what you think it does - I guess you meant to use comm or join to filter elements from one newline separated separated that are present in the other list.

This array will be use to launch something like this " openssl -in $name_certificate[$i].crt -out $name_certificate[$i].pem PEM"

The best would be not do it, rather parse the data as they come, not store them in variables.

# get all .crt fiels
find /opt/Citrix/ICAClient/keystore/cacerts/ -maxdepth 1 -mindepth 1 -type f -name '*.crt' |
# remove extension
sed 's/\.crt$//' |
# filter out files where .pem does exists already
while IFS= read -r file; do
    if [[ -r "$file".pem ]]; then continue; fi
    printf "%s\n" "$file"
done |
# execute openssl for the rest
xargs -d'\n' -i{} openssl -in {}.crt -out {}.pem PEM

but if you want, rather use mapfile to store a string with a newline separated list into an array (be sure to understand how to store variables in a pipeline ).

mapfile -t Certificate_crt_WO_pem < <(something)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM