Онлайн-рабочие станции OnWorks Linux и Windows

Логотип

Бесплатный хостинг в Интернете для рабочих станций

<Предыдущая | Содержание: | Следующая>

Более современная версия теста

Последние версии колотить include a compound command that acts as an enhanced replace- ment for тестXNUMX. It uses the following syntax:

[[ выражение ]]

where, like тестXNUMX, выражение is an expression that evaluates to either a true or false re- sult. The [[]] команда очень похожа на тестXNUMX (it supports all of its expressions), but


adds an important new string expression:

string1 =~ регулярное выражение

which returns true if string1 is matched by the extended regular expression регулярное выражение. This opens up a lot of possibilities for performing such tasks as data validation. In our earlier example of the integer expressions, the script would fail if the constant INT contained anything except an integer. The script needs a way to verify that the constant contains an integer. Using [[]] с =~ string expression operator, we could improve the script this way:



#! / Bin / Баш


# test-integer2: оценить значение целого числа.


ИНТ = -5


если [["$ INT" = ~ ^ -? [0-9] + $]]; тогда

если [ $INT -eq 0 ]; затем отобразите «INT равно нулю».

еще

если [$ INT -lt 0]; затем эхо «INT отрицательно».

еще

echo "INT положительный."

fi

если [$ ((INT% 2)) -экв 0]; затем эхо "INT четное".

еще

echo "INT нечетный."

fi

fi

еще

echo "INT не является целым числом." > & 2 выход 1

fi

#! / Bin / Баш


# test-integer2: оценить значение целого числа.


ИНТ = -5


если [["$ INT" = ~ ^ -? [0-9] + $]]; тогда

если [ $INT -eq 0 ]; затем отобразите «INT равно нулю».

еще

если [$ INT -lt 0]; затем эхо «INT отрицательно».

еще

echo "INT положительный."

fi

если [$ ((INT% 2)) -экв 0]; затем эхо "INT четное".

еще

echo "INT нечетный."

fi

fi

еще

echo "INT не является целым числом." > & 2 выход 1

fi


By applying the regular expression, we are able to limit the value of INT to only strings that begin with an optional minus sign, followed by one or more numerals. This expres- sion also eliminates the possibility of empty values.

Another added feature of [[]] в том, что == operator supports pattern matching the same way pathname expansion does. For example:



[я @ linuxbox ~] $ FILE=foo.bar

[я @ linuxbox ~] $ FILE=foo.bar

Более современная версия теста


[я @ linuxbox ~] $ if [[ $FILE == foo.* ]]; then

> echo "$FILE matches pattern 'foo.*'"

> fi

foo.bar matches pattern 'foo.*'

[я @ linuxbox ~] $ if [[ $FILE == foo.* ]]; then

> echo "$FILE matches pattern 'foo.*'"

> fi

foo.bar matches pattern 'foo.*'


Это делает [[]] useful for evaluating file and pathnames.


Лучшие облачные вычисления для ОС в OnWorks: