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
🌎 Southern China’s Stone Forest, or Shilin, is a dramatic landscape of towering limestone pillars shaped by both natural erosion and centuries of quarrying. Human mining over the last thousand years has altered parts of this ancient karst region, creating new rock formations and open spaces alongside original spires. ✨
#geology⚡#humanimpact⚡#landscapes
👉subscribe Interesting Planet
👉more Channels
🌍 Cities often create "urban heat islands," where temperatures are higher than surrounding areas due to concrete, asphalt, and less greenery. This effect can raise cities’ temperatures by several degrees. ✨
#urbanization⚡#temperature⚡#humanimpact⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography🌍
🌎 One of Earth's largest artificial lakes, Lake Volta in Ghana, was created in the 1960s by damming the Volta River. Spanning about 8,500 square kilometers, it supports fisheries and transportation but submerged forests and displaced over 78,000 people during its construction. ✨
#humanimpact⚡#reservoirs⚡#Africa
👉subscribe Interesting Planet
👉more channels Channel list
🌎 The Florida Everglades, once a vast slow-moving river system, has been dramatically altered by canals, levees, and drainage for agriculture and cities. Over 50% of the original wetlands have been lost since the early 20th century, reducing habitat for species like the Florida panther and snail kite. ✨
#Everglades⚡#wetlands⚡#humanimpact
👉subscribe Interesting Planet
👉more Channels