bash while循环复合检查

时间:2018-09-26 02:42:49

标签: bash while-loop

这里是新手。尝试编写一个while循环,以检查3个条件,2个用户输入以及ping命令是否失败。如何将所有3个合并为1个大语句? 想检查用户没有输入Y或y或ping google.com失败

尝试:

# Ask user to confirm ethernet cable connected or quit with Yy or Qq in continous loop.
echo -e "Connect ethernet cable to Pi Slave. Type (Y) when done, (Q) to quit:" 
read confirm
# If confirm is Y or y, continue to rest of script
while [[ "$confirm" != "Y" && "$confirm" != "y" && ping -q -c 1 -w 1 google.com  ]]
do
    if [[ "$confirm" == "Q" || "$confirm" == "q" ]]; then
        exit 1
    fi
    echo -e "Connect ethernet cable to Pi Master. Type (Y) when done, (Q) to quit:" 
    read confirm
done

1 个答案:

答案 0 :(得分:3)

function nearestHighestThousand(value) { if (value < 1) { return 1000; } else if (value > 9000) { return 9000; } else { return Math.ceil(value / 1000) * 1000; } } var round = nearestHighestThousand(num); 仅接受简单的测试。像@ECHO OFF SETLOCAL FOR /f %%i in ('cd') do set MYPWD=%%i SET SCRIPT_PATH=%~dp0 FOR %%i IN ("%SCRIPT_PATH%") DO SET SCRIPT_PATH=%%~fsi @REM Set the ORACLE_HOME relative to this script... FOR %%i IN ("%SCRIPT_PATH%\..\..") DO SET ORACLE_HOME=%%~fsi @REM Set the MW_HOME relative to the ORACLE_HOME... FOR %%i IN ("%ORACLE_HOME%\..") DO SET MW_HOME=%%~fsi @REM Set the home directories... CALL "%SCRIPT_PATH%\setHomeDirs.cmd" @REM Set the config jvm args... SET CONFIG_JVM_ARGS=%CONFIG_JVM_ARGS% -DCOMMON_COMPONENTS_HOME=%COMMON_COMPONENTS_HOME% CALL "%SCRIPT_PATH%\commEnv.cmd" FOR %%i IN ("%JAVA_HOME%") DO SET JAVA_HOME=%%~fsi SET CLASSPATH=%FMWCONFIG_CLASSPATH%;%DERBY_CLASSPATH% :PARSEARGS SET VALIDATE=%2 FOR %%I IN (%VALIDATE%) DO SET VALIDATE=%%~I if NOT {%1}=={} ( IF "%1"=="-help" ( SET ARGUMENTS=%1 GOTO :RUN ) IF "%VALIDATE:~0,1%"=="-" ( ECHO ERROR! Missing equal^(=^) sign. Arguments must be -name=value! EXIT /B 1 ) IF "%VALIDATE%"=="" ( ECHO ERROR! Missing value! Arguments must be -name=value! EXIT /B 1 ) GOTO :SETARG ) ELSE ( GOTO :RUN ) :SETARG SET ARGNAME=%1 SET ARGVALUE=%2 SHIFT SHIFT FOR %%I IN (%ARGVALUE%) DO SET ARGVALUE=%%~I IF /i "%ARGNAME%"=="-log" ( IF "%ARGVALUE:~1,1%"==":" ( SET ARGUMENTS=%ARGUMENTS% %ARGNAME%=%ARGVALUE% ) ELSE ( SET ARGUMENTS=%ARGUMENTS% %ARGNAME%=%MYPWD%\%ARGVALUE% ) GOTO :PARSEARGS ) ELSE ( IF /i "%ARGNAME%"=="-silent_script" ( IF "%ARGVALUE:~1,1%"==":" ( SET ARGUMENTS=%ARGUMENTS% %ARGNAME%=%ARGVALUE% ) ELSE ( SET ARGUMENTS=%ARGUMENTS% %ARGNAME%=%MYPWD%\%ARGVALUE% ) GOTO :PARSEARGS ) ELSE ( IF /i "%ARGNAME%"=="-useXACML" ( SET UTILS_MEM_ARGS=%UTILS_MEM_ARGS% -DuseXACML=%ARGVALUE% ) ELSE ( SET ARGUMENTS=%ARGUMENTS% %ARGNAME%="%ARGVALUE%" ) GOTO :PARSEARGS ) ) :RUN PUSHD %COMMON_COMPONENTS_HOME%\common\lib if exist %SCRIPT_PATH%\cam_config.cmd ( call %SCRIPT_PATH%\cam_config.cmd ) SET JVM_ARGS=-Dprod.props.file="%WL_HOME%\.product.properties" -Dpython.cachedir="%TEMP%\cachedir" %UTILS_MEM_ARGS% %COMMON_JVM_ARGS% %CONFIG_JVM_ARGS% IF EXIST %JAVA_HOME% ( IF "%ARGUMENTS%" == "" ( %JAVA_HOME%\bin\javaw %JVM_ARGS% com.oracle.cie.wizard.WizardController %ARGUMENTS% ) ELSE ( %JAVA_HOME%\bin\java %JVM_ARGS% -Djdbc=true com.oracle.cie.wizard.WizardController %ARGUMENTS% ) ) ELSE ( CALL :SET_RC 1 ) SET RETURN_CODE=%ERRORLEVEL% POPD IF DEFINED USE_CMD_EXIT ( EXIT %RETURN_CODE% ) ELSE ( EXIT /B %RETURN_CODE% ) :SET_RC EXIT /B %1 这样的完整命令可以独立存在。

[[ ... ]]

要摆脱重复的提示代码,可以将循环重写为:

ping