检查密钥店里是否存在别名

发布于 2025-02-09 12:03:59 字数 547 浏览 0 评论 0原文

我是外壳脚本的新手,我有一个用于使用KeyTool导入CERT和导入密钥库的脚本。这个脚本在吊舱内,有时候我必须重新启动吊舱

存在现有的进入别名服务器,覆盖? [否]:输入新别名 名称(返回此条目取消导入):

我知道有命令列出

keytool -list  -keystore keystore.jks -storepass changeit -alias server

提供输出的

别名

服务器,6月22日,2022年,私人按键, 证书指纹(SHA-256):AC:DC:12:...

对于不存在的别名,此命令给出了例外

keytool错误:java.lang.exception:别名不存在

如果存在键盘别名存在,如果不允许 keytool -importcert 命令,则可以编写检查吗?我可以比较任何可以与执行的返回代码?

谢谢

I am new to shell scripting, I have a script that is used to import cert and import keystore using keytool. This script is inside a Pod, there are times when I have to restart my pod and sometimes when this script runs I get this error

Existing entry alias server exists, overwrite? [no]: Enter new alias
name (RETURN to cancel import for this entry):

I know that there is command to list the alias

keytool -list  -keystore keystore.jks -storepass changeit -alias server

which gives the output

server, Jun 22, 2022, PrivateKeyEntry,
Certificate fingerprint (SHA-256): AC:DC:12:...

for an alias it that is not there, this command gives an exception

keytool error: java.lang.Exception: Alias does not exist

Is there a way in which I can write a check if the an keystore alias exists, if not allow the keytool -importcert command to execute ? Are there any return codes that I can compare to move forward with the execution ?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

じ违心 2025-02-16 12:03:59

您可以使用$?查找最后执行命令的返回值。
0返回值意味着成功,否则,如果有例外,您会得到其他数字,然后再获得0。

keytool -list -keystore keystore.jks -storepass changeit -alias server

if [[ $? = 0 ]]; then
        echo "alias is present"
else
        echo "alias is not present"
fi

edit
正如查尔斯·达菲(Charles Duffy)指出的$? 不建议
您可以简单地使用

if keytool -list -keystore keystore.jks -storepass changeit -alias server; then
        echo "alias is present"
else
        echo "alias is not present"
fi

You can use $? to find the return value of the last executed command.
0 return value means success, otherwise, in case of exception, you get a number other then 0.

keytool -list -keystore keystore.jks -storepass changeit -alias server

if [[ $? = 0 ]]; then
        echo "alias is present"
else
        echo "alias is not present"
fi

EDIT
As Charles Duffy points out the use of $? is discouraged.
You can simply use

if keytool -list -keystore keystore.jks -storepass changeit -alias server; then
        echo "alias is present"
else
        echo "alias is not present"
fi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文