Skip to content

Commit 63da967

Browse files
committed
test
Signed-off-by: Yanxuan Liu <[email protected]>
1 parent c6442a9 commit 63da967

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

shell-test.sh

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#!/bin/bash
2+
# shellcheck disable=SC2317 # Temporarily disable checks to demonstrate errors
3+
4+
# =====================
5+
# Basic Syntax Errors (detectable by both bash -n and shellcheck)
6+
# =====================
7+
8+
# 1. Unmatched quotes
9+
echo "Hello World # Error: Missing closing quote
10+
11+
# 2. Unclosed control structure
12+
if [ -f "test_file.txt" ]; then
13+
echo "File exists"
14+
# Error: Missing 'fi'
15+
16+
# 3. Incomplete pipe command
17+
cat "some_file.txt" | # Error: Nothing after pipe
18+
19+
# 4. Unclosed function
20+
function test_function {
21+
echo "Inside function"
22+
# Error: Missing '}'
23+
24+
# =====================
25+
# Semantic Errors (primarily detected by shellcheck)
26+
# =====================
27+
28+
# 5. Unquoted variable with spaces
29+
filename="file with spaces.txt"
30+
rm $filename # Error: Should be rm "$filename"
31+
32+
# 6. Misuse of exit status
33+
grep "pattern" "some_file.txt"
34+
if [ $? = 0 ]; then # Error: Should use if grep -q "pattern" file.txt
35+
echo "Pattern found"
36+
fi
37+
38+
# 7. Uninitialized variable
39+
echo "Value: $undefined_var" # Error: Variable never defined
40+
41+
# 8. Arithmetic expression error
42+
count=5+3 # Error: Should be count=$((5+3))
43+
44+
# 9. Test expression error
45+
var="value"
46+
if [ "$var" = value ]; then # Error: value should be quoted
47+
echo "Equal"
48+
fi
49+
50+
# =====================
51+
# Security/Best Practice Issues (shellcheck only)
52+
# =====================
53+
54+
# 10. Command injection risk
55+
user_input="; echo 'malicious command'"
56+
ls $user_input # Error: Should use ls -- "$user_input"
57+
58+
# 11. Inefficient loop
59+
for file in $(ls *.txt); do # Error: Should use for file in *.txt
60+
echo "$file"
61+
done
62+
63+
# 12. Constant conditional
64+
if [[ 1 -eq 1 ]]; then # Error: Always true
65+
echo "Always true"
66+
fi
67+
68+
# 13. Unclosed file descriptor
69+
exec 3< "input.txt"
70+
# Error: Missing exec 3>&-
71+
72+
# =====================
73+
# Portability Issues (shellcheck only)
74+
# =====================
75+
76+
# 14. Bashism in sh script
77+
(
78+
#!/bin/sh
79+
echo {1..10} # Error: {} expansion invalid in POSIX sh
80+
)
81+
82+
# 15. Incompatible test syntax
83+
(
84+
#!/bin/sh
85+
if [[ $var == "test" ]]; then # Error: [[ ]] is bash-specific
86+
echo "Match"
87+
fi
88+
)
89+
90+
# =====================
91+
# Compound Error Section
92+
# =====================
93+
94+
# 16. Multiple compound errors
95+
echo "Starting complex error section"
96+
97+
# Unmatched quotes
98+
echo "Unclosed quote
99+
100+
# Uninitialized variable + unquoted
101+
if [ $debug_mode = "true" ]; then
102+
echo "Debug mode"
103+
fi
104+
105+
# Broken pipe
106+
cat /var/log/syslog |
107+
108+
# Security risk
109+
find . -name $user_input
110+
111+
# Unclosed loop
112+
for item in {1..5}; do
113+
echo "Processing $item"
114+
115+
# =====================
116+
# Cleanup (no actual errors)
117+
# =====================
118+
echo "All errors are intentional test cases"
119+
exit 0

0 commit comments

Comments
 (0)