Linux Basics (6)

Source: Internet
Author: User

Linux Basics (vi)

Three loops and function knowledge points in shell scripts

First, the process control if structure

1. Simple If instance:

#!/bin/bashVar='/ETC/INIT.D'#var= '/DEV/SDA 'if[ -d $var] then echo"$var is directory"elif[ -b $var] then echo"$var is block"elif[ -F $var] then echo"$var is regular file"ElseEcho'Unknow'fi
View Code

Execution effect:

2. Passing parameters to a script

eg

#3.shEcho $0echo $1echo $2echo $3Echo ${11}echo'$$'$ $echo'$*'$*Echo'[email protected]'[Email Protected]echo'$#'$#Echo'$?'$?
View Code

Test results:

[[Email protected] ~] #  ./3.sh 1 2 3 4 5 6 7 8 9./3. SH1238755$* 1 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 9$#  ten$? 0
View Code

Two. While of the cyclic structure

while (condition)

Do
Action
Done

When an infinite loop is required we choose the while:
Write a script to let the user input, enter the number through, output error re-enter

#!/bin/bashlogin=0 while[$login! = 1]do Read-P'Please input your name:'name Read-P'Please input your password:'pwdif[$name = ='Egon'] && [$PWD = ='123'] then echo'Login sucessful'Login=1Fidone
View Code

Run and view results:

[[Email protected] ~] # ./4.sh  123login Sucessful[[email protected] Gandong]#
View Code

Third, the circular structure for

Shell format for

 for inch {1..10}doecho $idone
View Code
[[Email protected] ~] #  12345678910
View Code

An example of applying a loop and conditional judgment write:

Vim 6. SH while:d o Read-P'Please input your name:'name Read-P'Please input your password:'PSDif[-Z $name] | | [-z $PSD] ThenContinuefiif[$name ='Alex'-A $psd ='alex3714'] then echo'login successful, welcome da SB'             while: Do read-P'Please input your cmd:'cmdif[$cmd ='quit'] Then Breakfi $cmd doneElseEcho'Username or password is error'Fidone Echo'========================================================'~
View Code

Four, the function knowledge point

Functions in the interactive shell

eg

Vim 7'aaa'bbbb';} ABC
View Code

Operation Result:

[[Email protected] ~] # Vim 7.sh [[Email protected] ~] # ./7.sh aaabbbb
View Code

Linux Basics (6)

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.