Linux Series-shell Learning Notes (cont.) Handling User Input

Source: Internet
Author: User

1. Running a program with parameters

$ A indicates the name of the program, which represents the first argument, and the second argument, and so on, until the nineth parameter $9

# VI Factorial

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

[Email protected] test]#/factorial 5120

Note: If there are multiple parameters, each parameter must have a space, and if a parameter contains spaces, it must be enclosed in single or double quotation marks.

2. Read the program name

Write scripts that perform different functions based on the name of the script you use

# VI addem

#!/bin/shname= ' basename $ ' echo $nameif [$name = "Addem"]then        echo $[$1+$2]elif [$name = "Mulem"]then        Echo $[ $ * $2]FI

# CP Addem Mulem

Test:

[[Email protected] test]# sh addem 3 47[[email protected] test]# SH Mulem 33 399

3. Parameter Count

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

You can get the last command-line argument variable by ${!#} , or params=$#, and then get it with $params.

4. Get all the data

$* and [email protected] variables provide quick access to all parameters.

[Email protected] test]# VI test11#!/bin/sh# testing $* and [email Protected]echo "Using the \$* method:$*" echo "Using th e \[email protected] method:[email protected] "test: [[email protected] test]#./test11 Rich Jjds FDs Qaa dsddusing the $* met Hod:rich jjds FDS QAA dsddusing The [email protected] Method:rich jjds FDS QAA DSDD

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

[email protected]: stored data is equivalent to multiple separate words.

5.shift Moving variable

Use shift to move the parameters forward one bit, $ $ will be shifted, and the last $ #的大小会减掉1

Use shift 2 to move two bits at a time

Determines whether the parameter that is brought in is not empty: if [-n] To determine if the argument is empty

6. Find Options

# VI test12

#!/bin/sh# extracting command line options as parameterswhile [-n "$]do Case" $ "in-a        ) echo"-A option "; -        B) echo "-B option";;        -c) echo "-C option";;        *) echo "is not a option";;        Esac        Shiftdone

Test:

[Email protected] test]#/test12-a-a option[[email protected] test]#./test12 cc is not an option[[email protected] tes t]#

7.getopt Command and Getopts command

8. Get user input

# VI test13

#!/bin/shecho "Please enter your name:" Read Nameecho "Hello $name, where come to Here" test: [[email protected] test]#./test13 Please enter your name:hahahello haha, where come to here

You can follow the specified prompt directly after read:

Read–p "Please entry your age:" Age

You can also use the-T to set timers

Read–t 5–p "Please entry your age:" Age

Other parameters:

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

9. Reading data from a file

# VI test15

#!/bin/shcount=1cat Test | While read Linedo        echo "line $count: $line"        count=$[$count + 1]doneecho "Finish file!"
Test
[Email protected] 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!

Linux Series-shell Learning Notes (cont.) Handling User Input

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.