


Classic Thirteen Questions About Shell Scripts
1. Why is it called Shell?
We know that the operation of a computer is inseparable from hardware, but it cannot directly operate the hardware. The driver of the hardware can only be controlled by a software called "OS (Operating System)" .Strictly speaking, Linux is an operating system (OS).
Users have no way to directly operate the Kernel, but communicate with the Kernel through the Kernel's "shell" program, which is the so-called Shell. Shell is a The interactive interface (Interface) between the user and the system can only use the system to complete work through the command line (Command line). Therefore, the simplest definition of Shell is: Command Interpreter (Command Interpreter)
Translate the user's commands to the kernel for processing;
At the same time, translate the kernel processing results to the user.
Different OSs use different Kernels; on the same kernel, you can also use different Shells. Common Shells include sh, bash, csh, ksh, etc.
2. What is the relationship between Shell prompt (PS1) and Carriage Return (CR)?
After successfully logging in to a Shell terminal, the part to the left of the cursor is called the prompt. Usually, ordinary users use $
, and administrator users use ##.
##Shell Prompt: You can enter the command. After typing the command, wait until the CR (Carriage Return) character is read
Carriage Return: The command can be executed
From the technical details, the Shell will convert the Command line according to IFS (Internal Field Seperator) The entered text is broken down into "fields" (word/field). Then the special characters (meta) are processed first, and finally the entire command line is reorganized.
3. Others echo, and you also echo. How much do you know about echo?
echo sends argument to standard output (stdout), which is usually displayed on the screen.stdin standard input
stdout standard output
stderr standard error output
echo -n # 取消换行符 echo -e # 启用反斜杠转译
4. What is the difference between double quotes "" and single quotes ''?
hard quote:
''
(single quote), close all quotessoft quote:
""
(double quotation marks), keep the $ reference
5, var=value? What is the difference between export and before?
Variable definition:
name=value
, separators cannot be used on the left and right sides of the equal sign.Variable substitution:
echo ${name}
export变量:
export name=value
,使变量成为环境变量
# 本地变量 A=B # 取消变量 unset A # 环境变量export A=B
6、exec 跟 source 差在哪?
环境变量只能从父进程到子进程单向传递。换句话说:在子进程中环境如何变更,均不会影响父进程的环境。
当我们执行一个shell script时,其实是先产生一个sub-shell的子进程, 然后sub-shell再去产生命令行的子进程。关注Linux中文社区
# 创建子shell执行脚本 ./1.sh # 当前shell执行 source 1.sh # 当前shell执行后退出 exec 1.sh
7、( ) 与 { } 差在哪?
( )
将 command group 置于 sub-shell 执行{ }
则是在同一个shell内完成
8、$(()) 与 $() 还有 ${} 差在哪?
# 假设我们定义了一个变量为: file=/dir1/dir2/dir3/my.file.txt # 我们可以用 ${ } 分别替换获得不同的值: # 1. shell字符串的非贪婪(最小匹配)左删除 ${file#*/} # 拿掉第一条 / 及其左边的字符串:dir1/dir2/dir3/my.file.txt # 2. shell字符串的贪婪(最大匹配)左删除 ${file##*/} # 拿掉最后一条 / 及其左边的字符串:my.file.txt ${file##*.} # 拿掉最后一个 . 及其左边的字符串:txt # 3. shell字符串的非贪婪(最小匹配)右删除: ${file%/*} # 拿掉最后条 / 及其右边的字符串:/dir1/dir2/dir3 ${file%.*} # 拿掉最后一个 . 及其右边的字符串:/dir1/dir2/dir3/my.file # 4. shell字符串的贪婪(最大匹配)右删除: ${file%%/*} # 拿掉第一条 / 及其右边的字符串:(空值) ${file%%.*} # 拿掉第一个 . 及其右边的字符串:/dir1/dir2/dir3/my 记忆的方法为: # 是去掉左边(在键盘上 # 在 $ 之左边) % 是去掉右边(在键盘上 % 在 $ 之右边) 单一符号是最小匹配﹔两个符号是最大匹配。
# 5. shell字符串取子串: ${file:0:5}:提取最左边的 5 个字节:/dir1 ${file:5:5}:提取第 5 个字节右边的连续 5 个字节:/dir2 # 6. shell字符串变量值的替换: ${file/dir/path}:将第一个 dir 提换为 path:/path1/dir2/dir3/my.file.txt ${file//dir/path}:将全部 dir 提换为 path:/path1/path2/path3/my.file.txt # 7. ${}还可针对变量的不同状态(没设定、空值、非空值)进行赋值: ${file-my.file.txt} :假如 $file 没有设定,则使用 my.file.txt 作传回值。(空值及非空值时不作处理) ${file:-my.file.txt} :假如 $file 没有设定或为空值,则使用 my.file.txt 作传回值。(非空值时不作处理) ${file+my.file.txt} :假如 $file 设为空值或非空值,均使用 my.file.txt 作传回值。(没设定时不作处理) ${file:+my.file.txt} :若 $file 为非空值,则使用 my.file.txt 作传回值。(没设定及空值时不作处理) ${file=my.file.txt} :若 $file 没设定,则使用 my.file.txt 作传回值,同时将 $file 赋值为 my.file.txt 。(空值及非空值时不作处理) ${file:=my.file.txt} :若 $file 没设定或为空值,则使用 my.file.txt 作传回值,同时将 $file 赋值为 my.file.txt 。(非空值时不作处理) ${file?my.file.txt} :若 $file 没设定,则将 my.file.txt 输出至 STDERR。(空值及非空值时不作处理) ${file:?my.file.txt} :若 $file 没设定或为空值,则将 my.file.txt 输出至 STDERR。(非空值时不作处理) tips: 以上的理解在于, 你一定要分清楚 unset 与 null 及 non-null 这三种赋值状态. 一般而言, : 与 null 有关, 若不带 : 的话, null 不受影响, 若带 : 则连 null 也受影响.
# 8. 计算shell字符串变量的长度:${#var} ${#var} 可计算出变量值的长度: ${#file} 可得到 27 ,因为 /dir1/dir2/dir3/my.file.txt 刚好是 27 个字节... # 9. bash数组(array)的处理方法 数组: A=(a b c d) 引用数组: ${A[@]} ${A[*]} 访问数组成员 ${A[0]} 计算数组长度 ${#A[@]} ${#A[*]} 数组重新赋值 A[2]=xyz # 10.$(( ))是用来做整数运算的 a=5;b=7;c=2; echo $(( a + b * c))
9、$@ 与 $* 区别在哪?
"$@"
则可得到 “p1” “p2 p3” “p4” 这三个不同的词段"$*"
You can get the entire string of single segments "p1 p2 p3 p4"In addition, Search the public account Linux and this is how you should learn to reply "git books" in the background and get a surprise gift package.
##10. What is the difference between && and ||?
1. The test command has two formstest expression
[ expression ]
string: string
integer:integer
file:file
3、当 expression 为真是返回 0(true) ,否则返回 非0(false)
command1 && command2 command2 只有在 command1 的RV为0(True)的条件下执行。
command1 || command2 command2只有在command1的RV为非0(False)的条件下执行。
4、先替换变量再比较
A=123[ -n "$A" ] && ([ "$A" -lt 100 ] || echo "too big")unset A
11、> 与 < 差在哪?
0: Standard Input(STDIN)
1: Standard Output (STDOUT)
2: Standard Error Output(STDERR)
我们可用 < 来改变读进的数据信道(stdin),使之从指定的档案读进。
我们可用 > 来改变送出的数据信道(stdout, stderr),使之输出到指定的档案。
ls my.file no.such.file 1> file.out 2>file.err # 2>&1 就是将stderr并进stdout做输出 ls my.file no.such.file 1> file.out 2>&1 # /dev/null 空 ls my.file no.such.file >/dev/null 2>&1 cat < file > file # 在 IO Redirection 中,stdout 与 stderr 的管道会先准备好,才会从 stdin 读进资料。 # 也就是说,在上例中,> file 会先将 file 清空,然后才读进 < file , # 但这时候档案已经被清空了,因此就变成读不进任何数据了
12、你要if还是case呢?
# if echo -n "Do you want to continue?(Yes/No):" read YN if [ "$YN"=Y -o "$YN"=y -o "$YN"="Yes" -o "$YN"="yes" -o "$YN"="YES"];then echo "continue" else exit 0 fi # case echo -n "Do you want to continue?(Yes/No):" read YN case "$YN" in [Yy]|[Yy][Ee][Ss]) echo "continue" ;; *) exit 0 esac
13、for what? while与until差在哪?
# for for ((i=1;i<=10;i++)) do echo "num is $i" done # while num=1 while [ "$num" -le 10 ]; do echo "num is $num" num=$(($num + 1)) done # until num=1 until [ "$num" -gt 10 ]; do echo "num is $num" num=$(($nu + 1)) done
break 是结束 loop
return 是结束 function
exit 是结束 script/shell
The above is the detailed content of Classic Thirteen Questions About Shell Scripts. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to execute .sh file in Linux system? In Linux systems, a .sh file is a file called a Shell script, which is used to execute a series of commands. Executing .sh files is a very common operation. This article will introduce how to execute .sh files in Linux systems and provide specific code examples. Method 1: Use an absolute path to execute a .sh file. To execute a .sh file in a Linux system, you can use an absolute path to specify the location of the file. The following are the specific steps: Open the terminal

