10 useful "Interview Questions and Answers" for Linux Shell scripts"

Source: Internet
Author: User
Tags printable characters

Linux is vast, so that people can always submit different content each time. Our "The-Tecmint-Team" job is to provide some unique content for our readers, which is not only useful for their career, but also increases their knowledge. Here, we will try to do this. As for how much success we can achieve, we will judge it by our readers and friends.

We have provided many tutorials on Shell scripting language and interview questions for various users. You can visit the following links to read these articles.

  • Shell script Series
  • Interview Questions and Answers Series

Here, as an additional content of the shell script, we will explain the issues related to the Linux Shell from the interview perspective.

1. How can I interrupt script execution before the shell script is successfully executed?

A: We need to use the 'exit 'command to implement the situation described above. When the 'exit 'command is forced to output a value other than 0, the script reports an error and exits. In a shell script in a Unix environment, the value 0 indicates successful execution. Therefore, executing the 'exit-1' command without quotation marks before the script ends will abort the script.

For example, create a script named "anything. sh.

 
 
  1. #!/bin/bash 
  2. echo "Hello" 
  3. exit -1 
  4. echo "bye" 

Save the file and execute it.

 
 
  1. # sh anything.sh 
  2. Hello 
  3. exit.sh: 3: exit: Illegal number: -1 

From the above script, we can clearly see that the script runs well before the exit-1 command.

2. How to remove a file header using Linux commands?

A: When we need to delete the specified row in the file, the 'sed 'command can be used to solve this problem.

This is the correct command used to delete the first line of the file header file.

 
 
  1. # sed '1 d' file.txt 

The problem with the above command is that it will output the file content without the first line on the standard output device. To save the output to a file, we need to use the redirection operator, which will help you redirect the output to the file.

 
 
  1. # sed '1 d' file.txt > new_file.txt 

Well, in fact, the sed Command's built-in '-I' switch can do this, so there is no need for a redirection character.

 
 
  1. # sed -i '1 d' file.txt 

3. How do you check the length of a row in a text file?

A: 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. Here 'n' indicates the row number, and 'P' prints the Matching content to the standard output ), this command is usually used with the-n command line option. So how can we get the length count? Obviously, we need to output it to the 'wc 'command through a pipeline for calculation.

 
 
  1. # sed –n 'n p' file.txt | wc –c 

To get the length of the fifth line of the Upload File 'tecmint.txt ', run the following command:

 
 
  1. # sed -n '5 p' tecmint.txt | wc -c 

4. Can I view all non-printable characters in Linux? How did you do it?

A: Yes. You can view all non-printable characters in Linux. To implement the above solution, we need the help of the 'vi' editor. How does one display non-printable characters in the 'vi' editor?

  • Open the vi editor.
  • Press the [esc] key and then press ':' to enter the command mode of the vi editor.
  • Finally, enter the set list command in the command interface of the 'vi' editor and execute it.

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

5. Assume that you are a team leader in an employee group and work for xyz. The company requires you to create a 'dir _ xyz' directory so that all members of this group can create or access files under this directory. However, no one except the file creator can delete the files, what do you do?

A: This is an interesting solution. Well, the above solution needs to be implemented through the following steps. This is just a piece of cake.

  
  
  1. # mkdir dir_xyz 
  2. # chmod g+wx dir_xyz 
  3. # chmod +t dir_xyz 

The first line of command creates a directory dir_xyz). The second line of command above makes group g) have 'write' and 'execute 'permissions, in the last command line above, the '+ t' at the end of the permission bit is 'stick', which is used to replace 'x', indicating that in this directory, files can only be deleted by their owner, directory owner, or Super User root.

6. Can you tell me the stages of a Linux Process?

A: A Linux process usually goes through four major stages in its life.

Here is the four phases of the Linux Process.

  • Wait: the Linux Process waits for resources.
  • Running: the Linux Process is currently in progress.
  • Stop: the Linux Process stops after it is successfully executed or after receiving the killing process signal.
  • BotNet: if the process has ended but remains in the progress table, it is called a 'botnet '.

7. How to Use the cut command in Linux?

A: 'Cut 'is a very useful Linux Command. When we want to extract the specified part of the file and print it to the standard output, when the text area and the file itself are large, this command is useful.

For example, extract the first 10 columns of the 'txt _ tecmint' file.

 
 
  1. # cut -c1-10 txt_tecmint 

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

 
 
  1. # cut -d;-f2 -f5 -f7 txt_tecmint 

8. What is the difference between the 'cmp 'and the 'diff' command?

A: The 'cmp 'and 'diff' commands are used to obtain the same things, but each of them focuses on the same things.

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

9. Can I use the 'echo 'command to replace the 'LS' command?

A: 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 preceding command, we can use 'echo * '. The outputs of the two commands are identical.

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

A: 'inode' is a 'data struct' that is used for file identification on Linux. Each file has an independent 'inode' and a unique 'inode number on a Unix system.

So far. In the next article, we will discuss other interesting and knowledgeable interview questions. At that time, don't run away. Please go to Tecmint.com. Don't forget to give us some valuable feedback in the comment section below.

Via: http://www.tecmint.com/interview-questions-on-shell-scripting/

Translator: GOLinux Proofreader: wxy

Address: http://linux.cn/article-3101-1.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.