如何將 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")
}'

調整值,並根據需要添加標題。

相關內容