在我编写的脚本中,输出被发送到日志文件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

最佳答案

你可以用这样的东西;

#!/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

关于linux - 如何选择日志级别到bash脚本的输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41014933/

10-11 15:37