Bird's Linux private cuisine--16th chapter: Learning Shell Scripts

Source: Internet
Author: User

Video link:

1. What is shell script (script written by Shell)
1.1 Why study shell scripts? ()
1.2 Writing and execution of the first script
1.3 Creating a good habit of writing shell script
2. Simple shell Script Exercise: (read-p date)
3. Use the Judgment formula:
3.1 Testing function with test command
3.2 Using the judging symbol []
3.3 The default variables for Shell script ($ ...)
4. Conditional Judgment Type:
4.1 Using If ... then
4.2 Using case ... Esac judgment
4.3 Using function functions
5. Loops (Loop)
5.1 While....do....done, Until....do....done
5.2 For...do...done
6. Shell script Tracking and debug

The first shell scripts is written, we write a test.sh in the document

#!/bin/Bash
Path= himself echo $PATH a bit
" Hello shuyunquan \ n " 0

And then execute it.

Bash test.sh

Next come an advanced scripts, we add a read

The test command is actually the purpose of the judgment, I list the commonly used parameters, and then give an example

[] The command is also used for judgment, and interestingly, the parameter of the test command [] can be used

[] points to note

Let's practice an example.

1. When executing a program, this program will let the user choose Y or N,
2. If the user enters Y or y, the "OK, continue" is displayed
3. If the user enters n or n, it displays "Oh, Interrupt!" 』
4. If it is not the other characters within the y/y/n/n, it will show "I Don t know what is your choise"

#!/bin/Bashpath=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/Gamesexport Pathread-P"Please enter (y/n):"yn["$yn"=="Y"-O"$yn"=="y"] &&Echo "ok,continue"&& exit0[ "$yn"=="N"-O"$yn"=="N"] &&Echo "OH,INTSDSD"&& exit0Echo "I don't believe this, I can come out."&& exit0

Default variables for shell scripts ($ $)

This is easy to understand, the script name is $ A and then the first parameter is the second argument is $ ... And so on

Okay, let's take a simple example.

 #!/bin/bash  echo  "  the scripts name is $   "" Span style= "COLOR: #000000" >[-n  " $1   "] && echo  "   The first parameter is: $   " | | Exit 0  [-n  $2   "] && echo  "   The second parameter is: $ $   " | | Exit 0  

If...then

if []; Then

......

elif []; Then

.......

Else

........

Fi

Exit 0

#!/bin/Bashpath=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/Gamesexport Pathread-P"Please enter (y/n):"ynif["$yn"=="Y"-O"$yn"=="y"]; Then         Echo "ok,continue"elif["$yn"=="N"-O"$yn"=="N"]; Then        Echo "OH,INTSDSD" Else        Echo "I don't believe this, I can come out." fiExit0       

Case ... Esac

#!/bin/Bash Case$1 inch        "Hello")                Echo "hello,how is You"                ;; "")                Echo "You must enter the parameters"                ;; *)                Echo "to enter hello."                ;;Esac

function functions

#!/bin/Bashfunctionh () {Echo "Shu Yun Quan is so handsome.The//function function also has variables, which are the parameters of the function, which are different from the variables of the script.} Case$1 inch        " One") H1                ;; " Both") H2                ;; "three") H3                ;; *)                Echo "only with 123 ."                ;;Esac

Loop loops

While do-done note, while inside the judgment! Go into the loop if it meets the while condition

#!/bin/Bashread-P"Please enter a number:"Numberi=0s=0 while["$i"-lt"$number"]//The less-than number here is-LT Note!  DoI=$ (($i +1) //Why do we need two () here? Because () there is the meaning of the operation S=$ (($s +$i)) DoneEcho-E"the result of ' 1+2+...+ $number ' is: $s"                             

You can try Echo (1+2) is not out of 3, and Echo ((1+2) will come out 3, so () have a whole operation meaning

Untile do-done conditions don't fit in.

For...do...done here is the first use of

#!/bin/bashs=0for ((i=1; i<=, i++)) do        s=$ (($s +$i))doneecho $s

For the second use of animal, the loop will show three different things in turn

#!/bin/bashfor oncat  elephantdo        echo"  This time the animals are $animal" Done      

Shell script Tracking and debug

Is that the script you wrote has no problem, bash comes with parameters to check

Bird's Linux private cuisine--16th chapter: Learning Shell Scripts

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.