0

i'm trying to write a script to get the name and the pid of the process lanched from bash script.

example:

#!/bin/bash
pass=(`command1 | grep .... | tail ... `)&
if [ "$?" = "0" ]; then
    echo "pid of the sub-shell "$!
    echo "pid of the shell "$$       
else
    echo $?
    exit 1
fi

the output of this script is

pid of the sub-process 22725
pid of the shell 22724

My question is how can i get the the pid and the command of command1 from shell script, and the result of the command in pass variable.

  1. if i remove the "&" i get the result of the command ( password) but not the pid of sub-shell
  2. if i set the "&" i get the pid but not the password
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
mzpx
  • 39
  • 1
  • 2
  • Don't use *code snippets* for shell code. *code snippets* is a feature adapted by Joel Spolsky from JsFiddle.com when he was pretty drunken. He never finished his work. Use a *code block* instead. – hek2mgl Nov 26 '15 at 16:34
  • ok, still not working. i need 3 values : the command executed, it's pid, and the value of the command ( in my example the password ) – mzpx Nov 26 '15 at 16:39

1 Answers1

1

A bit more context would be helpful, for example, why do you need the subshell to output the name of command1, when you must write "command1" to execute it in the subshell in the first place?

If the requirement is just to get the subshell process ID along with its result, output them on separate lines:

{ read pid; read result; } < <(echo $$; echo mypass)

However the question suggests you want to launch the child process (which may be long-running), perform other activities, then get its output. For this try named pipes (examples here) or a temporary file:

temp=$(mktemp)
command1 ... >$temp &
pid=$!
# other activities here
# ...
wait $pid
read result <$temp
Community
  • 1
  • 1
Jonathan Ross
  • 530
  • 3
  • 10