Shell script Learning (1)

Source: Internet
Author: User

Common shell script interpreters in Unix/Linux include bash, sh, CSH, and KSh. Bash is the most common interpreter in Linux.

Script Language isInterpreted languageWhen executing such programs, the interpreter (Interpreter) needs to read the source code written by us, convert it into the target code (object code), and then run it on the computer.

The common C, C ++, and JavaCompiled language. This type of language needs to convert the source code we have written into the object code in advance. This process is called "Compilation ".

#!/bin/bashecho "Hello World !"

"#!" Is an agreed tag, which tells the system what interpreter is required to execute this script.

For Perl scripts

#!/usr/bin/perl -w

For scripts with these tags, you can directly execute them by modifying permissions, such:

chmod +x ./test.sh./test.sh

Chmod is a command used to modify permissions in Linux. + X indicates that executable attributes are added to the following file.

Then run the script using./test. Sh.

Why do I have to use./test. Sh to run the script, but test. Sh cannot be used directly?

Because path is not followed by the environment variable by default./, If you execute test. Sh directly, it cannot be found.

[[email protected] addPort]# echo $PATH/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

 

Of course, if you do not add the mark of this convention, you only need to execute the bash script in this way.

bash test.sh

Use the READ command to read the data of the terminal stdin.

read inputStringecho "input String is $inputString"

The above two inputstrings are added with $ and none. Why?

In my understanding, when this variable is treated as the left value, you do not need to add $. When this variable is changed to the right value, you must add $

Shell declares a variable

testString1="hello"testString2=‘world‘testInt=123

During initialization, note that no space is allowed on both sides of the = sign. Otherwise, it is incorrect. Shell has strict format requirements.

You need to add the $ symbol when using the variable, because it is used as a right value.

echo $testString1echo $testString2echo "${testInt}456"

Of course, you need to add curly brackets when necessary to separate the strings and use them as the interpreter to limit the boundary.

Readonly: Before the variable, add readonly to limit it to a read-only variable.

Unset: You can use unset to delete a variable. If you delete a variable, no data is output to this variable.

There are two types of variables in shell:Local variableAndEnvironment Variable.

Local variables can only be used in the current shell script, while environment variables can be used in shell scripts created and derived sub-processes.

There are some special variables in shell.

Variable Description
$0 File Name of the current script
$ N Parameters passed to the script or function. N is a number that represents the number of parameters. For example, the first parameter is $1, and the second parameter is $2.
$ # The number of parameters passed to the script or function.
$ * All parameters passed to the script or function.
[Email protected] All parameters passed to the script or function. When enclosed by double quotes (""), it is slightly different from $.
$? The exit status of the previous command or the return value of the function.
$ The ID of the Current Shell Process. Shell scripts are the process IDs of these scripts.

$ N is similar to the char * argv [] in the C main function. They are all passed parameters. $ # Corresponds to int argc. For example, there are two paths, you need to run the LS command to view the directory information, which can be as follows:

#!/bin/bashmyPath1=$1myPath2=$2ls $1ls $2echo $#

Then execute the script

[[email protected] /]# chmod +x 123.sh [[email protected] /]# ./123.sh /mnt/ /usr/cdrom  hgfsbin  etc  games  include  lib  lib64  libexec  local  sbin  share  src  tmp2

The first line prints the list after the first parameter ls, the second line prints the list after the second parameter ls, and the third line prints the number of parameters.

When $ * and [email protected] are directly used, their parameters will be used as one by one, and when both are "" included, "$ *" combines parameters into a whole string for output, while the parameters of "[email protected]" are either one by one.

Shell script Learning (1)

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.