Linux series-shell learning notes (Continued 1) process user input,-shell learning notes

Source: Internet
Author: User

Linux series-shell learning notes (Continued 1) process user input,-shell learning notes

1. Run the program with Parameters

$0 indicates the program name, $1 indicates the first parameter, $2 indicates the second parameter, and so on until the ninth parameter $9

# Vi factorial

#!/bin/shf=1for((i=1;i<=$1;i++))do        f=$[ $f * $i]doneecho $f
Test:

[root@master test]# ./factorial 5120

NOTE: If there are multiple parameters, each parameter must have a space. If a parameter contains a space, it must be enclosed by single quotation marks or double quotation marks.

2. Read program name

Compile scripts that execute different functions based on the script name used

# Vi addem

#!/bin/shname=`basename $0`echo $nameif [ $name = "addem" ]then        echo $[$1+$2]elif [ $name = "mulem" ]then        echo $[ $1 * $2]fi

# Cp addem mulem

Test:

[root@master test]# sh addem 3 47[root@master test]# sh mulem 33 399

3. Parameter count

You can use$ #To count the number of parameters.

You can use$ {! #}To obtain the parameter variable of the last command line, or params =$ #, and then get it with $ params.

4. Obtain all data

$ * And $ @ variables provide quick access to all parameters.

[Root @ master test] # vi test11 #! /Bin/sh # testing $ * and $ @ echo "Using the \ $ * method: $ *" echo "Using the \ $ @ method :$ @" test: [root @ master test] #. /test11 rich jjds fds qaa dsddUsing the $ * method: rich jjds fds qaa dsddUsing the $ @ method: rich jjds fds qaa dsdd

$ *: The stored data is equivalent to a single word;

$ @: The stored data is equivalent to multiple independent words.

5. shift variable

Shift can be used to move all parameters one by one, $1 will be transferred, and the last $ # size will be reduced by 1

Shift 2 indicates moving two digits at a time

Whether the input parameter is not null: if [-n $1] indicates to judge whether $1 is null.

6. search options

# Vi test12

#!/bin/sh# extracting command line options as parameterswhile [ -n "$1" ]do        case "$1" in        -a) echo "-a option" ;;        -b) echo "-b option" ;;        -c) echo "-c option" ;;        *) echo "$1 is not an option" ;;        esac        shiftdone

Test:

[root@master test]# ./test12 -a-a option[root@master test]# ./test12 cc is not an option[root@master test]#

7. getopt and getopts commands

 

8. Get user input

# Vi test13

#! /Bin/shecho "please enter you name:" read nameecho "hello $ name, where come to here" test: [root @ master test] #. /test13please enter you name: hahahahello haha, where come to here

You can directly follow the specified prompt after reading:

read –p "please entry you age:" age

You can also use-t to set the timer.

read –t 5 –p "please entry you age:" age

Other parameters:

-S is implicitly read and data is displayed, but the read command sets the text color to the same as the background color;

9. read data from a file

# Vi test15

#!/bin/shcount=1cat test | while read linedo        echo "Line $count: $line"        count=$[$count + 1]doneecho "finish file!"
Test
[root@master test]# ./test15Line 1: #!/bin/shLine 2: if dateLine 3: thenLine 4: echo "it worked!"Line 5: fiLine 6: echo "this is a test file"Line 7: echo "this is a test file"finish file!

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.