Basic syntax of shell programming for Linux

Source: Internet
Author: User
Tags linux shell commands

The shell is an interface that users interact with the kernel, and the most popular shell is now called the Bash shell. The shell is also a programming language < interpreted programming language >, Shell Scripting < is programming > with Linux shell commands. A system can have multiple shells, and you can view the shells installed in the system with the Cat/etc/shells command, which may be different from the command syntax that the shell might support.

The original text is discussed with the author: http://www.cnblogs.com/intsmaze/p/6681562.html

: Intsmaze

Basic Format

The code is written in a plain text file, usually with an. sh suffix

VI intsmaze.sh

# !/bin/bash    # # Indicates which Shell parser to use to parse the execution of our script  "helloWorld"   # #  Comments can also be written here ## This is a line of comments

Execute script

SH intsmaze.sh

or add x permissions to the script, directly execute, otherwise only through the SH xxx.sh execution

chmod 755 intsmaze.sh

./intsmaze.sh

Basic Syntax System Variables

The variables in the Linux shell are divided into "system variables" and "user-defined variables". You can view system variables by using the SET command.

[[Email protected]shtest]$ Setbash=/bin/BashHadoop_home=/home/hadoop/hadoop-2.6.4Histcontrol=Ignoredupshistfile=/home/hadoop/. Bash_historyhistfilesize=1000histsize=1000HOME=/home/Hadoophostname=centos-reall-131HOSTTYPE=I386id=500Java_home=/home/hadoop/app/jdk1.7. 0_65lang=en_us. UTF-8Lessopen='|/usr/bin/lesspipe.sh%s'LINES=24LOGNAME=hadoop

Access System variables: $HOME, $PWD, $SHELL, $java_home , and more

Custom Variables

1. Grammar

variable = value (e.g. STR=ABC)

Cannot have spaces on either side of the equals sign

Using variables: $arg

Double quotes and single quotes are different: double quotation marks only take spaces in the string, and single quotes will refer to variables in the string such as $param.

2. Example


-bash:world:command not found
[Email protected] ~]$ str="helloWorld"[[email protected]-reall-131 ~]$ a=9[ [Email protected] -reall-131 ~]$ echo $A9[[email protected]-reall-131 ~]$ echo $STRhello World

What if I want to print Hello Worlds is greater?

Echo $STRs is greate OK?

No, the correct wording is:

echo ${str}s is greate

IS was greate[[email protected]'${str}s is greate'      is greate

Pleasehello World
[[email protected] ~]$ echo ' please$str '
Please$str

Unset a undo variable a

ReadOnly b=2 declares a static variable b=2 and cannot unset

Export A #可把变量提升为当前shell进程中的全局环境变量 for use by other child shell programs

VI a.sh

# !/bin/basha="A in a.sh"echo $a/root/scripts/b.sh

VI b.sh

# !/bin/bashb="b in b.sh"echo $becho $a

Then execute./a.sh, and you will find that the a variable defined in the a script is not printed in the B script.

If you want to print out variant A of the a script in B, you need to define the variable A in the a script as an export, at which point the A variable is the global variable of the bash process where the a.sh script is located, and all the child processes of the process can access the variable A.

Another way:

If you call b.sh in the a.sh script as follows

. ./b.sh # # Note: Focus on the front one "." Number

Or

source./b.sh # #

, b.sh runs in the bash process space where a.sh is located, rather than in a subprocess in a process.

Summarize:

1. Calling b.sh directly in a.sh will allow b.sh to execute in the "child process" space of the bash process where a is located

2. Child process space can only access variables defined by export in the parent process

3. A shell process cannot elevate its defined variables to the parent process space

4, "." Script executes the script in the shell process space where the caller resides.

3, anti-quote value

A= ' Ls-la ' # # Counter quote, run the command inside and return the result to the variable a

a=$ (Ls-la) # # equivalent to anti-quote

4. Special variables

$? Indicates the status code of the last command exit

$$ indicates the current process number

$ A indicates the current script name

$n represents the n position input parameter (n for number, n>=1)

$# represents the number of parameters, often used in loops

$* and [email protected] All represent parameter lists

Note: The difference between $* and [email protected]

$* and [email protected] All represent all parameters passed to a function or script

ü Not be enclosed by double quotes ""--

$* and [email protected] are all in the form of a list of parameters in "$ $ ... $n"

ü When they are enclosed by double quotes ""--

"$*" takes all the parameters as a whole, forming an entire string in the form of "$ $ ... $n" ;

"[Email protected]" will separate the various parameters to "$" "$" ... "$n" form a list of parameters

Basic syntax of shell programming for Linux

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.