reload_flag=""
if [[ -n "${DEBUG}" ]]; then
reload_flag="--reload"
fi
if [[ -n "${WORKER_COUNT}" ]]; then
workers=${WORKER_COUNT}
else
workers=2
fi
gunicorn --workers ${workers} \
--bind 0.0.0.0:8000 \
${reload_flag} main.wsgi
Писали такие конструкции чтобы проверить наличие флага и сформировать команду правильно?
На самом деле можно сделать тоже самое проще. Для этого используются операторы условной подстановки, доступные в оболочках семейства POSIX.
:- для установки значений по умолчанию
${WORKER_COUNT:-2}
Если переменная не объявлена, то будет дефолтное значение 2.
:+ подставляет указанный текст, если переменная не пуста
${DEBUG:+--reload}
Если что-то есть в переменной то распечатается текст после символа +, в противном случае - ничего. Удобно для опциональных флагов, как в нашем примере.
Итого наш скрипт может выглядеть так:
gunicorn --workers ${WORKER_COUNT:-2} \
--bind 0.0.0.0:8000 \
${DEBUG:+--reload} main.wsgi
Есть еще два оператора.
:= не только подставить дефолтное значение, но и присвоить его переменной, если она пуста
# никаких переменных еще нет
VAL1=${VAL2:=hello}
# теперь доступны обе
echo $VAL1 $VAL2
# hello hello
:? остановить выполнение с ошибкой, если переменной нет.
echo ${MISS:?is required}
bash: MISS: is required
Код выхода будет 1.
#tricks#linux
🌍 In 2023, explorers finished mapping over 75% of the world’s seafloor in high detail, uncovering thousands of undersea mountains and canyons hidden beneath the oceans. ✨
#exploration⚡#mapping⚡#discovery⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 Over 80% of Earth’s ocean remains unmapped and unexplored by humans. Modern sonar and robotic submersibles are revealing new mountains, trenches, and species with every deep-sea journey. ✨
#exploration⚡#ocean⚡#discovery⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography🌍
🌍 The first map to show the Americas as a separate continent was made by German cartographer Martin Waldseemüller in 1507. It’s where the name “America” first appeared on a world map. ✨
#cartography⚡#history⚡#discovery⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography🌍
🌍 In 2022, explorers used sonar to map a hidden undersea volcano off Madagascar’s coast, revealing a peak 800 meters tall that was completely unknown before their expedition. ✨
#exploration⚡#discovery⚡#oceans⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌎 While mapping the ocean floor in 1947, researchers aboard HMS Challenger discovered the Mid-Atlantic Ridge, revealing Earth's largest mountain range hidden underwater. This seismic finding triggered the theory of plate tectonics—explaining how continents drift. The ridge stretches for over 16,000 kilometers beneath the Atlantic Ocean. ✨
#discovery⚡#geology⚡#oceanography
👉subscribe Interesting Planet
👉more Channels