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.