BASH自动化流程

时间:2012-03-01 20:13:22

标签: bash

我设法让Ubuntu在移动设备上运行。我需要在其上自动化一些过程,因为如果没有复杂的设置和焊接线,用户输入是完全不可能的。

我需要运行“parted print”然后将“yes,fix,fix”管道输入到stdin这里是所需的输出:

~ # parted /dev/block/mmcblk0 print
parted /dev/block/mmcblk0 print
Warning: /dev/block/mmcblk0 contains GPT signatures, indicating that it has a
GPT table.  However, it does not have a valid fake msdos partition table, as it
should.  Perhaps it was corrupted -- possibly by a program that doesn't
understand GPT partition tables.  Or perhaps you deleted the GPT table, and are
now using an msdos partition table.  Is this a GPT partition table?
Yes/No? yes
yes
yes
Error: The backup GPT table is not at the end of the disk, as it should be.
This might mean that another operating system believes the disk is smaller.
Fix, by moving the backup to the end (and removing the old backup)?
Fix/Ignore/Cancel? fix
fix
fix
Warning: Not all of the space available to /dev/block/mmcblk0 appears to be
used, you can fix the GPT to use all of the space (an extra 569312 blocks) or
continue with the current setting?
Fix/Ignore? fix
fix
fix
Model: MMC SEM16G (sd/mmc)
Disk /dev/block/mmcblk0: 15.9GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system  Name        Flags
 1      131kB   262kB   131kB                xloader
 2      262kB   524kB   262kB                bootloader
 3      524kB   16.3MB  15.7MB               recovery
 4      16.8MB  33.6MB  16.8MB               boot
 5      33.6MB  83.9MB  50.3MB               rom
 6      83.9MB  134MB   50.3MB               bootdata
 7      134MB   522MB   388MB                factory
 8      522MB   1164MB  642MB                system
 9      1164MB  1611MB  447MB                cache
10      1611MB  2684MB  1074MB               media
11      2684MB  15.6GB  12.9GB               userdata

这是我起草的内容..

#! /bin/bash
mkfifo Input
mkfifo Output
#Redirect commandline input from fifo to parted, Redirect output to fifo, background
cat Input &| - parted print >Output &
Line=""
while [ 1 ]
  do 
  while read Line
  do 
      if [ $Line == *Yes\/No\?* ]; then
        echo "yes">Input
      fi
      if [ $Line == *Fix\/Ignore/\Cancel\?* ]; then
        echo "fix">Input
      fi
      if [ $Line == *Fix\/Ignore\?* ]; then
        echo "fix">Input
      fi
      test $Line == *userdata* && break
  done<Output
  test $Line == *userdata* && break
done

但这不起作用。如果有人可以协助我将程序中的输出重定向到fifo,那么分析该数据并将输出引导到另一个fifo中以便放回到原始程序中?所需的结果位于第一个代码块中。

1 个答案:

答案 0 :(得分:0)

如果您始终知道所需的输入是什么 - 如果它们从不在运行中更改 - 您只需重定向文件或HERE文档中的输入,您就不需要做任何复杂的事情。< / p>

如果所需的输入在运行之间发生变化,则需要使用shell之外的其他内容,因为它不会使您尝试执行的操作成为可能。 perl可能是个不错的选择。 (你不需要在这里使用expect,因为你没有尝试模拟tty。)