bash 脚本中的 pgrep 不工作

pgrep inside bash script not working

我正在尝试将 pgrep -f "instance" 输出保存在 bash 脚本中的一个变量中。由于某种原因,它不起作用: 这是代码:

function check_running() {
  local component_identifier=
  local process_check=`get_component_param_value $component_identifier $process_check_col | tr -d '\n'`
  if [ "$process_check" != "n/a" ]; then
    log "process to check: ****""$process_check""****"
    pid=`pgrep -f $process_check`
    log "pid: " $pid
  fi
}

我尝试过不同的方式,单引号和双引号。 此外,这都不起作用:

pid=$(pgrep -f "$process_check")

请注意 process_check 变量 returns 正确且确实有效。 我认为问题在于该字段位于该行的末尾并且可能包含一个 \n 字符,这就是为什么我在 process_check var.[= 中添加了一个 tr 17=]

有什么想法吗?

这是日志的输出:

process to check: ****"instance"****
pid:

我找到了回答这个问题的方法:

echo `ps -ef | grep "$process_check" | grep -wv 'grep\|vi\|vim'` | awk '{print }'

我希望其他人会觉得这有用

嗯,就我而言,它不喜欢我试图传递的论点。

pgrep -f "example" # working
pgrep -f "-f -N example" # not working