A method of determining whether an input variable or an argument is null in a shell script _linux shell

Source: Internet
Author: User

1. Judging variables

Copy Code code as follows:

Read-p "Input a word:" Word
if [!-n "$word"]; then
echo "You have not input a word!"
Else
echo "The word you are $word"
Fi

2. Judging input parameters

Copy Code code as follows:

#!/bin/bash
if [!-n ' $ ']; then
echo "You have not input a word!"
Else
echo "The word you are the input is $"
Fi

The following is not validated.

3. Directly through the variable judgment

As follows: The result is NULL

Copy Code code as follows:

#!/bin/sh
para1=
if [! $para 1]; Then
echo "is NULL"
Else
echo "Not NULL"
Fi

4. Use test to judge

The result is that DMin is not set!

Copy Code code as follows:

#!/bin/sh
dmin=
If Test-z "$dmin"
Then
echo "DMin is not set!"
Else
echo "DMin is set!"
Fi

5. Use "" to determine

Copy Code code as follows:

#!/bin/sh
dmin=
If ["$dmin" = ""]
Then
echo "DMin is not set!"
Else
echo "DMin is set!"
Fi

Here's a little bit of scripting code I wrote in a project that I used when the system started:

Copy Code code as follows:

#! /bin/bash

echo "Input Param is [$]"

if [!-n ' $ ']; then
echo "You have not input a null word!"
./app1; /APP12;. /app123
elif [$1-eq 2];then
./app12; /app123
elif [$1-eq];then
echo "YY";
Fi

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.