An esd file is a compression format used in Windows operating systems, while an ISO file is a disc image file used to create a disc copy or virtual optical drive. When we need to convert esd files to iso files, it may be because ISO files are more commonly used and easier to use. The following will introduce you to some common methods to complete this conversion process. Method 1: Use ESDDecrypter ESDDecrypter is a program specially used to decrypt and convert esd files to iso files.

When processing files under Linux systems, it is sometimes necessary to delete lines at the end of the file. This operation is very common in practical applications and can be achieved through some simple commands. This article will introduce the steps to quickly delete the line at the end of the file in Linux system, and provide specific code examples. Step 1: Check the last line of the file. Before performing the deletion operation, you first need to confirm which line is the last line of the file. You can use the tail command to view the last line of the file. The specific command is as follows: tail-n1filena

Linux is an open source, portable, and customizable operating system that is widely used in various fields, such as servers, desktops, embedded devices, etc. The core of Linux is the kernel, which is responsible for managing hardware resources and providing basic services. However, the kernel is not an independent entity and requires a file system to store and access various data and programs. A file system is a method of organizing and managing files. It defines the file's name, location, attributes, permissions and other information. In Linux, there are many different types of file systems, such as ext4, xfs, btrfs, etc., each of which has its own characteristics and advantages. However, among all file systems, there is a special file system, which is the foundation and core of the Linux system, which is

