Linux Shell user Input--organize

Source: Internet
Author: User

There are three main forms of processing Linux input in shell scripts:

1) Treat them like command line arguments, and obtain corresponding input parameters by corresponding positional parameters

2) through getopt and getopts These two commands

3) interactively get the input parameters of the user via the Read command

1. Get through the corresponding position parameters

  The position parameter in the shell is calculated from 0 to the next 1 corresponding to the user's input parameters, for example, the user's program name, $ $ corresponding to thefirst parameter, thesecond argument, and so on until the After 10 parameters, the curly brackets are used to obtain the corresponding position parameter package in ${10}.

  $#,$*,[email protected] has a special meaning in the parameters :

  $ #统计输入的参数的总数这个总数不包含程序名

  $* returns all parameters of the user as a single word

  [Email protected] Save all variables as a single word.

But ${$#} This method gets the last input parameter to return an incorrect value, in the correct way for ${!#}

The Shift command allows you to move the parameters of the corresponding parameter forward by using the SHIFT key to traverse the user input parameters .

Count=1

While [-N "$"]

Do

echo "Parameter # $count = $"

echo "Parameter \$0 # $count =$0"

count=$[$count + 1]

Shift

Done

Shift moves the $ after one to $ $ and the parameters will be deleted without being moved to the top.

2)getopt and getopts commands

  Getopt and getopts allow users to enter data according to the Linux command format, which is broadly similar to the format used by getopt and getopts. The getopt command formats the original command arguments to the set command to replace the original command-line arguments. The format used is as follows:

  Set--' getopt ab:c ' [email protected] ': Indicates that the corresponding B option needs to be followed by a parameter

At the same time, the getopt is separated by the --symbol to distinguish between option parameters and input transmissions.

  The getopt parameter is also obtained by corresponding parameters in the acquisition process.

The corresponding things are listed as follows:

Set--' getopt-q ab:c ' [email protected] '

While [-N "$"]

Do

Case "$" in

-a) echo "Found the-a option";;

-B) param=$2

echo "Found the-b option,with parameter Value $param"

shift;

-c) echo "Found the-c option";;

--) shift

break;;

*) echo "is not a option";;

Esac

Shift

Done

Count=1

for param in "[email protected]"

Do

echo "Parameter # $count: $param"

count=$[$count + 1]

Done

Input -ac–b test1 test2 output to -a-c-B test1-test2 after getopt format.

Getopts is slightly different from getopt. If the option needs to be followed by a parameter, this parameter will be saved to the optarg environment variable. The optind environment variable holds the parameter position that getopts is processing in the argument list.

The corresponding things are listed as follows:

While Getopts ab:c opt

Do

Case ' $opt ' in

A) echo "Found the-a option";;

b) echo "Found the-b option,with value $OPTARG";;

c) echo "Found the-c option";;

*) echo "unknow option: $opt";;

Esac

Done

Shift $[$OPTIND-1]

Count=1

for param in "[email protected]"

Do

echo "Parameter $count: $param"

count=$[$count + 1]

Done

3) interactively get the input parameters of the user via the Read command

Read-t 5-p "Enter a numer:" opt

-T set timeout time

-P Output Interactive string prompt, the user's input will be saved to the opt variable. If the OPT variable is not set , the user's input variable is saved to the $REPLY environment variable

-S will block recall of user input.

Count=1

Cat testcommand.sh | While Read line

Do

echo "line $count: $line"

count=$[$count + 1]

Done

Using read to read the data stored in the file, the readcommand reads a line of text from the file each time.

Linux Shell user Input--organize

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.