16th Shell Script Basics (i)

Source: Internet
Author: User
Tags variable scope

1. What is a shell?
The shell is a command interpreter. is an insulating layer between the operating system kernel and the user. For a Linux system user, the shell is the most basic tool for you to navigate a Linux-like system. All system commands and tools, plus applications, are callable for shell scripts.
The shell plays the role of the operating system kernel and the user's middleman by typing the shell command, then the shell passes the user input and then forwards the request to the operating system's kernel for processing.

1) A system can have more than one shell, you can use the Cat/etc/shells command to view the shell installed in the system, different shells may support the command syntax is not the same.
2) You can view the currently used shell through the echo $SHELL

2. Why use shell programming?
No programming language is perfect. There is not even one of the best languages, only for specific purposes, more appropriate and inappropriate for the programming language. Mastering Shell Scripting Knowledge is fundamental to anyone who wants to properly master some of the system's management knowledge. To play Linux, you need to know that the Shell,shell command is the password that commands Linux.
We often encounter a lot of repetitive operation on the Linux system, and manual operation, low efficiency, error prone, wasting our time. Consider using shell scripts at this time.
It would be nice to let the script free us from the tedious work and leave more time to enjoy life.

Small knowledge:
1) Query the Linux version of the command lsb_release-a
[Email protected]:~$ lsb_release-a
No LSB modules is available.
Distributor Id:ubuntu
Description:ubuntu 14.04 LTS
release:14.04
Codename:trusty

2) Query the BASH version of the command echo $BASH _version
[Email protected]:~$ echo $BASH _version
4.3.8 (1)-release


3. How are shell commands layered?
The shell commands are divided into the following five types:
alias (alias)
function (function)
Shell built in (internal command)
keyword (keywords)
file
Type of shell command can be queried with type
Type LS quote pwd do ID


4. Shell Editor
Vim is recommended as a Linux user, and using the Vim editor is a must.
I use Ubuntu system (other systems may not be the same), configure Vim file VIMRC
sudo vi/etc/vim/vimrc

Detailed configuration is as follows:
Showmode:ensures We see when we is in insert mode
nohlsearch:does Not highlight the words, we have a searched for
Autoindent:we indent our code often; This allows us to return
Indent level rather than the start of a new in each carriage return
tabstop=4:sets a tab to being four spaces
expandtab:converts tabs to spaces, which was useful when the file moves to
Other systems
syntax on:note that this does not use the SET command and are used to turn
On syntax highlighting
When these options is set, the $HOME/.VIMRC file should look similar to this:
Set Showmode Nohlsearch
Set Autoindent tabstop=4
Set Expandtab
Syntax on

5. Create a Hello World Shell program.
1) VI $HOME/bin/hello1.sh, add the following statement
#!/bin/bash
echo "Hello world."
Exit 0
2) chmod +x $HOME/bin/hello1.sh, add executable permissions
3) hello1.sh, execution procedure

6. Checking The exit status
The shell has a special variable that can query the return value of the previous shell command
hello1.sh
echo $?
We'll see output 0.

7. Introduction to Shell variables
As we all know, programming will use variables, here must say the shell variables, according to the purpose of three types of use:
1) Environment variables: bash,pwd,shell,user, etc. (can be queried with the set command)
2) System Preset variables:
Bash defines 9 positional variables, namely $, $, $ $, $4...$9. The script writer references these 9 variables in the script to get the arguments passed to the script.
$ A: Saves the name of the current process or script.
$* or [email protected]: Saves all parameters passed to the script or process.
$$: PID of the current process or script.
$!: The PID number of the last process running in the background.
$?: Used to return whether the previous command executed successfully. If executed successfully, the number 0 is returned, otherwise a non-0 number is returned (the number 1 is usually returned).
$#: The number of arguments used to save the script.
3) user-defined variables
"Naming rules for custom variables"
Because a variable has no specific data type, you can define the variable without having to define its type and assign it directly to the value.
In the shell, variable names can consist of uppercase, lowercase, underline, and number symbols.
When defining variables, it is recommended that you use uppercase letters, underscores, and numbers to make variable names to avoid unnecessary misunderstanding.
The variable is case sensitive.

"Scope of variables"
The scope of a variable refers to the range of references to a variable, or it can be understood as the area in which the variable functions. The variable scope rules in Bash can be summed up in the following two points:
Unless the export command is used, the scope of the variables defined in the script (including variables defined in the function) is the entire script.
The variables defined with export are global variables. Global variables can be referenced in any script, command of the current user.
As can be seen from the above two points, if the user needs to define global variables in the script, the Export command must be used.

16th Shell Script Basics (i)

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.