Shell experience A

Source: Internet
Author: User

What is a shell, what can you do?
1. Procedures
Narrow: Operating system programs and applications.
The operating system program is used to invoke the monitoring kernel, which runs all the programs we can apply on this operating system.
The operation of the computer is such a connection (or support)
Hardware = = "Kernel = =" operating system = = "Shell = =" program
Kernel = = "operating system = =" Shell (I think they are all included in the operating system) * * * *
(Here we just look at the operating system from the point of view of program and function.) Everyone says that the Linux operating system is a large collection is a comprehensive concept. Just like
China is porcelain. also China).
Simplified under: hardware = = "operating system = =" Program
The shell is a command interpreter. Between the Linux operating system (kernel) and the application. Interpret and run the application through the methods provided by the operating system.
The bash shell is the CentOS default shell. Contains simple syntax and environment. Look at the shell type (echo $SHELL) on this machine to see the shell version (
Bash--version).
Shell Script's Open environment
Any editor is available. (class Windows Notepad) under CentOS VI or VIM, simple operation can also echo "" >filename
Shell's file name:
In fact, Linux is all files. Unlike window, the prefix name is different. Here we give the. sh suffix is a conventional way. Convenient
Everyone's identification and management. Therefore, it is not a problem to add. Sh.
As an example:
$ cat-n test_hello.sh
1 #!/bin/bash
2 #author: Swallow
3 echo ' Hello World '

Syntax format for Shell:
1 #! These 2 characters are the start sign that the shell interpreter starts reading the shell. /bin/bash is the time to create a user. Defines the type of shell. Related to the shell's operating environment.
2 # is the comment content of the shell header is usually a date. Author. If it is in the shell script the large section of # is to facilitate the understanding of the syntax and written comments.
3 echo ' Hello World ' These are the main body of the script. are some basic commands or logical structures such as if and for.
It is easy to understand that the shell body is a collection of commands that can implement the functionality we need.
Execution of the shell:
The Linux file permission system consists of R W x.
If you have X permission, you can do it directly: with the file name
A file created by a general user cannot be executed without an X permission.
You can give permission when practicing: chmod +x test_hello.sh
or through bash or sh test_hello.sh (bash/sh can write full path name/bin/bash/bin/sh)
Here sh is a soft link to bash (similar to win's shortcut)
$ ll/bin/sh
lrwxrwxrwx. 1 root root 4 Jan 15:01/bin/sh bash
(People who know Linux should be at a glance)
$ bash <test_hello.sh (this is also possible)
If the script can be piped | Bash execution
Another way is:. Or the source nature is the same (read into the script and execute)
For example, the difference between a source and SH;
$ cat-n/home/swallow/test_source.sh
1 #!/bin/bash
2 #
3 cd/tmp
4 echo "The script is OK"
5 pwd
[Email protected] ~]$ sh test_source.sh
The script is ok
/tmp
[Email protected] ~]$ source test_source.sh
The script is ok
/tmp
[[email protected] tmp]$ ************ switch to the/tmp directory with source execution program)
SH does not go into the/tmp, and the source enters the/tmp directory.
Because: the shell will open a sub-process when the SH command executes. Execution of the shell executes within the child process. In our
The execution of the shell is actually still in the parent process. Without cutting to the sub-process below.
The source command executes without opening a new child process directly inside the parent process. Generally, if the script contains other
Shell script. Recommended, execute with source.
To view the execution results of the shell:
echo $?
Typically 0 is normal execution. Because Linux originates from UNIX. Unix inside the file after the release process of the token 0 is a success.
View the specific environment of the current shell by: The env command to view the global variable (System level) set command outputs all variables. Including
Global variables and local variables. The declare command outputs all variables, functions, integers, and variables that have been exported.
General ways to modify variables:
Add a sentence at the end of the/etc/profile file;
Export Path=/softwaredirectory/bin: $PATH
Generally speaking, the bin directory of the path to the new installation software is placed at the end of the/etc/profile file. and redefine path. In order not to affect the original
PATH. General assignment: Path=directory: $PATH (new directory and reference original directory)
Comparison of several references: "" "' $ '
# var=123
# echo "$var"
123
# echo ' $var '
$var
# echo $var
123# echo ' Date '
Sat Jan 16:29:15 CST 2017
# echo $ (date)
Sat Jan 16:32:15 CST 2017
General single quotes: Compare directly inside what is, directly output. Double quotes are encountered such as \ $ will escape
·· The effect of double anti-quotes is similar to $ (): typically used for a reference to a command.
Tip: Generate a sequence {a. Z} {1..100} seq 30
One example is: $: Current script name $$; script execution pid $#: Number of parameters passed in
$ vim test.sh
#!/bin/bash
#
Echo-e "script name is: $0\n"
ECHO-E "pid is: $$ \ n"
Echo-e "Sum_file is: $# \ n"
$ sh test.sh test1 test2 test3
Script Name is:test.sh

PID is:26918

Sum_file Is:3

This article is from the "Shong Linux Tour" blog, make sure to keep this source http://12042068.blog.51cto.com/12032068/1893578

Shell experience A

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.