Why can't win7 run bat files? Recently, many users using the Windows7 operating system have reported that they cannot run .bat files. This sparked widespread discussion and confusion. Why can't a well-functioning operating system run a simple .bat file? First, we need to understand the background of the .bat file. A .bat file, also known as a batch file, is a plain text file that contains a series of commands that can be used by the Windows command interpreter (cmd.ex

We've designed this Windows PowerShell scripting tutorial for beginners, whether you're a tech enthusiast or a professional looking to improve your scripting skills. If you have no prior knowledge of PowerShell scripting, this article will start with the basics and be tailored for you. We'll help you master the installation steps for a PowerShell environment and walk you through the main concepts and features of PowerShell scripts. If you're ready to learn more about PowerShell scripting, let's embark on this exciting learning journey together! What is WindowsPowerShell? PowerShell is a hybrid command system developed by Microsoft

If you are an IT administrator or technology expert, you must be aware of the importance of automation. Especially for Windows users, Microsoft PowerShell is one of the best automation tools. Microsoft offers a variety of tools for your automation needs, without the need to install third-party applications. This guide will detail how to leverage PowerShell to automate tasks. What is a PowerShell script? If you have experience using PowerShell, you may have used commands to configure your operating system. A script is a collection of these commands in a .ps1 file. .ps1 files contain scripts executed by PowerShell, such as basic Get-Help

Methods for using URL files to open Internet resources include: double-clicking to open using a web browser. Open it with a text editor, copy the link address and paste it into the browser address bar. Through the command line, use the "start" or "open" command to specify the URL file path. Create a script file that contains the command to open the URL file.
