How to call a function with parameter in a bash script?

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.

  • 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

coffeeho... at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.