Shell script learning-accessing shell script Parameters

Source: Internet
Author: User

The so-called location parameter refers to the shell script command line parameter. In Shell functions, their colleagues can also be function parameters. Each parameter is named by an integer. For historical reasons, when it exceeds 9, it should be framed with braces:

 
Echo first Arg is$1Echo tenth Arg is$ {10}

In addition, through special variables, we can also get the total number of parameters and get all parameters at a time.

Case

If you want to know what terminal a user is using, you can use the who command directly, and then find it in the output. This is troublesome and error-prone, especially when many users are in the system. All you want to do is find the user in the output of WHO. In this case, you can use the grep command to perform the search operation. It lists each row that matches the first grep parameter. Assume that the user you are looking for is Betsy:

 
Who|Grep BetsyBetsy PTS/3 Dec 27 (flags-r-us.example.com)

After knowing how to find a specific user, we can put the command into the script. The first parameter of this script is the user name we are looking:

 CAT> finduser #  Create a new file  # ! /Bin/sh  #  finduser --- check whether the user specified by the first parameter is logged on   who  | grep 1 1   ^ d  #  end-of-file  
Chmod + x finduser#Set execution permission./Finduser Betsy#Test: Search for BetsyBetsy pts/3 Dec 27 :07 (flags-r-Us.example.com )./Finduser Benjamin#Find another friend, Ben.Benjamin dtlocal Dec 27 :55(Kites.example.com) mV finduser$ Home/Bin#Store this file in your bin directory.

Of course, thisProgramNot yet perfect. What would happen if we didn't give any parameters?

 
Finduserusage: grep [Option]... pattern [file]... Try 'grep-- Help'ForMore information.

specific test parameters to be continued...

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.