Shell-Script

如何將 Bash 數組拆分為參數

  • October 3, 2016

我編寫了一個 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函式。

我想我誤會了什麼。

我建議您從中提取您需要的資訊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")
}'

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

引用自:https://unix.stackexchange.com/questions/314048