Ali Linux Shell script interview 25 Classic Quiz

Source: Internet
Author: User
Tags arithmetic case statement


What is a q:1 shell script and is it required?


A: A shell script is a text file that contains one or more commands. As a system administrator, we often need to use multiple commands to accomplish a task, and we can add all these commands in a text file (shell script) to accomplish these everyday tasks.




q:2 What is the default login shell, how to change the login shell for a specified user


A: On the Linux operating system, "/bin/bash" is the default login shell, which is assigned when the user is created. Use the CHSH command to change the default shell. Examples are shown below:






q:3 What types of variables can be used in shell scripts?


A: In shell scripts, we can use two types of variables:


    • System-defined variables

    • User-defined variables


System variables are created by the system system itself. These variables are usually made up of uppercase letters and can be viewed through the "set" command.



User variables are generated and defined by the system user, and the values of the variables can be viewed through the command "echo $<变量名>".




Q:4 How can I redirect standard output and error output to the same location at the same time?


A: There are two ways to accomplish this:



Method One:



2>&1 (# Ls/usr/share/doc > OUT.txt 2>&1)



Method Two:



&> (# Ls/usr/share/doc &> out.txt)




Q:5 How is the "if" syntax nested in a shell script?


A: The basic syntax is as follows:







Q:6 "$?" in shell scripts What is the purpose of the tag?



A: When writing a shell script, if you want to check if the previous command was successful, use "$?" in the IF condition. You can check the end state of the previous command. A simple example is as follows:





If the end state is 0, the previous command executed successfully.




If the end state is not 0, the command execution fails.




How do q:7 compare two numbers in a shell script?


A: Use test commands (-GT, etc.) in If-then to compare two numbers, as in the following example:







Q:8 what does the break command do in a shell script?



A: A simple use of the break command is to exit the loop in execution. We can use the break command to jump out of the loop in the while and until loops.




Q:9 The role of the continue command in a shell script?


A: The continue command differs from the break command, and it only jumps out of the iteration of the current loop, not the entire loop. Continue commands are useful in many cases, such as when an error occurs, but we still want to continue the execution of cycle.




q:10 tell me the syntax of the case statement in the shell script?


A: The basic syntax is as follows:







Q:11 shell script in the while loop syntax?



A: Like a For loop, the while loop repeats its command block whenever the condition is true. Unlike a For loop, the while loop iterates continuously until its condition is not true. Basic syntax:







Q:12 How do I make a script executable?



Answer: Use the chmod command to make the script executable. Examples are as follows:


1 # chmod a+x myscript.sh
  q:13 The role of "#!/bin/bash"?


A: #!/bin/bash is the first line of the shell script, called the Release Companion (shebang) line. Here the # symbol is called hash, and! Called Bang. It means that the order is executed by/bin/bash.




q:14 shell script for the FOR loop syntax?


A: The underlying syntax for a for loop:







q:15 How do I debug a shell script?



A: Use the '-X ' parameter (sh-x myscript.sh) to debug the shell script. Another way is to use the '-nv ' parameter (SH-NV myscript.sh).




How do q:16 shell scripts compare strings?


Answer: The test command can be used to compare strings. Test commands are compared by comparing each character in a string.




What are the special variables in q:17 Bourne shell (bash)?


A: The following table lists the special variables that the Bourne shell sets for the command line.







Q:18 in the shell script, how to test the file?



Answer: The test command can be used for testing files. The Basic usage table is as follows:




 q:19 in a shell script, how do I write a comment?


A: Comments can be used to describe what a script can do and how it works. Each line of comments begins with #. Examples are as follows:






q:20 How do I let the shell get input from the terminal on the script?


A: The read command can read data from the terminal (using the keyboard). The read command gets the user's input and is placed in the variable you gave. Examples are as follows:






q:21 How do I cancel a variable or cancel a variable assignment?


A: the "unset" command is used to cancel a variable or cancel a variable assignment. The syntax is as follows:


1 # unset <Name_of_Variable>
 q:22 How to perform arithmetic operations?


Answer: There are two ways to perform arithmetic operations:



Useexprcommand (# expr 5 + 2) 2. Use a dollar sign and square brackets ($[ 表达式 ]) For example: Test=$[16 + 4]; test=$[16 + 4]


 q:23 The basic format of the Do-while statement?


A: The Do-while statement is similar to the while statement, but the command is executed before the conditional statement is checked (LCTT: at least once. )。 Here is the syntax for using the Do-while statement


1 2 3 4 do{statements

}while(condition)

 

q:24 How does a shell script define a function?


A: A function is a block of code that has a name. When we define the code block, we can invoke the function name in our script and the block will be executed. Examples are shown below:


1

$ diskusage () {df-h ; }

 

 


q:25 How do i use the BC (Bash Calculator) in shell scripts?



A: Use the BC in the shell script using the following format:





1

variable=`echo“options; expression” |bc`








This article references:http://mp.weixin.qq.com/s?__biz=MjM5ODI5Njc2MA==&mid=205719951&idx=1&sn= 4f5072fe426d94d42c4a74564f588b0c&key= 1936e2bc22c2ceb5ccb960c8f73dee606e822d4059e3e261bab194cc3f09c4cc46a21d6aedcc24b8e14289bc81767c99&ascene=1 &uin=mzi2mjq2odm1&devicetype=windows+7&version=61000721&pass_ticket= 04zrsm4kbntosuaor1q4btv0ksmltpz8u3uol2g3eg4%2bm7qbbgckcujzw5i%2bvnpj



Ali Linux Shell script interview 25 Classic Quiz


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.