Bash Check If File Exists and Not Empty
Code Implementation
#!/bin/bash
FILE="data.txt"
if [ -s "$FILE" ]; then
echo "File exists and is not empty."
else
echo "File does not exist or is empty."
fi
In Bash scripts, it is often necessary to check if a file exists and is not empty.
Snippet Description
- The
-sparameter indicates that the file exists and its size is greater than 0 - If the file does not exist or is empty, it enters the else branch
- Can be used in scenarios such as scheduled tasks and log monitoring
Recommended Snippets
JavaScript Debounce Async Function
In JavaScript, the debounce function is a core tool for optimizing high-frequency and time-consuming operations. Its core logic lies in delaying function execution while canceling repeated delays. This ensures that when a function is triggered multiple times within a short period, it will only execute after waiting for a specified delay following the last trigger. This avoids performance overhead caused by unnecessary invocations. The working principle can be analogized to "an elevator closing its doors": After an elevator opens, it waits for a fixed period (e.g., 2 seconds) by default before closing. If a new passenger enters during this waiting period (corresponding to a new trigger of the function), the original waiting timer is canceled and the countdown restarts. Only when no new triggers occur after the countdown ends will the "door-closing" action (corresponding to the function execution) take place.
SQL Filtering and Sorting Data
Retrieve data from a table that meets specific criteria and sort it by a certain field. This SQL code snippet assumes you want to retrieve users over 25 years old from the users table and sort them by age in ascending order.
Hello World
Perl Hello World Example, Perl is a powerful text processing language, known for its flexibility and rich regular expression support