Find the answer to your Linux question:
Results 1 to 4 of 4
Hi folks, I need each command line typed on Console/Xterm window to be automatically copied to a file after hitting [ENTER] and the last "ten/x" lines of each corresponding output ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Guru
    Join Date
    Sep 2004
    Posts
    1,804

    Script to create log file


    Hi folks,

    I need each command line typed on Console/Xterm window to be automatically copied to a file after hitting [ENTER] and the last "ten/x" lines of each corresponding output on the screen to be recorded on the file simultaneously. There are >300 comand lines to be executed. I'm building LFS-LinuxFromScratch. I only need the last "x" lines of the output on the screen to be recorded because on compilation maybe there are >10 pages of output. It serves as a log file.

    I don't expect adding >file/>>file to the end of each command lines typed, if possible.

    Please advise whether it is possible to run a shell script so that my requirement can be done. If YES, please advise how to prepare this shell script.

    Is there another alternative to do it instead of running a shell script.

    TIA

    B.R.
    satimis

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Just wrote this, and I believe it works:

    Code:
    #!/usr/bin/perl -w
    
    use strict;
    
    open LOG, "> log" or die "$0: Cannot create log file!\n";
    
    while(1)
    {
        print "Please Enter a Command To Run: ";
        chomp(my $command = <STDIN>);
    
        last if $command eq 'exit';
    
        open COMM, "$command |" or die "$0: Cannot Execute Command!";
    
        print LOG "----  $command  ----\n";
    
        my @lines;
    
        while(<COMM>)
        {
            print;
    
            push @lines,$_;
        }
    
        for(my $i = 0; $i < 10; $i++)
        {
            my $line = pop @lines;
    
            last unless defined $line;
    
            print LOG $line;
        }
    
        print LOG "\n";
    
        print "\n\n";
    }
    I just tested it out, and it seems to work for me. Save it, make it executable, and run it. Then you just enter your commands into it. To stop it, enter 'exit' as your command.

    Be warned, it will wipe out any file called 'log' that is sitting in the current directory. This is where it will write to.

  3. #3
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    It might be easier to just use the `script' command. Check the manpage and see if it's the right kind of program for you.

  4. #4
    Linux Guru
    Join Date
    Sep 2004
    Posts
    1,804
    Hi folks,

    Tks for your advice.

    Now I use 'script' as Dolda2000 suggested.

    BR
    satimis

Posting Permissions

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