Sunday, August 2, 2009

Beginner Bash scripting question? FASTEST CORRECT ANSWER GETS BEST ANSWER ***?

This is my script, I am trying to write a script to take a PID as a parameter, and display the parent PID of it, and the parent PID of its parent PID, all the way until I get back to process 1. Basically, showing the lineage of any PID back to process 1. Please let me know what I'm doing wrong.





#!/bin/bash





if [ ! -d /proc/$1/ ]; then


echo "That is not a valid PID"


elif [ ! $# -eq 1 ]; then


echo "I need one PID parameter"


else


echo $1


helper $1


fi





function helper{


declare -i TEMPVAR=$(grep "PPid" /proc/$1/status | cut -c 7-)


echo $TEMPVAR


if [ ! $TEMPVAR -eq 1 ]; then


helper $TEMPVAR


fi


}











This is my first attempt at a function, and when I try to run the script I get


line 9: helper: command not found


line 13: syntax error near unexpected token 'declare'


line 13: 'declare -i TEMPVAR=$(grep blah blah)'








Please Help!!! First correct answer gets best answer.

Beginner Bash scripting question? FASTEST CORRECT ANSWER GETS BEST ANSWER ***?
#!/bin/bash





if [ ! -d /proc/$1/ ]; then


echo "That is not a valid PID"


exit


fi


if [ ! $# -eq 1 ]; then


echo "I need one PID parameter"


exit


fi





# specified pid:


echo $1





# next pid up the tree:


TEMPVAR=$(grep "PPid" /proc/$1/status | cut -c 7-)





while [ ! "$TEMPVAR" = "1" ]; do


TEMPVAR=$(grep "PPid" /proc/$TEMPVAR/status | cut -c 7-)


echo $TEMPVAR


done

curse of the golden flower

No comments:

Post a Comment