Logo video2dn
  • Сохранить видео с ютуба
  • Категории
    • Музыка
    • Кино и Анимация
    • Автомобили
    • Животные
    • Спорт
    • Путешествия
    • Игры
    • Люди и Блоги
    • Юмор
    • Развлечения
    • Новости и Политика
    • Howto и Стиль
    • Diy своими руками
    • Образование
    • Наука и Технологии
    • Некоммерческие Организации
  • О сайте

Скачать или смотреть Unix & Linux: Testing if a variable is empty in a shell script (4 Solutions!!)

  • Роэль Ван де Паар (Техническая помощь Роэля)
  • 2020-08-13
  • 58
Unix & Linux: Testing if a variable is empty in a shell script (4 Solutions!!)
shellshell scriptportabilitytestanswerscriptingunixlinux
  • ok logo

Скачать Unix & Linux: Testing if a variable is empty in a shell script (4 Solutions!!) бесплатно в качестве 4к (2к / 1080p)

У нас вы можете скачать бесплатно Unix & Linux: Testing if a variable is empty in a shell script (4 Solutions!!) или посмотреть видео с ютуба в максимальном доступном качестве.

Для скачивания выберите вариант из формы ниже:

  • Информация по загрузке:

Cкачать музыку Unix & Linux: Testing if a variable is empty in a shell script (4 Solutions!!) бесплатно в формате MP3:

Если иконки загрузки не отобразились, ПОЖАЛУЙСТА, НАЖМИТЕ ЗДЕСЬ или обновите страницу
Если у вас возникли трудности с загрузкой, пожалуйста, свяжитесь с нами по контактам, указанным в нижней части страницы.
Спасибо за использование сервиса video2dn.com

Описание к видео Unix & Linux: Testing if a variable is empty in a shell script (4 Solutions!!)

Unix & Linux: Testing if a variable is empty in a shell script


The Question: I have seen the following technique used many times on many different shells,
to test if a variable is empty:
if [ "x$1" = "x" ]; then
Variable is empty
fi
Are there any advantages on using this over the more canonical if [ -z "$1" ]?
Could it be a portability issue?

Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful

== This solution helped 5 people ==
According to http://www.mpi-inf.mpg.de/~uwe/lehre/...,
the -z test is unsafe in some implementations, presumably when "interesting"
strings like "-o a=a" are tested.

== This solution helped 17 people ==
Some historical shells implemented a very simple parser that could get confused
by things like [ -n = "" ] where the first operand to = looks like an operator,
and would parse this as [ -n = ] or cause a syntax error. In [ "x$1" = x"" ],
the x prefix ensures that x"$1" cannot possibly look like an operator, and so
the only way the shell can parse this test is by treating = as a binary
operator.
All modern shells, and even most older shells still in operation, follow the
http://pubs.opengroup.org/onlinepubs/...
test.html#tag_04_140_05 which mandate that all test expressions of up to 4
words be parsed correctly. So [ -z "$1" ] is a proper way of testing if $1 is
empty, and [ "$x" = "$y" ] is a proper way to test the equality of two
variables.
Even some current shells can get confused with longer expressions, and a few
expressions are actually ambiguous, so avoid using the -a and -o operators to
construct longer boolean tests, and instead use separate calls to [ and the
shell's own && and || boolean operators.

== This solution helped 3 people ==
The above tests will also cause an error if you run with "set -u" or "set -
o nounset"
A more stable way to check for an empty variable would be to use http://
www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html:
MYVAR=${MYVAR:-"Bad Value"}
This method works for the traditional bourne shell, as well as ksh, and bash.

function isBlank {
valueNoSpaces=$(echo "$@" | tr -d ' ')

With thanks & praise to God, and with thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free... | Images: https://stocksnap.io/license & others | With thanks to user steve (https://unix.stackexchange.com/users/..., user Scott Hoffman (https://unix.stackexchange.com/users/..., user rahmu (https://unix.stackexchange.com/users/..., user Gilles 'SO- stop being evil' (https://unix.stackexchange.com/users/..., user aslam mohammed (https://unix.stackexchange.com/users/..., user Ansgar Esztermann (https://unix.stackexchange.com/users/..., and the Stack Exchange Network (http://unix.stackexchange.com/questio.... Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything is amiss at Roel D.OT VandePaar A.T gmail.com.

Комментарии

Информация по комментариям в разработке

Похожие видео

  • О нас
  • Контакты
  • Отказ от ответственности - Disclaimer
  • Условия использования сайта - TOS
  • Политика конфиденциальности

video2dn Copyright © 2023 - 2025

Контакты для правообладателей [email protected]