Bash循环在Android中只能进行几次迭代

时间:2016-01-15 03:04:57

标签: android bash while-loop

我尝试在bash中创建一个程序来捕获Android中的原始屏幕截图,然后将其提供给计算机中的程序。但是脚本代码似乎只运行了两次(之前只运行了8次)。

我已经多次手动检查变量的值,用引号将它们关闭,甚至在无限循环中打破条件。不知何故,它总是在几次迭代后停止(早先它停在8,现在只停在两个)。

#!/bin/bash
set -x # Debug
# Arguments: [[Sector[0-F]] alternate]
# Grab the screenshot
screencap /sdcard/screen.raw
# Grab the header
sxy=(`od -An -tu -N12 /sdcard/screen.raw`)
x=${sxy[0]}
y=${sxy[1]}
fmt=${sxy[2]}
# If the format is not RGB888, return
if [[ $fmt -ne 1 ]]; then
    return
fi
# Remove the "information", so it is easier to work using dd
tail -c+13 /sdcard/screen.raw > /sdcard/tmpscreen.raw
rm /sdcard/screen.raw
mv /sdcard/tmpscreen.raw /sdcard/screen.raw
# Output screen info
echo -n ":::$x:::$y:::"
maxBytes=$(($x*$y/4)) #one-sixteenth lines
case $1 in
    1 ) curline=$(($x/16)) ;;
    2 ) curline=$(($x/8)) ;;
    3 ) curline=$(($x*3/16)) ;;
    4 ) curline=$(($x/4)) ;;
    5 ) curline=$(($x*5/16)) ;;
    6 ) curline=$(($x*3/8)) ;;
    7 ) curline=$(($x*7/16)) ;;
    8 ) curline=$(($x/2)) ;;
    9 ) curline=$(($x*9/16)) ;;
    A ) curline=$(($x*5/8)) ;;
    B ) curline=$(($x*11/16)) ;;
    C ) curline=$(($x*3/4)) ;;
    D ) curline=$(($x*13/16)) ;;
    E ) curline=$(($x*7/8)) ;;
    F ) curline=$(($x*15/16)) ;;
    * ) curline=0 ;;
esac
line=0
bs=$(($x*4))
# Let the script end and program pull first screen
if [[ ! -f /sdcard/oldscreen.raw ]]; then
    mv /sdcard/screen.raw /sdcard/oldscreen.raw
    return
fi
maxLine=$(($maxBytes/$bs))
truncate -s 0 /sdcard/pull.raw
while [[ ("$curline" < "$y") && ("$line" < "$maxLine") ]]; do
    dd if=/sdcard/screen.raw of=/sdcard/line.raw bs="$bs" count=1 \
     skip="$curline" 2> /dev/null
    dd if=/sdcard/oldscreen.raw of=/sdcard/oldline.raw bs="$bs" \
     count=1 skip="$curline" 2> /dev/null
    # Compare lines
    cmp -s /sdcard/oldline.raw /sdcard/line.raw > /dev/null
    # if don't match add line to out file and increment line counter
    # cmp returns 0 for equal, 1 for unequal and >1 for error
    if [[ $? -ne 0 ]]; then # || $(stat -c%s /sdcard/oldline.raw) -eq 0 
        echo -n "$curline" >> /sdcard/pull.raw
        `cat /sdcard/line.raw >> /sdcard/pull.raw`
        ((line++))
    fi
    echo $curline # Debug
    ((curline++))
done
# Clean up
rm /sdcard/line.raw
rm /sdcard/oldline.raw
rm /sdcard/oldscreen.raw
mv /sdcard/screen.raw /sdcard/oldscreen.raw

