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
#quoteoftheday
💬“If you are not embarrassed by the first version of your product, you launched too late.” — Reid Hoffman, LinkedIn co-founder
🚀 While you are redesigning your logo for the tenth time, someone else is already monetizing their product at the MVP stage.
Follow Startup Base to stay updated on startups, opportunities, and the latest trends.
@startupbaseuz
LinkedIn | Facebook | Instagram | Website
#quoteoftheday
💬“Agar mahsulotingizning birinchi versiyasidan uyalmasangiz, siz uni juda kech ishga tushiribsiz”—Rid Hofman, LinkedIn asoschilaridan biri
🚀Siz logotipni o‘ninchi bor o‘zgartirayotganingizda, kimdir allaqachon mahsulotini MVP’da pullayapti.
Startaplar, ularga oid imkoniyatlar va trendlardan xabardor bo‘lish uchun Startup Base’ni kuzatib boring.
@startupbaseuz
LinkedIn | Facebook | Instagram | Website
☀️#Quoteoftheday
😇“Don't let yesterday take up too much of today.”
- Will Rogers
❤️@QuotesPoint
✅@Quotes_Positive_Inspirational
✔️@Quotes_Motivational_Inspiring
☀️#Quoteoftheday
😇“You cannot protect yourself from sadness without protecting yourself from happiness.”
- Jonathan Safran Foer
❤️@QuotesPoint
✅@Quotes_Positive_Inspirational
✔️@Quotes_Motivational_Inspiring
☀️#Quoteoftheday
😇“It's up to you how far you go. If you don't try, you'll never know!”
- Merlin, "Sword in the Stone"
❤️@QuotesPoint
✅@Quotes_Positive_Inspirational
✔️@Quotes_Motivational_Inspiring
☀️#Quoteoftheday
😇“Pursue the things you love doing and then do them so well that people can’t take their eyes off of you.”
- Maya Angelou
❤️@QuotesPoint
✅@Quotes_Positive_Inspirational
✔️@Quotes_Motivational_Inspiring
☀️#Quoteoftheday
😇“This is your life. Do what you love, and do it often.”
- Holstee Manifesto
❤️@QuotesPoint
✅@Quotes_Positive_Inspirational
✔️@Quotes_Motivational_Inspiring
☀️#Quoteoftheday
😇“Focus is the art of knowing what to ignore.”
- James Clear
❤️@QuotesPoint
✅@Quotes_Positive_Inspirational
✔️@Quotes_Motivational_Inspiring
☀️#Quoteoftheday
😇“Always remember that the future comes one day at a time.”
- Dean Acheson
❤️@QuotesPoint
✅@Quotes_Positive_Inspirational
✔️@Quotes_Motivational_Inspiring