Shell script already has a variable name configured for the parameter.

Source: Internet
Author: User

/path/to/scriptname  opt1  opt2  opt3  opt4        $0             $1    $2     $3    $4

Is that clear enough for you? Run the script file named "$" variable, the first parameter is $ ~ ~ So, as long as we use in script, we can easily release some command function immediately! In addition to the variables of these numbers, we have a few more special variables that can be used within a script to call these parameters Oh!

    • $#: On behalf of the following parameters "number", the above table as an example shown here as "4";
    • [Email protected]: for "$" "" $ "" $ "" $ "" $4 "meaning, each variable is independent (enclosed in double quotation marks);
    • $*: Represents "" "$C$ c$ C $4" ", where C is a delimited byte and the default is a blank key, so in this case, the meaning of" $ $ $4 "is represented in this example.

That [email protected] and $* are basically different! However, the general use of the situation can be directly memory [email protected] can! All right, let's do an example. Let's say I'm going to run a script that can carry parameters, and the screen will display the following data when I run it:

    • What is the file name of the program?
    • How many parameters are there?
    • If the number of parameters is less than 2, it tells the user that the number of parameters is too low
    • What are all the parameters?
    • What is the first parameter?
    • Why the second parameter

Script name sh04. SH

#!/bin/bash# program:# program shows the sripts name, parameters...# history:# -/9/6ZENGDP First Releasepath=/bin:/sbin/:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/Binexport PATHEcho "The script name is ==> $"Echo "Total parameter number is ==> $#"[ "$#"-lt2] && Acho"The number of parameter is less than 2. Stop here."&& exit0Echo "whole parameter is ==> ' [email protected] '"Echo "The 1st parameter ==> $"Echo "The 2nd parameter ==>"

Enter the command sh sh04. SH The one haha quot

After the script is run, output:

The script name is          ==> sh06.  SH total   parameternumber are4whole parameter      is ==>'the One haha quot'the1st parameter           ==> thethe 2nd parameter           ==> one

Shell script already has a variable name configured for the parameter.

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.