上述程序的作用是:

  1. 将屏幕尺寸详细信息放入控制台
  2. 从扇区向前抓取线(0-F:每个是屏幕的十六分之一)
  3. 与较旧的屏幕截图相比较,仅输出过时的行(最多一次十六分之一),行号[4个字符:使用功能完成]
  4. 清理将当前屏幕截图保留为旧屏幕截图和过时数据
  5. 如果没有较旧的屏幕截图,它会在第一次迭代中退出。

    修改1:

    上述代码的测试环境是:

    • 启用USB调试的Android设备/模拟器
    • 在任何操作系统(用于计算机)上通过adb桥连接(工作)
    • 通过screencap截取的屏幕截图提供了raw format
    • 将上述代码添加到/sdcard/screenshot.sh
    • 运行模拟器/命令提示符
    • 在终端上运行adb shell-s _device_sh /sdcard/screenshot/sh(假设您位于 adb路径

    现在,当我在第二行添加set -x后运行时,我得到以下输出:

    C:\Program Files (x86)\Minimal ADB and Fastboot>adb shell
    shell@osprey_ud2:/ $ sh /sdcard/screenshot.sh
    + screencap /sdcard/screen.raw
    + od -An -tu -N12 /sdcard/screen.raw
    + set -A sxy -- 720 1280 1
    + x=720
    + y=1280
    + fmt=1
    + >/sdcard/tmpscreen.raw
    + tail -c+13 /sdcard/screen.raw
    + rm /sdcard/screen.raw
    + mv /sdcard/tmpscreen.raw /sdcard/screen.raw
    + echo -n :::720:::1280:::
    :::720:::1280:::+ maxBytes=230400
    + curline=0
    + line=0
    + bs=2880
    + mv /sdcard/screen.raw /sdcard/oldscreen.raw
    + return
    shell@osprey_ud2:/ $ sh /sdcard/screenshot.sh
    + screencap /sdcard/screen.raw
    + od -An -tu -N12 /sdcard/screen.raw
    + set -A sxy -- 720 1280 1
    + x=720
    + y=1280
    + fmt=1
    + >/sdcard/tmpscreen.raw
    + tail -c+13 /sdcard/screen.raw
    + rm /sdcard/screen.raw
    + mv /sdcard/tmpscreen.raw /sdcard/screen.raw
    + echo -n :::720:::1280:::
    :::720:::1280:::+ maxBytes=230400
    + curline=0
    + line=0
    + bs=2880
    + maxLine=80
    + truncate -s 0 /sdcard/pull.raw
    + 2>/dev/null
    + dd 'if=/sdcard/screen.raw' 'of=/sdcard/line.raw' 'bs=2880' 'count=1' 'skip=0'
    + 2>/dev/null
    + dd 'if=/sdcard/oldscreen.raw' 'of=/sdcard/oldline.raw' 'bs=2880' 'count=1' 'skip=0'
    + >/dev/null
    + cmp -s /sdcard/oldline.raw /sdcard/line.raw
    + >>/sdcard/pull.raw
    + echo -n 0
    + >>/sdcard/pull.raw
    + cat /sdcard/line.raw
    +
    + 'let]' line++
    + echo 0
    0
    + 'let]' curline++
    + 2>/dev/null
    + dd 'if=/sdcard/screen.raw' 'of=/sdcard/line.raw' 'bs=2880' 'count=1' 'skip=1'
    + 2>/dev/null
    + dd 'if=/sdcard/oldscreen.raw' 'of=/sdcard/oldline.raw' 'bs=2880' 'count=1' 'skip=1'
    + >/dev/null
    + cmp -s /sdcard/oldline.raw /sdcard/line.raw
    + >>/sdcard/pull.raw
    + echo -n 1
    + >>/sdcard/pull.raw
    + cat /sdcard/line.raw
    +
    + 'let]' line++
    + echo 1
    1
    + 'let]' curline++
    + rm /sdcard/line.raw
    + rm /sdcard/oldline.raw
    + rm /sdcard/oldscreen.raw
    + mv /sdcard/screen.raw /sdcard/oldscreen.raw
    shell@osprey_ud2:/ $ sh /sdcard/screenshot.sh
    + screencap /sdcard/screen.raw
    + od -An -tu -N12 /sdcard/screen.raw
    + set -A sxy -- 720 1280 1
    + x=720
    + y=1280
    + fmt=1
    + >/sdcard/tmpscreen.raw
    + tail -c+13 /sdcard/screen.raw
    + rm /sdcard/screen.raw
    + mv /sdcard/tmpscreen.raw /sdcard/screen.raw
    + echo -n :::720:::1280:::
    :::720:::1280:::+ maxBytes=230400
    + curline=0
    + line=0
    + bs=2880
    + maxLine=80
    + truncate -s 0 /sdcard/pull.raw
    + dd 'if=/sdcard/screen.raw' 'of=/sdcard/line.raw' 'bs=2880' 'count=1' 'skip=0'
    1+0 records in
    1+0 records out
    2880 bytes transferred in 0.001 secs (2880000 bytes/sec)
    + dd 'if=/sdcard/oldscreen.raw' 'of=/sdcard/oldline.raw' 'bs=2880' 'count=1' 'skip=0'
    1+0 records in
    1+0 records out
    2880 bytes transferred in 0.001 secs (2880000 bytes/sec)
    + cmp -s /sdcard/oldline.raw /sdcard/line.raw
    + >>/sdcard/pull.raw
    + echo -n 0
    + >>/sdcard/pull.raw
    + cat /sdcard/line.raw
    +
    + 'let]' line++
    + echo 0
    0
    + 'let]' curline++
    + dd 'if=/sdcard/screen.raw' 'of=/sdcard/line.raw' 'bs=2880' 'count=1' 'skip=1'
    1+0 records in
    1+0 records out
    2880 bytes transferred in 0.001 secs (2880000 bytes/sec)
    + dd 'if=/sdcard/oldscreen.raw' 'of=/sdcard/oldline.raw' 'bs=2880' 'count=1' 'skip=1'
    1+0 records in
    1+0 records out
    2880 bytes transferred in 0.001 secs (2880000 bytes/sec)
    + cmp -s /sdcard/oldline.raw /sdcard/line.raw
    + >>/sdcard/pull.raw
    + echo -n 1
    + >>/sdcard/pull.raw
    + cat /sdcard/line.raw
    +
    + 'let]' line++
    + echo 1
    1
    + 'let]' curline++
    + rm /sdcard/line.raw
    + rm /sdcard/oldline.raw
    + rm /sdcard/oldscreen.raw
    + mv /sdcard/screen.raw /sdcard/oldscreen.raw
    shell@osprey_ud2:/ $
    

    我对输出的看法:当我检查if而不是$? -ne 1时,它没有进入$? -ne 0声明。所以,我进行了修正,但在第二次迭代后它仍然存在。

    所以,我将输出删除到/dev/null以查看错误。但这只给了我更多的输出,没有关于循环退出的原因的信息。

1 个答案:

答案 0 :(得分:0)

检查shell http://www.tldp.org/LDP/abs/html/comparison-ops.html

的运算符语法

你的while循环应该是这样的 ( -lt 而不是&lt;

while [[ ("$curline" -lt "$y") && ("$line" -lt "$maxLine") ]]