Default variable of shell script ($0, $1 ...)

Source: Internet
Author: User

 

Suppose I want to run a script with parameters. After running the script, the screen displays the following data:

  • What is the program file name?
  • How many parameters are there?
  • If the number of parameters is smaller than 2, the user is notified that the number of parameters is too small.
  • What are all the parameters?
  • What is the first parameter?
  • What is the second parameter?

#! /Bin/bash
# The program shows the script name, and the parameters ....

Echo "The Script Name Is ==>$ 0"
Echo "The num of the parameters is ==>$ #"
[$ #-Lt 2] & Echo "The num is less than 2. Stop here! "& Exit 0
Echo "the whole parameter is ==> '$ @'"
Echo "the 1st parameter is ==>$ 1"
Echo "the 2nd parameter is> $2"

 

Run the script:
[Oracle @ sor_sys ~] $ Sh parameters. Sh opt1 Oracle 192.168.50.229 8081
The script name is ==> parameters. Sh
The num of the parameters is => 4
The whole parameter is ==> 'opt1 Oracle 192.168.50.229 100'
The 1st parameter is ==> opt1
The 2nd parameter is> Oracle
[Oracle @ sor_sys ~] $

  • $ #: Indicates the number of connected parameters. The preceding table is shown as "4 』;
  • $ @: Represents "" $1 "" $2 "" $3 "" $4 "". Each variable is independent (enclosed in double quotation marks );
  • $ *: "" $1C$2C$3C$4 "", where
    CIs the delimiter. The default Delimiter is a blank key. Therefore, this example indicates "$1 $2 $3 $4.

 

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.