Bash - günlük fonksiyonu - printf
Aug 30 2020
Bash için bir kaydedici oluşturmaya çalışıyorum. Sorun, doğrudan baskının çalışıyor olmasıdır, ancak LOGGER_FUNC diziyi doğru şekilde işlemez.
Şu anda günlüğe kaydedilmesi gereken verileri yazdırabilirim:
DEBUG_data_ARRAY=(hi ho no bugs here no)
printf "\n%s" "${DEBUG_data_ARRAY[@]}" printf "\n%s %s" "${DEBUG_data_ARRAY[@]}"
Printf şu şekilde değiştirilmelidir:
LOGGER_FUNC "\n%s" "${DEBUG_data_ARRAY[@]}" LOGGER_FUNC "\n%s %s" "${DEBUG_data_ARRAY[@]}"
Kaydedici işlevi:
LOGGER_FUNC () {
format=$1 message=$2
if [[ $VERBOSE == 0 ]]; then printf "${format[@]}" "${message[@]}" fi if [[ $VERBOSE == 1 ]]; then
printf "${format[@]}" "${message[@]}" >> $DEBUG_FILE fi if [[ $VERBOSE == 2 ]]; then
printf "${format[@]}" "${message[@]}"
printf "${format[@]}" "${message[@]}" >> $DEBUG_FILE
fi
}
Beklenen sonuç şudur:
hi
ho
no
bugs
here
no
hi ho
no bugs
here no
Yanıtlar
4 JohnKugelman Aug 30 2020 at 20:35
format=$1 message=$2
Bu, iki skaler değişken oluşturur. Yapmak için messageiçeren bir dizi $2, $3, $4vb yazma:
format=$1
message=("${@:2}")
O zaman formatbir skaler olduğu için şunlardan $formatziyade yazabilirsiniz ${format[@]}:
if [[ $VERBOSE == 0 ]]; then
printf "$format" "${message[@]}"
fi
if [[ $VERBOSE == 1 ]]; then printf "$format" "${message[@]}" >> "$DEBUG_FILE"
fi
if [[ $VERBOSE == 2 ]]; then printf "$format" "${message[@]}" printf "$format" "${message[@]}" >> "$DEBUG_FILE"
fi
1 LéaGris Aug 30 2020 at 21:08
İşleve sağlanan bağımsız değişkenleri kullanma:
#!/usr/bin/env sh
LOGGER_FUNC() {
# shellcheck disable=SC2059 # Variable format string
printf "$@" | case $VERBOSE in
1) cat ;;
2) cat >>"$DEBUG_FILE" ;; 3) tee -a "$DEBUG_FILE" ;;
esac
}
Veya içerik için bağımsız değişkenlere ihtiyaç duymayan ancak onu şunlardan alan bir akış günlüğü uygulayın stdin:
#!/usr/bin/env bash
# stream_logger
# Log stdin with options
# &1: Verbose level:
# 1: stdout only
# 2: debug file only
# 3: both stdout and debug file
# &2: Optional debug file path
stream_logger() {
if [ $# -eq 0 ] || [ "$1" -eq 0 ]; then
cat >/dev/null
elif [ $# -eq 1 ] || [ "$1" -eq 1 ]; then
cat
elif [ $# -eq 2 ]; then if [ "$1" -eq 2 ]; then
cat >>"$2" elif [ "$1" -eq 3 ]; then
tee -a "$2" fi fi } DEBUG_data_ARRAY=(hi ho no bugs here no) echo 'hello' | stream_logger # print nothing # Output to stdout only printf '\n%s' "${DEBUG_data_ARRAY[@]}" | stream_logger 1
printf '\n%s %s' "${DEBUG_data_ARRAY[@]}" | stream_logger 1 # Output to file1.log only printf '\n%s' "${DEBUG_data_ARRAY[@]}" | stream_logger 2 file1.log
printf '\n%s %s' "${DEBUG_data_ARRAY[@]}" | stream_logger 2 file1.log # Output to file2.log and stdout printf '\n%s' "${DEBUG_data_ARRAY[@]}" | stream_logger 3 file2.log
printf '\n%s %s' "${DEBUG_data_ARRAY[@]}" | stream_logger 3 file2.log
Nicole Kidman, Michael Keaton ve Val Kilmer'in Batman Olarak Paylaştığı Bu 1 Çekici Özelliğe Bayıldı
Donovan, Şarkılarından 1'ini The Beatles'ın "Lucy in the Sky with Diamonds" şarkısıyla karşılaştırdı
Tom Girardi Dolandırıcılık Suçlamalarından Yargılanma Yetkisinin Belirlenmesi İçin Duruşmaya Katıldı