skip to Main Content

how do i set the virtual terminal size with shell_exec() ? i tried

function shell_exec_with_size(string $cmd, int $columns = 999, int $rows = 999): string
{
    $cmd = '/bin/bash -c ' . escapeshellarg("stty columns {$columns}; stty rows {$rows};{$cmd}");
    return shell_exec($cmd);
}

but that seems to have no effect, at least the columns property wasn’t actually applied (don’t know if the rows property was set or not)..

having a problem with yum check-updates changing the format depending on column size, with a "small" column size and a long update name, the format is

python-devel.x86_64                   2.7.18-1.amzn2.0.5       amzn2-core       
python-libs.x86_64                    2.7.18-1.amzn2.0.5       amzn2-core       
python-pillow.x86_64                  2.0.0-23.gitd1c6db8.amzn2.0.1
                                                               amzn2-core       

(where python-pillow’s update name 2.0.0-23.gitd1c6db8.amzn2.0.1 is too long), but with a large terminal size it’s instead printed as

python.x86_64                                                                                  2.7.18-1.amzn2.0.5                                                                      amzn2-core       
python-devel.x86_64                                                                            2.7.18-1.amzn2.0.5                                                                      amzn2-core       
python-libs.x86_64                                                                             2.7.18-1.amzn2.0.5                                                                      amzn2-core       
python-pillow.x86_64                                                                           2.0.0-23.gitd1c6db8.amzn2.0.1                                                           amzn2-core       
python2-rpm.x86_64                                                                             4.11.3-48.amzn2.0.2                                                                     amzn2-core

and im trying to parse the list programmatically, so specifying the terminal size would help get a consistent format from yum check-updates… also i have checked if yum perhaps separates the properties with tabs or something: it doesn’t. it just seem to separate the properties with spaces, or newline and spaces, depending on terminal size.

2

Answers


  1. Chosen as BEST ANSWER

    ... don't know why, but using util-linux's script command instead of bash makes stty functional, combining script --command 'stty columns 999;cmd' /dev/null works:

    function shell_exec_with_size(string $cmd, int $columns = 999, int $rows = 999): string
    {
        $cmd = "stty columns {$columns}; stty rows {$rows};{$cmd}";
        $cmd = 'script --quiet --return --command ' . escapeshellarg($cmd) . ' /dev/null';
        return shell_exec($cmd);
    }
    

    (i also suspect, but haven't confirmed, that script calls /bin/sh behind-the-scene)


  2. An alternate approach is to pipe it out to a file and then read it back with file_get_contents

    EDIT: Although now that I think about it, I’m not sure how yum will play with being piped to a file. Can you look in /var/log/yum instead?

    <?php
    
    /*
    
    Author: hanshenrik
    Question: php how to specify virtual terminal size in shell_exec()?
    URL: https://stackoverflow.com/questions/74179969/php-how-to-specify-virtual-terminal-size-in-shell-exec
    Tags: php, terminal, pty
    
    */
    
    function shell_exec_with_file_output(string $cmd, string $tmp_file = 'shell_exec_with_file_output'): string 
    {
        shell_exec($cmd . ' > ' . $tmp_file);
        $content = file_get_contents($tmp_file);
        unlink($tmp_file);
        return $content;
    }
    
    $output = shell_exec_with_file_output('bash ./test.sh');
    
    var_dump($output);
    

    Then with test.sh

    #!/bin/bash
    
    echo "python.x86_64                                                                                  2.7.18-1.amzn2.0.5                                                                      amzn2-core"      
    echo "python-devel.x86_64                                                                            2.7.18-1.amzn2.0.5                                                                      amzn2-core"      
    echo "python-libs.x86_64                                                                             2.7.18-1.amzn2.0.5                                                                      amzn2-core"      
    echo "python-pillow.x86_64                                                                           2.0.0-23.gitd1c6db8.amzn2.0.1                                                           amzn2-core"      
    echo "python2-rpm.x86_64                                                                             4.11.3-48.amzn2.0.2                                                                     amzn2-core"
    

    Outputs:

    string(975) "python.x86_64                                                                                  2.7.18-1.amzn2.0.5                                                                      amzn2-core
    python-devel.x86_64                                                                            2.7.18-1.amzn2.0.5                                                                      amzn2-core
    python-libs.x86_64                                                                             2.7.18-1.amzn2.0.5                                                                      amzn2-core
    python-pillow.x86_64                                                                           2.0.0-23.gitd1c6db8.amzn2.0.1                                                           amzn2-core
    python2-rpm.x86_64                                                                             4.11.3-48.amzn2.0.2                                                                     amzn2-core
    "
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search