获取数组中命令的每一行 bash raspbian
get each line of a command in an array bash raspbian
我正在努力提高我的脚本编写技能。
我知道有在线转换器正在这样做,但我想制作一个脚本来比较某个文件夹 (/opt/Citrix/ICAClient/keystore/cacerts/
),如果 .crt 已经是 .pem 格式,如果不转换它们。
所以我知道这显然不是最好的方法...但我是这样开始的:
`
#!/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/\(.*\)\..*//')
#certificate selection in .pem format
Certificate_pem=$(sudo ls /opt/Citrix/ICAClient/keystore/cacerts/ | grep .pem | sed 's/\(.*\)\..*//')
#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
`
问题是,当我的“for”启动时,它将“Certificate_crt_WO_pem”的所有结果存储在数组 $name_certificate[0] 中,然后自行停止。
我想要的是逐行存储“cat crt.txt | grep -v "$Certificate_pem" | tr " " "\n"
”的结果到数组name_certificate。
这个数组将用于启动像这样的东西“openssl -in $name_certificate[$i].crt -out $name_certificate[$i].pem PEM
”(在 for 循环中将每个 namefile.crt 转换为每个 namefile.pem)。
如果有人能帮助我,我将不胜感激...(是的,我已经尝试在网上搜索,听过一些在线课程,但 none 他们说bash 的数组也一样,所以我有点迷路了...)
What i want is to store, line by line, the result of
那就这样做吧。
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
是 useless use of cat。使用 grep .. file
或 < file grep ...
.
记住do not parse ls
output。不要ls | something
。更喜欢 globulation 或 find
。
阅读how to read a stream line by line in bashfaq. Read how to use arrays in bashfaq。
请注意 grep
解析正则表达式,因此 grep .pem
匹配 任何字符 后跟 pem
。我猜你想要 grep '\.pem'
。我不认为 grep -v "$Certificate_pem"
做你认为它做的事 - 我猜你打算使用 comm
或 join
来过滤另一个列表中存在的换行符分隔的元素。
This array will be use to launch something like this " openssl -in $name_certificate[$i].crt -out $name_certificate[$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 "%s\n" "$file"
done |
# execute openssl for the rest
xargs -d'\n' -i{} openssl -in {}.crt -out {}.pem PEM
但如果你愿意,可以使用 mapfile
将带有换行符分隔列表的字符串存储到数组中(一定要理解 how to store variables in a pipeline)。
mapfile -t Certificate_crt_WO_pem < <(something)
我正在努力提高我的脚本编写技能。
我知道有在线转换器正在这样做,但我想制作一个脚本来比较某个文件夹 (/opt/Citrix/ICAClient/keystore/cacerts/
),如果 .crt 已经是 .pem 格式,如果不转换它们。
所以我知道这显然不是最好的方法...但我是这样开始的:
`
#!/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/\(.*\)\..*//')
#certificate selection in .pem format
Certificate_pem=$(sudo ls /opt/Citrix/ICAClient/keystore/cacerts/ | grep .pem | sed 's/\(.*\)\..*//')
#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
`
问题是,当我的“for”启动时,它将“Certificate_crt_WO_pem”的所有结果存储在数组 $name_certificate[0] 中,然后自行停止。
我想要的是逐行存储“cat crt.txt | grep -v "$Certificate_pem" | tr " " "\n"
”的结果到数组name_certificate。
这个数组将用于启动像这样的东西“openssl -in $name_certificate[$i].crt -out $name_certificate[$i].pem PEM
”(在 for 循环中将每个 namefile.crt 转换为每个 namefile.pem)。
如果有人能帮助我,我将不胜感激...(是的,我已经尝试在网上搜索,听过一些在线课程,但 none 他们说bash 的数组也一样,所以我有点迷路了...)
What i want is to store, line by line, the result of
那就这样做吧。
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
是 useless use of cat。使用 grep .. file
或 < file grep ...
.
记住do not parse ls
output。不要ls | something
。更喜欢 globulation 或 find
。
阅读how to read a stream line by line in bashfaq. Read how to use arrays in bashfaq。
请注意 grep
解析正则表达式,因此 grep .pem
匹配 任何字符 后跟 pem
。我猜你想要 grep '\.pem'
。我不认为 grep -v "$Certificate_pem"
做你认为它做的事 - 我猜你打算使用 comm
或 join
来过滤另一个列表中存在的换行符分隔的元素。
This array will be use to launch something like this " openssl -in $name_certificate[$i].crt -out $name_certificate[$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 "%s\n" "$file"
done |
# execute openssl for the rest
xargs -d'\n' -i{} openssl -in {}.crt -out {}.pem PEM
但如果你愿意,可以使用 mapfile
将带有换行符分隔列表的字符串存储到数组中(一定要理解 how to store variables in a pipeline)。
mapfile -t Certificate_crt_WO_pem < <(something)