shell中 和 的用法

2021-08-21 04:15:29 字數 1219 閱讀 7222

格式

command1 && command2

&&左邊的命令(命令1)返回真(即返回0,成功被執行)後,&&右邊的命令(命令2)才能夠被執行;換句話說,「如果這個命令執行成功&&那麼執行這個命令」。

命令之間使用 && 連線,實現邏輯與的功能。

只有在 && 左邊的命令返回真(命令返回值 $? == 0),&& 右邊的命令才會被執行。

只要有乙個命令返回假(命令返回值 $? == 1),後面的命令就不會被執行。

例子:

[root@ol01 htlin]# [[ -e "/tmp/htlin/htlin.txt" ]] && echo "file exits"

file exits

如果存在檔案/tmp/htlin/htlin.txt,則輸出「file exits」

[root@ol01 htlin]# [[ ! -e "/tmp/htlin/htlin.txt" ]] && echo "file not exits"

file not exits

反之不存在檔案/tmp/htlin/htlin.txt,則輸出「file not exits」

格式command1 || command2

||則與&&相反。如果||左邊的命令(command1)未執行成功,那麼就執行||右邊的命令(command2);或者換句話說,「如果這個命令執行失敗了||那麼就執行這個命令。

命令之間使用 || 連線,實現邏輯或的功能。

只有在 || 左邊的命令返回假(命令返回值 $? == 1),|| 右邊的命令才會被執行。這和 c 語言中的邏輯或語法功能相同,即實現短路邏輯或操作。

只要有乙個命令返回真(命令返回值 $? == 0),後面的命令就不會被執行。

例子:

[root@ol01 htlin]# [[ -e "/tmp/htlin/htlin.txt" ]] ||  echo "file not exits"

file not exits

1:怎麼將&&和||一起使用呢?

[root@ol01 htlin]# [[ -e "/tmp/htlin/htlin.txt" ]] ||  echo "file not exits" && echo "do something"

file not exits

do something

Shell中 和 的異同

是關鍵字,許多shell 如ash bsh 並不支援這種方式。ksh,bash 據說從2.02起引入對 的支援 等支援。是一條命令,與test等價,大多數shell都支援。在現代的大多數sh實現中,與 test 是內部 builtin 命令,換句話說執行 test 時不會調 1 gt lt 是算術比...

shell中 , 和 的區別

1.首先,儘管很相似,但是從概念上講,二者是不同層次的東西。是關鍵字,許多shell 如ash bsh 並不支援這種方式。ksh,bash 據說從2.02起引入對 的支援 等支援。是一條命令,與test等價,大多數shell都支援。在現代的大多數sh實現中,與 test 是內部 builtin 命令...

巨集中 和 的用法

巨集中 和 的用法 一 一般用法 我們使用 把巨集引數變為乙個字串,用 把兩個巨集引數貼合在一起.用法 i nclude i nclude using namespace std define str s s define cons a,b int a e b int main 二 當巨集引數是另乙...