Shell script Quick Start

Source: Internet
Author: User

I. Variable assignment

 
#! /Bin/bash
A = "Hello World"#= No space is allowed on both sides!
Echo"A is$"
UnsetA
Echo"This is $ {A} Nd"#It is best to use {} to expand the variable to avoid confusion.
 
Declare-I num1 = 10
Declare-I num2 = 20
Declare-I num3 =$ Num1+$ Num2

Important commands:

Ls wc cp mv grep cut cat file read sort uniq expr find Tee basename dirname head tail sed awk

 

2. Process Control

1. Parameter Control

 
#! /Bin/bash
Echo$1 $2 # parameter name
Echo$0 #ProgramName
Echo$ @ # List all parameters in a variable $ *
Echo$# Number of parameters
Echo$ # Process ID
Echo$? # Exit status of the previous command

2. If loop (parameters must be separated by spaces !!)

#! /Bin/bash
ForDay in 1 2 3 4 5 6 7
Do
If[$ Day= 6-o$ Day= 7]
Then
Echo"$ DayIs a nice day"
Elif [$ Day= 1]
Then
Echo"Starting to work ..."
Fi
Done

3. Case

Read-P 'Please input a num: 'num
Case $ NumIn
1)
Echo1 ;;
2)
Echo2 ;;
3 | 4)
Echo3 or 4 ;;
*)
Echo'Other ';;
Esac

4.

 #  ! /Bin/bash  
Read Dir
If [-D" $ Dir "-A-r" $ Dir "]; Then
File = $ (LS $ Dir )

For Filename in $ File
Do
If [-U" $ Dir / $ Filename "]; Then
Echo " $ Filename Has uid"
Ls-l" $ Dir / $ Filename "
Fi
Done
Else
Echo "Sorry $ Dir Does not exist or cocould not read"
Fi

In this example, enter/usr/bin.

5. While

 
#! /Bin/bash
Read Str
While["$ Str"! = 'Ubuntu ']
Do
Echo'Not right'
Read Str
Done
Echo"You have guessed it! "

6.

#! /Bin/bash
Read Str
Until [$ Str= 'Quit']
Do
Echo'Keep on'
Read Str
Done
Printf"You have entered quit! \ N"

 

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.