-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash.sh
More file actions
47 lines (39 loc) · 840 Bytes
/
bash.sh
File metadata and controls
47 lines (39 loc) · 840 Bytes
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
#!/bin/bash
# --- Variables ---
name="Hackerman"
count=3
readonly pi=3.14
declare -a colors=("red" "green" "blue")
# --- Functions ---
greet() {
echo "Hello, $name!"
}
factorial() {
local n=$1
if (( n <= 1 )); then
echo 1
else
echo $(( n * $(factorial $((n - 1)) ) ))
fi
}
# --- Control Flow ---
for color in "${colors[@]}"; do
echo "Color: $color"
done
if [[ $count -gt 2 ]]; then
greet
else
echo "Too few counts"
fi
# --- Command Substitution and Pipes ---
date_now=$(date "+%Y-%m-%d %H:%M:%S")
echo "Current date: $date_now"
ps aux | grep "$USER" | awk '{print $1, $2, $11}' | head -n 5
# --- Heredoc and Redirection ---
cat <<EOF > output.txt
This is a test.
Generated on: $date_now
EOF
# --- Arithmetic and Subshell ---
((count++))
echo "Factorial of $count: $(factorial $count)"