From 08fc012c001ca1d3ecf807e444e884cbd63c1d46 Mon Sep 17 00:00:00 2001 From: GOLinux Date: Tue, 27 May 2014 13:35:48 +0800 Subject: [PATCH] Translated:10 Useful 'Interview Questions and Answers' on Linux Shell Scripting.md --- ...ns and Answers'on Linux Shell Scripting.md | 150 ------------------ ...s and Answers' on Linux Shell Scripting.md | 129 +++++++++++++++ 2 files changed, 129 insertions(+), 150 deletions(-) delete mode 100644 sources/talk/10 Useful'Interview Questions and Answers'on Linux Shell Scripting.md create mode 100644 translated/talk/10 Useful 'Interview Questions and Answers' on Linux Shell Scripting.md diff --git a/sources/talk/10 Useful'Interview Questions and Answers'on Linux Shell Scripting.md b/sources/talk/10 Useful'Interview Questions and Answers'on Linux Shell Scripting.md deleted file mode 100644 index 1cd290f20e..0000000000 --- a/sources/talk/10 Useful'Interview Questions and Answers'on Linux Shell Scripting.md +++ /dev/null @@ -1,150 +0,0 @@ -10 Useful ‘Interview Questions and Answers’ on Linux Shell Scripting -================================================================================ -Greeting of the day. The vastness of Linux makes it possible to come up with a unique post every time. We ‘**The-Tecmint-Team**‘ works to provide our readers with unique contents which is useful for them from career perspective as well as adding to the Knowledge base. Here is an attempt and it is on our readers to judge how far we succeed. - -![Questions on Shell Scripting](http://www.tecmint.com/wp-content/uploads/2014/05/Questions-on-Shell-Scripting.png) - -We have lots of tutorials on **Shell Scripting** language and **Interview Questions** for readers of all kind, here are the links to those articles. - -- [Shell Scripting Series][1] -- [Interview Question and Answer Series][2] - -Adding to the shell scripting posts here, in this article we will be going through questions related to Linux Shell from interview point of view. - -#### 1. How will you abort a shell script before it is successfully executed? #### - -> **Answer** : We need to use ‘exit’ command to fulfil the above described situation. A ‘exit’ command when forced to output any value other than 0 (zero), the script will throw an error and will abort. The value 0 (zero) under Unix environment shell scripting represents successful execution. Hence putting ‘exit -1’, without quotes before script termination will abort the script. - -For example, create a following shell script as ‘**anything.sh**‘. - - #!/bin/bash - echo "Hello" - exit -1 - echo "bye" - -Save the file and execute it. - - # sh anything.sh - - Hello - exit.sh: 3: exit: Illegal number: -1 - -From the above script, it is clear that the execution went well before exit -1 command. - -#### 2. How to remove the headers from a file using command in Linux? #### - -> **Answer** : A ‘sed’ command comes to rescue here, when we need to delete certain lines of a file. - -Here it the exact command to remove headers from a file (or first line of a file). - - # sed '1 d' file.txt - -The only problem with above command is that, it outputs the file on standard output without the first line. In order to save the output to file, we need to use redirect operator which will redirects the output to a file. - - # sed '1 d' file.txt > new_file.txt - -Well the built in switch ‘**-i**‘ for **sed** command, can perform this operation without a redirect operator. - - # sed -i '1 d' file.txt - -#### 3. How will you check the length of a line from a text file? #### - -> **Answer** : Again ‘sed’ command is used to find or check the length of a line from a text file. - -A ‘**sed –n ‘n p’ file.txt**‘, where ‘**n**‘ represents the line number and ‘**p**‘ print out the pattern space (to the standard output). This command is usually only used in conjunction with the **-n** command-line option. So, how to get the length count? Obviously! we need to pipeline the output with ‘**wc**‘ command. - - # sed –n 'n p' file.txt | wc –c - -To get the length of line number ’5′ in the text file ‘**tecmint.txt**‘, we need to run. - - # sed -n '5 p' tecmint.txt | wc -c - -#### 4. Is it possible to view all the non-printable characters from a text file on Linux System? How will you achieve this? #### - -> **Answer** : Yes! it is very much possible to view all the non-printable characters in Linux. In order to achieve the above said scenario, we need to take the help of editor ‘vi’. - -How to show non-printable characters in ‘**vi**‘ editor? - -- Open vi editor. -- Go to command mode of vi editor by pressing [esc] followed by ‘:’. -- The final step is to type execute [set list] command, from command interface of ‘vi’ editor. - -**Note**: This way we can see all the non-printable characters from a text file including **ctrl+m (^M)**. - -#### 5. You are a Team-Leader of a group of staffs working for a company xyz. The company ask you to create a directory ‘dir_xyz’, such that any member of the group can create a file or access a file under it, but no one can delete the file, except the one created it. what will you do? #### - -> **Answer** : An interesting scenario to work upon. Well in the above said scenario we need to implement the below steps which is as easy as cake walk. - - # mkdir dir_xyz - # chmod g+wx dir_xyz - # chmod +t dir_xyz - -The first line of command create a directory (**dir_xyz**). The second line of command above allow group (g) to have permission to ‘**write**‘ and ‘**execute**‘ and the last line of the above command – The ‘**+t**‘ in the end of the permissions is called the ‘**sticky bit**‘. It replaces the ‘x‘ and indicates that in this directory, files can only be deleted by their owners, the owner of the directory or the root superuser. - -#### 6. Can you tell me the various stages of a Linux process, it passes through? #### - -> **Answer** : A Linux process normally goes through four major stages in its processing life. - -Here are the 4 stages of Linux process. - -- Waiting: Linux Process waiting for a resource. -- Running : A Linux process is currently being executed. -- Stopped : A Linux Process is stopped after successful execution or after receiving kill signal. -- Zombie : A Process is said to be ‘Zombie’ if it has stopped but still active in process table. - -#### 7. What is the use of cut command in Linux? #### - -> **Answer** : A ‘cut’ is a very useful Linux command which proves to be helpful when we need to cut certain specific part of a file and print it on standard output, for better manipulation when the field of the file and file itself is too heavy. - -For example, extract first 10 columns of a text file ‘**txt_tecmint**‘. - - # cut -c1-10 txt_tecmint - -To extract 2nd, 5th and 7th column of the same text file. - - # cut -d;-f2 -f5 -f7 txt_tecmint - -#### 8. What is the difference between commands ‘cmp’ and ‘diff’? #### - -> **Answer** : The command ‘cmp’ and ‘diff’ means to obtain the same thing but with different mindset. - -The ‘**diff**‘ command reports the changes one should make so that both the files look the same. Whereas ‘**cmp**‘ command compares the two files byte-by-byte and reports the first mismatch. - -#### 9. Is it possible to substitute ‘ls’ command with ‘echo’ command? #### - -> **Answer** : Yes! the ‘ls’ command can be substituted by ‘echo’ command. The command ‘ls’ lists the content of file. From the point of view of replacement of above command we can use ‘echo *’, obviously without quotes. The output of both the commands are same. - -#### 10. You might have heard about inodes. can you describe inode briefly? #### - -> **Answer** : A ‘inode’ is a ‘data-structure’, which is used for file identification on Linux. Each file on an Unix System has a separate ‘inode’ and an ‘Unique’ inode Number. - -That’s all for now. We will be coming up with another interesting and knowledgeable Interview questions, in the next article. Till then Stay tuned and connected to Tecmint.com. Don’t forget to provide us, with your valuable feedback in the comment section below. - --------------------------------------------------------------------------------- - -via: http://www.tecmint.com/interview-questions-on-shell-scripting/ - -译者:[译者ID](https://github.com/译者ID) 校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出 - -[1]:http://www.tecmint.com/category/bash-shell/ -[2]:http://www.tecmint.com/category/interview-questions/ -[3]: -[4]: -[5]: -[6]: -[7]: -[8]: -[9]: -[10]: -[11]: -[12]: -[13]: -[14]: -[15]: -[16]: -[17]: -[18]: -[19]: -[20]: \ No newline at end of file diff --git a/translated/talk/10 Useful 'Interview Questions and Answers' on Linux Shell Scripting.md b/translated/talk/10 Useful 'Interview Questions and Answers' on Linux Shell Scripting.md new file mode 100644 index 0000000000..ff09472530 --- /dev/null +++ b/translated/talk/10 Useful 'Interview Questions and Answers' on Linux Shell Scripting.md @@ -0,0 +1,129 @@ +关于Linux Shell脚本的10个有用的“面试问题和解答” +================================================================================ +首先致上每日问候。Linux的浩瀚无垠,使人总能每次都提交与众不同的内容。我们“**The-Tecmint-Team**”的工作是给我们的读者提供一些独特的内容,这些内容不仅对他们的职业生涯很有用,同时也让他们增长知识。在此,我们就尝试这么去做,至于能取得多大的成功,就由我们的读者朋友们来判断吧。 +![Questions on Shell Scripting](http://www.tecmint.com/wp-content/uploads/2014/05/Questions-on-Shell-Scripting.png) + +我们为各类用户提供了关于**Shell脚本**语言和**面试问题**的很多教程,可以访问以下链接去阅读这些文章。 +- [Shell脚本系列][1] +- [面试问题与解答系列][2] + +在此,作为shell脚本的附加内容,在本文中我们将从面试的角度解读与Linux Shell相关的问题。 +#### 1. 在shell脚本成功执行后,你怎么退出脚本? #### +> **解答**:我们需要使用‘exit’命令来实现以上描述的情境。但‘exit’命令被强制输出非0值,脚本会报错并退出。在Unix环境下的shell脚本中,0值表示成功执行。因此,在脚本终止前赋予一个不带引号的‘exit -1’命令将使脚本退出。 + +例如,创建以下一个名为“**anything.sh**”的脚本。 + + #!/bin/bash + echo "Hello" + exit -1 + echo "bye" + +保存文件并执行。 + + # sh anything.sh + + Hello + exit.sh: 3: exit: Illegal number: -1 + +从上面的脚本中可以清楚地看到,在exit -1命令前,脚本执行得很好。 +#### 2. 如何使用Linux命令来移除文件头? #### +> **解答**:当我们需要删除文件中的指定行时,‘sed’命令可以用来解决该问题。 +这个是用来删除文件头(文件的首行)的确切命令。 + + # sed '1 d' file.txt + +上面命令的问题是,它会在标准输出设备上输出不带首行的文件内容。为了保存输出到文件,我们需要使用重定向操作符,它将帮助你将输出重定向到文件。 + + # sed '1 d' file.txt > new_file.txt + +好吧,其实**sed**命令内建的‘**-i**’开关就可以干这活,就不需要重定向符了吧。 + + # sed -i '1 d' file.txt + +#### 3. 你怎么检查一个文本文件中某一行的长度? #### +> **解答**:‘sed’命令也可以用来查找文本文件中的某一行或者检查其长度。 +‘**sed -n ‘n p’ file.txt**’可以解决,这里‘**n**’表示行号,‘**p**’打印出模式空间(到标准输出),该命令通常与**-n**命令行选项连用。那么,怎样来获取长度计数呢?很明显,我们需要通过管道输出给‘**wc**’命令来计算。 + + # sed –n 'n p' file.txt | wc –c + +To get the length of line number ’5′ in the text file ‘**tecmint.txt**‘, we need to run. + + # sed -n '5 p' tecmint.txt | wc -c + +#### 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 + +第一行命令创建了一个目录(**dir_xyz**),上面的第二行命令让组(g)具有‘写’和‘执行’的权限,而上面的最后一行命令——权限位最后的‘+t’是‘粘滞位’,它用来替换‘x’,表明在这个目录中,文件只能被它们的拥有者、目录的拥有者或者是超级用户root删除。 +#### 6. 你能告诉我一个Linux进程经历的各个阶段吗? #### +> **解答**:一个Linux进程在它的处理生活中,通常经历了四个主要阶段。 +这里是Linux进程要经历的四个阶段。 + +- 等待:Linux进程等待资源。 +- 运行:Linux进程当前正在执行中。 +- 停止:Linux进程在成功执行或收到杀死进程信号后停止。 +- 僵尸:当一个进程被称为‘僵尸’,如果该进程已经结束,那么它会仍然活动在进程表中。 + +#### 7. Linux中cut命令怎么用? #### +> **解答**:‘cut’是一个很有用的Linux命令,当我们要截取文件的指定部分并打印到标准输出,当文本区域以及文件本身很大时,该命令被证明对操作很有帮助。 +例如,截取‘**txt_tecmint**’文件的前10列。 + + # cut -c1-10 txt_tecmint + +要截取该文件中的第二,第五和第七列。 + + # cut -d;-f2 -f5 -f7 txt_tecmint + +#### 8. ‘cmp’和‘diff’命令的区别是什么? #### +> **解答**:‘cmp’和‘diff’命令用来获取相同的东西,但各有侧重。 +‘**diff**’命令对文件的修改,所以这两个文件看起来相同。而‘**cmp**’命令将两个文件逐字节对比,并报告第一个不匹配的项。 + +#### 9. 可以用‘echo’命令来替换‘ls’命令吗? #### +> **解答**:可以的。‘ls’命令可以用‘echo’命令来替代。‘ls’命令列出文件内容,从替换上述命令的角度讲,我们可以使用‘echo *’,很明显不需要引号,两个命令的输出完全一样。 + +#### 10. 你可能听过inode吧。你能简要描述一下inode吗? #### +> **解答**:‘inode’是一个‘数据结构’,在Linux上用于文件标识。每个文件在Unix系统上有一个独立的‘inode’和一个‘唯一的’inode号。 + +到此为止吧。在下一篇文章中,我们将讨论另外一些有趣味性而又有知识性的面试问题。到那时,别跑开,请上Tecmint.com,别忘了在下面的评论部分给我们提供一些有价值的反馈哦。 + +-------------------------------------------------------------------------------- + +via: http://www.tecmint.com/interview-questions-on-shell-scripting/ + +译者:[GOLinux](https://github.com/GOLinux) 校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出 + +[1]:http://www.tecmint.com/category/bash-shell/ +[2]:http://www.tecmint.com/category/interview-questions/ +[3]: +[4]: +[5]: +[6]: +[7]: +[8]: +[9]: +[10]: +[11]: +[12]: +[13]: +[14]: +[15]: +[16]: +[17]: +[18]: +[19]: +[20]: \ No newline at end of file