對于練習,我需要創建一個以 2 個引數作為輸入的腳本,
<n>一個整數值<path>目錄的路徑
我需要在路徑中創建與至少<n>運行行程的用戶一樣多的目錄。這些檔案具有這種格式<pid>.txt并且必須包含ppid、time和command
#! /bin/bash
if [ $# -lt 2 ]
then
echo "Hai inserito $#/2 parametri"
echo "<n><path>"
exit 1
fi
if [ "$1" -lt 0 ]
then
echo "errore: Il valore inserito non può essere negativo"
exit 1
fi
if ! [ -d "$2" ]
then
echo "errore: Il path indicato non è una directory"
exit 1
fi
OCCURRENCE=$1
PAT=$2
users=($(ps -Af | awk '{print $1}' | sort | uniq ))
for user in "${users[@]}"
do
processes=$(ps -Af | awk -v user="$user" '$1==user'|wc -l)
if [ "$processes" -gt "$OCCURRENCE" ]
then
mkdir $PAT/$user
ps -Af | awk -v user="$user" -v path="$PAT/$user" '$1=="user" {print $2,$7,$8 >>"$path/"$1".txt" }'
fi
done
問題是沒有創建包含相應目錄中內容的檔案,但程式只正確創建了擁有多個<n>行程的用戶的目錄。
awk 命令有什么錯誤嗎?有沒有更好的方法來執行這項任務?
uj5u.com熱心網友回復:
您的第一個awk過濾器是正確的:$1==user. 但是第二個是錯誤的:$1=="user"將第一個欄位與文字 string 進行比較user,而不是與 variable 的值進行比較user。"$path"如評論中所述,您應該有類似的問題path(不$,沒有引號)。
另請注意,您應該過濾掉輸出的第一行ps,并且與 bash 重定向不同,>連接,不需要>>.
正如您awk在此處使用的,另一種解決方案完全基于awk(使用 GNU 測驗awk):
$ ps -Af | awk -v n="$1" -v p="$2" 'NR>1 {
num[$1] = 1
proc[$1][$2 " " $7 " " $8]
}
END {
for(u in num) {
if(num[u] >= n) {
d = p "/" u
system("mkdir -p " d)
for(e in proc[u])
print e > d "/" u ".txt"
}
}
}'
uj5u.com熱心網友回復:
這個Shellcheck -clean Bash 代碼演示了另一種執行任務的方法:
#! /bin/bash -p
if (( $# < 2 )); then
echo "Hai inserito $#/2 parametri" >&2
echo "<n><path>" >&2
exit 1
fi
min_proc_count=$1
output_dir=$2
if [[ -z $min_proc_count || $min_proc_count == *[^[:digit:]]* ]]; then
echo 'errore: il valore inserito deve essere un numero positivo' >&2
exit 1
fi
if [[ ! -d $output_dir ]]; then
echo 'errore: Il path indicato non è una directory' >&2
exit 1
fi
ps_output=$(ps -Af --no-headers)
# Get counts of processes for each user
declare -A user_proc_count
while read -r user _; do
user_proc_count[$user]=$(( ${user_proc_count[$user]-0} 1 ))
done <<<"$ps_output"
# Generate per-process output files for users with enough processes
while read -r user pid ppid _ _ _ time cmd; do
(( ${user_proc_count[$user]} < min_proc_count )) && continue
dir=$output_dir/$user
[[ -d $dir ]] || mkdir -v -- "$dir"
printf '%d,%s,%s\n' "$ppid" "$time" "$cmd" >"$dir/${pid}.txt"
done <<<"$ps_output"
- 請參閱正確的 Bash 和 shell 腳本變數大寫,了解為什么我使用小寫變數名稱而不是 ALL_UPPERCASE 名稱,如
PATandOCCURRENCE。 - 請參閱為什么 printf 比 echo 更好?解釋為什么我使用
printf而不是echo輸出資料。
uj5u.com熱心網友回復:
這是一個awk基于 - 的解決方案,不需要任何額外的 shell 級處理,并直接生成必要的輸出。
根據我對ps的業余時間的理解,我認為ps -Ao 'uid pid time command'可能會更好地滿足他/她的目的。
由于我真正不知道的原因,我只能讓它在gawkor上作業mawk2,但不是mawk-1or nawk。
代碼
ps -Ao 'uid pid time command' |
gawk -be '
{
528 ___[_=$!-__]
528 sub(_ "[ \t] ","")
528 sub("$",($-__)__,___[_])
} END {
1 sub("^[~][/]",ENVIRON["HOME"]"/",____)
gsub("[/] ", "/",____)
sub("[/]$", "",____)
gsub(/\47/,"&\\&&",____)
1 system(" mkdir -p \47" (____) "\47 2>/dev/null")
35 for (_ in ___) {
35 if ( _____ < (gsub(__, "&", ___[_]))) {
5 printf("%s", ___[_]) > (____ "/" _".txt")
}
}
}' _____='THRESHOLD_NUMBER' ____="${DESTINATION_PATH}" __='\n'
“${DESTINATION_PATH}”之后
total 800
-rw-r--r-- 1 501 staff 11709 May 30 11:13 0.txt
-rw-r--r-- 1 501 staff 509 May 30 11:13 205.txt
-rw-r--r-- 1 501 staff 655 May 30 11:13 262.txt
-rw-r--r-- 1 501 staff 1575 May 30 11:13 278.txt
-rw-r--r-- 1 501 staff 791516 May 30 11:13 501.txt
296 lines 791,175 utf8 (175 uc) 0.755 MB ( 791515) 501.txt
158 lines 11,708 utf8 (0. uc) 0.011 MB ( 11708) 0.txt
6 lines 508 utf8 (0. uc) 0.000 MB ( 508) 205.txt
12 lines 1,574 utf8 (0. uc) 0.002 MB ( 1574) 278.txt
7 lines 654 utf8 (0. uc) 0.001 MB ( 654) 262.txt
==> 0.txt <==
1 4:36.92 /sbin/launchd
105 1:00.85 /usr/libexec/logd
==> 205.txt <==
148 0:32.10 /usr/libexec/locationd
286 0:00.56 /System/Library/PrivateFrameworks/GeoServices.framewor
==> 262.txt <==
346 0:00.84 /usr/sbin/distnoted agent
365 0:00.06 /System/Library/Frameworks/CoreMediaIO.framework/Versi
==> 278.txt <==
113 0:00.46 /System/Library/PrivateFrameworks/MobileAccessoryUpdat
376 0:00.83 /usr/sbin/distnoted agent
==> 501.txt <==
184 0:01.64 /System/Library/CoreServices/loginwindow.app/Contents/
426 0:22.44 /usr/sbin/distnoted agent
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/483470.html
