linux shell if

if statement format

if ....; then
  ....
elif ....; then
  ....
else
  ....
be

 

 

code example

#!/bin/bash
if [ ! -d $1 ]; then
  echo "not a directory"
elif [ -d $1 ]; then
  echo "is a directory"
else
  echo "haha"
be

 

 

execute the test

root@bosh:# ./test /root
is a directory
root@bosh:# ./test /root1
not a directory

 

 

Description:
The [ ] part is to judge the expression
-d to judge whether it is a directory (directory)
$1 is used to receive the parameters passed in from the command line, the first parameter is represented by $1, and the second parameter is represented by $2. . . And so on ($0 is the filename of the execution).

 

Note:
1. There must be a space between if and [, otherwise an error will be reported.
2. There should be a space between elif and [, otherwise an error will be reported.
3. There must be a space between [] and the inner expression, otherwise an error will be reported.

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326940299&siteId=291194637