Shell programming Basics

Source: Internet
Author: User

(1) it must start with the following line (the first line of the file must be placed ):
#! /Bin/sh
Symbol #! The program used to tell the system to run the script. This example uses/bin/sh. After the editing is completed and saved, it is assumed that the script must be run first:
Chmod + x filename
Then enter./filename in the folder where the script is located to run the script.
(2) variable assignment and reference. In shell programming, variables do not need to be declared in advance. When you need to assign values to variables, you can write the following: variable name = value.
To use the value of a variable, you only need to add $ before the variable name (NOTE: When assigning values to the variable, you cannot leave spaces on both sides of "=)
# Assign values to string variables:
A = "Hello World"
# Print the value of string variable:
Echo "A is:" $
Running result: A is: Hello World

Assume that the variables are called directly within the sh file without being output to the terminal, and you can use the single-cited token:

Product = 'real6410'

Used in the called place:

Mkdir-P out/target/product/$ product/obj/lib
(3) If statement. If the expression assumes that the condition is true, the part after then is run:
If...; then
....
Elif...; then
....
Else
....
Fi
It is often used "[]" to indicate a conditional trial. Note that the spaces here are very important. Make sure that the spaces before and after square brackets are used. For example:
[-F "somefile"]: determines whether it is a file.
[-X "/bin/ls"]: determines whether/bin/ls exists and has the running permission.
[-N "$ Var"]: determines whether the $ var variable has a value.
["$ A" = "$ B"]: determines whether $ A and $ B are equal.

(4) loop.
While...; do
....
Done
The while loop will be executed until the condition of the explain expression is true. Keyword "break" is used to jump out of the loop, while keyword "continue" can skip the rest of the loop and directly jump to the next loop.

(5) Function

In SH, you can define an integration statement segment similar to a function, and declare it before the name. The method is as follows:

Function real6410_prebuild ()
{
Mkdir-P out/target/product/$ product/obj/lib
Mkdir-P out/target/product/$ product/system/lib
CP vendor/realarm/real6410/*. So out/target/product/$ product/obj/lib
CP vendor/realarm/real6410/*. So out/target/product/$ product/system/lib
}

Then, you can directly use real6410_prebuild elsewhere.

(6) sync

Synchronize the file handle and refresh all data not written to the hard disk.

(7) The content of the instance that can run the script update is as follows.

#!/bin/bashif [ "$1" = "" ]; thenecho "Please input resolution,"echo "Such as: qvga, wqvga, wvga, hvga"exitfip=$1./tool/bmp_to_raw ./temp0.raw ./$p/"${p}_uboot".bmp./tool/bmp_to_raw ./temp1.raw ./$p/"${p}_battery00".bmp./tool/bmp_to_raw ./temp2.raw ./$p/"${p}_battery02".bmp./tool/bmp_to_raw ./temp3.raw ./$p/"${p}_battery04".bmp./tool/bmp_to_raw ./temp4.raw ./$p/"${p}_battery06".bmp./tool/bmp_to_raw ./temp5.raw ./$p/"${p}_battery08".bmp./tool/bmp_to_raw ./temp6.raw ./$p/"${p}_low_battery1".bmp./tool/bmp_to_raw ./temp7.raw ./$p/"${p}_low_battery2".bmp./tool/bmp_to_raw ./temp8.raw ./$p/"${p}_batteryfull".bmp./tool/bmp_to_raw ./temp9.raw ./$p/"${p}_charger_ov".bmp./tool/bmp_to_raw ./boot_logo ./$p/"${p}_kernel".bmp./tool/zpipe -l 9 ./"${p}.raw" temp0.raw temp1.raw temp2.raw temp3.raw temp4.raw temp5.raw temp6.raw temp7.raw temp8.raw temp9.rawrm -rf ./temp0.raw ./temp1.raw ./temp2.raw ./temp3.raw ./temp4.raw ./temp5.raw ./temp6.raw ./temp7.raw ./temp8.raw ./temp9.raw ./bootlogo.rawecho "conversion finished"

Note: $1 indicates the first worker number. The preceding commands are run in sequence. Used for running. /update xxx can be run, but sometimes shell scripts are used. or source. These scripts must be run in the Current Shell environment (no sub-processes are created), that is, scripts that contain built-in commands.


Exam Original: http://bbs.chinaunix.net/thread-2231835-1-1.html

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.