您好,我有一個任務來計算目錄 /bin 中符號鏈接(不是常規檔案或目錄等)的數量,其中 b 作為其名稱的第一個字母。
> find /bin -name "b*" -type l -printf l > list.txt
但它輸出
1111
我應該怎么做才能將輸出更改為這四個 1 中的一個數字(我的意思是 4)?
uj5u.com熱心網友回復:
要正確計算帶有換行符的符號鏈接:
find /bin -name "b*" -type l -printf "l" | wc -c >list.txt
uj5u.com熱心網友回復:
可以通過將 的輸出存盤find在陣列中并計算陣列條目來計算 Bash 腳本中的鏈接數:
#!/usr/bin/env bash
# Singular|Plural format string
__links_count_format=('There is %d link\n' 'There are %d links\n')
# Capture the links found by the find command into the links array
mapfile -d '' links < <(find /bin -name "b*" -type l -print0)
# Get the links count
links_count="${#links[@]}"
# Print the number of entries/links captured in the array
# shellcheck disable=SC2059 # dynamic format string
printf "${__links_count_format[links_count>1]}" "$links_count"
# Print the links captured in the array
printf '%s\n' "${links[@]}"
uj5u.com熱心網友回復:
-type l將找到所有符號鏈接并為每個鏈接列印一個 1。然后wc用來統計字符
find /bin -name "b*" -type l -printf 1|wc -c > list.txt
uj5u.com熱心網友回復:
您也可以使用以下awk基于實作
controlplane ~ ? find /bin -type l | awk -F'/' '$3 ~ /^f/ {count }END{print count}'
5
controlplane ~ ? find /bin -type l | awk -F'/' '$3 ~ /^b/ {count }END{print count}'
2
controlplane ~ ?
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/368375.html
下一篇:從bash檔案中獲取特定文本
