使用条件"IF statement"的 bash 脚本的 Windows 批处理等效项



我有一个安装程序包,我想编写静默安装脚本。该软件包具有适用于Linux和Windows的版本。它需要存在两个文件;一个 bin (*nix) 或 exe (Win) 和一个额外的数字证书 SSL 文件。

我编写了一个 bash 脚本,在继续在 Linux 上安装之前检查这两个文件是否存在。

#!/bin/bash
# Variables
CFILE="/tmp/cert.ssl"
BFILE="/tmp/installer.bin"
SRV="192.168.1.2"
APORT="443"

    if [[ -e ${BFILE} && -e ${CFILE} ]] && echo "Both cert and bin files exist in /tmp"
then
    echo "Proceeding with installation!"
chmod 764 ${BFILE}  
${BFILE} -silent -server=${SRV} -cert=${CFILE} -agentport=${APORT} 
else
    echo "Installation aborted. Please ensure that the cert and bin file are located in /tmp"
fi

我正在尝试在 Windows 批处理中编写类似的东西来运行安装程序.exe使用嵌套的"如果存在"。我正在使用"echo"测试脚本,但它似乎没有正确处理嵌套的 IF。如果我删除安装程序.exe,则 ELSE 条件有效。如果我删除 cert.ssl 它不会。

::=========================
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
IF EXIST "C:tempinstaller.exe" (
    IF EXIST "C:tempcert.ssl" (
        echo "Both cert and bin files exist in "C:temp". Proceeding with the installation!"
        timeout /t 10 )
    ) ELSE (
        echo "Installation aborted. Please ensure that the cert and bin file are located in "C:temp""
        timeout /t 10
    )
:END

您是否关闭了外部if语句?

IF EXIST "C:tempinstaller.exe" (
    IF EXIST "C:tempcert.ssl" (
        echo "Both cert and bin files exist in "C:temp". Proceeding with the installation!"
        timeout /t 10
    ) ELSE (
        echo "Installation aborted. Please ensure that the cert and bin file are located in "C:temp""
        timeout /t 10
    )
REM Closing outer if
)
:END

就个人而言,我更喜欢使用if not

IF NOT EXIST "C:tempinstaller.exe" (
    echo "Missing bin file"
    goto END
)
IF NOT EXIST "C:tempcert.ssl" (
    echo "Missing cert file"
    goto END
)
echo "Both cert and bin files exist in "C:temp". Proceeding with the installation!"
timeout /t 10
:END

相关内容

  • 没有找到相关文章

最新更新