Error: Script fails or behaves unexpectedly.
Example:
echo "Hello World"
Running ./script.sh may fail if no interpreter is specified.
Fix:
#!/bin/bash
echo "Hello World"
Always include a shebang at the top.
Make the script executable:
chmod +x script.sh
2. Permission Denied
Error:
bash: ./script.sh: Permission denied
Cause: The script file is not executable.
Fix:
chmod +x script.sh
./script.sh
3. Syntax Errors
Error:
syntax error near unexpected token `do'
Common Causes:
Missing do or done in loops
Missing then in if statements
Unmatched quotes (" or ')
Example:
if [ $num -eq 5 ]
echo "Number is 5"
fi
Fix:
if [ $num -eq 5 ]; then
echo "Number is 5"
fi
4. Variable Errors
Symptoms: Variables don’t expand, or you get errors like unary operator expected.
Common Causes:
Not using $ when referencing variables
Using spaces incorrectly
Example:
name=John
echo "Hello name"
Fix:
name=John
echo "Hello $name"
Another example (spaces):
count = 5 #
Fix:
count=5 # No spaces around =
5. Command Not Found
Error:
./script.sh: line 3: somecommand: command not found
Causes:
Typo in command
Command not installed
Command not in $PATH
Fix:
Check spelling
Install the command
Use full path (/usr/bin/somecommand)
6. File or Directory Not Found
Error:
./script.sh: line 4: /path/to/file: No such file or directory
Fix:
Make sure the file exists
Use correct paths
Use quotes for paths with spaces:
file="/path/to/My File.txt"
cat "$file"
7. Infinite Loops
Symptoms: Script never ends, CPU usage spikes.
Example:
while [ $count -le 5 ]
do
echo $count
# Missing increment
done
Fix:
while [ $count -le 5 ]
do
echo $count
((count++))
done
8. Quoting Issues
Symptoms: Unexpected word splitting or globbing.
Example:
name="John Doe"
echo $name # Works
touch $name # Creates two files: John and Doe
Fix: Always quote variables:
touch "$name" # Creates file "John Doe"
9. Using the Wrong Test Syntax
[ ... ] vs [[ ... ]]
-eq for numbers, = for strings
Example (error):
if [ $num = 5 ]; then #
Fix:
if [ $num -eq 5 ]; then # For numbers
Symptoms: Variables don’t expand, or you get errors like unary operator expected.
Common Causes:
Not using $ when referencing variables
Using spaces incorrectly
Example:
name=John
echo "Hello name"
Fix:
name=John
echo "Hello $name"
Another example (spaces):
count = 5 #
Fix:
count=5 # No spaces around =
5. Command Not Found
Error:
./script.sh: line 3: somecommand: command not found
Causes:
Typo in command
Command not installed
Command not in $PATH
Fix:
Check spelling
Install the command
Use full path (/usr/bin/somecommand)
6. File or Directory Not Found
Error:
./script.sh: line 4: /path/to/file: No such file or directory
Fix:
Make sure the file exists
Use correct paths
Use quotes for paths with spaces:
file="/path/to/My File.txt"
cat "$file"
7. Infinite Loops
Symptoms: Script never ends, CPU usage spikes.
Example:
while [ $count -le 5 ]
do
echo $count
# Missing increment
done
Fix:
while [ $count -le 5 ]
do
echo $count
((count++))
done
8. Quoting Issues
Symptoms: Unexpected word splitting or globbing.
Example:
name="John Doe"
echo $name # Works
touch $name # Creates two files: John and Doe
Fix: Always quote variables:
touch "$name" # Creates file "John Doe"
9. Using the Wrong Test Syntax
[ ... ] vs [[ ... ]]
-eq for numbers, = for strings
Example (error):
if [ $num = 5 ]; then #
Fix:
if [ $num -eq 5 ]; then # For numbers
10. Missing exit Codes
Scripts sometimes fail silently.
Always use exit 0 for success, exit 1 for failure if needed.
Example:
#!/bin/bash
if ! grep "something" file.txt; then
echo "Not found"
exit 1
fi
echo "Done"
exit 0
Scripts sometimes fail silently.
Always use exit 0 for success, exit 1 for failure if needed.
Example:
#!/bin/bash
if ! grep "something" file.txt; then
echo "Not found"
exit 1
fi
echo "Done"
exit 0
Tips to Avoid Errors
Always use #!/bin/bash or appropriate shebang.
Quote variables when in doubt.
Test scripts incrementally.
Use set -e (exit on error) and set -x (debug mode).
#!/bin/bash
set -e # Exit on first error
set -x # Print commands as they run
Check return codes with $?.
No comments:
Post a Comment