← Back to Bash Course | Chapter 13: Advanced Scripting | Lesson 6 of 7

Heredoc Templating (Variables से Config File बनाना)

आप एक text file बना सकते हैं जिसकी contents आपके script में variables के आधार पर बदलती हैं, desired layout को एक बार template के रूप में लिखकर और Bash को blanks भरने देकर।
Syntax
bash
cat > output_file <<EOF
setting1=$variable1
setting2=$(command)
EOF

Variables से एक Config File Generate करना

क्योंकि यहां heredoc delimiter unquoted है, block के अंदर हर $variable लिखे जाने से पहले इसकी current value तक expand हो जाता है, effectively heredoc को एक template बनाते हुए।

उदाहरण: Generating a Config File from Variables

bash
#!/bin/bash
host="db.example.com"
port=5432
user="app_user"

cat > db_config.ini <<EOF
[database]
host=$host
port=$port
user=$user
EOF

cat db_config.ini
rm -f db_config.ini

Template में Command Substitution Include करना

क्योंकि expansion उसी तरह होता है जैसे double-quoted strings में होता है, एक unquoted heredoc के अंदर $(command) command run करता है और इसका output insert करता है, generated file को एक timestamp या hostname के साथ stamp करने के लिए उपयोगी।

उदाहरण: Including Command Substitution in a Template

bash
#!/bin/bash
version="1.2.0"

cat > release_notes.txt <<EOF
Release: $version
Generated year: $(date +%Y)
EOF

cat release_notes.txt
rm -f release_notes.txt

Templating से पहले Required Variables Validate करना

पहले से check करना कि template को ज़रूरी हर variable actually set है silently blank या missing values के साथ एक config file generate करने से बचता है, जो बाद में एक hard-to-diagnose bug हो सकता है।

उदाहरण: Validating Required Variables Before Templating

bash
#!/bin/bash
host="localhost"
port=""

if [[ -z $host || -z $port ]]; then
    echo "missing required template variable(s), skipping generation" >&2
else
    cat > out.ini <<EOF
host=$host
port=$port
EOF
fi
echo "validation check complete"
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. Heredoc delimiter को quote करना (<<EOF) जब आपको actually variable substitution चाहिए थी, जो expanded value के बजाय literal $var text produce करता है।
  2. यह भूल जाना कि command substitution और arithmetic expansion एक unquoted heredoc के अंदर भी होते हैं, सिर्फ simple variable expansion नहीं।
  3. एक config file generate करने से पहले यह validate न करना कि सभी required variables set हैं, जो silently empty values के साथ एक broken file produce कर सकता है।
चैप्टर सारांश
  • एक unquoted heredoc delimiter block के अंदर variables, command substitution, और arithmetic expansion allow करता है।
  • एक heredoc को > filename से एक file में redirect करना templates से config files generate करने का एक simple, readable तरीका है।
  • Heredoc templating को set -u (या explicit checks) के साथ combine करें ताकि एक missing variable generated file में एक empty gap के बजाय एक clear error का कारण बने।
  • एक unquoted heredoc के अंदर nested double quotes को उस तरह escape करने की ज़रूरत नहीं जैसे वे एक quoted string के अंदर होती।

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.