【问题标题】:Windows batch equivalent of bash script using conditional "IF statement"使用条件“IF 语句”的 Windows 批处理等效的 bash 脚本
【发布时间】:2016-09-18 03:19:17
【问题描述】:

我有一个安装程序包,我想编写静默安装脚本。该软件包有适用于 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

【问题讨论】:

  • 试试这个:ss64.com/nt/if.html
  • 有没有办法在一个 IF 语句中同时搜索两个文件,或者我没有嵌套命令?
  • 我想我从未尝试过! :) 我不会推荐它。 IMO 最好分别测试每个文件,这样您就可以输出一条消息(可能是日志文件),说明哪个文件丢失了。我确定您可以嵌套 IF:IF EXIST "\tmp\cert.ssl" (IF EXIST "\tmp\installer.bin" ([do stuff]))",但我不知道您是否可以使用任何条件来进一步压缩。
  • 我编辑了我的原始帖子,添加了到目前为止的内容。它还不能正常工作。
  • 奇数。你所拥有的对我有用(如果我将路径更改为我机器上两个文本文件的路径)。

标签: windows bash batch-file


【解决方案1】:

您是否关闭了外部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

【讨论】:

    猜你喜欢
    • 2012-05-11
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    • 2018-01-01
    • 2014-11-21
    • 2021-06-10
    • 2021-03-28
    • 1970-01-01
    相关资源
    最近更新 更多