巴什"若平,然后与其他QUOT;我别的不工作 [英] Bash "if ping then else" My else is not working

查看:120
本文介绍了巴什"若平,然后与其他QUOT;我别的不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该工作的基础上我是否能ping通我的网络地址或者不是一个bash脚本。将有实例时,这个脚本跑我的网络在这种情况下,我不希望它运行之外。所以我把这个一起

I have a bash script that should work based on whether I can ping my network address or not. There will be instances when this script is ran outside of my network in which case I don't want it to run. So I put together this

if ping -c 1 xxx.xxx.x.x > /dev/null; then

在它的网络上,然后语句的工作就好了。但是,每当我关掉我的无线网卡,并断开我的以太网电缆连接到测试别人我else语句永远得不到的执行。有另一种方法,我应该写这个?

When it's in the network, the "then" statement works just fine. However, whenever I turn off my wireless card and disconnect my ethernet cable to test the "else" my else statement never get's executed. Is there another way I should be writing this?

下面是完整的剧本;

#!/bin/bash

computerid=`/usr/sbin/scutil --get LocalHostName`

# Standard parameters
domain="xxx.xxxxxxx.xxx"                            # fully qualified DNS name of Active Directory Domain
udn="xxxxxx"                                            # username of a privileged network user
password="xxxxxx"                                           # password of a privileged network user
ou="OU=MacOS,DC=xxx,DC=xxxxxxx,DC=xxx"              # Distinguished name of container for the computer

# Advanced options
alldomains="enable"                 # 'enable' or 'disable' automatic multi-domain authentication
localhome="enable"                  # 'enable' or 'disable' force home directory to local drive
protocol="smb"                              # 'afp' or 'smb' change how home is mounted from server
mobile="enable"                         # 'enable' or 'disable' mobile account support for offline logon
mobileconfirm="disable"             # 'enable' or 'disable' warn the user that a mobile acct will be created
useuncpath="enable"                 # 'enable' or 'disable' use AD SMBHome attribute to determine the home dir
user_shell="/bin/bash"              # e.g., /bin/bash or "none"
preferred="-preferred xxx.xxxxxx.xxx"       # Use the specified server for all Directory lookups and authentication
# (e.g. "-nopreferred" or "-preferred ad.server.edu")
admingroups="xxx\domain admins,xxx\enterprise admins,xxx\teacher98,xxx\ADManagement - Computers,xxx\admin employees"        # These comma-separated AD groups may administer the machine (e.g. "" or "APPLE\mac admins")

### End of configuration

# Delay the login window by unloading the com.apple.loginwindow
# LaunchDaemon in /System/Library/LaunchDaemons/

launchctl unload -w /System/Library/LaunchDaemons/com.apple.loginwindow.plist


## Wait until all network services are up.
sleep 10

# Check to see if we're in the district
if ping -c 1 xxx.xxx.x.x > /dev/null; then


    #make sure time is set correctly
    ntpdate -u time.xxxxxxx.xxx


    # Activate the AD plugin
    defaults write /Library/Preferences/DirectoryService/DirectoryService "Active Directory" "Active"
    plutil -convert xml1 /Library/Preferences/DirectoryService/DirectoryService.plist
    sleep 5

    # Remove computer from OU
    dsconfigad -f -r -u xxxxxx -p xxxxxx
    sleep 5

    # Bind to AD
    dsconfigad -f -a $computerid -domain $domain -u $udn -p "$password" -ou "$ou"

    # Configure advanced AD plugin options
    if [ "$admingroups" = "" ]; then
        dsconfigad -nogroups
    else
        dsconfigad -groups "$admingroups"
    fi

    dsconfigad -alldomains $alldomains -localhome $localhome -protocol $protocol \
        -mobile $mobile -mobileconfirm $mobileconfirm -useuncpath $useuncpath \
        -shell $user_shell $preferred

    # Restart DirectoryService (necessary to reload AD plugin activation settings)
    killall DirectoryService

    # Add the AD node to the search path
    if [ "$alldomains" = "enable" ]; then
        csp="/Active Directory/All Domains"
    else
        csp="/Active Directory/$domain"
    fi

    # This works in a pinch if the above code does not
    defaults write /Library/Preferences/DirectoryService/SearchNodeConfig "Search Node Custom Path Array" -array "/Active Directory/All Domains"
    defaults write /Library/Preferences/DirectoryService/SearchNodeConfig "Search Policy" -int 4
    defaults write /Library/Preferences/DirectoryService/ContactsNodeConfig "Search Node Custom Path Array" -array "/Active Directory/All Domains"
    defaults write /Library/Preferences/DirectoryService/ContactsNodeConfig "Search Policy" -int 4

    plutil -convert xml1 /Library/Preferences/DirectoryService/SearchNodeConfig.plist

    ## Remove the script and launchd job. Be sure to delete the script. 
    rm /Library/LaunchDaemons/com.xxxx.adbind.plist
    rm /usr/local/bin/adbind.bash
    launchctl unload -w /Library/LaunchDaemons/com.xxxx.adbind.plist

    # Remove the loginwindow delay by loading the com.apple.loginwindow   
    # LaunchDaemon in /System/Library/LaunchDaemons/

    launchctl load -wF /System/Library/LaunchDaemons/com.apple.loginwindow.plist

    exit 0

else

    echo "District not Available Quitting"

    # Remove the loginwindow delay by loading the com.apple.loginwindow   
    # LaunchDaemon in /System/Library/LaunchDaemons/

    launchctl load -wF /System/Library/LaunchDaemons/com.apple.loginwindow.plist

fi

exit 1

感谢您的任何建议!

Thanks for any suggestions!

推荐答案

您可能需要使用超时(-W)人平

You may need to use a timeout (-W) man ping

这篇关于巴什"若平,然后与其他QUOT;我别的不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