Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Qutting command after so many lines of output

  • 13-12-2008 12:08AM
    #1
    Moderators, Motoring & Transport Moderators Posts: 266 Mod ✭✭✭✭


    Hi guy's

    I'm currently writing a shell script that will run a particular command and print its output to a file.

    The only problem is that when I run the command in question it will print out the first 20 lines and then require the user to press RETURN to see the rest.
    I am however only interested in the first 20 lines so I was wondering if there was a way to quit the command once someone lines had been output.

    Eg.

    The script runs the command, reads the first 20 lines then quits the process rather than just sitting there waiting for user input

    Any help will be greatly appreciated


Comments

  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    You could pipe it into head and redirect the output into a file?

    something like

    command | head - 20 > myfile.out - I'm only guessing this, still relatively new to linux unfort.

    Edit- Just noticed it probably the command that pauses automatically after 20 lines? - in that cause you will have to kill the process after the redirection. I amn't really sure how it will work to be honest, but worth a try


  • Closed Accounts Posts: 1,444 ✭✭✭Cantab.


    perl...

    Something like...

    'stopper.pl'
    #!/usr/bin/perl
    my $data=<CIN>;
    my $n=0;
    while($data)
    {
       if($data+=m/\n/)
       {
          $n=$n+1;
          if($n>=20)
          {
              print "Finished.\n";
              exit 0;
          }
       }
    }
    

    bash> ./myprog > perl stopper.pl

    Might be a bit long-winded. I'm sure awk could do it all in one line.


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭Khannie


    Cantab. wrote: »
    bash> ./myprog > perl stopper.pl

    That should be a | instead of a > or it'll overwrite your perl script. :)


Advertisement