Find the answer to your Linux question:
Results 1 to 3 of 3
this is what I was working on then thought to myself then a wait a minute showed up and here it is. There has to be a way to pipe ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2013
    Posts
    19

    How do I get what I want using Pipes and other programs


    this is what I was working on then thought to myself then a wait a minute showed up and here it is.

    There has to be a way to pipe some other program that will give me just the first pram and not the rest or I can pull one at a time out of it. I just do not know every little program that is in here. every time I read to learn I see this other little program I had no idea was in here what one can use to do this or that with and save a whole lot of coding

    learning all that in itself is a whole little world in its own

    -------------------------------------------

    when one does a pgrep to get the PID by name of the app running if there are more then one hinstances of said app running then the output is more then one PID -- I am trying to get the first PID logically being the first one created for said app so I can kill it and only have one hinstance running only that being the last one created not any before hand --

    I say first PID because logically if this is written right then there should be no more then two because it will kill the first PID before creating the second PID therefore no third PID will be created



    using pgrep app name | wc -l returns how many PID's there are

    there has to be hidden within this GNU/Linux another little program (not documented clearly) that one can pipe to to get the first return value then perhaps with a little more code the second and or third and so on for good error checking to be sure that there is only one PID the last one created still living and all the others were killed off

    the second problem that I am working on is how to lean how to know what belongs to what as within this script I am using to learn this so I can apply it else where if needed is it calling another program within it thus it creates a PID for the main script another PID for the program that it called -- the problem that may arise is that if another program is using the same program that this script uses then it now has two PID s for this one program one for each separate script that called it.

    how does one find out which PID belongs to each of the two separate scripts that called the same program causing the system to create two separate PIDs assigned to one each -- I hope that is understandable - I don't know how to draw pictures with this html stuff



    Code:
    #!/bin/bash
    
    
    for ((i=1; i<=$(pgrep CalTime | wc -l ); i++)); do
    	
    	echo "for grep caltime pipe wc is $(pgrep CalTime | wc -l ) "
    
    
    	arry[i]=$(pgrep CalTime)
    	echo ${arry[i]} "Arry in for loop"
    	echo ${arry[1]} "1 in for loop"
    	echo ${arry[2]} "2 in for loop"
    	echo ${arry[3]} "3 in for loop"
    	echo ${arry[4]} "4 in for loop"
            echo ${arry[5]} "5 in for loop"
    done
    
    }
    out put

    Code:
    ~/lscripts$ ./funtimes
    for grep caltime pipe wc is 3 
    3861 3868 3875 Arry in for loop
    3861 3868 3875 1 in for loop
    2 in for loop
    3 in for loop
    4 in for loop
    5 in for loop
    for grep caltime pipe wc is 3 
    3861 3868 3875 Arry in for loop
    3861 3868 3875 1 in for loop
    3861 3868 3875 2 in for loop
    3 in for loop
    4 in for loop
    5 in for loop
    for grep caltime pipe wc is 3 
    3861 3868 3875 Arry in for loop
    3861 3868 3875 1 in for loop
    3861 3868 3875 2 in for loop
    3861 3868 3875 3 in for loop
    4 in for loop
    5 in for loop
    as you can see the $(pgrep CalTime | wc -l) returns the proper number of PIDs that are running for the app of the same name --
    again my problem is how to separate them -- too I noticed it did not kick out on 3 but ran the extra two lines of code

    I'm rusty on everything

    thanks

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    hi,

    can you examine the arguments to the process, to help identify it? here's an idea (w/o knowing a great deal about CalTime...):

    Code:
    #!/bin/bash
    
    declare -a pids
    pids=($(pgrep mingetty))
    for (( p=0;p<${#pids[*]};p++ )); do
      pid=${pids[$p]}
      [ $p -eq 0 ] && opts='' || opts='--no-heading'
      ps $opts -p $pid -o user,pid,ppid,tty,etime,cmd
    done

  3. #3
    Just Joined!
    Join Date
    Nov 2013
    Posts
    3
    Is this what you're looking for?

    Code:
    pgrep CalTime | head -1

Posting Permissions

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