shell 之 流程控制語句if

2021-09-28 05:48:19 字數 2053 閱讀 7795

if 條件

then

commands

fi

#!/bin/bash

ifls /shell

then

echo

"there is a dir named shell"

fi

執行結果

注:根據我們命令退出的碼來進行判斷($? 是否為 0 ),如果是0,那麼就會執行then後面的命令

if 條件 ;

then

commands1

else

commands2

fi

1)檢視/etc/passwd檔案中是否包含root

#! /bin/bash

ifgrep root /etc/passwd;

then

# 「;」分號用於分隔同一行上的多條命令

echo

"it is ok"

else

echo

"error"

fi

執行結果

2)檢視/etc/passwd檔案中是否包含admin

#! /bin/bash

ifgrep admin /etc/passwd;

then

echo

"it is ok"

else

echo

"error"

fi

執行結果

# 語法結構

if 條件測試操作1

;then

commands1

elif 條件測試操作2

;then

commands2

elif 條件測試操作3

;then

commands3..

....

else

commands

fi

判斷使用者在系統中是否存在,是否有家目錄

#! /bin/bash

read -p "input a user:"

uname

ifgrep

$uname /etc/passwd;

then

echo

"the user $uname exists on this system"

elif

ls -d /home/$uname

;then

echo

"the user $uname not exists on this system"

echo

"the user $uname has a home directory"

else

echo

"the user $uname not exists on this system"

echo

"the user $uname has no directory"

fi

執行結果

shell指令碼之流程控制語句

一 分支控制語句 1 if fi條件 if condition then action fi2 if else fi條件 if condition then action else action fi3 if else if else fi條件 if condition then action el...

Mysql之流程控制語句

這篇部落格主要是總結一下mysq中的流程控制語句的用法,主要是 case,if,ifnull,nullif 1.case case value when compare value then result when compare value then result else result end ...

PL SQL之 流程控制語句

一 簡介 像程式語言一樣,oracle pl sql也有自己的流程控制語句。通過流程控制語句,我們可以在pl sql中實現一下比較複雜的業務邏輯操作。而無需到程式中去控制,在一定程度上提高了效率,這也是pl sql的強大之處。pl sql流程控制語句有如下幾種 二 語句種類 1 控制語句 a if語...