使用条件“IF语句”的Windows批处理相当于bash脚本

时间:2016-05-20 18:44:14

标签: windows bash batch-file

我有一个安装程序包,我想编写静默安装脚本。该软件包具有适用于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 Batch中编写类似的内容,以使用嵌套的If Exists运行installer.exe。我正在使用“echo”测试脚本,但它似乎没有正确处理嵌套的IF。如果我删除installer.exe,则ELSE条件有效。如果我删除cert.ssl则不会。

::=========================
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

IF EXIST "C:\temp\installer.exe" (
    IF EXIST "C:\temp\cert.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

1 个答案:

答案 0 :(得分:2)

您是否关闭了外部if声明?

IF EXIST "C:\temp\installer.exe" (
    IF EXIST "C:\temp\cert.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:\temp\installer.exe" (
    echo "Missing bin file"
    goto END
)

IF NOT EXIST "C:\temp\cert.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