Search code examples
androidshellstdoutdd

Printing a file to a process stdout, using dd (Android)


I am trying to get the output of dd the following way. The idea is to programmatically capture whatever dd sends to "of".

//Process p;
// ...
p = Runtime.getRuntime().exec(new String[]{"su", "-c", "dd if=/dev/something of=stdout bs=1024"});

Using "cat /some_file" instead of "dd ..." command works as expected but I wanted this to work with dd.

What should I use for stdout, as /dev/stdout does not exist in Android?

Will this even work?

You can assume the app has root privileges.


Solution

  • If the device is not too big you could dd it to a file and then cat the file to stout ...

    //Process p; 
    // ... 
    p = Runtime.getRuntime().exec(new String[]{"su", "-c", "dd if=/dev/something of=/dev/mnt/sdcard/myfile bs=1024"}); 
    p = Runtime.getRuntime().exec(new String[]{"cat", "/dev/mnt/sdcard/myfile"}); 
    

    good luck

    --EDITED--

    I've just remembered that if you omit the "of=" operand the "dd" command will write to the stdout. So your code change to:

    //Process p;  
    // ...  
    p = Runtime.getRuntime().exec(new String[]{"su", "-c", "dd if=/dev/something bs=1024"});
    

    I've tested it and it works.