读变量到ps ax脚本

时间:2016-03-18 20:55:53

标签: bash ps

您好我一直在论坛上搜索,但我似乎无法做到这一点。我正在尝试创建一个脚本,询问用户他们正在搜索哪个进程,然后如果进程正在运行则返回1。

这有效:

#!/bin/bash
SERVICE='httpd'
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
echo "$SERVICE service running, everything is fine"
else
echo "$SERVICE is not running"
fi

我想将其添加到脚本中:

 echo -e "please enter process name: \c"
 read word

类似于:

#!/bin/sh
echo -e "please enter process name: \c"
read input_variable
if ps ax | grep -v grep | grep $varname > /dev/null
then
echo "$SERVICE service running, everything is fine"
else
echo "$SERVICE is not running"
fi

1 个答案:

答案 0 :(得分:1)

使用pgrep搜索进程:

read process_name
if pgrep "${process_name}" >/dev/null 2>&1 ; then
    "echo ${process_name} found"
else
    "echo ${process_name} not found"
fi