Find the answer to your Linux question:
Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By atreyu
Hi all, This is the first shell script i am attempting and i have few queries . I need a write a shell script to autmate a command . ex: ...
  1. #1
    Just Joined!
    Join Date
    Jul 2011
    Posts
    1

    Exclamation loop through the lines of text file

    Hi all,

    This is the first shell script i am attempting and i have few queries .
    I need a write a shell script to autmate a command .

    ex:
    bash-3.00$ nctrl systatus
    Connecting to Node Controller at localhost:16015....
    Issuing 'sysstatus' request request to Node Controller..

    Status for node : wbes890a

    Module Name Process Name Pid status
    AA A 1 Running
    BB B 2 Dead
    CC C 3 Running
    DD D 4 Running
    EE E 5 Running
    FF F 6 Running
    GG G 7 Running


    Above the sample output.Check attachment for the real output.
    What i have to achieve through script is After issuing the
    command nctrl systatus.I will get the above output .I have to check if any process is in running state .If yes then i have to issue 'nctrl stop <process Name>.

    How it is achievable ?.

    What i am planning is write the output of the command to a file and using grep command check for running process.Which will return the lines which will have running as status.But how to fetch the exact process name and issue the nctl stop
    Attached Images Attached Images

  2. #2
    Linux Guru
    Join Date
    May 2011
    Posts
    1,842
    This could be done in Bash, but it is a classic case for Perl. Here's a quick script that you can try. Run your nctrl command and redirect the output to a file (like you suggested). Then pass that file as an argument to this perl script, which you must of course first create (and make executable).

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    my $file = shift || die "Give me a file\n";
    die $file,": No such file\n" unless(-f$file);
    open(FH,'<',$file) or die "can't read '$file': $!\n";
    while(<FH>){
    
      # remove new-line char
      chomp;
    
      # condense white-spaces to single spaces
      $_ =~ s/[ \t]+/ /g;
      
      # remove trailing white-space
      $_ =~ s/[ \t]$//;
    
      # split words in line into an array
      my @a = split;
      
      # get the last word in the line
      my $last = $a[$#a];
      next unless($last);
    
      # only continue if the line's last word matches "running" (case-insensitive)
      next unless($last =~ /(running)/i);
      
      # get pid and process name from running processes
      my $state = $1;
      my $pid = $a[$#a - 1];
      my $process = $a[$#a - 2];
      print "Process $process (pid $pid) is $state\n";
    
      # stop the process
      system("nctrl stop $process");
    }
    close(FH);
    Maddy1234 likes this.

Posting Permissions

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