我有以下命令可以正常工作,但我想以某种方式添加另一个变量:
#!/bin/bash
x=40000
until [ $x = "180000" ]; do
dd bs=1 if=static.file of=extracted${x}.file skip=12345 count=$x;
first_ten=$(hexdump -e '1/1 "%.2X"' "extracted${x}.file" | head -c 10);
if [ "$first_ten" == "1234567890" ]
then
echo "${x}" >> correct.txt;
fi;
rm extracted${x}.file;
((x++))
done
我想在跳过部分添加一个增量变量,这样一旦完成“x”变量,就会将(跳过)“y”变量增加 1,并重新开始该过程。
#!/bin/bash
y=12345
x=40000
until [ $x = "180000" ]; do
dd bs=1 if=static.file of=extracted${x}.file skip=$x count=$x;
first_ten=$(hexdump -e '1/1 "%.2X"' "extracted${x}.file" | head -c 10);
if [ "$first_ten" == "1234567890" ]
then
echo "${y}_${x}" >> correct.txt;
fi;
rm extracted${x}.file;
((x++))
done
只是不完全确定如何实现这一点。
答案1
您可以使用两个for
循环,即一个 fory
和一个 for x
(或while
/until
在y
的循环中,即外循环):
#!/bin/bash
for ((y=0; y<12345; y++)); do
for ((x=40000; x<180000; x++)); do
dd bs=1 if=static.file of=extracted${x}.file skip=12345 count=$x;
first_ten=$(hexdump -e '1/1 "%.2X"' "extracted${x}.file" | head -c 10);
if [ "$first_ten" == "1234567890" ]
then
echo "${x}" >> correct.txt;
fi;
rm extracted${x}.file;
done
done
更改值以满足您的需要。
例子:
% cat scr.sh
#!/bin/bash
for ((y=0; y<=3; y++)); do
for ((x=0; x<=2; x++)); do
echo "This is $x : $y"
done
done
% ./scr.sh
This is 0 : 0
This is 1 : 0
This is 2 : 0
This is 0 : 1
This is 1 : 1
This is 2 : 1
This is 0 : 2
This is 1 : 2
This is 2 : 2
This is 0 : 3
This is 1 : 3
This is 2 : 3
答案2
如果你的意思是要测试两个变量,请使用 && 运算符和另一个测试
until [ $x -eq 180000" ] && [ $y -eq 9999 ]; do