2010-03-22 106 views

回答

11

在我所有的Linux機器,代碼要做到這一點是這樣的文件:

/etc/init.d/functions 

如果包含該文件(. /etc/init.d/functions),然後運行代碼,這樣做:

action /path/to/prog args 

你會得到你想要的功能。

+0

感謝,我看到了文件和行動的用法是:$行動「做了......」 /路徑/到/ PROG ARGS。 ;) 謝謝! – Neuquino 2010-03-22 19:27:44

+1

我的linux沒有這個(xandros,eeePC上的debian變體)。但是看一下/etc/init.d/*,這些文件似乎以相同的方式使用'/ lib/lsb/init-functions',調用它定義的函數,如log_begin_msg,log_progress_msg,log_warning_msg,log_failure_msg和log_end_msg。其中一個也引用了'/ etc/init.d/functions',但只有當系統是redhat的時候。 – 13ren 2010-03-23 02:14:29

4

/etc/init.d/*腳本遵循一個相當好用的模板。找到一個並複製並修改它。

[OK]/[ERROR]東西是通過在您的腳本(通常位於頂部)採購文件/etc/init.d/functions完成的。

1

使用printf。我喜歡有顏色編碼的東西。 :)

這是我在我的腳本來設置顏色和一些printf語句使用序言...

#!/bin/bash 
# checkload.sh - script to check logs for errors. 
# 
# Created by Ryan Bray, [email protected] 


set -e 

# Text color variables 
txtund=$(tput sgr 0 1) # Underline 
txtbld=$(tput bold)  # Bold 
txtred=$(tput setaf 1) # Red 
txtgrn=$(tput setaf 2) # Green 
txtylw=$(tput setaf 3) # Yellow 
txtblu=$(tput setaf 4) # Blue 
txtpur=$(tput setaf 5) # Purple 
txtcyn=$(tput setaf 6) # Cyan 
txtwht=$(tput setaf 7) # White 
txtrst=$(tput sgr0)  # Text reset 

然後,我有這樣的語句,在輸出時使用的顏色:

printf "Checking for descrepancies in $LOAD_DATE$ADD_COMP\n" 
DELTAS=$(awk 'BEGIN { FS = "\"" } {print $24,$26,$28}' $COMP_FILE) 
if [[ "$DELTAS" == *[1-9]* ]]; then 
     printf "%74s[${txtred}FAIL${txtrst}]\n" 
     printf "$COMP_FILE contains descrepancies.\n" 
     exit 1 
else 
     printf "%74s[${txtgrn}PASS${txtrst}]\n" 
fi 

希望這有助於!

-Ryan