I am writing a cmd script for creating self signed certificate using keytool. I wanted to check if the alias already present in keystore ,
我正在编写一个cmd脚本,用于使用keytool创建自签名证书。我想检查别名是否已存在于密钥库中,
So I need to execute the below command and store it in a variable. So that if the variable is null I can assume that alias doesnt exists,
所以我需要执行以下命令并将其存储在变量中。因此,如果变量为null,我可以假设别名不存在,
keytool -list -v -storepass "%KEYSTORE_PASS%" -keystore cert/seurat-keystore.jks ^| find "Alias name: 1"
I tried with
我试过了
FOR /F "tokens=* delims=" %%A IN ('keytool -list -v -storepass "%KEYSTORE_PASS%" -keystore cert/seurat-keystore.jks ^| find "Alias name: 1"') DO SET Variable=%%A
echo %Variable%
but it displays echo off. Same I achieved in shell script using the below code,
但它显示回声。我使用下面的代码在shell脚本中实现了相同,
if [ -n "$(keytool -list -v -storepass "${KEYSTORE_PASS}" -keystore cert/seurat-keystore.jks | grep 'Alias name: 1')" ] ; then
fi
How can I achieve the same in cmd script ? Any help will be really appreciable !!!
如何在cmd脚本中实现相同的功能?任何帮助都会非常值得!
2 个解决方案
#1
0
Excuse me. You wrote "if the variable is null I can assume that alias doesnt exists". If the echo %variable%
command show ECHO OFF, then the variable is null! I think your code is correct, but slightly change it this way:
劳驾。你写道“如果变量为null,我可以假设别名不存在”。如果echo%variable%命令显示ECHO OFF,则变量为null!我认为你的代码是正确的,但稍微改变一下:
FOR /F "tokens=* delims=" %%A IN ('keytool -list -v -storepass "%KEYSTORE_PASS%" -keystore cert/seurat-keystore.jks ^| find "Alias name: 1"') DO SET Variable=%%A
if "%Variable%" == "" (
echo The alias doesn't exists
) else (
echo The alias is: %Variable%
)
Another way is:
另一种方式是:
if not defined Variable (
echo The alias doesn't exists
) else (
echo The alias is: %Variable%
)
#2
0
As I haven't got this keytool
thing, I do not know what it is or does. But I replaced it with an ECHO
command and it worked for me:
由于我没有这个keytool的东西,我不知道它是什么或做什么。但我用ECHO命令替换它,它对我有用:
FOR /F "tokens=* delims=" %%A IN ('ECHO Alias name: 1 ^| find "Alias name: 1"') DO SET Variable=%%A
echo %Variable%
The output was:
输出是:
Alias name: 1
And when I changed the replacement command to ECHO Alias name: 2
, the output was different:
当我将替换命令更改为ECHO别名:2时,输出不同:
ECHO is off.
which means the variable wasn't initialised, and that was expected, because the FIND
command couldn't find the specified string in the input stream this time.
这意味着变量未初始化,这是预期的,因为这次FIND命令无法在输入流中找到指定的字符串。
Conclusion: the output of keytool
probably doesn't contain the searched term when you run it, and, as a result, your variable doesn't get initialised.
结论:keytool的输出在运行时可能不包含搜索的术语,因此,您的变量不会被初始化。
#1
0
Excuse me. You wrote "if the variable is null I can assume that alias doesnt exists". If the echo %variable%
command show ECHO OFF, then the variable is null! I think your code is correct, but slightly change it this way:
劳驾。你写道“如果变量为null,我可以假设别名不存在”。如果echo%variable%命令显示ECHO OFF,则变量为null!我认为你的代码是正确的,但稍微改变一下:
FOR /F "tokens=* delims=" %%A IN ('keytool -list -v -storepass "%KEYSTORE_PASS%" -keystore cert/seurat-keystore.jks ^| find "Alias name: 1"') DO SET Variable=%%A
if "%Variable%" == "" (
echo The alias doesn't exists
) else (
echo The alias is: %Variable%
)
Another way is:
另一种方式是:
if not defined Variable (
echo The alias doesn't exists
) else (
echo The alias is: %Variable%
)
#2
0
As I haven't got this keytool
thing, I do not know what it is or does. But I replaced it with an ECHO
command and it worked for me:
由于我没有这个keytool的东西,我不知道它是什么或做什么。但我用ECHO命令替换它,它对我有用:
FOR /F "tokens=* delims=" %%A IN ('ECHO Alias name: 1 ^| find "Alias name: 1"') DO SET Variable=%%A
echo %Variable%
The output was:
输出是:
Alias name: 1
And when I changed the replacement command to ECHO Alias name: 2
, the output was different:
当我将替换命令更改为ECHO别名:2时,输出不同:
ECHO is off.
which means the variable wasn't initialised, and that was expected, because the FIND
command couldn't find the specified string in the input stream this time.
这意味着变量未初始化,这是预期的,因为这次FIND命令无法在输入流中找到指定的字符串。
Conclusion: the output of keytool
probably doesn't contain the searched term when you run it, and, as a result, your variable doesn't get initialised.
结论:keytool的输出在运行时可能不包含搜索的术语,因此,您的变量不会被初始化。