Linux Shell script interview 25 questions

Source: Internet
Author: User

Linux Shell script interview 25 questions

Q: What is a 1 Shell script and is it necessary?

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 complete a task. We can add all these commands in a text file (Shell script) to complete these daily tasks.

Q: 2 What is the default logon shell and how to change the logon shell of a specified user?

A: In Linux, "/bin/bash" is the default logon shell, which is allocated when a user is created. Use the chsh command to change the default shell. Example:

  1. # Chsh <User Name>-s <new shell>
  2. # chsh linuxtechi -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 itself. These variables are usually composed of uppercase letters and can be viewed using the "set" command.

User variables are generated and defined by the System user. You can run the"Echo $ <variable name>"View.

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

A: There are two methods to achieve this:

Method 1:

  1. 2> & 1 (for example, # ls/usr/share/doc> out.txt 2> & 1)

Method 2:

  1. &> (For example, # ls/usr/share/doc &> out.txt)
Q: How to nest the "if" syntax in 5 shell scripts?

A: The basic syntax is as follows:

  1. If [condition]
  2. then
  3. Command 1
  4. Command 2
  5. …..
  6. else
  7. If [condition]
  8. then
  9. Command 1
  10. Command 2
  11. ….
  12. else
  13. Command 1
  14. Command 2
  15. …..
  16. fi
  17. fi
Q: "$?" In 6 shell scripts ?" What is the purpose of the tag? ?

A: When writing a shell script, if you want to check whether the previous command is successfully executed, use "$?" In the if condition ?" You can check the end status of the previous command. A simple example is as follows:

  1. root@localhost:~# ls /usr/bin/shar
  2. /usr/bin/shar
  3. root@localhost:~# echo $?
  4. 0

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

  1. root@localhost:~# ls /usr/bin/share
  2. ls: cannot access /usr/bin/share:No such file or directory
  3. root@localhost:~# echo $?
  4. 2

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

Q: 7. How do I compare two numbers in shell scripts?

A: Use the test command (such as-gt) in if-then to compare two numbers. The example is as follows:

  1. #!/bin/bash
  2. x=10
  3. y=20
  4. if[ $x -gt $y ]
  5. then
  6. echo “x is greater than y”
  7. else
  8. echo “y is greater than x”
  9. fi
Q: What is the role of the break command in 8 shell scripts?

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

Q: 9 What is the function of the continue command in shell scripts?

A: The continue command is different from the break command. It only jumps out of the iteration of the current loop, not the whole loop. The continue command is often useful, for example, when an error occurs, but we still want to continue executing the large loop.

Q: 10 tell me the syntax of the Case statement in the shell script?

A: The basic syntax is as follows:

  1. Case variable in
  2. Value 1)
  3. Command 1
  4. Command 2
  5. …..
  6. Last Command
  7. !!
  8. Value 2)
  9. Command 1
  10. Command 2
  11. ……
  12. Last Command
  13. ;;
  14. esac
Q: What is the while LOOP syntax in the 11 shell script?

A: Like a for loop, A while LOOP repeats its command block as long as the condition is true. Unlike for loop, while loop continues iteration until its condition is not true. Basic Syntax:

  1. While [condition]
  2. do
  3. Command...
  4. done
Q: 12 how can I make the script executable?

A: run the chmod command to make the script executable. Example:

  1. # chmod a+x myscript.sh
Q: 13 "#! What is the role of/bin/bash?

A :#! /Bin/bash is the first line of shell script, called the release companion line (shebang. Here # The symbol is called hash, and! It is called bang. It means that the command is executed through/bin/bash.

Q: 14 is the for loop syntax in shell scripts?

A: The basic syntax of the for Loop:

  1. For variable in loop list
  2. do
  3. Command 1
  4. Command 2
  5. ….
  6. Last Command
  7. done
Q: 15 How do I debug shell scripts?

A: You can use the '-x' parameter (sh-x myscript. sh) to debug shell scripts. Another method is to use the '-nv' parameter (sh-nv myscript. sh ).

Q: How do 16 shell scripts compare strings?

A: The test command can be used to compare strings. The test command compares each character in the comparison string.

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

A: The following table lists the special variables set by the Bourne shell for the command line.

Built-in Variables

Explanation

$0

Script Name in command line

$1

The First Command Line Parameter

$2

The second command line parameter

.....

.......

$9

Ninth Command Line Parameter

$ #

Number of command line parameters

$ *

All command line parameters, separated by Spaces

Q: 18 How to test files in a shell script? Q: 18 in shell scripts, how do I test files?

A: The test command can be used to test files. The basic usage 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 object exists and is a normal object.

-R file name

Returns true if the object 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 can be written.

-X file name

Returns true if the file exists and is executable.

Q: 19 in shell scripts, how do I write comments?

A: Annotations can be used to describe what a script can do and how it works. Each line of comment starts. Example:

  1. #!/bin/bash
  2. # This is a command
  3. echo “I am logged inas $USER”
Q: 20 how can I get the input from the terminal from the shell script?

A: The read command can read data from a terminal (using a keyboard. Read command to get the user input and place it in the variables you give. Example:

  1. # vi /tmp/test.sh
  2. #!/bin/bash
  3. echo ‘Please enter your name’
  4. read name
  5. echo “MyNameis $name”
  6. # ./test.sh
  7. Please enter your name
  8. LinuxTechi
  9. MyNameisLinuxTechi
Q: 21. How do I cancel a variable or assign a value to a variable?

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

  1. # Unset <variable name>
Q: 22. How do I perform arithmetic operations?

A: There are two ways to perform arithmetic operations:

1. UseexprCommand (# expr 5 + 2) 2. Use a dollar sign and square brackets ($ [Expression]) Example: test = $[16 + 4]; test = $[16 + 4]

Q: 23 is the basic format of the do-while statement?

A: the do-while statement is similar to the while statement, but the command is executed at least once before the Condition Statement is checked .). The following is the syntax of the do-while statement.

  1. do
  2. {
  3. Command
  4. } While (condition)
Q: How do I define functions in shell scripts?

A: A function is a code block with a name. When we define a code block, we can call the function name in our script and the block will be executed. Example:

  1. $ diskusage (){ df -h ;}
  2. Note: Below is the shell function syntax I have given.
  3. [Function] function name [()]
  4. {
  5. Command;
  6. [returnint;]
  7. }
Q: 25 How do I use the BC (bash calculator) in shell scripts )?

A: Use the following format and use bc in shell scripts:

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

Cat commands use Linux redirection to merge files

Shell programming

Replace Linux Shell Parameters

Shell for parameters

Pass Linux/Unix Shell parameters to SQL scripts

Introduction to parameter passing methods in Shell scripts

PASS command line parameters through Shell scripts

Linux Shell wildcards, escape characters, metacharacters, and special characters

This article permanently updates the link address:

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.