当前位置:网站首页>Lesson 3 shell syntax
Lesson 3 shell syntax
2022-07-22 16:21:00 【Zhi Zi】
An introduction to
shell It is the language that we communicate with the operating system through the command line .
shell Scripts can be executed directly on the command line , You can also organize a set of logic into a file , Convenient reuse .
AC Terminal The command line in can be regarded as a “shell The script executes line by line ”.
Linux Common in shell There are many kinds of scripts , Common are :
Bourne Shell(/usr/bin/sh or /bin/sh)
Bourne Again Shell(/bin/bash)
C Shell(/usr/bin/csh)
K Shell(/usr/bin/ksh)
zsh
…
Linux Generally, the system uses... By default bash, So let's talk about bash Chinese grammar .
The beginning of the file needs to be written #! /bin/bash, To specify bash For the script interpreter .
Script example
Create a new one test.sh file , The contents are as follows :
#! /bin/bash
echo "Hello World!"
Pay attention to save after writing
Operation mode
As an executable
[email protected]:~$ chmod +x test.sh # Make the script executable
Be careful +x Must be written together
Defining variables
The general definition is string
Variable type
1, Custom variable ( local variable )
Variables that cannot be accessed by child processes
2, environment variable ( Global variables )
Variables accessible to child processes
Enter a bash, Enter subprocess , The previous process went into sleep ,exit sign out
character string
Strings can be in single quotes , You can also use double quotes , You can also use no quotes .
The difference between single quotation marks and double quotation marks :
- The contents in single quotation marks will be output as is , Not execute 、 Will not take variables ;
- The contents in double quotation marks can execute 、 Variable can be taken ;
Be careful : Use of escape characters , The effect of not adding quotation marks is the same as that of double quotation marks
Extract substring
name="hello, yxc"
echo ${name:0:5} # Extract from 0 At the beginning 5 Characters
Pay attention to subscripts from 0 Start
Other parameters and related variables
Parameters explain
$* A space separated string consisting of all parameters , As in the above example, the median value is "$1 $2 $3 $4"
[email protected] Each parameter is a string enclosed in double quotation marks , As in the above example, the median value is "$1" "$2" "$3" "$4"
Be careful :
When $* and [email protected] Not double quoted
" "
When surrounded , There's no difference between them , Each parameter received is regarded as a piece of data , They are separated by spaces .
But when they're double quoted" "
Inclusion time , There will be a difference :
"$*"
All parameters will be regarded as one piece of data as a whole , Instead of treating each parameter as a piece of data ."[email protected]"
Still treat each parameter as a piece of data , They are independent of each other .
For example, it was delivered 5 Parameters , So for"$*"
Come on , this 5 The parameters will be combined to form a data , They are inseparable ; And for"[email protected]"
Come on , this 5 The parameters are independent of each other , They are 5 Copy of the data .
If you use echo Direct output"$*"
and"[email protected]"
comparing , There is no difference ; But if for Loop to output data one by one , You can see the difference immediately .
Parameters explain
$? Exit status of the last command ( Notice that it's not stdout, It is exit code).0 Indicates normal exit , Other values indicate an error
such as C++/C Medium return 0
Parameters explain
$(command) return command This order is stdout( Can be nested )
`command` return command This order is stdout( Cannot be nested )
give an example :
String expressions
length STRING
return STRING The length of
index STRING CHARSET
CHARSET in arbitrarily Single character in STRING in front Character position of , Subscript from 1 Start . If in STRING It doesn't exist at all CHARSET in The characters of , Then return to 0.
substr STRING POSITION LENGTH
return STRING String from POSITION Start , Maximum length LENGTH The string of . If POSITION or LENGTH It's a negative number ,0 Or non numeric , Returns an empty string .
Example :
str="Hello World!"
echo `expr length "$str"` # `` Not single quotes , Indicates that the command is executed , Output 12
echo `expr index "$str" aWd` # Output 7, Subscript from 1 Start
echo `expr substr "$str" 2 3` # Output ell
Be careful : The reason $str Put it in quotation marks , Because Hello World There's a space in the middle , The default before the space is a string . So enclose it in quotation marks to ensure its integrity , That is, all are one string .
for…in…do…done
Example 3, Output 1-10
for i in $(seq 1 10)
do
echo $i
done
Example 4, Use {1..10} perhaps {a..z}
for i in {a..z}
do
echo $i
done
Be careful :
(1)seq It means traversing a sequence , You can write directly in the terminal
(2){} You can't write directly in the terminal
边栏推荐
猜你喜欢
Command line program test automation
Session共享问题
M using similink simulation module to realize dynamic simulation of multipath channel
DOM browser rendering process and repaint reflow
Dominoes staged: the beginning and end of the three arrow capital crash
SOC key control LED
Docker data management case - MySQL data persistence
【解决】npm ERR! code E401
Trigger Basics (medium)
硬件工程师有没有35岁危机?
随机推荐
Trigger Basics (medium)
这个好用的办公网优化工具,官宣免费了
Use of yarn
博途PLC和ABB变频器PN通讯详解
Summary of product related issues (Part 1)
Data structure in redis (2): jump table
网络相关概念面试题
Matlab simulation of bit error rate performance of RS coding and decoding
分布式调度问题
【解决】npm ERR! code E401
C# ftp检测目录是否存在和创建文件夹
Dominoes staged: the beginning and end of the three arrow capital crash
AlterNET脚本编写,用户界面设计功能
Monopoly of Web3 social protocol and soul binding token
Why does the system we developed have concurrent bugs? What is the root cause of concurrent bugs?
Jd-h5 development
C#上传文件到共享文件夹
图计算-图简介
NFT 多样化的发行方式
自动化测试工程师面试简历参考