Shell Programming Quick Start _shell

Source: Internet
Author: User
Tags chmod php programming posix
What is a shell script.

Shell scripting (English: Shell script) is a computer program and text file that consists of a series of shell commands that operate after the Unix shell literal translation of its contents. Designed as a scripting language, it works in the same way as the literal language, where the Unix shell plays the role of a command-line interpreter, running the shell command in sequence after reading the shell script, and then outputting the result. Shell script can be used for system management, file operation and so on.

In Unix and all Unix-like systems, such as Linux, FreeBSD and other operating systems, there are shell Script. Depending on the various types of Unix shells, Shell script also has a variety of dialects. Batch files in DOS, OS/2, and Microsoft windows have similar functionality to shell script.

Take a look at an example

#!/bin/sh
CD ~
mkdir shell_tut
cd shell_tut for

(i=0; i<10; i++));
does Touch test_$i.txt Done
Instance resolution: Line 1th: Specify the script interpreter, this is the 2nd line of the/BIN/SH interpreter: Switch to the current user's home directory, line 3rd: Create a directory shell_tut 4th line: Switch to Shell_tut directory 5th line: Cycle conditions, total circulation 10 times Line 6th: Create a test_1 ... 10.txt file line 7th: End of loop body

CD, mkdir, Touch are the system's own program, generally in the/bin or/usr/bin directory. For, does, done is the keyword of the SH scripting language. Environment

Shell programming is like Java and PHP programming, as long as there is a text editor that can write code and a script interpreter that interprets execution.

Shell programming is supported by the current mainstream operating system, and shell programming described in this document refers to the shell under Linux, which is essentially a POSIX-standard feature, so it also applies to UNIX and BSD (such as Mac OS). Linux

The Linux default installation takes the Shell interpreter. Mac OS

Mac OS not only brings the most basic interpreter of SH, bash, but also built-in Ksh, CSH, zsh, and other common interpreter. Emulator on Windows

Windows does not have a built-in shell interpreter at the factory and needs to be installed on its own, in order to simultaneously use grep, awk, curl and other tools, it is best to install a Cygwin or MinGW to simulate the Linux environment. Cygwin MinGW script interpreter sh

The Bourne Shell,posix (Portable operating System Interface) standard shell interpreter, its binary file path is usually/bin/sh, developed by Bell Labs. Bash

Bash is a substitute for the Bourne Shell, which is GNU Project, and the binary path is usually/bin/bash. The industry typically mixes bash, sh, and Shell, as you would often see in the copy of the hiring engineer: familiar with Linux bash programming, and proficient in shell programming.

In CentOS,/bin/sh is a symbolic link to/bin/bash:

[Root@centosraw ~]# ls-l/bin/*sh
-rwxr-xr-x. 1 root 903272 Feb-05:09/bin/bash-rwxr-xr-x
. 1 root root 1 06216 Oct  2012/bin/dash
lrwxrwxrwx. 1 root      4 Mar 10:22/bin/sh-> Bash

But not on Mac OS,/bin/sh and/bin/bash are two different files, although their size is only about 100 bytes:

imac:~ wuxiao$ ls-l/bin/*sh
-r-xr-xr-x  1 root  wheel  1371648  6 Nov
16:52/bin/bash-rwxr-xr-x  2 root  wheel   772992  6 Nov 16:52/bin/csh
-r-xr-xr-x  1 root  wheel  2180736  6 Nov 16:52/bin/ksh
-r-xr-xr-x  1 root  wheel  1371712  6 Nov
16:52/bin/sh-rwxr-xr-x  2 root  wheel   772992  6 Nov 16:52/bin/tcsh
-rwxr-xr-x  1 root  wheel  1103984  6 Nov 16:52/bin/zsh
Advanced programming languages

In theory, as long as a language provides an interpreter (not just a compiler), the language is capable of scripting, and common interpretive languages can be used as scripting, such as Perl, TCL, Python, PHP, and Ruby. Perl is the oldest scripting language, and Python has become a preset interpreter for Linux distributions over the years.

A compiled language, as long as there is an interpreter, can also be used as scripting, such as the C shell is built in (/BIN/CSH), and Java has a Third-party interpreter Jshell,ada a billing interpreter adascript.

The following is a sample PHP Shell script (assuming file name test.php):

#!/usr/bin/php
<?php for
($i =0; $i < $i + +) {
    echo $i. "\ n";
}

Perform:

/usr/bin/php test.php

Or:

chmod +x test.php
./test.php
How to choose Shell programming language familiarity vs unfamiliar

If you have mastered a programming language such as PHP, Python, Java, JavaScript), it is recommended that you write a script directly in this language, although some places may be a bit verbose, but you can take advantage of the experience in this language field (unit test, Single-step debugging, IDE, Third party class library).

The new learning costs are small, as long as you learn how to use the Shell interpreter (Jshell, adascript). Simple vs Advanced

If you feel familiar with the language (such as Java, C) to write a shell script is too verbose, you just want to do some backup files, install software, download data and other things, learn to use Sh,bash would be a good idea.

The shell only defines a very simple programming language, so if your script is more complex, or the data structure you want to manipulate is complex, you should still use scripting languages like Python or Perl, or a high-level language that you already excel at. Because SH and bash are weak in this area, for example: its function can only return strings, cannot return array it does not support object-oriented, you can not implement some elegant design pattern it is interpreted, while the interpretation side of execution, even PHP kind of precompilation is not, if your script contains errors ( For example, calling a nonexistent function), as long as the line is not executed, the environment compatibility is not error

If your script is made available to other users, using SH or bash, your script will have the best environmental compatibility, Perl is the standard for Linux early on, and Python has become the standard for some Linux distributions these years, as for Mac OS, it defaults to installing Perl, Python, Ruby, PHP, Java and other mainstream programming languages. The first shell script is written

Open a text editor, create a new file, the extension is sh (sh represents shell), the extension does not affect the script execution, see the name is good, if you use PHP to write the shell script, the extension will use PHP.

Enter some code, the first line is usually this:

#!/bin/bash
#!/usr/bin/php

"#!" is a convention tag that tells the system what interpreter the script needs to execute. Run

There are two ways to run a shell script:

There are two ways to run a shell script: as an executable program

chmod +x test.sh
./test.sh

Note that you must write./test.sh, rather than test.sh, run other binary programs as well, directly written test.sh,linux system will go to path to find there is no call test.sh, and only/bin,/sbin,/USR/BIN,/USR /sbin wait in path, your current directory is usually not in path, so write test.sh will not find the command, to use./test.sh told the system that it was looking in the current directory.

By running the bash script this way, the first line must be written correctly so that the system can find the correct interpreter.

The "system" here is actually the shell application (imagine Windows Explorer), but I intentionally wrote the system to make it easy to understand that since the system refers to the shell, does a script using/bin/sh as an interpreter omit the first line? Yes. As an interpreter parameter

This is run by running the interpreter directly, whose argument is the file name of the shell script, such as:

/bin/sh test.sh
/bin/php test.php

The script that runs this way does not need to specify the interpreter information in the first line, and it is useless to write it.


From:http://www.runoob.com/w3cnote/shell-quick-start.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.