Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: How can I redirect stdout to a variable

  1. #1
    Join Date
    Mar 2008
    Location
    Oz
    Posts
    10,946
    Blog Entries
    2

    Default How can I redirect stdout to a variable

    I can store the stdout from a shell command by redirecting it to a file. For example I can store the partition structure in data.txt like this:
    Code:
    fdsik -l > data.txt
    But I don't want to store that on disk. Is there a way to define a file in RAM and store it there for a while? Or is htere a way to store it in an environment variable for a while?

  2. #2
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,941
    Blog Entries
    4

    Default Re: How can I redirect stdout to a variable

    You can store it into a (environment) variable with:

    Code:
    var="$(fdisk -l)"
    or using old style shell backtick syntax for portability:

    Code:
    var="`fdisk-l`"
    However this variable is only available to the current shell. If it's an environment variable, you can export it to descendant shells:

    Code:
    export VAR="$(fdisk -l)"
    or

    Code:
    VAR="`fdisk -l`"
    export VAR
    If you want it to hang around for a while, you'll have to use a file.

  3. #3
    Join Date
    Mar 2008
    Location
    Oz
    Posts
    10,946
    Blog Entries
    2

    Default Re: How can I redirect stdout to a variable

    Thanks ken_yap. Works a treat.

    Is there a way to keep the CR/LF stuff in, or grep the data into individual lines, or replace CR/LFs with another character so I can later parse them in a language that's a derivative of c. Currently I get it in an unbroken string and I want to ingest the $var into a program using c's environment variable ability, parse it and use it to paint a GUI of the partitions. Ordinarily a storage file would be OK, but I want to do this on a live CD (which is another issue as you know).

  4. #4
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,941
    Blog Entries
    4

    Default Re: How can I redirect stdout to a variable

    On Unix it's only LF, no CR.

    You have to realise that the loss of the LF is at the expansion stage. Try this:

    Code:
    a=$(echo -e "a\nb")
    echo "$a" | od -bc

  5. #5
    Join Date
    Mar 2008
    Location
    Oz
    Posts
    10,946
    Blog Entries
    2

    Default Re: How can I redirect stdout to a variable

    OK thanks -- nearly got it.
    I have a book "From Bash to Z shell". It's time to read it and follow the examples.

    The thing I've learned is that "Yes" it's possible to define an environment variable and use it to get info into the program that interrogated the system it is running in.

  6. #6
    Join Date
    Mar 2008
    Location
    Oz
    Posts
    10,946
    Blog Entries
    2

    Default Re: How can I redirect stdout to a variable

    OK progress in leaps and bounds:
    I got to thinking about that stuff and pipes -- must be much the same thing as a pipe -- so:
    This code reads stuff in line by line through a pipe that preserves the lines beautifully for parsing:
    Code:
    Dim as string cmd, txt(1000)
    Dim as Integer i
    cmd = "fdisk -l"
    
    Open Pipe cmd For Input As #1
    Print "Output of", cmd
    
    i = 0
    Do While Not EOF(1)
    i = i + 1
    Line Input #1, txt(i)
    Print txt(i)
    Loop
    
    Close #1
    End
    And it runs nicely, printing out the variable txt(i) line by line as a faithful reproduction of the fdisk command. So thanks ken_yap for the ideas.

  7. #7
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,941
    Blog Entries
    4

    Default Re: How can I redirect stdout to a variable

    Or in Perl:

    Code:
    #!/usr/bin/perl -w
    open(P, "fdisk -l |") or die;
    while (<P>) {
      print $_;
    }

  8. #8
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    15,739

    Default Re: How can I redirect stdout to a variable

    Now this is a quicky because I am almost gone for a few weeks. And I am not sure if you are now after saving in a variable or interpreting the output in a script. I always do interpreting in a shell in this form:
    Code:
    fdisk -l | while read LINE
    do      ; # the parsing of a line
    done
    or when you e.g. want only to interprete the lines with the partitions:
    Code:
    fdisk -l | grep '^/' | while read DEV T1 T2 T3 T4 T5 T6
    do      ; # remember that sometimes T1 is a *, but mostly it is the start 
    done
    Hope this helps. And now: OFF I go!
    Henk van Velden

  9. #9
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    16,167
    Blog Entries
    14

    Default Re: How can I redirect stdout to a variable

    On Tue, 22 Jul 2008 13:56:04 GMT
    ken yap <ken_yap@no-mx.forums.opensuse.org> wrote:

    >
    > Or in Perl:
    >
    >
    > Code:
    > --------------------
    > #!/usr/bin/perl -w
    > open(P, "fdisk -l |") or die;
    > while (<P>) {
    > print $_;
    > }
    > --------------------
    >
    >

    A bit longer code, but if you need access to the data later, don't
    untie the array.

    #!/usr/bin/perl
    # ***** Define PERL functions *****
    use warnings;
    # Access the lines of a file via an array
    use Tie::File;
    # Set Access Control
    use Fcntl 'O_RDWR', 'O_RDONLY';
    # ***** Define global variables *****
    my $file = "/tmp/fdisk_out";
    my $count = 0;
    my $line = 0;
    # Create fdisk listing
    `/sbin/fdisk -l > $file`;
    # Tie $file
    tie @fdiskdata, 'Tie::File', "$file" , mode => O_RDWR or die;
    foreach $line (@fdiskdata) {
    $count ++;
    print "$count\. $fdiskdata[$count-1]\n";
    }
    untie @fdiskdata;

    --
    Cheers Malcolm (Linux Counter #276890)
    SLED 10 SP2 i586 Kernel 2.6.16.60-0.25-default
    up 4:51, 2 users, load average: 0.12, 0.11, 0.18
    GPU GeForce Go 6600 TE/6200 TE Version: 173.14.09


  10. #10
    Join Date
    Mar 2008
    Location
    Oz
    Posts
    10,946
    Blog Entries
    2

    Default Re: How can I redirect stdout to a variable

    You guys are so good -- thanks for the tips.

    Four of the things I like about my clumsy, old fashioned code are:

    it compiles into a c executable (very portable)
    it speaks in long-winded fashion (easy debugging)
    it has string handling (up there with PHP)
    it has great intrinsic graphics

    and with pipes I can get to and from the OS almost as easily as a script does.

    I tried a bit of conceptualising in PHP, running stuff in a web browser which is one of the ways of the future, and that's very promising, so Perl, which is much the same in power as PHP, would be great too, but I want a portable product one day.

    And those tips you guys gave are eye openers.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •