rerboi
rerboi

Reputation: 93

Error while trying to print PPIDS in bash

Hello i am trying to recursevely print PPID's (parent, grandparent and so on). I wrote a function:

function parents(){
    smth=$(ps -o ppid= -p "$1")
    echo $smth
    if test "$smth" = "1"; then
        echo "Top process"
    else
        parents $sth
    fi
}
read -p "Enter PID:" upid
parents $upid

when i run the script i get the error:

error: list of process IDs must follow -p

I have no idea what im doing wrong.

Upvotes: 0

Views: 127

Answers (1)

Freddy
Freddy

Reputation: 4688

There are two mistakes in your script:

  1. You're calling parents with variable $sth instead of $smth
  2. The output of ps -o ppid= -p "$1" contains space characters which makes your test fail. Change the quoted "$smth" in the test to $smth.
function parents(){
    smth=$(ps -o ppid= -p "$1")
    echo $smth
    if test $smth = "1"; then
        echo "Top process"
    else
        parents $smth
    fi
}

Upvotes: 2

Related Questions