openssl genrsa -out dkim1024.key 1024
openssl rsa -in dkim1024.key -out dkim1024.pub -pubout -outform PEM
我有一个使用awk提取密钥文件的bash脚本
KEY = /usr/bin/awk’NR> 1 {a [k] = $0} END {for(i = 1; i< k; i)print a [i]}'file.key file.key内容
-----BEGIN PUBliC KEY-----MIGfMA0GCSqasdfadaBAQUAA4GNADCBiQKBsdfadsfsDgrKE5D2atbGtaqVmAZwzy1YNY8pUYgF6qQ4zecRrpk3Uam52asdfafasdasdfasgwYAA0x3roKIYAeHFFrFm9aCSWKpM+na/03akmpewn4cjT4sm9qTHP0q/VM/YtHy3rGkPj+xzOKgO9rzyZo3E1a1OHYu6TIqqzlqtadfafafafdas-----END PUBliC KEY-----
我需要删除这些行.
—–开始公钥—–
—– END PUBliC KEY —–
我在我的awk cmd中完成了这个,但当我回显$KEY时有空格.我猜这些是返回字符,但我不知道如何摆脱它们但保留密钥.
sh-3.2# echo $KEYMIGfMA0GCSqasdfadaBAQUAA4GNADCBiQKBsdfadsfsDgrKE5D2atbGtaqVmAZwzy1YNY 8pUYgF6qQ4zecRrpk3Uam52asdfafasdasdfasgwYAA0x3roKIYAeHFFrFm9aCS WKpM+na/03akmpewn4cjT4sm9qTHP0q/VM/YtHy3rGkPj+xzOKgO9rzyZo3E1a1O HYu6TIqqzlqtadfafafafdas
谢谢
解决方法 由于 word splitting,你得到的是空格而不是行.你应该用双引号引用你的变量echo "$KEY"
这也可以是一种替代解决方案:
awk '/-END PUBliC KEY-/ { p = 0 }; p; /-BEGIN PUBliC KEY-/ { p = 1 }' file.key
输出:
MIGfMA0GCSqasdfadaBAQUAA4GNADCBiQKBsdfadsfsDgrKE5D2atbGtaqVmAZwzy1YNY8pUYgF6qQ4zecRrpk3Uam52asdfafasdasdfasgwYAA0x3roKIYAeHFFrFm9aCSWKpM+na/03akmpewn4cjT4sm9qTHP0q/VM/YtHy3rGkPj+xzOKgO9rzyZo3E1a1OHYu6TIqqzlqtadfafafafdas
即使在块之前或之后存在其他行,该解决方案也将起作用.
您也可以使用Bash本身来完成:
#!/bin/bashfile='file.key' ## Or file=KEY=()while IFS=$' \t\r\n' read -r liNE; do [[ $liNE == *'-END PUBliC KEY-'* ]] && P=0 (( P )) && KEY+=("$liNE") ## Store every line as an array element. [[ $liNE == *'-BEGIN PUBliC KEY-'* ]] && P=1done < "$file"echo --------------------printf '%s\n' "${KEY[@]}"echo --------------------IFS= eval 'MERGED_KEY="${KEY[*]}"' ## Merge key without spaces.echo "$MERGED_KEY"
输出:
--------------------MIGfMA0GCSqasdfadaBAQUAA4GNADCBiQKBsdfadsfsDgrKE5D2atbGtaqVmAZwzy1YNY8pUYgF6qQ4zecRrpk3Uam52asdfafasdasdfasgwYAA0x3roKIYAeHFFrFm9aCSWKpM+na/03akmpewn4cjT4sm9qTHP0q/VM/YtHy3rGkPj+xzOKgO9rzyZo3E1a1OHYu6TIqqzlqtadfafafafdas--------------------MIGfMA0GCSqasdfadaBAQUAA4GNADCBiQKBsdfadsfsDgrKE5D2atbGtaqVmAZwzy1YNY8pUYgF6qQ4zecRrpk3Uam52asdfafasdasdfasgwYAA0x3roKIYAeHFFrFm9aCSWKpM+na/03akmpewn4cjT4sm9qTHP0q/VM/YtHy3rGkPj+xzOKgO9rzyZo3E1a1OHYu6TIqqzlqtadfafafafdas总结
以上是内存溢出为你收集整理的linux – 使用awk提取密钥的BASH脚本全部内容,希望文章能够帮你解决linux – 使用awk提取密钥的BASH脚本所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)