Article From:https://www.cnblogs.com/sea-stream/p/9968886.html

 

One sentence judgement

[ ! $a ] && echo "a is null"

 

1.Judgement variable

read -p "input a word :" word
if  [ ! -n "$word" ] ;then
    echo "you have not input a word!"
else
    echo "the word you input is $word"
fi

perhaps

#!/bin/sh
a=
if [ ! -n "$a" ]; then
echo "IS NULL"
else
echo "NOT NULL"
fi

perhaps

#!/bin/sh
a=
if [ ! $a ]; then
echo "IS NULL"
else
echo "NOT NULL"
fi

 

2.Judging input parameters

#!/bin/bash
if [ ! -n "$1" ] ;then
    echo "you have not input a word!"
else
    echo "the word you input is $1"
fi

 

 

The following is not verified.

3. Judgment by variables directly

 

The results are as follows: IS NULL

#!/bin/sh
para1=
if [ ! $para1 ]; then
  echo "IS NULL"
else
  echo "NOT NULL"
fi 

 

 

4. Use test to judge

The result is: Dmin is not set!

#!/bin/sh
dmin=
if test -z "$dmin"
then
  echo "dmin is not set!"
else  
  echo "dmin is set !"
fi

perhaps

#!/bin/sh
a=
if test -z "$a" then 
echo "a is not set!" 
else
echo "a is set !"
fi

 

 

5. Use “judgment”

#!/bin/sh 
dmin=
if [ "$dmin" = "" ]
then
  echo "dmin is not set!"
else  
  echo "dmin is set !"
fi

perhaps

#!/bin/sh
a=
if [ "$a" = "" ]; then
echo "a is not set!"
 else
echo "a is set !"
fi

 

 

 

Here’s a bit of script code I wrote in a project for system startup:

#! /bin/bash
echo "Input Param Is [$1]"

if [ ! -n "$1" ] ;then
 echo "you have not input a null word!"
 ./app1;./app12;./app123
elif [ $1 -eq 2 ];then
 ./app12;./app123
elif [ $1 -eq 90 ];then
 echo "yy";
fi

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *