Table of Contents
目录
引言
shell中的数组
数组的定义
数组的使用
实际的例子
shell中的大小比较
shell中的括号
shell中函数的定义
杂项知识点
字符串转数组
常用判断标志
linux后台运行相关
参考文献
Home Backend Development PHP Tutorial shell知识点总结

shell知识点总结

Jun 13, 2016 pm 12:23 PM
echo email protected quot runtime

shell知识点小结

目录
  • 引言
  • shell中的数组

    • 数组的定义
    • 数组的使用
    • 实际的例子
  • shell中大小的比较
  • shell中的括号
  • shell中函数的定义
  • 杂项知识点

    • 字符串转数组
    • 常用判断标志
    • linux后台运行相关
  • 参考文献

引言

SHELL在处理一些问题的时候有得天独厚的优势,快捷方便,学会了还可以显摆显摆,当然了,shell的语法有点坑爹,没有系统的学过,只能一点一点的积累。

今天这个是在实现一个刷新数据库数据的脚本的时候碰到的一些知识点,刷新的时候用到了正则匹配、数学运算、比较等等。


shell中的数组

数组的定义
<code>arr=(1 2 3 4 5)arr=(燕睿涛 yrt lulu yanruitao)arr=('^[0-9]+$' '^yrt\.(\d+)\.log$')arr=(	"燕睿涛" \    "yanruitao" \    "today is a good day!")</code>
Copy after login

数组的使用
<code>len=${#arr[@]}	#返回的是数组元素的个数echo ${arr[0]}	#数组中的第一个元素,这个和其他语言的数组类似,下表从0开始echo ${arr[2]}	#数组中的第3个元素</code>
Copy after login

实际的例子
<code>[[email protected]_runtime sh]$ arr=(> "燕睿涛"> "http:\/\/www\.baidu\.com\/(\d+)\.html"> "yanruitao"> "lulu"> "yrt"> )[[email protected]_runtime sh]$ echo ${#arr[@]}5[[email protected]_runtime sh]$ echo ${arr[1]}http:\/\/www\.baidu\.com\/(\d+)\.html[[email protected]_runtime sh]$ echo ${arr[0]}燕睿涛[[email protected]_runtime sh]$ echo ${arr[5]}[[email protected]_runtime sh]$</code>
Copy after login

shell中的大小比较

<code>#第一种(())if((6 8)); then echo "yes 燕睿涛"; fiif(($a 'ab' ]]; then echo "iforever 燕睿涛"; fi	#iforever 燕睿涛if [[ 2 </code>
Copy after login

可以看到上面这几种还是有些规律的:

  • 双小括号[(())]里面是可以直接使用大于小于号进行比较(>、=),而且不需要“坑爹”的空格,用于数学计算
  • 单中括号([])里面比较必须使用-gt、-lt、-ne、-eq这些运算符,而且必须要有严格的空格要求
  • 双中括号([[]])里面比较可以使用>、、

shell中的括号

<code>#看看小括号的用法,首先是在for循环里面,相当于还是数学计算[[email protected]_runtime ad]$ for((a=0;a do> echo $a> done0123456789#对变量进行++,还是相当于数序运算[[email protected]_runtime ad]$ i=1[[email protected]_runtime ad]$ echo $i1[[email protected]_runtime ad]$ let i++[[email protected]_runtime ad]$ echo $i2[[email protected]_runtime ad]$ ((i++))[[email protected]_runtime ad]$ echo $i3#数学运算[[email protected]_runtime ad]$ echo 1+21+2[[email protected]_runtime ad]$ echo $((1+2))3#单括号里面是一个命令组,括号中的命令将会新开一个shell顺序执行,所以这个里面相当于一个封闭的空间,里面的变量什么的不能被剩余代码使用[[email protected]_runtime ad]$ a=1[[email protected]_runtime ad]$ (a=3;echo $a)3[[email protected]_runtime ad]$ echo $a1#括号中and的使用if [[ -n "$ret" && $ret -gt 123 ]]...		#[[]]双中括号中只能使用&&,不能使用-aif [ -n "$ret" -a $ret -gt 123 ]...			#[]单中括号中只能使用-a,不能使用&&if(($ret)) && (($ret >123 ))...				#(())双小括号使用&&	</code>
Copy after login

shell中函数的定义

<code>function getId(){	local url=$1	#local限定了变量url的作用域只在函数里面,不然会污染全局的作用域    ereg="http:\/\/www\.baidu\.com\/\([0-9]\+\)\.html"    local ret=$(expr $url : $ereg)    if [[ -n "$ret" && $ret -gt 0 ]]; then	#当ret为null时使用[]会报错,-n这里的双引号一定要加上,不然当$ret为null时,一直返回真    	echo $ret        return 0    fi    return 1}[[email protected]_runtime sh]$ echo $?0[[email protected]_runtime sh]$ getId "http://www.baidu.com/123.htl"[[email protected]_runtime sh]$ echo $?1[[email protected]_runtime sh]$ getId "http://www.baidu.com/123.html"123[[email protected]_runtime sh]$ echo $?0    </code>
Copy after login

函数的整体形式如上面的例子,这里面注意两点:

  • 首先就是返回值,通过return的返回值只能是整数,并且在调用完成之后使用echo $?可以查看返回值。
  • 要使用赋值的形式需要有echo,就像ret=$(getId "http://www.baidu.com.1234.html"),只有echo的值会传递给ret变量。

杂项知识点

字符串转数组
<code>[[email protected]_runtime sh]$ str="燕睿涛 lulu yrt yanruitao"[[email protected]_runtime sh]$ arr=($str)			#这一步将字符串转化为了数组[[email protected]_runtime sh]$ echo ${arr[*]}燕睿涛 lulu yrt yanruitao[[email protected]_runtime sh]$ echo ${#arr[@]}4</code>
Copy after login

常用判断标志
<code>[ -z STRING ]  “STRING” 的长度为零则为真。  [ -n STRING ] or [ STRING ]  “STRING” 的长度为非零 non-zero则为真。[ -d FILE ]  如果 FILE 存在且是一个目录则为真。[ -a FILE ]  如果 FILE 存在则为真。</code>
Copy after login

linux后台运行相关
<code>& 	#在一个命令的最后加上这个命令,可以将该命令放到后台执行./update.sh 100 500 &ctrl + z		#讲一个正在前台执行的命令放到后台,并且处于暂停状态jobs		#查看当前后台运行的命令jobs -l		#可以显示所有后台任务的PID[[email protected]_runtime sh]$ jobs -l[1]   9681 Running                 ./t.sh 100 300 &[2]   9683 Running                 ./t.sh 100 300 &[3]-  9685 Running                 ./t.sh 100 300 &[4]+  9688 Running                 ./t.sh 100 300 &fg 		#把后台中的命令调至前台继续运行,如果后台有多个命令可以使用`fg %jobnumber`将选中命令调出[[email protected]_runtime sh]$ jobs -l[2]  10033 Running                 ./t.sh 100 300 &[3]  10035 Running                 ./t.sh 100 300 &[4]- 10037 Running                 ./t.sh 100 300 &[5]+ 10039 Running                 ./t.sh 100 300 &[[email protected]_runtime sh]$ fg %2./t.sh 100 300    bg 		#讲一个在后台暂停的命令变成在后台继续执行。同样,如果有多个命令,可以使用bg %jobnumber[[email protected]_runtime sh]$ jobs -l[1]- 11655 Running                 ./t.sh 100 300 &[2]+ 11662 Running                 ./t.sh 100 300 &[[email protected]_runtime sh]$ fg %1./t.sh 100 300^Z[1]+  Stopped                 ./t.sh 100 300[[email protected]_runtime sh]$ jobs -l[1]+ 11655 Stopped                 ./t.sh 100 300[2]- 11662 Running                 ./t.sh 100 300 &[[email protected]_runtime sh]$ bg %1[1]+ ./t.sh 100 300 &[[email protected]_runtime sh]$ jobs -l[1]- 11655 Running                 ./t.sh 100 300 &[2]+ 11662 Running                 ./t.sh 100 300 &kill	#终止进程kill %num	#通过jobs查看的job号,进行杀死kill pid 	#通过进程号杀掉进程ctrl + C 	#终止当前前台的进程</code>
Copy after login

参考文献

  • Bash Shell 里的各种括号
  • shell中各种括号的作用()、(())、[]、[[]]、{}
  • linux shell 数组建立及使用技巧
  • shell脚本----if(数字条件,字符串条件,字符串为空)
  • Shell for&while 循环详细总结

微信号: love_skills

越努力,越幸运!越幸运,越努力!

做上CEO不是梦

赢取白富美不是梦

屌丝逆袭不是梦

就是现在!!加油
shell知识点总结

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
How to use email, smtplib, poplib, imaplib modules to send and receive emails in Python How to use email, smtplib, poplib, imaplib modules to send and receive emails in Python May 16, 2023 pm 11:44 PM

The journey of an email is: MUA: MailUserAgent - Mail User Agent. (i.e. email software similar to Outlook) MTA: MailTransferAgent - Mail transfer agent, which is those email service providers, such as NetEase, Sina, etc. MDA: MailDeliveryAgent - Mail delivery agent. A server of the Email service provider sender->MUA->MTA->MTA->if

7 Ways to Fix API-Ms-Win-Crt-Runtime DLL Missing Error 7 Ways to Fix API-Ms-Win-Crt-Runtime DLL Missing Error Apr 16, 2023 pm 01:52 PM

Missing DLL error is not a very rare Windows problem. For example, many users have reported api-ms-win-crt-runtime-l1-1-0.dllismissing errors while trying to launch specific software in Windows 11/10. The error displays the following message: The program can't start because api-ms-win-crt-runtime-l1-1-0.dll is missing from your computer. Try reinstalling the program to resolve this issue. Try reinstalling the program to resolve this issue. This error can occur in a variety of gaming, design, and image editing software. Due to this issue, users are unable to open and use programs. missing api-m

How to solve 'undefined: runtime.GOMAXPROCS' error in golang? How to solve 'undefined: runtime.GOMAXPROCS' error in golang? Jun 25, 2023 pm 07:31 PM

When developing with golang, many developers will encounter some errors, among which "undefined: runtime.GOMAXPROCS" is a common error. This error usually occurs when using multi-threaded programming in the Go language. The most common situation is when executing the following code: import "runtime" funcmain(){runtime.GOMAXPR

Five selected Go language open source projects to take you to explore the technology world Five selected Go language open source projects to take you to explore the technology world Jan 30, 2024 am 09:08 AM

In today's era of rapid technological development, programming languages ​​are springing up like mushrooms after a rain. One of the languages ​​that has attracted much attention is the Go language, which is loved by many developers for its simplicity, efficiency, concurrency safety and other features. The Go language is known for its strong ecosystem with many excellent open source projects. This article will introduce five selected Go language open source projects and lead readers to explore the world of Go language open source projects. KubernetesKubernetes is an open source container orchestration engine for automated

Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Jun 13, 2023 pm 05:01 PM

Laravel is a popular PHP framework that is highly scalable and efficient. It provides many powerful tools and libraries that allow developers to quickly build high-quality web applications. Among them, LaravelEcho and Pusher are two very important tools through which WebSockets communication can be easily implemented. This article will detail how to use these two tools in Laravel applications. What are WebSockets? WebSockets

Detailed explanation of the role and usage of the echo keyword in PHP Detailed explanation of the role and usage of the echo keyword in PHP Jun 28, 2023 pm 08:12 PM

Detailed explanation of the role and usage of the echo keyword in PHP PHP is a widely used server-side scripting language, which is widely used in web development. The echo keyword is a method used to output content in PHP. This article will introduce in detail the function and use of the echo keyword. Function: The main function of the echo keyword is to output content to the browser. In web development, we need to dynamically present data to the front-end page. At this time, we can use the echo keyword to output the data to the page. e

What are the most popular golang frameworks on the market? What are the most popular golang frameworks on the market? Jun 01, 2024 pm 08:05 PM

The most popular Go frameworks at present are: Gin: lightweight, high-performance web framework, simple and easy to use. Echo: A fast, highly customizable web framework that provides high-performance routing and middleware. GorillaMux: A fast and flexible multiplexer that provides advanced routing configuration options. Fiber: A performance-optimized, high-performance web framework that handles high concurrent requests. Martini: A modular web framework with object-oriented design that provides a rich feature set.

Go language development essentials: 5 popular framework recommendations Go language development essentials: 5 popular framework recommendations Mar 24, 2024 pm 01:15 PM

&quot;Go Language Development Essentials: 5 Popular Framework Recommendations&quot; As a fast and efficient programming language, Go language is favored by more and more developers. In order to improve development efficiency and optimize code structure, many developers choose to use frameworks to quickly build applications. In the world of Go language, there are many excellent frameworks to choose from. This article will introduce 5 popular Go language frameworks and provide specific code examples to help readers better understand and use these frameworks. 1.GinGin is a lightweight web framework with fast

See all articles