Linux Bash Shell Learning notes

Source: Internet
Author: User

The basic syntax for BASH
• The simplest example--hello world www.111cn.net!

• about input, output, and error output

The bash of variables in the Chinese language (and the similarities and differences between C languages)

The basic Process Control syntax in bash

• Use of functions

2.1 Simplest example--hello world!
The first example of almost all the programming books that give readers is the Hello world program, so we're starting with this example today to learn more about BASH.

Edit a hello file with the VI editor as follows:

#!/bin/bash
# This is a very simple example
Echo Hello World

So the simplest BASH program was written. Here are a few questions to explain:

One, the first line of #! What is the meaning
Second, what does the/bin/bash of the first line mean?
Three, is the second line a comment?
Iv. the Echo statement
Five, how to execute the program

#! Is the type of the file that describes Hello, which is somewhat similar to the meaning (but not the same) of different file types in Windows system with different file suffixes. The Linux system determines the type of the file based on the "#!" and the information that follows it, and the students can learn more about this by using the "Man Magic" command and the/usr/share/magic file after they go back. The "#!" and "/bin/bash" in the first line of bash indicate that the file is a bash program that needs to be interpreted by the Bash program in the/bin directory. Bash This program is generally stored in the/bin directory, if your Linux system is more special, bash may be stored in/sbin,/usr/local/bin,/usr/bin,/usr/sbin or/usr/local/sbin such a target If you can't find it, you can use the three commands "locate bash" "Find/-name bash 2>/dev/null" or "Whereis bash" to locate the place where bash is located; If you still can't find it, you may need to install a BASH software package.

The second line of Www.111cn.net "# is a ..." Is the comment of the Bash program, from the "#" Sign in the Bash program (note: followed by "!") A number of the beginning to the end of the line is considered to be a program annotation. The function of the three-line Echo statement is to output the string behind the echo into the standard output. Since Echo is followed by the "Hello World" string, the word "Hello World" is displayed on the console terminal screen. Note that most of the statements in BASH have no semicolon at the end.

How do you execute the program? There are two ways: one is to explicitly make BASH to execute:

$ bash Hello or
$ sh Hello (here sh is a link to bash, "lrwxrwxrwx 1 root 4 Aug 05:41/bin/sh-> bash")

Alternatively, you can change the hello file to a file that can be executed, and then run it directly, as the first line of the hello file "#! /bin/bash "function, the system will automatically use the/bin/bash program to explain the execution of the hello file:

$ chmod u+x Hello
$./hello

There is no direct "$ hello" because the current directory is not the default directory where the current user can execute the file, and the current directory "." Setting as the default directory is an unsafe setup.

It should be noted that after the BASH program was executed, the Linux system actually opened a separate process to run.

2.2 about input, output, and error output
In a character terminal environment, the concept of standard input/standard output is well understood. Input refers to the input of an application or command, whether from keyboard input or from another file; the output refers to some information generated by an application or command; Unlike Windows, there is a concept of standard error output under the Linux system. This concept is mainly for program debugging and system maintenance purposes set, error output from the standard output can let some advanced error information does not interfere with the normal output information, so as to facilitate the use of general users.

In Linux systems: standard input (stdin) defaults to keyboard input, standard output (STDOUT) defaults to screen output, and standard error output (STDERR) defaults to the screen (STD-standard above). When these concepts are used in BASH, the standard output is typically represented as 1, and the standard error output is expressed as 2. Here are some examples of how to use them, especially standard output and standard error output.

Input, output, and standard error outputs are primarily used for I/O redirection, which means changing their default settings. First look at this example:

$ ls > Ls_result
$ ls-l >> Ls_result

These two commands redirect the result output of the LS command to the Ls_result file and append to the Ls_result file, instead of the output to the screen. ">" is the output (standard output and standard error output) redirection of the representative symbol, two consecutive ">" symbol, that is, ">>" means not to clear the original and append output. Let's look at a slightly more complicated example:

$ find/home-name lost* 2> Err_result

This command has an extra "2" before the ">" Symbol, and "2>" means redirecting the standard error output. Because some directories under the/home directory cannot be accessed due to permission restrictions, some standard error outputs are stored in the Err_result file. Can you imagine what the find/home-name lost* 2>>err_result command would produce?

If you execute Find/home-name lost* > All_result directly, the result is that only standard output is stored in the All_result file, so what if you want the standard error output to be stored in the same file as the standard input? Look at the following example:

$ find/home-name lost* > All_result 2>& 1

The above example will first redirect the standard error output to the standard output, and then redirect the standard output to the All_result file. So we can store all the output in a file. To achieve the above function, there is also a simple way to read as follows:

$ find/home-name lost* >& All_result


If the error message is not important, the following command allows you to avoid the distractions of many useless error messages:

$ find/home-name lost* 2>/dev/null

Students can also go back to try the following several redirect methods to see what results, why?

$ find/home-name lost* > All_result 1>& 2
$ find/home-name lost* 2> All_result 1>& 2
$ find/home-name lost* 2>& 1 > All_result

Another very useful redirection operator is "-", see the following example:

$ (cd/source/directory && tar CF-.) | (Cd/dest/directory && tar xvfp-)

This command means that all files under the/source/directory directory are compressed and decompressed, and quickly move all the way to the/dest/directory directory, which is in/source/directory and/dest/directory There is a special advantage when you are not in the same file system.


Conditional statement (note: spaces, quotes, equals) on both sides of the condition
If ["$var" = "abc"]; Then

...

elif ["$var" = "AC"]; Then

...

Else

...

Fi

For loop
For Var in $ (LS *.sh); Todo

Echo $var

Done

While loop
Var=1

While ["$var"-le 20]; Todo

var=$ (($var + 1))

Done

Until loops (contrary to the while loop)
Until condition

Todo

...

Done

Case conditions (available regular,;; Equivalent to a break)
Case "$var" in

Yes | YES | Y)

echo "YES"

echo "haha"

;;

[nn]*) echo "NO";;

* echo "other";;

Esac

Definition/Assignment variable
Var=xxx (no spaces on either side of the equals sign)

Variable Read
Echo $var

Reading user input
Read Var

Do not output line wrapping
Echo-n

Execute command and capture return value
$ (command)

Other
The default type in the shell is string type

Posted in programming algorithm already tagged bash, shell. Bookmark the link. Trackbacks are closed, but you can post a comment.

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.