bat脚本for循环
for /l %%i in (490,28,742) do (
adb shell setprop vendor.debug.af_motor.position %%i
adb shell sleep 0.4 )
基本语法
echo test
@echo off
for %%a in (A B C) do echo %%a
pause
在Windows批处理(.bat)脚本中,for
命令用于重复执行一系列命令,直到遍历完指定的集合。以下是一些for
循环的常见用法示例:
-
基本用法:
for %%i in (1 2 3 4 5) do ( echo Number: %%i )
-
遍历文件:
for %%i in (*.txt) do ( echo Processing file: %%i )
-
遍历目录中的文件:
for /r %%i in (*) do ( echo Found file: %%i )
-
使用变量作为循环范围:
set start=1 set end=5 for /l %%i in (%start%,1,%end%) do ( echo Number: %%i )
-
遍历命令输出:
for /f "tokens=*" %%i in ('dir') do ( echo File: %%i )
-
遍历环境变量:
set fruits=apple banana cherry for %%i in (%fruits%) do ( echo Fruit: %%i )
-
嵌套for循环:
for /l %%i in (1,1,3) do ( for /l %%j in (1,1,3) do ( echo i: %%i, j: %%j ) )
-
使用
find
命令遍历文件:for /f "delims=" %%i in ('findstr /m "search_pattern" file.txt') do ( echo Found: %%i )
-
使用
findstr
命令遍历字符串:set "text=This is a test string with the word test." for /f "tokens=*" %%i in ("%text%") do ( echo Word: %%i )
-
使用
for /f
命令遍历分隔符分隔的字符串:set "data=one,two,three,four" for /f "tokens=1* delims=," %%a in ("%data%") do ( echo First: %%a for /f "tokens=1* delims=," %%b in ("%%a") do ( echo Second: %%b ) )
-
使用
for /f
命令遍历命令输出:for /f "tokens=*" %%i in ('ipconfig ^| findstr /i "IPv4"') do ( echo IP Address: %%i )
-
使用
for /f
命令遍历文件行:for /f "delims=" %%i in (file.txt) do ( echo Line: %%i )
在批处理脚本中,%%
是变量的扩展符号,用于在for
循环中引用变量。在命令行直接执行时,使用单个百分号%
。例如,如果你在命令行中而不是在批处理文件中运行这些命令,你应该将%%i
替换为%i
。
shell脚本
# 循环设置af_motor.position属性
for i in $(seq 463 34 768); do
adb shell setprop vendor.debug.af_motor.position $i
adb shell sleep 0.4
done
在Linux shell脚本中,for
循环是一种常用的控制结构,用于重复执行一系列命令。以下是一些for
循环的常见用法示例:
-
基本用法:
for i in 1 2 3 4 5 do echo "Number: $i" done
-
遍历数组:
array=("apple" "banana" "cherry") for i in "${array[@]}" do echo "Fruit: $i" done
-
C风格的for循环:
for ((i=0; i<5; i++)) do echo "Number: $i" done
-
使用seq生成序列:
for i in $(seq 1 5) do echo "Number: $i" done
-
遍历文件行:
for line in $(cat file.txt) do echo "Line: $line" done
-
使用通配符遍历文件:
for file in *.txt do echo "Processing file: $file" done
-
遍历目录:
for dir in /path/to/directory/*/ do echo "Directory: $dir" done
-
使用变量作为循环范围:
start=1 end=5 for ((i=start; i<=end; i++)) do echo "Number: $i" done
-
使用管道传递数据:
for i in $(ls | grep ".txt") do echo "File: $i" done
-
嵌套for循环:
for i in {1..3} do for j in {1..3} do echo "i: $i, j: $j" done done
-
使用read命令遍历文件:
while IFS= read -r line do echo "Line: $line" done < "file.txt"
-
使用find命令遍历文件系统:
for file in $(find /path/to/search -type f) do echo "File: $file" done