Linux learning-shell simple writing-shell

Source: Internet
Author: User

Linux learning-shell simple writing-shell

It is recommended that all scripts be stored in/usr/local/sbin.

Run the sh-x script. sh-x to view the execution process. 1. When using a variable in a script, use the $ symbol: #! /Bin/bash # Assign a value to a variable using reverse quotation marks D = 'date + "% H: % M: % S "' Echo "The script begin at $ d" Echo "Now we'll sleep 2 seconds" Sleep 2 D1 = 'date + "% H: % M: % S "' Echo "The script end at $ d" 2. Use mathematical operations in the script to [] include the following: #! /Bin/bash  A = 1 B = 2 # Mathematical operations are included in [] Sum = $ [$ a + $ B] Echo "$ a + $ B = $ sum" 3. Use the read command to interact with the console in the script #! /Bin/bash  Read-p "Please input a number:" x Read-p "Please input a number:" y Sum = $ [$ x + $ y] Echo "The sum of the tow numbers is: $ sum" Preset variables in 4shell #! /Bin/bash  Echo "$1 $2 $3 $0" 1 2. There is no limit on the preset variables in the script. 0 indicates the script file book. # Sh option. sh 1 2 3 The command output is as follows: 1 2 3 option. sh   Logical judgment in 5shell If without else, note that the judgment statement after if must use (); otherwise, an error is reported. #! /Bin/bash  Read-p "Please input your score:" If (a <60); then Echo "You didn't pass the exam ." Fi 5.1 The code for an else instance is as follows: #! /Bin/bash  Read-p "Please input your score:" If (a <60); then Echo "You didn't pass the exam ." Else Echo "Good! You passed the exam ." Fi 5.2 With else if (this is the statement in c) expressed as elif in shell #! /Bin/bash  Read-p "Please input your score:" If (a <60); then Echo "You didn't pass the exam ." Elif (a> = 60) & (a <85); then Echo "Good! You passed the exam ." Else Echo "Very good! Your score is very heigh" Fi 5.3 case logic judgment #! /Bin/bash  Read-p "Input a number:" n A = $ [$ n % 2] Case $ a in 1) Echo "The number is odd ." ;; 0) Echo "The number is even ." ;; *) Echo "It's not a number" ;; Esac * Other values 6 For Loop The instance code is as follows: #! /Bin/bash  For file in 'LS'; do Echo $ file Done 7 while loop #! /Bin/bash  A = 5 While [$ a-ge 1]; do Echo $ A = $ [$ A-1] Done          

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.