bash 脚本可能有语法错误不确定是否尝试 until 循环
bash script maybe syntax error not sure trying a until loop
大家好,我正在尝试为我的服务器编写一个 wol 脚本。 raspberry pi 每次启动时都会执行。
我猜这是一个语法错误,但我现在不知道解决方案,所以我问你。
我在第 5 行遇到错误,但不知道如何更正它。
#!/bin/bash
nas=[ping -c 1 192.192.168.222.5 &> /dev/null ]
until [ $nas = "1" ];do
python wol.py
sleep 2
nas=[ping -c 1 192.192.168.222.5 &> /dev/null ]
done
wol.py 是 marc balmar 发送 wol 包的脚本
#!/usr/bin/env python
#coding: utf8
# Wake-On-LAN
#
# Copyright (C) 2002 by Micro Systems Marc Balmer
# Written by Marc Balmer, marc@msys.ch, http://www.msys.ch/
# Modified by saarnu for nerdoskop.wordpress.com
# This code is free software under the GPL
import struct, socket, time, os
def WakeOnLan(ethernet_address):
# Construct a six-byte hardware address
addr_byte = ethernet_address.split(':')
hw_addr = struct.pack('BBBBBB', int(addr_byte[0], 16),
int(addr_byte[1], 16),
int(addr_byte[2], 16),
int(addr_byte[3], 16),
int(addr_byte[4], 16),
int(addr_byte[5], 16))
# Build the Wake-On-LAN "Magic Packet"...
msg = '\xff' * 6 + hw_addr * 16
# ...and send it to the broadcast address using UDP
time.sleep(5)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.sendto(msg, ('<broadcast>', 9))
s.close()
WakeOnLan('C8:60:00:6D:CF:54') # MAC-Adresse der DiskStation
我在 powershell 中写了一个类似的代码,它更容易,我正在尝试复制它或将它翻译成 bash。
$NAS = test-connection -count 1 -quiet 192.168.222.5
if ($NAS -like "False"){
do
{
$Mac = "C8:60:00:6D:CF:54"
$MacByteArray = $Mac -split "[:-]" | ForEach-Object { [Byte] "0x$_"}
[Byte[]] $MagicPacket = (,0xFF * 6) + ($MacByteArray * 16)
$UdpClient = New-Object System.Net.Sockets.UdpClient
$UdpClient.Connect(([System.Net.IPAddress]::Broadcast),7)
$UdpClient.Send($MagicPacket,$MagicPacket.Length)
$UdpClient.Close()
Start-Sleep -s 5
$NAS = test-connection -count 1 -quiet 192.168.222.5
}
until
(
$NAS -like "True"
)
}
Firstly, use http://www.shellcheck.net to fix your syntax issues in script.
您 command-substitution 的语法是错误的,通过该修复并直接在 until-loop
中使用 ping
的退出代码
until ping -c 1 192.192.168.222.5 &> /dev/null
do
python wol.py
sleep 2
done
应该可以解决您的问题。 ping
的 man
页面显示
If ping does not receive any reply packets at all it will exit with code 1. If a packet count and deadline are both specified, and fewer than count packets are received by the time the deadline has arrived, it will also exit with code 1. On other error it exits with code 2. Otherwise it exits with code 0. This makes it possible to use the exit code to see if a host is alive or not.
嘿,你的 bash 脚本中有几个错误,不只是一个。
#!/bin/bash
# This is a silent ping. The output is redirected to /dev/null. The only visible
# change in user-land will be the exit code, available as $?.
# The exit code "0" means success, the exit code "1" means some error.
ping -c 1 192.192.168.222.5 &> /dev/null
# You need double round parenthesis here to evaluate a comparison.
# Also a comparison, as almost everywhere in programming, is "==" instead of "=".
until (( $? == "0" ));
do
python wol.py
sleep 2
# There is the silent ping again. Updating $?.
ping -c 1 192.192.168.222.5 &> /dev/null
done
有关退出代码,请参见:
Exit Shell Script Based on Process Exit Code
对于不同的 parenthesis/brackets,请参阅 man bash
、How to use double or single brackets, parentheses, curly braces or .
中非常好的 bash 手册
大家好,我正在尝试为我的服务器编写一个 wol 脚本。 raspberry pi 每次启动时都会执行。
我猜这是一个语法错误,但我现在不知道解决方案,所以我问你。 我在第 5 行遇到错误,但不知道如何更正它。
#!/bin/bash
nas=[ping -c 1 192.192.168.222.5 &> /dev/null ]
until [ $nas = "1" ];do
python wol.py
sleep 2
nas=[ping -c 1 192.192.168.222.5 &> /dev/null ]
done
wol.py 是 marc balmar 发送 wol 包的脚本
#!/usr/bin/env python
#coding: utf8
# Wake-On-LAN
#
# Copyright (C) 2002 by Micro Systems Marc Balmer
# Written by Marc Balmer, marc@msys.ch, http://www.msys.ch/
# Modified by saarnu for nerdoskop.wordpress.com
# This code is free software under the GPL
import struct, socket, time, os
def WakeOnLan(ethernet_address):
# Construct a six-byte hardware address
addr_byte = ethernet_address.split(':')
hw_addr = struct.pack('BBBBBB', int(addr_byte[0], 16),
int(addr_byte[1], 16),
int(addr_byte[2], 16),
int(addr_byte[3], 16),
int(addr_byte[4], 16),
int(addr_byte[5], 16))
# Build the Wake-On-LAN "Magic Packet"...
msg = '\xff' * 6 + hw_addr * 16
# ...and send it to the broadcast address using UDP
time.sleep(5)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.sendto(msg, ('<broadcast>', 9))
s.close()
WakeOnLan('C8:60:00:6D:CF:54') # MAC-Adresse der DiskStation
我在 powershell 中写了一个类似的代码,它更容易,我正在尝试复制它或将它翻译成 bash。
$NAS = test-connection -count 1 -quiet 192.168.222.5
if ($NAS -like "False"){
do
{
$Mac = "C8:60:00:6D:CF:54"
$MacByteArray = $Mac -split "[:-]" | ForEach-Object { [Byte] "0x$_"}
[Byte[]] $MagicPacket = (,0xFF * 6) + ($MacByteArray * 16)
$UdpClient = New-Object System.Net.Sockets.UdpClient
$UdpClient.Connect(([System.Net.IPAddress]::Broadcast),7)
$UdpClient.Send($MagicPacket,$MagicPacket.Length)
$UdpClient.Close()
Start-Sleep -s 5
$NAS = test-connection -count 1 -quiet 192.168.222.5
}
until
(
$NAS -like "True"
)
}
Firstly, use http://www.shellcheck.net to fix your syntax issues in script.
您 command-substitution 的语法是错误的,通过该修复并直接在 until-loop
ping
的退出代码
until ping -c 1 192.192.168.222.5 &> /dev/null
do
python wol.py
sleep 2
done
应该可以解决您的问题。 ping
的 man
页面显示
If ping does not receive any reply packets at all it will exit with code 1. If a packet count and deadline are both specified, and fewer than count packets are received by the time the deadline has arrived, it will also exit with code 1. On other error it exits with code 2. Otherwise it exits with code 0. This makes it possible to use the exit code to see if a host is alive or not.
嘿,你的 bash 脚本中有几个错误,不只是一个。
#!/bin/bash
# This is a silent ping. The output is redirected to /dev/null. The only visible
# change in user-land will be the exit code, available as $?.
# The exit code "0" means success, the exit code "1" means some error.
ping -c 1 192.192.168.222.5 &> /dev/null
# You need double round parenthesis here to evaluate a comparison.
# Also a comparison, as almost everywhere in programming, is "==" instead of "=".
until (( $? == "0" ));
do
python wol.py
sleep 2
# There is the silent ping again. Updating $?.
ping -c 1 192.192.168.222.5 &> /dev/null
done
有关退出代码,请参见: Exit Shell Script Based on Process Exit Code
对于不同的 parenthesis/brackets,请参阅 man bash
、How to use double or single brackets, parentheses, curly braces or .