Shell Scripting Basics Learning

Source: Internet
Author: User
Tags arithmetic

See other people's study summary, feel good turn over (transfer from tryfly)

First, Shell Script Foundation

The shell script is a program written using the shell's function, which uses a plain text file, writes some shell syntax and instructions inside, and then uses regular expressions, pipe commands, and redirect functions to achieve what we want to do. Its basic uses are:

    1.自动化常用命令    2.执行系统管理和故障排除    3.创建简单的应用程序    4.处理文本或文件    ...
Second, create shell script

The first step, using a text editor to create a text file

第一行必须包括shell 声明序列:#! 也就是我们通常所说的“蛇棒”,如:`#!/bin/bash` 添加注释,注释以#开头,注释行会被解释器忽略。

Step two, run the script

给予执行权限,在命令行上指定脚本的绝对或相对路径直接运行解释器,将脚本作为解释器程序的参数运行

Script Example:

#!/bin/bash# ------------------------------------------# Filename: hello.sh# Revision: 1.0# Date: 2017/09/16# Author: fly# Email: # Description: This is the first script# ------------------------------------------echo “hello world”
Third, script debugging
1.检测脚本的语法错误bash -n /path/to/some_script只检测,不执行脚本,并且不能检测脚本的逻辑错误
2.调试执行bash -x /path/to/some_script逐行执行脚本,便于直接判断脚本错误,但可能会有些不安全
Iv. Custom variables of common variables

Depending on the effective scope of the variable, the custom variable is divided into:

本地变量: 仅对当前SHELL有效,对子SHELL也无效环境变量:对当前SHELL及其子SHELL均有效。局部变量:生效范围为当前shell、进程中某代码片断( 通常指函数)

Before using a custom variable, we need to assign a value to the variable, and here is the variable naming rule:

1)使用变量无需事先声明2)首个字符必须为字母(a-z,A-Z)3)中间不能有空格,可以使用下划线(_)4)不能使用标点符号5)不能使用bash里的关键字(可用help命令查看保留关键字)6)需要给变量赋值时,可以这么写:7)变量名=值8)取一个变量的值,只需在变量名前面加一个$ ( 注意: 给变量赋值的时候,不能在"="两边留空格 )

Cases:

A=1 (本地变量)export B=2(环境变量)local C=3(局部变量)

After using the variable, you can unset name delete the custom variable

Positional variables

Positional variables: Calling parameters passed to the script through the command line in script code

$1, $2, ... :对应第1 、第2 等参数,shift [n] 换位置$0: 命令本身$*: 传递给脚本的所有参数,全部参数合为一个字符串[email protected]: 传递给脚本的所有参数,每个参数为独立字符串$#: 传递给脚本的参数的个数[email protected] $*只在被双引号包起来的时候才会有差异

set --all positional variables can be emptied

Special variables
variables meaning
$? Variable saves the most recent command exit status
$ File name of the current script
$# The number of arguments passed to the script or function
$*/[email protected] All parameters passed to the script or function. When enclosed by double quotation marks (""),[email protected] is slightly different from $*
$$ The current shell process ID, which is the process ID for the shell script, which is where these scripts are located

Take a look at the following script:

#!/bin/bashecho "File Name: $0"echo "First Parameter : $1"echo "First Parameter : $2"echo "Quoted Values: [email protected]"echo "Total Number of Parameters : $#"

Operation Result:

./test.sh a b  File Name : test.shFirst Parameter : aSecond Parameter : bQuoted Values: a bTotal Number of Parameters : 2
Arithmetic Operations let Operations command
#!/bin/basha=2;b=3;let sum=a+becho $sum
[] Method of operation
#!/bin/basha=2;b=3;sum=$[a+b]echo $sum
(()) Method of operation
#!/bin/basha=2;b=3;sum=$((a+b))echo $sum
Expr Arithmetic method
a=1b=2sum=$(expr $a + $b)echo $sum
Precision Computing
echo "1*2*3"|bc 

Today, the shell script is introduced here, later on the Advanced shell script to organize, goodbye.

Shell Scripting Basics Learning (GO)

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.