我正在努力提高我的脚本编写技能。我知道有在线转换器在做这件事,但如果.crt已经是.pem格式,如果不转换它们,我想在某个文件夹(/opt/Citrix/ICAClient/keystore/cacerts/
(中制作一个比较脚本。
所以我知道这显然不是最好的方法……但我已经开始这样做了:
`
#!/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
`
问题是,当我的";对于";则它存储"0"的所有结果;Certificate_crt_WO_pem";在数组$name_certificate[0]中,然后自行停止。
我想要的是一行一行地存储"CCD_ 2";到数组name_certificate中。
这个阵列将被用来启动类似于";CCD_ 3";(在遗嘱中的for循环中,转换每个namefile.pem中的每个namefile.crt(.
如果有人能帮助我,我将不胜感激。。。(是的,我已经尝试过在网上搜索,关注过一些在线课程,但他们都没有对bash的数组说同样的话,所以我有点迷路了…(
我想要的是逐行存储的结果
然后执行该操作。
var=$(something)
# ^^ - normal variable assignment
var=( $(something) )
# ^^ ^ - array assignment
# the unquoted result of expansions is split on IFS
# default on tabs, newlines and spaces
所以我想你想要:
Certificate_crt_WO_pem=($(grep -v "$Certificate_pem" crt.txt))
做cat file | grep
是对cat的无用利用。使用grep .. file
或< file grep ...
。
记住不要解析ls
输出。不要ls | something
。更喜欢球化或find
。
阅读如何在bashfaq中逐行读取流。阅读如何在bashfaq中使用数组。
注意,grep
解析正则表达式,因此grep .pem
匹配后面跟有pem
的任何字符。我猜你想要grep '.pem'
。我不认为grep -v "$Certificate_pem"
会像你想象的那样——我想你是想用comm
或join
来过滤另一个列表中一个换行符中的元素。
这个数组将用于启动类似的东西"openssl-in$name_certificate[$i].crt-out$name_ccertificate[$i].pem pem";
最好不要这样做,而是在数据到来时对其进行解析,而不是将其存储在变量中。
# 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 "%sn" "$file"
done |
# execute openssl for the rest
xargs -d'n' -i{} openssl -in {}.crt -out {}.pem PEM
但如果您愿意,可以使用mapfile
将带有换行符列表的字符串存储到数组中(一定要了解如何在管道中存储变量(。
mapfile -t Certificate_crt_WO_pem < <(something)