


10 Interesting Linux Shell Scripts 'Interviews and Answers', Can Experienced Drivers 'Overturn'?
The vastness of Linux allows people to always present unique content every time. This content is not only beneficial for their career development but also allows them to increase their knowledge. Here, we try to do this, and let our readers judge how effective it can be.
Here, as a supplement to shell scripts, in this article we will answer questions related to Linux Shell from an interview perspective.
1. How to interrupt script execution before the shell script is successfully executed?
Answer: We need to use the exit
command to achieve the scenario described above. exit
When the command is forced to output a non-zero value, the script will report an error and exit. In a Unix shell script, a value of 0 indicates successful execution. Therefore, executing an unquoted exit -1
command before the end of the script will terminate the script.
#!/bin/bash echo "Hello" exit -1 echo "bye"
Save the file and execute.
#!/bin/bash echo "Hello" exit -1 echo "bye"
It can be clearly seen from the above script that the script executes well before the exit -1
command.
2. How to use Linux commands to remove file headers?
Answer: When we need to delete specified lines in a file, the sed
command can be used to solve the problem.
This is the correct command to delete the file header (the first line of the file).
# sed '1 d' file.txt
Well, in fact, the -i
switch built into the sed command can do this job, so there is no need for a redirection character.
# sed -i '1 d' file.txt
3. How do you check the length of a line in a text file?
Answer: sed
The command can also be used to find a certain line in a text file or check its length.
sed -n 'n p' file.txt
can be solved, where n
represents the line number, p
prints out the matching content (to standard output) , this command is usually used with the -n
command line option. So, how to get the length count? Obviously, we need to pipe the output to the wc
command for calculation.
# sed –n 'n p' file.txt | wc –c
To get the length of the fifth line of the text file ‘linuxmi.txt’, run the following command:
# sed -n '5 p' linuxmi.txt | wc -c

4. Can all non-printing characters be viewed on a Linux system? how did you do it?
Answer: Yes. All non-printing characters can be viewed in Linux. To implement the solution mentioned above, we need the help of the vi
editor. How to display non-printing characters in the vi
editor?
Open the vi editor.
First press the [esc] key, and then press :
Enter the command mode of the vi editor.
最后,从 vi 编辑器的命令界面输入set list
命令并执行。
“
注: 这种方式可以查看文本文件中的所有非打印字符,包括 ctrl+m(^M)。
”
5. 假如你是一个员工组的团队领导,为xyz公司工作。公司要求你创建一个**dir_xyz
目录,让该组成员都能在该目录下创建或访问文件,但是除了文件创建者之外的其他人不能删除文件,你会怎么做?**
解答:这真是个有趣的工作方案。好吧,上面所讲的方案,我们需要通过下面的步骤来实施,这简直就是小菜一碟。
# mkdir dir_xyz # chmod g+wx dir_xyz # chmod +t dir_xyz
第一行命令创建了一个目录(dir_xyz),上面的第二行命令让组(g)具有‘写’和‘执行’的权限,而上面的最后一行命令——权限位最后的‘+t’是‘粘滞位’,它用来替换‘x’,表明在这个目录中,文件只能被它们的拥有者、目录的拥有者或者是超级用户root删除。
6. 你能告诉我一个Linux进程经历的各个阶段吗?
解答:一个 Linux 进程在它的一生中,通常经历了四个主要阶段。
这里是Linux进程要经历的四个阶段。
- 等待:Linux进程等待资源。
- 运行:Linux进程当前正在执行中。
- 停止:Linux进程在成功执行后或收到杀死进程信号后停止。
- 僵尸:如果该进程已经结束,但仍然留在进程表中,被称为‘僵尸’。
7. Linux中**cut
命令怎么用?**
解答:cut
是一个很有用的 Linux 命令,当我们要截取文件的指定部分并打印到标准输出,当文本区域以及文件本身很大时,这个命令很有用。
例如,截取txt_linuxmi
文件的前10列。
# cut -c1-10 txt_linuxmi
要截取该文件中的第二,第五和第七列。
# cut -d;-f2 -f5 -f7 txt_linuxmi
8. cmp
和diff
命令的区别是什么?
解答:cmp
和diff
命令用来获取相同的东西,但各有侧重。diff
命令输出为了使两个文件一样而应该做的修改。而‘cmp’命令则将两个文件逐字节对比,并报告第一个不匹配的项。
9. Can the **echo
command be used to replace the ls
command? **
Answer: Yes. The 'ls' command can be replaced with the 'echo' command. The ‘ls’ command lists the directory contents. From the perspective of replacing the above command, we can use ‘echo *’. The output of the two commands is exactly the same.
10. You may have heard of inode. Can you briefly describe the inode?
Answer: inode is a data structure used for file identification on Linux. Each file has a separate inode and a unique inode number on Unix systems.
The above is the detailed content of 10 Interesting Linux Shell Scripts 'Interviews and Answers', Can Experienced Drivers 'Overturn'?. 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

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

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

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

Causes and solutions for the VS Code terminal commands not available: The necessary tools are not installed (Windows: WSL; macOS: Xcode command line tools) Path configuration is wrong (add executable files to PATH environment variables) Permission issues (run VS Code as administrator) Firewall or proxy restrictions (check settings, unrestrictions) Terminal settings are incorrect (enable use of external terminals) VS Code installation is corrupt (reinstall or update) Terminal configuration is incompatible (try different terminal types or commands) Specific environment variables are missing (set necessary environment variables)
