Python bash脚本可能出现语法错误,不确定是否尝试使用until循环
大家好,我正在为我的服务器编写一个wol脚本。每次启动时,覆盆子pi都会执行 我想这是一个语法错误,但我现在不知道解决方案,所以我问你。 我在第5行遇到一个错误,但不知道如何更正它Python bash脚本可能出现语法错误,不确定是否尝试使用until循环,python,bash,powershell,wake-on-lan,Python,Bash,Powershell,Wake On Lan,大家好,我正在为我的服务器编写一个wol脚本。每次启动时,覆盆子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 &> /
#!/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
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
首先,使用修复脚本中的语法问题
您用于命令替换的语法是错误的,使用该修复程序并直接在until循环中使用退出代码ping
until ping -c 1 192.192.168.222.5 &> /dev/null
do
python wol.py
sleep 2
done
应该能解决你的问题。ping
的man
页面显示
如果ping根本没有收到任何回复数据包,它将以代码1退出。如果同时指定了数据包计数和截止日期,并且截止日期到达时收到的数据包少于计数,则它也将以代码1退出。在另一个错误中,它以代码2退出。否则它将以代码0退出。这使得可以使用退出代码查看主机是否处于活动状态
嘿,你的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
有关退出代码,请参阅:
对于不同的括号/括号,请通过manbash
查看真正优秀的bash手册,或者。非常简单,谢谢,它现在可以用更少的资源工作了code@SelimAkca:很乐意帮忙!
#!/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