当前位置:网站首页>Getting started with shell
Getting started with shell
2022-04-22 14:19:00 【Lantern dream in southern China】
Preface
Shell It's a use. C A program written in a language , It is used by users Linux The bridge .Shell It's a command language , Another programming language .
generally speaking , We will be in Liuux The system uses shell Access the system and carry out some interactive operation of data .
This blog , According to his own experience at ordinary times , It summarizes some commonly used Shell Instructions . Writing is not good , I would appreciate your comments. .
common shell Instructions
first shell The procedure to be carried out
The first program to execute Compared with the output you guessed hello,world 了 . ad locum , In order to ensure the integrity of the entry Foundation , Here I will briefly describe how to use shell Output one helloworld.
- vi Create a .sh The file of ( for example ,vi hello.sh)
echo “Hello, World. I come from China” - Give the script executable permissions
chmod +x ./hello.sh - Execute the script ( Here is ./hello.sh)
Show “Hello, world. I come from China”
echo The command is used to output text... To the window
Using variables : Use a defined variable , Just prefix the variable name with $ Symbol , You can also put curly braces on variables {}, Curly braces outside variable names are optional , Add it or not , Curly braces are used to help the interpreter identify the boundaries of variables . Curly brackets are recommended for all variables , It's a good programming habit .
your_name="qinjx"
echo $your_name
echo ${your_name}
for skill in Ada Coffe Action Java; do
echo "I am good at ${skill}Script"
done
If not skill Variable with curly bracket , It's written in echo “I am good at $skillScript”, The interpreter will $skillScript As a variable ( Its value is empty. ), The result of code execution is not what we expect .
Use unset Command to delete variables
Variable cannot be used again after being deleted ,unset Command cannot delete read-only variables
#!/bin/sh
myUrl="http://www.baidu.com"
unset myUrl
echo $myUrl
In the include file
A read-only variable : Use readonly Commands can define variables as read-only variables , The value of a read-only variable cannot be changed .
#!/bin/bash
myUrl="http://www.google.com"
readonly myUrl
myUrl="http://www.baidu.com"
Run script , The result is : /bin/sh: NAME: This variable is read only.
Variable type
Variable type : function shell when , There will be three variables at the same time
• 1) local variable Local variables are defined in scripts or commands , At present only shell Valid in instance , other shell Started program cannot access local variables .
• 2) environment variable All procedures , Include shell Program started , Can access environment variables , Some programs need environment variables to ensure their normal operation . When necessary shell Scripts can also define environment variables .
• 3) shell Variable shell Variables are derived from shell Special variables for program settings .shell Some of the variables are environment variables , Some of them are local variables , These variables guarantee shell Normal operation of
Shell How to use strings in
Get string length
string="abcd"
echo ${#string} # Output 4
Extract substring
The following example starts from the string No 2 Characters begin to intercept 4 Characters :
string="runoob is a great site"
echo ${string:1:4} # Output unoo
Shell Array
bash Supports one dimensional array ( Multidimensional arrays are not supported ), And there's no limit to the size of the array .
Be similar to C Language , The subscripts of array elements are 0 Numbered starting . To get the elements in an array, you need to use subscripts , Subscripts can be integers or arithmetic expressions , Its value should be greater than or equal to 0.
Define an array
stay Shell in , Use parentheses to represent arrays , For array elements " Space " Symbol split . The general form of defining an array is :
Array name =( value 1 value 2 … value n)
for example :
array_name=(value0 value1 value2 value3)
perhaps
array_name=(
value0
value1
value2
value3
)
You can also define the components of an array separately :
array_name[0]=value0
array_name[1]=value1
array_name[n]=valuen
Can not use consecutive subscripts , And there is no limit to the scope of subscripts .
Be careful : Use @ or * You can get all the elements in the array
Read array
The general format for reading array element values is :
${ Array name [ Subscript ]}
for example :
valuen=${array_name[n]}
Use @ The symbol can get all the elements in the array , for example :
echo ${array_name[@]}
Gets the length of the array
The way to get the array length is the same as the way to get the string length , for example :
Get the number of array elements
length=KaTeX parse error: Expected '}', got '#' at position 2: {#̲array_name[@]} …{#array_name[*]}
Get the length of a single element of an array
lengthn=${#array_name[n]}
Shell Basic operators
Shell Basic operators
Shell Like any programming language , Support for multiple operators , Include :
Arithmetic operator
Relational operator
Boolean operator
String operators
File test operators
Native bash Simple mathematical operations are not supported , But you can do it with other commands , for example awk and expr,expr The most commonly used .
expr Is an expression calculation tool , It can be used to evaluate expressions .
for example : Add two numbers : sum=`expr 2 + 2` #sum=4
Two points attention :
• Space between expression and operator , for example 2+2 It is not right , Must be written as 2 + 2, This is different from most programming languages we are familiar with .
• The complete expression is to be ’ ’ contain , Note that this character is not a common single quotation mark , stay Esc Key bottom .
Show variable
read Command to read a line from standard input , And assign the value of each field of the input line to shell Variable
#!/bin/sh
read name
echo “$name It is a test”
The above code is saved as test.sh,name Receive standard input variables , The result will be :
[root@www ~]# sh test.sh
OK # The standard input
OK It is a test # Output
Reference material :
1.Shell Novice tutorial
版权声明
本文为[Lantern dream in southern China]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221414011228.html
边栏推荐
- Greedy dynamic rules 𞓜 Jumping Games 45, 55
- 字节跳动的面试分享,为了拿下这个offer鬼知道我经历了什么
- TopK
- [summary of Kunpeng migration and practice Posts] the first play~~
- Apache IoTDB’s UDF源碼分析(1)
- 获取数据库中数值时,数据库有值,却为空??
- 10天完成民猫电商毕设——用户模块实现(2nd day)
- 双指针快慢指针||快乐数、寻找重复数202、287、|141、142、143、234、457
- 2D转换(移动:translate,旋转:rotate,缩放:scale,2D转换综合写法)
- Fastdfs installation and configuration
猜你喜欢

leetcode:215. 数组中的第K个最大元素

深入理解读写锁ReentrantReadWriteLock和并发容器CopyOnWriteArrayList

嵌入式软件bug从哪来,怎么去

2022 welder (elementary) examination questions and answers

Linked list ring linked list linked list ring judgment to find the ring in nodes 141 and 142

北斗gps卫星时间同步装置(卫星时钟)在广电系统的应用

Binary Tree递归||二叉树展开为链表 530. 二叉搜索树的最小绝对差

Activity preview | on April 23, a number of wonderful openmldb sharing came, which lived up to the good time of the weekend!

获取数据库中数值时,数据库有值,却为空??

How to get tuphub Today's hot list and heat?
随机推荐
Shiro's cache management
Deep transfer learning
Ibeacon development summary of Internet of things solution based on tailing micro tlsr825x
Binarytree practice binary tree sequence traversal 𞓜 sequence traversal with queue
数据库基础(二)
When getting the value in the database, the database has a value, but it is empty??
Hashtable hash table and index 1, 599, 219
每日一题——三国游戏(对角矩阵)
Crater encountered in calling bash script by makefile
字符串的反转练习 344、541、557、151
Tools applicable to any database - Shanghai daoning brings a powerful general database management tool dbeaver to developers and database administrators
How to get tuphub Today's hot list and heat?
Exercises and answers for the examination of main principals of hazardous chemical business units in 2022
天梯赛--L2-003 月饼 (25 分)
深入剖析Lock与AQS
Linked list ring linked list linked list ring judgment to find the ring in nodes 141 and 142
TensorFlow-ValueError: ‘images‘ contains no shape
Development environment of expert system
贪心 动规||跳跃游戏45、55
Operation of 2022 chemical automation control instrument examination practice question simulation examination platform