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

The vastness of Linux makes it possible to submit something different every time. This content is not only useful for their careers, but also allows them to increase their knowledge. Here we try to do just that, and let our readers and friends judge how successful we are.

Here, as an addition to shell scripting, in this article we will interpret Linux Shell related questions from an interview perspective.

1. Before the shell script is successfully executed, how to interrupt the script execution?

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 shell scripts under Unix environments, a value of 0 indicates successful execution. Therefore, executing an unquoted exit -1 command before the script terminates will cause the script to abort.

#!/bin/bashecho "Hello"exit -1echo "bye"

Save the file and execute.

# sh linuxmi.shHellolinuxmi.sh:行3: exit-1: 未找到命令bye

As can be clearly seen from the above script, the script executes fine until the exit -1 command.

2. How to use Linux command to remove file header?

Answer: When we need to delete a specified line 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 built-in -i switch of 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: The sed command can also be used to find a line in a text file or check its length.

sed -n 'n p' file.txt can be solved, where n represents the line number, and p prints out the matching content (to standard output). This command is usually used in conjunction with the -n command line option. So, how to get the length count? Obviously, we need to pipe the output to the wc command to calculate.

# 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 '5p' linuxmi.txt | toilet -c

4. Can I see all non-printing characters on a Linux system? how did you do it?

Answer: Yes. All non-printing characters can be viewed in Linux. To realize the scheme mentioned above, we need the help of vi editor. How to display non-printing characters in vi editor?

Open vi editor.

First press the [esc] key, then press: to enter the command mode of the vi editor.

Finally, enter the set list command from the command interface of the vi editor and execute it.

Note: This way you can view all non-printing characters in the text file, including ctrl+m (^M).

5. Suppose you are a team leader of an employee group, working for xyz company. The company requires you to create a dir_xyz directory, so that all members of this group can create or access files in this directory, but other people except the file creator cannot delete files, what will you do?

Answer: This is a really interesting working solution. Well, the solution mentioned above, we need to implement the following steps, which is simply a piece of cake.

# mkdir dir_xyz# chmod g+wx dir_xyz# chmod +t dir_xyz

The first line of command creates a directory (dir_xyz), the second line of command above gives the group (g) permission to 'write' and 'execute', and the last line of command above - the last '+t' of the permission bit Is the 'sticky bit', which is used to replace the 'x', indicating that in this directory, files can only be deleted by their owner, the owner of the directory, or the superuser root.

6. Can you tell me the stages a Linux process goes through?

Answer: A Linux process usually goes through four main stages during its lifetime.

Here are the four stages that a Linux process goes through.

  • Waiting: Linux processes are waiting for resources.

  • Running: The Linux process is currently executing.

  • Stop: A Linux process stops after successful execution or after receiving a signal to kill the process.

  • Zombie: If the process has ended, but still remains in the process table, it is called a 'zombie'.

7. How to use the cut command in Linux?

Answer: cut is a very useful Linux command. When we want to intercept a specified part of a file and print it to the standard output, this command is very useful when the text area and the file itself are large.

For example, intercept the first 10 columns of the txt_linuxmi file.

# cut -c1-10 txt_linuxmi

To intercept the second, fifth and seventh columns in the file.

# cut -d;-f2 -f5 -f7 txt_linuxmi

8. What is the difference between cmp and diff commands?

Answer: The cmp and diff commands are used to obtain the same thing, but each has its own focus.

The diff command outputs the changes that should be made to make two files identical. The 'cmp' command compares two files byte by byte and reports the first mismatch.

9. Can echo command be used instead of ls command?

Answer: Yes. The 'ls' command can be replaced by the 'echo' command. The 'ls' command lists the contents of the directory. From the perspective of replacing the above command, we can use 'echo *', and the output of the two commands is exactly the same.

10. You may have heard of inodes. Can you briefly describe inodes?

Answer: An 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.

Guess you like

Origin blog.csdn.net/qq_27817851/article/details/128195577