如何将 Bash 数组拆分为参数

如何将 Bash 数组拆分为参数

我编写了一个 bash 脚本,用于以人类可读的形式用彩色线条列出 python 进程、RAM 使用情况以及 PID 和状态。但我在脚本的工作时间方面遇到了一些麻烦。由于重复编写ps命令,工作时间花费了太多时间。

SCRPITS=`ps x | grep python | grep -v ".pyc" | grep -v grep | awk  '{print $NF}'`

prepare_text () {
    if [[ ${2%.*} -gt ${RAMLIMIT} ]]; then
        # RED
        TEXT=`printf "\033[31m%-62s %'10d %2s %5s %6s\n\033[0m" "${1}" "${2}" "KB" "${3}" "${4}"`
    elif [[ ${2%.*} -gt ${RAMLIMIT}/2 ]]; then
        # YELLOW
        TEXT=`printf "\033[33m%-62s %'10d %2s %5s %6s\n\033[0m" "${1}" "${2}" "KB" "${3}" "${4}"`
    else
        # GREEN
        TEXT=`printf "\033[32m%-62s %'10d %2s %5s %6s\n\033[0m" "${1}" "${2}" "KB" "${3}" "${4}"`
    fi
    TEXTBODY+=${TEXT}
}

display () {
    printf "$SUBJECT\n"
    printf "%-62s %13s %5s %8s\n" "PROCESS" "RAM USAGE" "PID" "STATUS"
    printf "===========================================================================================\n"
    printf "${TEXTBODY}\n"
}


for SCRIPT in ${SCRPITS}
do
    USAGE=`ps aux | grep ${SCRIPT} | grep -v "grep" | awk '{print $6}'`
    PID=`ps aux | grep ${SCRIPT} | grep -v "grep" | awk '{print $2}'`
    STATUS=`ps aux | grep ${SCRIPT} | grep -v "grep" | awk '{print $8}'`
    prepare_text ${SCRIPT} ${USAGE} ${PID} ${STATUS}
done
display
exit $?

我决定改变这种方法,并重新安排所有脚本以缩短工作时间,如下所示:

OIFS=$IFS #save original
IFS='\n'
SCRIPTS=`ps aux | grep python | grep -v ".pyc" | grep -v grep | awk '{print $NF,",",$5,",",$2,",",$8}'`
IFS=${OIFS}
prepare_text () {
    if [[ $((${2%.*})) -gt ${RAMLIMIT} ]]; then
        # RED
        TEXT=`printf "\033[31m%-62s %'10d %2s %5s %6s\n\033[0m" "${1}" "${2}" "KB" "${3}" "${4}"`
    elif [[ $((${2%.*})) -gt ${RAMLIMIT}/2 ]]; then
        # YELLOW
        TEXT=`printf "\033[33m%-62s %'10d %2s %5s %6s\n\033[0m" "${1}" "${2}" "KB" "${3}" "${4}"`
    else
        # GREEN
        TEXT=`printf "\033[32m%-62s %'10d %2s %5s %6s\n\033[0m" "${1}" "${2}" "KB" "${3}" "${4}"`
    fi
    TEXTBODY+=${TEXT}
}

display () {
    printf "$SUBJECT\n"
    printf "%-62s %13s %5s %8s\n" "PROCESS" "RAM USAGE" "PID" "STATUS"
    printf "===========================================================================================\n"
    OIFS=$IFS
    IFS=","
    set ${SCRIPTS}
    for SCRIPT in ${SCRIPTS}
    do
    prepare_text $1 $2 $3 $4
    done
    printf "\n\n"
    IFS=${OIFS}
    printf "${TEXTBODY}\n"
}


display
exit $?

现在我可以立即获取我想要的信息,ps但我在格式化和显示该信息时遇到一些问题。

我不知道如何从 中获取每个参数${SCRIPTS},将它们拆分并传递给prepare_text函数。

我想我误解了一些东西。

答案1

我建议您从 中提取所需的信息ps,而不是其他任何信息,然后让awk(而不是bash)完成其余的工作:grepping、比较、格式化。例子:

ps -ax --no-headers -o pid,vsz,stat,command |
awk -v lim=23000 '
# let awk do the grepping
/bash/ && !/awk/ {
  # save first 3 fields
  pid=$1
  vsz=$2
  stat=$3
  # rest is command line, possibly spanning multiple fields
  for (i=4;i<=NF;++i) $(i-3)=$i
  NF-=3
  # decide on color
  if (vsz>lim) col="\033[31m"
  else if (vsz>lim/2) col="\033[33m"
  else col="\033[32m"
  # printout
  printf("%s%-62s %10d KB %5s %6s%s\n",
    col, $0, vsz, pid, stat, "\033[0m")
}'

调整值,并根据需要添加标题。

相关内容