解释一下这段代码set "line=%%a" if "!line:%config_item%=!" neq "!line!"
时间: 2023-12-08 10:06:09 浏览: 28
这段代码是在 Windows 系统的批处理脚本中使用的,其作用是判断一个字符串变量 `line` 是否包含另一个字符串变量 `config_item`,如果包含则执行相应的操作。
具体解释如下:
1. `set "line=%%a"`:将循环变量 `%%a` 的值赋给 `line` 变量。
2. `!line:%config_item%=!`:这是一个字符串替换操作,将 `line` 变量中所有的 `config_item` 字符串替换为空字符串。
3. `neq`:表示不相等的条件判断符号。
4. `"!line:%config_item%=!" neq "!line!"`:如果 `line` 变量中包含 `config_item` 字符串,则经过替换后的值与原值不相等,此时条件成立,执行相应的操作。注意 `!` 符号是用来开启延迟扩展的,这样可以将变量 `line` 和 `config_item` 的值在运行时才进行替换和比较,而不是在解析时就替换和比较。
总的来说,这段代码的作用是在批处理脚本中,根据 `config_item` 变量的值,判断 `line` 变量中是否包含该字符串,如果包含,则执行相应的操作。
相关问题
@echo off setlocal EnableDelayedExpansion set "file=path/to/file.txt" set "tempfile=%file%.temp" set "linenumber=10" rem read the file and write the modified content to a temporary file set "count=0" (for /f "usebackq delims=" %%i in ("%file%") do ( set /a "count+=1" if !count! neq %linenumber% echo %%i )) > "%tempfile%" rem overwrite the original file with the modified content move /y "%tempfile%" "%file%" > nul 添加一个替换指定多少行文本内容功能
好的,以下是一个示例批处理脚本,用于替换指定路径下的notepad文本文件中的指定行的内容,并将修改后的内容保存到原文件中。请将其中的"path/to/file.txt"替换为您需要处理的文件路径,将"10"替换为您需要替换内容的行数,将"old text"替换为需要替换的旧文本内容,将"new text"替换为需要替换成的新文本内容。
```
@echo off
setlocal EnableDelayedExpansion
set "file=path/to/file.txt"
set "tempfile=%file%.temp"
set "linenumber=10"
set "oldtext=old text"
set "newtext=new text"
rem read the file and write the modified content to a temporary file
set "count=0"
(for /f "usebackq delims=" %%i in ("%file%") do (
set /a "count+=1"
if !count! neq %linenumber% (
echo %%i
) else (
echo %%i | findstr /v /c:"%oldtext%" || (
set "line=%%i"
set "line=!line:%oldtext%=%newtext%!"
echo !line!
)
)
)) > "%tempfile%"
rem overwrite the original file with the modified content
move /y "%tempfile%" "%file%" > nul
```
注意:请谨慎使用此脚本,尤其是对于重要的文件。建议先备份原文件,以免数据丢失。
将如下bat脚本转换为shell脚本语言:@echo off set count=0 dir E:\ > nul 2>&1 if not %errorlevel% == 0 ( (echo E not exist) & pause && exit ) else ( (echo E exist) ) setlocal enabledelayedexpansion :loop for /f "skip=1 tokens=1" %%a in ('adb devices') do ( if not "%%a"=="List" ( set /a count+=1 ) ) if %count% gtr 0 ( echo found %count% devices goto end ) else ( echo device list is empty goto loop ) :end :read sn :: 将文件系统挂载为可写 adb root adb remount :: 获取当前时间戳,格式为yyyymmdd_hhmmss set timestamp=%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%%time:~6,2% :: 获取设备SN set retry_times=0 :get sn for /f "delims=" %%i in ('adb shell getprop ro.serialno') do set "devicesn=%%i" set /a "retry_times=retry_times+1" if not defined devicesn ( if %retry_times% leq 2 ( echo Failed to read device SN, try again. Retry count= %retry_times% goto :get sn ) else ( echo Failed to read device SN after multiple retries, please check the connection and try again later. pause && exit ) ) echo devicesn=%devicesn% timestamp=%timestamp% :: 以设备序列号和时间戳为文件夹名创建目录 set device_dir=%devicesn%_%timestamp% md "E:\Raven_DualCam\%device_dir%" && echo create "%device_dir%" success if %errorlevel% neq 0 ( echo create "%device_dir%" fail pause && exit ) :: 将指定的文件pull到E盘 adb pull /mnt/vendor/persist/camera/dualcam_cali.bin E:\Raven_DualCam\%device_dir%\dualcam_cali.bin adb pull /mnt/sdcard/DCIM/DoubleCam E:\Raven_DualCam\%device_dir%\DoubleCam pause
#!/bin/bash
count=0
ls /E > /dev/null 2>&1
if [ $? -ne 0 ]
then
echo "E not exist"
read -p "Press any key to continue..." -n1 -s
exit
else
echo "E exist"
fi
while true
do
count=0
for device in $(adb devices | awk 'NF==2{print $1}')
do
if [ $device != "List" ]
then
((count++))
fi
done
if [ $count -gt 0 ]
then
echo "found $count devices"
break
else
echo "device list is empty"
fi
done
devicesn=""
retry_times=0
while [ -z "$devicesn" ]
do
devicesn=$(adb shell getprop ro.serialno | tr -d '\r')
((retry_times++))
if [ -z "$devicesn" ]
then
if [ $retry_times -le 2 ]
then
echo "Failed to read device SN, try again. Retry count=$retry_times"
else
echo "Failed to read device SN after multiple retries, please check the connection and try again later."
read -p "Press any key to continue..." -n1 -s
exit
fi
fi
done
echo "devicesn=$devicesn"
timestamp=$(date +"%Y%m%d_%H%M%S")
echo "timestamp=$timestamp"
device_dir="$devicesn"_"$timestamp"
mkdir -p "/E/Raven_DualCam/$device_dir" && echo "create $device_dir success"
if [ $? -ne 0 ]
then
echo "create $device_dir fail"
read -p "Press any key to continue..." -n1 -s
exit
fi
adb root
adb remount
adb pull /mnt/vendor/persist/camera/dualcam_cali.bin "/E/Raven_DualCam/$device_dir/dualcam_cali.bin"
adb pull /mnt/sdcard/DCIM/DoubleCam "/E/Raven_DualCam/$device_dir/DoubleCam"
read -p "Press any key to continue..." -n1 -s