tree bash
Ushbu maqolada 21-maktab basseynining "Piscine C" dasturining birinchi bosqichida qilgan ba'zi ishlarim aks ettirilgan.
tree bash
Mana , tree.sh
skripti quyidagi funksiyalarni qo‘llab-quvvatlaydi:
./tree.sh
→ faqat katalog va fayllarni ko‘rsatadi (fayl ichidagi matn yo‘q)../tree.sh cat ./
→ katalog va fayllar bilan birga fayl ichidagi matnni ko‘rsatadi../tree.sh ./ 1
,./tree.sh ./ 2
,./tree.sh cat ./ 3
→ chuqurlikni belgilaydi.
Quyida to‘liq skript:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
# Copyrights 2025 yorenwyl
# All rights reserved
# Ranglar
BLUE='\033[1;34m' # kataloglar
GREEN='\033[1;32m' # fayllar
GRAY='\033[1;90m' # fayl ichidagi matn
RESET='\033[0m'
# Default qiymatlar
SHOW_CONTENT=false
dir="."
MAX_DEPTH=999
# Argumentlarni tahlil qilish
args=("$@")
arg_index=0
# 'cat' bor-yo‘qligini tekshirish
if [[ "${args[0]}" == "cat" ]]; then
SHOW_CONTENT=true
arg_index=1
fi
# Katalog argumenti
if [[ -n "${args[$arg_index]}" ]]; then
dir="${args[$arg_index]}"
arg_index=$((arg_index + 1))
fi
# Chuqurlik argumenti (ixtiyoriy)
if [[ -n "${args[$arg_index]}" && "${args[$arg_index]}" =~ ^[0-9]+$ ]]; then
MAX_DEPTH="${args[$arg_index]}"
fi
# Daraxt funksiyasi
draw_tree() {
local dir="$1"
local prefix="$2"
local depth="$3"
if (( depth > MAX_DEPTH )); then
return
fi
# Papka ichidagi fayllarni olamiz
local entries=()
while IFS= read -r -d $'\0' entry; do
entries+=("$entry")
done < <(find "$dir" -maxdepth 1 -mindepth 1 -print0 | sort -z)
local count=${#entries[@]}
local index=0
for entry in "${entries[@]}"; do
index=$((index + 1))
local base="$(basename "$entry")"
local connector="├──"
[ "$index" -eq "$count" ] && connector="└──"
if [ -d "$entry" ]; then
echo -e "${prefix}${connector} ${BLUE}${base}${RESET}"
draw_tree "$entry" "${prefix}$( [ "$index" -eq "$count" ] && echo ' ' || echo '│ ')" $((depth + 1))
else
echo -e "${prefix}${connector} ${GREEN}${base}${RESET}"
if $SHOW_CONTENT; then
local content_prefix="${prefix}$( [ "$index" -eq "$count" ] && echo ' ' || echo '│ ')"
local line
while IFS= read -r line; do
echo -e "${content_prefix}${GRAY}${line}${RESET}"
done < "$entry"
fi
fi
done
}
# Papka mavjudligini tekshir
if [ ! -d "$dir" ]; then
echo "Xatolik: '$dir' papka mavjud emas."
exit 1
fi
# Boshlanish
echo -e "${BLUE}${dir}${RESET}"
draw_tree "$dir" "" 1
Misollar:
1
2
3
4
5
6
7
8
9
10
11
./tree.sh
# Faqat katalog va fayllar
./tree.sh cat ./
# Fayllar matni bilan
./tree.sh ./ 2
# Faqat 2 bosqich chuqurlikkacha
./tree.sh cat ./ 3
# Fayllar matni bilan, 3 chuqurlikkacha
Ajoyib loyihangizga omad!
Mualliflik huquqi CC BY 4.0 litsenziyasi bilan himoyalangan.