← Back to Bash Course | Chapter 14: Best Practices & Real-World Scripting | Lesson 4 of 7

Idempotent Scripts (फिर से चलाने के लिए Safe)

एक idempotent script चाहे आप इसे कितनी भी बार run करें वही end result देती है, दूसरी बार problems पैदा करने के बजाय।
Syntax
bash
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

bash
#!/bin/bash
mkdir -p app_data
mkdir -p app_data
echo "directory ready, and running this twice caused no error"
rmdir app_data

Duplicate Appends से बचना

एक line append करने से पहले check करना कि यह किसी file में पहले से मौजूद है या नहीं एक setup script को हर re-run पर duplicate entries accumulate करने से रोकता है।

Note: grep -qxF पूरी line को exactly match करता है और pattern को एक literal string की तरह treat करता है, partial matches और regex surprises दोनों से बचते हुए।

उदाहरण: Avoiding Duplicate Appends

bash
#!/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

Idempotent Symlink Creation

ln -sf एक existing symlink को overwrite करता है fail होने के बजाय जब यह पहले से कहीं point करता हो, जबकि plain ln -s error out करता है अगर link पहले से मौजूद हो, -f को उन scripts के लिए safer choice बनाते हुए जो एक से ज़्यादा बार run हो सकती हैं।

उदाहरण: Idempotent Symlink Creation

bash
#!/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

One-Time Actions को Guard करना

उन actions के लिए जो truly सिर्फ एक बार होनी चाहिए (जैसे एक initial database seed), उन्हें run करने से पहले एक marker file या condition check करना एक script को उस action दोहराए बिना safely re-run होने देता है।

उदाहरण: Guarding One-Time Actions

bash
#!/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"
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक setup script में -p के बिना mkdir उपयोग करना, जो पहले के बाद हर run पर error out करता है क्योंकि directory पहले से मौजूद है।
  2. हर run पर यह check किए बिना एक file में append करना कि content पहले से वहां है या नहीं, time के साथ duplicated entries का result देते हुए।
  3. यह मान लेना कि एक script जो "एक बार काम कर गई" automatically re-run करने के लिए safe है बिना यह consider किए कि अगर कुछ steps पहले से complete हो गए तो क्या होगा।
चैप्टर सारांश
  • एक idempotent operation एक बार या कई बार run होने पर same result produce करता है, जो setup scripts, deployments, और cron jobs के लिए मायने रखता है।
  • mkdir -p, append करने से पहले check करना, और plain ln -s के बजाय ln -sf उपयोग करना common idempotency techniques हैं।
  • Destructive या one-time actions को perform करने से पहले एक existence check से guard करें।
  • आगे से idempotency के लिए design करना एक script के duplicate side effects cause कर देने के बाद इसे retrofit करने से कहीं easier है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.