10 Useful 'Interview Questions and Answers' for Linux Shell Scripting

Release: 2023-08-04 16:14:02
forward
1597 people have browsed it

The vastness of Linuxenables people to submit unique content every time.These contents are not only useful for their careers, but also allow them to increase their knowledge.Here, we try to do this. As for how successful we can achieve, it is up to our readers and friends to judge.

Here, as an additional content to the shell script, in this article we will explain the Linux Shell related questions from an interview perspective.
1. How to interrupt the script execution before the shell script is successfully executed?
Answer: We need to use the ‘exit’ command to achieve the scenario described above. When the ‘exit’ command is forced to output a non-zero value, the script will report an error and exit. In a shell script in a Unix environment, a value of 0 indicates successful execution. Therefore, executing an 'exit -1' command without quotes before the script terminates will abort the script.
For example, create the following script named "linuxmi.sh".
#!/bin/bash echo "Hello" exit-1 echo "bye"
Copy after login
Save the file and execute:
10 Useful 'Interview Questions and Answers' for Linux Shell Scripting
From the above script It can be clearly seen that the script executed fine before the exit -1 command.
2. 如何使用Linux命令来移除文件头?
解答:当我们需要删除文件中的指定行时,‘sed’命令可以用来解决该问题。
这个是用来删除文件头(文件的首行)的正确命令。
# sed '1 d' file.txt
Copy after login
上面命令的问题是,它会在标准输出设备上输出不带首行的文件内容。 为了保存输出到文件,我们需要使用重定向操作符,它将帮助你将输出重定向到文件。
# sed '1 d' file.txt > new_file.txt
Copy after login
好吧,其实sed命令内建的‘-i’开关就可以干这活,就不需要重定向符了吧。
# sed -i '1 d' file.txt
Copy after login
3. 你怎么检查一个文本文件中某一行的长度?
解答:‘sed’命令也可以用来查找文本文件中的某一行或者检查其长度。
# sed -n 'n p' file.txt
Copy after login
可以解决, 这里‘n’表示行号,‘p’打印出匹 配内容(到标准输出 ),该命令通常与-n命令行选项连用。 那么,怎样来获取长度计数呢? 很明显,我们需要通过管道输出给‘wc’命令来计算。
# sed –n 'n p' file.txt | wc –c
Copy after login
要得到文本文件‘linuxmi.txt’的第五行的长度,运行如下命令:
# sed -n '5 p' linuxmi.txt | wc -c
Copy after login
10 Useful 'Interview Questions and Answers' for Linux Shell Scripting
4. 可以在Linux系统上查看到所有非打印字符吗?你是怎么做到的?
解答:可以。可以在Linux中查看所有的非打印字符。要实现上面所讲的方案,我们需要‘vi’编辑器的帮助。怎样在‘vi’编辑器中显示非打印字符?
打开vi编辑器。
先按[esc]键,然后按‘:’进入到vi编辑器的命令模式。
最后,从‘vi’编辑器的命令界面输入set list命令并执行。
注: 这种方式可以查看文本文件中的所有非打印字符,包括ctrl+m(^M)。
5. 假如你是一个员工组的团队领导,为xyz公司工作。 公司要求你创建一个‘dir_xyz’目录,让该组成员都能在该目录下创建或访问文件,但是除了文件创建者之外的其他人不能删除文件,你会怎么做?
解答:这真是个有趣的工作方案。好吧,上面所讲的方案,我们需要通过下面的步骤来实施。
# mkdir dir_xyz # chmod g+wx dir_xyz # chmod +t dir_xyz
Copy after login
第一行命令创建了一个目录(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
Copy after login
要截取该文件中的第二,第五和第七列。
# cut -d;-f2 -f5 -f7 txt_linuxmi
Copy after login
8. ‘cmp’和‘diff’命令的区别是什么?
Answer: The ‘cmp’ and ‘diff’ commands are used to obtain the same thing, but each has its own emphasis.
The 'diff' command outputs the modifications that should be made to make the two files identical. The 'cmp' command compares the two files byte by byte and reports the first mismatch.
9. Can the ‘echo’ command be used instead of 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 an independent 'inode' and a 'unique' inode number on Unix systems.

The above is the detailed content of 10 Useful 'Interview Questions and Answers' for Linux Shell Scripting. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:Linux中文社区
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!