编辑:正如@Biffen的建议,将${SCRIPTS[*]} =~ ${ARG}
置于双括号中就可以了。
E.G。 if [[ ${SCRIPTS[*]} =~ ${ARG} ]]; then
我正在尝试将进程名称作为数组,并检查给定的文件名是否存在于该数组中。
当我尝试运行脚本时,它会在if [ ${SCRIPTS[*]} =~ ${ARG} ]; then
行引发错误。我不知道为什么我会收到这个错误。
#!/usr/bin/env bash
SCRIPTS=(`ps x | grep python | grep -v ".pyc" | grep -v grep | awk '{print $NF}'`)
ARGS="$@"
kill_funct () {
if [ !-z "$1" ]; then
# kill -9 "$1"
echo "$1 is killed."
fi
}
killall_funct () {
for SCRIPT in SCRIPTS
do
# kill -9 ${SCRIPT}
echo "${SCRIPT} is killed."
done
}
for ARG in ${ARGS}
do
if [ -z ${ARG} ]; then
echo "No file name or key given as parameter."
echo "For help please use -h key."
elif [ ${ARG} = "-h" ]; then
echo "USAGE:"
echo " -h : Displays this help"
echo " \"file name\" : Kills the process of the given file names"
echo " You can give more than one file name as argument"
echo " E.G. bash oldur.sh \"file1 name\" \"file2 name\" ..."
echo " All : This parameter kills all python processes found with 'ps' command"
elif [ ${ARG} = "All" ]; then
killall_funct
else
echo ${SCRIPTS}
if [ ${SCRIPTS[*]} =~ ${ARG} ]; then
kill_funct ${ARG}
else
echo "${ARG} not present as a process."
fi
fi
done