Heredoc Templating (Variables से Config File बनाना)
In this page:
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
#!/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
Login to try C/C++/Java/PHP code in the editor
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
#!/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
Login to try C/C++/Java/PHP code in the editor
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
#!/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"
Login to try C/C++/Java/PHP code in the editor
- Heredoc delimiter को quote करना (
<<EOF) जब आपको actually variable substitution चाहिए थी, जो expanded value के बजाय literal$vartext produce करता है। - यह भूल जाना कि command substitution और arithmetic expansion एक unquoted heredoc के अंदर भी होते हैं, सिर्फ simple variable expansion नहीं।
- एक 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 के अंदर होती।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first:
- getopts for Parsing Command-Line Flags
- Positional Parameters and shift
- Process Substitution in Practice (diff <(...) <(...))
- Arrays of Arguments ("$@" vs "$*" Quoting Difference)
- Nameref Variables (declare -n) for Passing Arrays by Reference
- Heredoc Templating (Generating a Config File from Variables)
- Default/Fallback Argument Handling