http://blog.sina.com.cn/s/blog_9d074aae01012ytf.html

本文详细介绍了Shell环境中for循环的多种用法,并通过实例展示了如何输出1-100间可以被3整除的数。此外,还探讨了在进行数字递增时遇到的问题及其解决方法。

转自http://blog.sina.com.cn/s/blog_9d074aae01012ytf.html

关于shell中的for循环用法很多,一直想总结一下,今天网上看到上一篇关于for循环用法的总结,感觉很全面,所以就转过来研究研究,嘿嘿...

1、 for((i=1;i<=10;i++));do echo $(expr $i \* 4);done
2、在shell中常用的是 for i in $(seq 10)
3、for i in `ls`

4、for i in ${arr[@]}
5、for i in $* ; do
6、for File in /proc/sys/net/ipv4/confaccept_redirects:'
18.for File in /proc/sys/net/ipv4/conf/*/accept_redirects; do
19.echo $File
20.done
21.echo "直接指定循环内容"
22.for i in f1 f2 f3 ;do
23.echo $i
24.done
25.echo
26.echo "C 语法for 循环:"
27.for (( i=0; i<10; i++)); do
28.echo $i
29.done

---------------------------------------------------------------------------------------------------------

shell中for循环用法
shell语法好麻烦的,一个循环都弄了一会 ,找了几个不同的方法来实现输出1-100间可以被3整除的数
1.用(())
#!/bin/bash
clear

for((i=1;i<100;i++))
for
do
if((i%3==0))
then
echo $i
continue
fi
done
2.使用`seq 100`
#!/bin/bash
clear

for i in `seq 100`
do
if((i%3==0))
then
echo $i
continue
fi
done
3.使用while
#!/bin/bash
clear

i=1
while(($i<100))
do
if(($i%3==0))
then
echo $i
fi
i=$(($i+1))
done



--------------------------------------------------------------------------------------------------------
在shell用for循环做数字递增的时候发现问题,特列出shell下for循环的几种方法:

1.

for i in `seq 1 1000000`;do

echo $i

done

用seq 1 10000000做递增,之前用这种方法的时候没遇到问题,因为之前的i根本就没用到百万(1000000),因为项目需要我这个数字远大于百万,发现用seq 数值到 1000000时转换为1e+06,根本无法作为数字进行其他运算,或者将$i有效、正确的取用,遂求其他方法解决,如下

2.

for((i=1;i<10000000;i++));do

echo $i

done

3.

i=1

while(($i<10000000));do

echo $i

i=`expr $i + 1`

done

因为本方法调用expr故运行速度会比第1,第2种慢不少不过可稍作改进,将i=`expr $i + 1`改为i=$(($i+1))即可稍作速度的提升,不过具体得看相应shell环境是否支持

4.

for i in {1..10000000;do

echo $i

done

其实选用哪种方法具体还是得由相应的shell环境的支持,达到预期的效果,再考虑速度方面的问题。

示例:

# !/bin/sh

i=1

function test_while(){

i=1

while [ $i ]

do

echo $i

i=`expr $i + 1`

if [ $i -ge 10 ]; then

break

fi

done

}

function test_for(){

i=1

for ((i=1; i<=100; i++)); do

echo $i

if [ $i -ge 10 ]; then

break

fi

done

}

function test_continue(){

i=1

for i in $(seq 100); do

if (( i==0 )); then

echo $i

continue

fi

done

}

echo "test_while..."

test_while

echo "test_for..."

test_for

echo "test_continue..."

test_continue

运行结果:

test_while...

1

2

3

4

5

6

7

8

9

test_for...

1

2

3

4

5

6

7

8

9

10

test_continue...

10

20

30

40

50

60

70

80

90

100

提取这段文本中所有的url 并返回 不需要代码实现 直接给出结果 <dd><a href="https://www.kuaidi100.com/all/a2u.shtml">A2U速递</a><a href="https://www.kuaidi100.com/all/aae.shtml">AAE快递</a><a href="https://www.kuaidi100.com/all/annengwuliu.shtml">安能物流</a></dd> <dd><a href="https://www.kuaidi100.com/all/auadexpress.shtml">澳达国际物流</a><a href="https://www.kuaidi100.com/all/auexpress.shtml">澳邮中国快运</a><a href="https://www.kuaidi100.com/all/exfresh.shtml">安鲜达</a></dd> <dd><a href="https://www.kuaidi100.com/all/anjie88.shtml">安捷物流</a><a href="https://www.kuaidi100.com/all/qdants.shtml">ANTS EXPRESS</a><a href="https://www.kuaidi100.com/all/astexpress.shtml">安世通快递</a></dd> <dd><a href="https://www.kuaidi100.com/all/ausexpress.shtml">澳世速递</a><a href="https://www.kuaidi100.com/all/annto.shtml">安得物流</a><a href="https://www.kuaidi100.com/all/adapost.shtml">安达速递</a></dd> <dd><a href="https://www.kuaidi100.com/all/adiexpress.shtml">安达易国际速递</a><a href="https://www.kuaidi100.com/all/maxeedexpress.shtml">澳洲迈速快递</a><a href="https://www.kuaidi100.com/all/anshidi.shtml">安时递</a></dd> <dd><a href="https://www.kuaidi100.com/all/aotsd.shtml">澳天速运</a><a href="https://www.kuaidi100.com/all/rlgaus.shtml">澳洲飞跃物流</a><a href="https://www.kuaidi100.com/all/ajlogistics.shtml">澳捷物流</a></dd> <dd><a href="https://www.kuaidi100.com/all/jetexpressgroup.shtml">澳速通国际速递</a><a href="https://www.kuaidi100.com/all/auod.shtml">澳德物流</a><a href="https://www.kuaidi100.com/all/ibuy8.shtml">爱拜物流</a></dd> <dd><a href="https://www.kuaidi100.com/all/anjiatongcheng.shtml">安家同城</a><a href="https://www.kuaidi100.com/all/anmin56.shtml">安敏物流</a><a href="https://www.kuaidi100.com/all/austa.shtml">Austa国际速递</a></dd> <dd><a href="https://www.kuaidi100.com/all/anxl.shtml">安迅物流</a><a href="https://www.kuaidi100.com/all/itaoexpress.shtml">爱淘速递</a><a href="https://www.kuaidi100.com/all/anjiexin56.shtml">安捷信物流</a></dd> <dd><a href="https://www.kuaidi100.com/all/andaoda.shtml">安道达物流</a></dd>
06-06
root@ros-virtual-machine:/home/ros# sudo add-apt-repository "deb http://ppa.launchpad.net/nemh/systemback > /ubuntu xenial main" 命中:1 http://mirrors.ustc.edu.cn/ubuntu bionic InRelease 获取:2 http://mirrors.ustc.edu.cn/ubuntu bionic-updates InRelease [102 kB] 获取:3 http://mirrors.ustc.edu.cn/ubuntu bionic-backports InRelease [102 kB] 获取:4 http://mirrors.ustc.edu.cn/ubuntu bionic-security InRelease [102 kB] 获取:6 http://mirrors.ustc.edu.cn/ubuntu bionic-updates/main amd64 DEP-11 Metadata [297 kB] 获取:7 http://mirrors.ustc.edu.cn/ubuntu bionic-updates/restricted amd64 DEP-11 Metadata [212 B] 获取:5 https://packages.microsoft.com/repos/code stable InRelease [3,590 B] 获取:8 http://mirrors.ustc.edu.cn/ubuntu bionic-updates/universe amd64 DEP-11 Metadata [303 kB] 命中:9 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic InRelease 获取:10 http://mirrors.ustc.edu.cn/ubuntu bionic-updates/multiverse amd64 DEP-11 Metadata [2,468 B] 获取:11 http://mirrors.ustc.edu.cn/ubuntu bionic-backports/main amd64 DEP-11 Metadata [8,108 B] 获取:12 http://mirrors.ustc.edu.cn/ubuntu bionic-backports/restricted amd64 DEP-11 Metadata [216 B] 获取:13 http://mirrors.ustc.edu.cn/ubuntu bionic-backports/universe amd64 DEP-11 Metadata [10.0 kB] 获取:14 http://mirrors.ustc.edu.cn/ubuntu bionic-backports/multiverse amd64 DEP-11 Metadata [216 B] 获取:15 http://mirrors.ustc.edu.cn/ubuntu bionic-security/main amd64 DEP-11 Metadata [77.0 kB] 命中:16 http://packages.osrfoundation.org/gazebo/ubuntu-stable bionic InRelease 获取:17 http://mirrors.ustc.edu.cn/ubuntu bionic-security/restricted amd64 DEP-11 Metadata [212 B] 获取:18 http://mirrors.ustc.edu.cn/ubuntu bionic-security/universe amd64 DEP-11 Metadata [61.9 kB] 忽略:19 http://ppa.launchpad.net/nemh/systemback /ubuntu InRelease 获取:20 http://mirrors.ustc.edu.cn/ubuntu bionic-security/multiverse amd64 DEP-11 Metadata [2,464 B] 获取:21 https://packages.microsoft.com/repos/code stable/main arm64 Packages [20.3 kB] 获取:22 https://packages.microsoft.com/repos/code stable/main amd64 Packages [20.2 kB] 命中:23 http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu bionic InRelease 错误:24 http://ppa.launchpad.net/nemh/systemback /ubuntu Release 404 Not Found [IP: 185.125.190.80 80] 获取:25 https://packages.microsoft.com/repos/code stable/main armhf Packages [20.4 kB] 错误:23 http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu bionic InRelease 下列签名无效: EXPKEYSIG F42ED6FBAB17C654 Open Robotics <info@osrfoundation.org> 正在读取软件包列表... 完成 E: 仓库 “http://ppa.launchpad.net/nemh/systemback /ubuntu Release” 没有 Release 文件。 N: 无法安全地用该源进行更新,所以默认禁用该源。 N: 参见 apt-secure(8) 手册以了解仓库创建和用户配置方面的细节。 W: 校验数字签名时出错。此仓库未被更新,所以仍然使用此前的索引文件。GPG 错误:http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu bionic InRelease: 下列签名无效: EXPKEYSIG F42ED6FBAB17C654 Open Robotics <info@osrfoundation.org>
07-10
import socket # 目标服务器配置 SERVER_IP = "192.168.137.128" SERVER_PORT = 2020 # 构造报文(注意保留原始格式中的转义字符) # http_request = ( # "POST /onvif/device_service HTTP/1.1\r\n" # "Host: 192.168.137.128:443\r\n" # "Content-Length: 961\r\n" # "\r\n" # &#39;<?xml version="1.0" encoding="utf-8"?>&#39; # &#39;<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">&#39; # &#39;<s:Header>&#39; # &#39;<Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">&#39; # &#39;<wsse:UsernameToken xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">&#39; # &#39;<wsse:Username>linzihao</wsse:Username>&#39; # &#39;<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">&#39; # &#39;Qtj7NStEy0jJamBcv7Oi1oU08Yw=&#39; # &#39;</wsse:Password>&#39; # &#39;<wsse:Nonce>Bbly9xla5Dy19zbcdPeWmQ==</wsse:Nonce>&#39; # &#39;<wsu:Created xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">&#39; # &#39;2025-10-30T05:46:37Z&#39; # &#39;</wsu:Created>&#39; # &#39;</wsse:UsernameToken>&#39; # &#39;</Security>&#39; # &#39;</s:Header>&#39; # &#39;<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">&#39; # &#39;<GetDeviceInformation xmlns="http://www.onvif.org/ver10/device/wsdl"/>&#39; # &#39;</s:Body>&#39; # &#39;</s:Envelope>&#39; # ) # 新报文内容(保持原始格式) # http_request = ( # "POST /onvif/device_service HTTP/1.1\r\n" # "Host: 192.168.137.128:2020\r\n" # "Content-Type: application/soap+xml; charset=utf-8\r\n" # "Content-Length: 299\r\n" # "\r\n" # &#39;<?xml version="1.0" encoding="utf-8"?>&#39; # &#39;<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">&#39; # &#39;<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">&#39; # &#39;<GetDeviceInformation xmlns="http://www.onvif.org/ver10/device/wsdl"/>&#39; # &#39;</s:Body>&#39; # &#39;</s:Envelope>&#39; # ) # # 完整的请求报文(包含认证头) # http_request = ( # "POST /onvif/device_service HTTP/1.1\r\n" # "Host: 192.168.137.128:2020\r\n" # "Content-Type: application/soap+xml; charset=utf-8\r\n" # "Authorization: Digest username=\"linzihao\",realm=\"TP-Link IP-Camera\",qop=\"auth\",algorithm=\"MD5\",uri=\"/onvif/device_service\",nonce=\"TP-Link IP-Camera:5f2904695f2904a96b523a5a27b254da817bcd066a3da5c9\",nc=00000001,cnonce=\"5114C0217B52D2821F1C47269DE5453E\",opaque=\"64943214654649846565646421\",response=\"3e0f24492be6874cb69e1955868fbbab\"\r\n" # "Content-Length: 299\r\n" # "\r\n" # &#39;<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"><s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><GetDeviceInformation xmlns="http://www.onvif.org/ver10/device/wsdl"/></s:Body></s:Envelope>&#39; # ) # http_request = ( # "POST /onvif/device_service HTTP/1.1\r\n" # "Host: 192.168.137.128:2020\r\n" # "Content-Type: application/soap+xml; charset=utf-8\r\n" # "Authorization: Digest username=\"linzihao\",realm=\"TP-Link IP-Camera\",qop=\"auth\",algorithm=\"MD5\",uri=\"/onvif/device_service\",nonce=\"TP-Link IP-Camera:06300469063004a9abd05593a8dd46cf654a949e09bcd19d\",nc=00000001,cnonce=\"E1E8051AAE6A37F8E1CC566BFE595431\",opaque=\"64943214654649846565646421\",response=\"d6bfcf48164f931d5cade139ca7db3b6\"\r\n" # "Content-Length: 961\r\n" # "\r\n" # &#39;<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"><s:Header><Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><wsse:UsernameToken xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><wsse:Username>linzihao</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">cBbNASgvn13AWzfBYG/McefZH1o=</wsse:Password><wsse:Nonce>VBuDixHPQa5VhxUtRapNzg==</wsse:Nonce><wsu:Created xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2025-10-31T03:46:58Z</wsu:Created></wsse:UsernameToken></Security></s:Header><s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><GetDeviceInformation xmlns="http://www.onvif.org/ver10/device/wsdl"/></s:Body></s:Envelope>&#39; # ) http_request = ( "POST /onvif/device_service HTTP/1.1\r\n" "Host: 192.168.1.108:80\r\n" "User-Agent: Happytime onvif client V12.7\r\n" "Content-Type: application/soap+xml; charset=utf-8; action=\"http://www.onvif.org/ver10/device/wsdl/GetSystemDateAndTime\"\r\n" "Content-Length: 1783\r\n" "Connection: close\r\n" "\r\n" &#39;<?xml version="1.0" encoding="UTF-8"?>&#39; &#39;<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" &#39; &#39;xmlns:enc="http://www.w3.org/2003/05/soap-encoding" &#39; &#39;xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" &#39; &#39;xmlns:xsd="http://www.w3.org/2001/XMLSchema" &#39; &#39;xmlns:wsa="http://www.w3.org/2005/08/addressing" &#39; &#39;xmlns:ds="http://www.w3.org/2000/09/xmldsig#" &#39; &#39;xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" &#39; &#39;xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" &#39; &#39;xmlns:wsnt="http://docs.oasis-open.org/wsn/b-2" &#39; &#39;xmlns:tt="http://www.onvif.org/ver10/schema" &#39; &#39;xmlns:tds="http://www.onvif.org/ver10/device/wsdl" &#39; &#39;xmlns:trt="http://www.onvif.org/ver10/media/wsdl" &#39; &#39;xmlns:tr2="http://www.onvif.org/ver20/media/wsdl" &#39; &#39;xmlns:tev="http://www.onvif.org/ver10/events/wsdl" &#39; &#39;xmlns:tptz="http://www.onvif.org/ver20/ptz/wsdl" &#39; &#39;xmlns:timg="http://www.onvif.org/ver20/imaging/wsdl" &#39; &#39;xmlns:tan="http://www.onvif.org/ver20/analytics/wsdl" &#39; &#39;xmlns:pt="http://www.onvif.org/ver10/pacs" &#39; &#39;xmlns:tmd="http://www.onvif.org/ver10/deviceIO/wsdl" &#39; &#39;xmlns:trp="http://www.onvif.org/ver10/replay/wsdl" &#39; &#39;xmlns:tse="http://www.onvif.org/ver10/search/wsdl" &#39; &#39;xmlns:trc="http://www.onvif.org/ver10/recording/wsdl" &#39; &#39;xmlns:tac="http://www.onvif.org/ver10/accesscontrol/wsdl" &#39; &#39;xmlns:tdc="http://www.onvif.org/ver10/doorcontrol/wsdl" &#39; &#39;xmlns:tth="http://www.onvif.org/ver10/thermal/wsdl" &#39; &#39;xmlns:tcr="http://www.onvif.org/ver10/credential/wsdl" &#39; &#39;xmlns:tar="http://www.onvif.org/ver10/accessrules/wsdl" &#39; &#39;xmlns:tsc="http://www.onvif.org/ver10/schedule/wsdl" &#39; &#39;xmlns:trv="http://www.onvif.org/ver10/receiver/wsdl" &#39; &#39;xmlns:tpv="http://www.onvif.org/ver10/provisioning/wsdl" &#39; &#39;xmlns:ter="http://www.onvif.org/ver10/error" >&#39; &#39;<s:Body>&#39; &#39;<tds:GetSystemDateAndTime />&#39; &#39;</s:Body>&#39; &#39;</s:Envelope>&#39; ) # http_request = ( # "POST /onvif/device_service HTTP/1.1\r\n" # "Host: 192.168.1.108\r\n" # "Content-Type: application/soap+xml; charset=utf-8\r\n" # "Authorization: Digest username=\"admin\",realm=\"Login to 595f962a6e49d8c157d1dea6218657f2\",qop=\"auth\",algorithm=MD5,uri=\"/onvif/device_service\",nonce=\"b252aWYtZGlnZXN0OjY0NDI1MzAwNDAw\",nc=00000001,cnonce=\"AEE942C585CA9337AA6C932729B168B3\",opaque=\"\",response=\"7641183682761179bf6925bc454c8e0e\"\r\n" # "Content-Length: 299\r\n" # "\r\n" # &#39;<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"><s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><GetDeviceInformation xmlns="http://www.onvif.org/ver10/device/wsdl"/></s:Body></s:Envelope>&#39; # ) try: # 创建TCP套接字 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((SERVER_IP, SERVER_PORT)) s.sendall(http_request.encode(&#39;utf-8&#39;)) print(f"已发送 {len(http_request)} 字节到 {SERVER_IP}:{SERVER_PORT}") # 接收响应(可选) response = s.recv(4096) print(f"收到响应: {response.decode(&#39;utf-8&#39;)}") except Exception as e: print(f"发送失败: {str(e)}") )——POST /onvif/device_service HTTP/1.1 Host: 192.168.1.108 Content-Type: application/soap+xml; charset=utf-8 Content-Length: 299 <?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"><s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><GetDeviceInformation xmlns="http://www.onvif.org/ver10/device/wsdl"/></s:Body></s:Envelope>——把这个报文转代码中形式
最新发布
11-04
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值