"Go" Linux shell script interview 25 Q

Source: Internet
Author: User
Tags 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:

# Chsh <username>-S<new_default_shell>-s/bin/sh

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:

if [Condition]  Then command1command2 ..... Else if [Condition]  Then command1command2 .... Else command1command2 ..... fi fi

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

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:

ls /usr/bin/shar/usr/bin/shar[email protected]:echo $? 0

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

ls /usr/bin/sharelsfile  or Directory[email protected]:Echo $? 2

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:

#!/bin/bashx=teny=if [$x-GT $y]thenEcho "x is greater than Y" Else Echo "Y is greater than X" fi

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:

 Case inch value1) Command1command2.....last_command!! value2) Command1command2......last_command;; Esac

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:

 while [Test_condition]  Do Commands ...  Done

Q:12 How do I make a script executable?

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

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:

 for inch List_of_items  Do Command1command2....last_command  Done

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.

Built-in variables

Explain

$

Script name in the command line

$

First command-line arguments

$

Second command-line argument

.....

.......

$9

Nineth command-line arguments

$#

Number of command-line arguments

$*

All command-line arguments, separated by spaces

Q:18 How to test files in a shell script? 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:

Test

Usage

-D file name

Returns true if the file exists and is a directory

-E File name

Returns true if the file exists

-F file name

Returns true if the file exists and is a normal file

-R file name

Returns true if the file exists and is readable

-S file name

Returns true if the file exists and is not empty

-W file name

Returns true if the file exists and is writable

-X file name

Returns true if the file exists and can be executed

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:

#!/bin/bash# a commandecho in as $USER "

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:

VI /tmp/test. SH #!/bin/bashecho  ' Please enter your name ' read nameecho ' My name is $ Name "#. /test. SH Please enter your namelinuxtechimy Name is Linuxtechi

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:

# unset <Name_of_Variable>

Q:22 How to perform arithmetic operations?

Answer: There are two ways to perform arithmetic operations:

Use expr command (# 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

do 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:

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:

Variable= 'echo ' options; Expression "| BC '


Original: http://blog.jobbole.com/86237/

"Go" Linux shell script interview 25 Q

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.