如何选择bash脚本输出的日志级别

时间:2016-12-07 10:24:56

标签: linux bash shell

在我编写的脚本中,输出被发送到日志文件mySysMonitor.log。我希望此脚本的用户能够选择要发送到日志文件的日志级别(调试或信息)。我该怎么做呢。以下面的脚本为例。

#!/bin/bash

processKD(){
  printf "\nCURRENT PROCESSES \r"
  ps -eo user,pid,pcpu,pmem,start | sort -rk 3,4 | head -n 11
  echo "TOTAL NUMBER OF RUNNING PROCESSES: $(ps aux | wc -l)"
}

loggedInKD(){
  printf "\nUSERS CURRENTLY LOGGED IN\r"
  echo "Number of users logged in: $(w | head -n 1 | cut -d"," -f 3)"
  who
}
processKd >> mySysMonitor.log
loggedInKd >> mySysMonitor.log

1 个答案:

答案 0 :(得分:1)

你可以使用这样的东西;

#!/bin/bash

processKD(){
  printf "\nCURRENT PROCESSES \r"
  ps -eo user,pid,pcpu,pmem,start | sort -rk 3,4 | head -n 11
  echo "TOTAL NUMBER OF RUNNING PROCESSES: $(ps aux | wc -l)"
}

loggedInKD(){
  printf "\nUSERS CURRENTLY LOGGED IN\r"
  echo "Number of users logged in: $(w | head -n 1 | cut -d"," -f 3)"
  who
}



LOG_LEVEL="info"
#LOG_LEVEL=$1  #with parameter ./script info 

#LOG_LEVEL="debug"

if [ "$LOG_LEVEL" == "info" ]; then
loggedInKd >> mySysMonitor.log
fi

if [ "$LOG_LEVEL" == "debug" ]; then
processKd >> mySysMonitor.log
loggedInKd >> mySysMonitor.log
fi