Idempotent Scripts (फिर से चलाने के लिए Safe)
In this page:
mkdir -p directory_path
ln -sf target_file link_name
grep -qxF "line" file || echo "line" >> file
Idempotent Directory Creation
mkdir -p silently succeed होता है चाहे directory पहले से मौजूद हो या न हो, plain mkdir के विपरीत, जो second run पर error देता है। इसे दो बार run करने से दोनों बार same outcome मिलता है।
उदाहरण: Idempotent Directory Creation
#!/bin/bash
mkdir -p app_data
mkdir -p app_data
echo "directory ready, and running this twice caused no error"
rmdir app_data
Login to try C/C++/Java/PHP code in the editor
Duplicate Appends से बचना
एक line append करने से पहले check करना कि यह किसी file में पहले से मौजूद है या नहीं एक setup script को हर re-run पर duplicate entries accumulate करने से रोकता है।
grep -qxF पूरी line को exactly match करता है और pattern को एक literal string की तरह treat करता है, partial matches और regex surprises दोनों से बचते हुए।उदाहरण: Avoiding Duplicate Appends
#!/bin/bash
config_line="export APP_ENV=production"
touch profile.sh
add_once() {
grep -qxF "$1" "$2" || echo "$1" >> "$2"
}
add_once "$config_line" profile.sh
add_once "$config_line" profile.sh
echo "line count after running twice: $(wc -l < profile.sh)"
rm -f profile.sh
Login to try C/C++/Java/PHP code in the editor
Idempotent Symlink Creation
ln -sf एक existing symlink को overwrite करता है fail होने के बजाय जब यह पहले से कहीं point करता हो, जबकि plain ln -s error out करता है अगर link पहले से मौजूद हो, -f को उन scripts के लिए safer choice बनाते हुए जो एक से ज़्यादा बार run हो सकती हैं।
उदाहरण: Idempotent Symlink Creation
#!/bin/bash
touch real_file_v1.txt real_file_v2.txt
ln -sf real_file_v1.txt current.txt
ln -sf real_file_v2.txt current.txt
echo "symlink now points to: $(readlink current.txt)"
rm -f real_file_v1.txt real_file_v2.txt current.txt
Login to try C/C++/Java/PHP code in the editor
One-Time Actions को Guard करना
उन actions के लिए जो truly सिर्फ एक बार होनी चाहिए (जैसे एक initial database seed), उन्हें run करने से पहले एक marker file या condition check करना एक script को उस action दोहराए बिना safely re-run होने देता है।
उदाहरण: Guarding One-Time Actions
#!/bin/bash
marker=".initialized"
if [[ -f $marker ]]; then
echo "already initialized, skipping setup"
else
echo "running one-time setup"
touch "$marker"
fi
rm -f "$marker"
Login to try C/C++/Java/PHP code in the editor
- एक setup script में
-pके बिनाmkdirउपयोग करना, जो पहले के बाद हर run पर error out करता है क्योंकि directory पहले से मौजूद है। - हर run पर यह check किए बिना एक file में append करना कि content पहले से वहां है या नहीं, time के साथ duplicated entries का result देते हुए।
- यह मान लेना कि एक script जो "एक बार काम कर गई" automatically re-run करने के लिए safe है बिना यह consider किए कि अगर कुछ steps पहले से complete हो गए तो क्या होगा।
- एक idempotent operation एक बार या कई बार run होने पर same result produce करता है, जो setup scripts, deployments, और cron jobs के लिए मायने रखता है।
mkdir -p, append करने से पहले check करना, और plainln -sके बजायln -sfउपयोग करना common idempotency techniques हैं।- Destructive या one-time actions को perform करने से पहले एक existence check से guard करें।
- आगे से idempotency के लिए design करना एक script के duplicate side effects cause कर देने के बाद इसे retrofit करने से कहीं easier है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first:
- Quoting Pitfalls and Why to Always Quote Variables
- set -euo pipefail as a Script Header
- Structuring a Script (Functions, main() Pattern, Exit at the End)
- Idempotent Scripts (Safe to Re-Run)
- Logging in Scripts (Timestamped log Function)
- Writing Portable Scripts (Bash-isms vs POSIX sh)
- Capstone: A Real-World Backup/Cleanup Script