shell script basis (b)

&& tasks before and after the success to be successful

|| There is a before and after the mission success even if successful

[ $USER == root ] && echo "ok" || echo "no"

[A == a] && echo "equal" || echo "unequal"

a=

[-Z $ a] && echo "empty" || echo "not empty"  

 

#!/bin/bash

read - p "Enter account:" u

[-Z $ a] && echo "account can not be empty" && exit

useradd $u

stty -echo

read -p "Enter password:" p

stty echo

echo $p | passwd -- stdin $u106

 

-eq equal

-ne not equal

-gt greater than

greater than or equal -ge

-le less

-lt less than

 

[-Z $ a] // determines whether empty, not empty true    

[! -Z $ a] determines whether the non-empty, empty true

 

To test the file

Whether -e file exists

-D if file exists and is a directory

-F if file exists and is a regular file

-r whether the current user has read access to the file

-w whether the current user has write access to the file

-x whether the current user has permission to execute the file

[-E / opt / 123] 123 test whether there is, do not care about file types

echo $?

Use if the judge sentences

1. Single branch

if the test condition; then

   Executing instructions

be

2. Dual Branch

if the test condition 1; then

   1 instruction execution

else

   2 instruction execution

be

3. Multi-Branch

if the test condition 1; then

   1 instruction execution

elif condition test 2; then

2 instruction execution

else

Executing instructions 3

be

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

发布了13 篇原创文章 · 获赞 5 · 访问量 1201

Guess you like

Origin blog.csdn.net/Gus_lin/article/details/103816454