Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the Shell script questions?

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What are the Shell script test questions? in view of this question, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Here are some shell script interview questions and answers that you often encounter during the interview process:

What is the QRV 1 Shell script and is it necessary?

A: an Shell script is a text file that contains one or more commands. As system administrators, we often need to use multiple commands to complete a task, and we can add all these commands in a text file (Shell script) to complete these daily tasks.

What is the default login shell and how to change the login shell of a specified user

Answer: in the Linux operating system, "/ bin/bash" is the default login to shell and is assigned when the user is created. The default shell can be changed using the chsh command. The example is as follows:

# chsh-s # chsh linuxtechi-s / bin/sh

What types of variables can QRV 3 use in shell scripts?

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

System definition variable

User-defined variable

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

User variables are generated and defined by the system user, and their values can be viewed with the command "echo $".

How to redirect standard output and error output to the same location at the same time?

A: there are two ways to do this:

Method 1:

2 > & 1 (e.g. # ls / usr/share/doc > out.txt 2 > & 1)

Method 2:

& > (e.g. # ls / usr/share/doc & > out.txt)

How is the "if" syntax nested in the QRZ 5 shell script?

A: the basic syntax is as follows:

If [condition] then Command 1 Command 2... .. elseif [condition] then Command 1 Command 2... .else command 1 command 2... .. fifi

"$?" in QRV 6 shell script What is the purpose of marking?

When writing a shell script, if you want to check whether the previous command was executed successfully, use "$?" in the if condition. You can check the end status of the previous command. Simple examples are as follows:

Root@localhost:~# ls / usr/bin/shar/usr/bin/sharroot@localhost:~# echo $? 0

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

Root@localhost:~# ls / usr/bin/sharels: cannot access / usr/bin/share: No such file or directoryroot@localhost:~# echo $? 2

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

How does QRZ 7 compare two numbers in a shell script?

A: use the test command (- gt, etc.) in if-then to compare two numbers, as shown in the following example:

#! / bin/bashx=10y=20if [$x-gt $y] thenecho "x is greater than y" elseecho "y is greater than x" fi

What is the role of the break command in the QRV 8 shell script?

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

What is the role of the continue command in the QRV 9 shell script?

Answer: unlike the break command, the continue command only jumps out of the iteration of the current loop, not the entire loop. The continue command is useful in many cases, such as when an error occurs, but we still want to continue to execute the big loop.

QR 10 tells me the syntax of the Case statement in the shell script.

A: the basic syntax is as follows:

Case variable in value 1) Command 1 Command 2... .. Final order! Value 2) Command 1 Command 2. Final order; esac

While Loop Syntax in QRU 11 shell script?

Answer: like the for loop, the while loop repeats its command block as long as the condition holds. Unlike the for loop, the while loop iterates until its condition is not true. Basic grammar:

While [condition] do command... Done

How do I make the script executable?

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

# chmod axix myscript.sh

The role of QRV 13 "#! / bin/bash"?

Answer: #! / bin/bash is the first line of the shell script, called the shebang line. The # symbol here is called hash, and! It's called bang. It means that commands are executed through / bin/bash.

For Loop Syntax in QRV 14 shell script?

Answer: the basic syntax of the for loop:

For variable in Loop list do Command 1 Command 2... . Finally, command done.

How does QRV 15 debug shell scripts?

Answer: use the'- x 'parameter (sh-x myscript.sh) to debug shell scripts. Another way is to use the'- nv' parameter (sh-nv myscript.sh).

How does the QRV 16 shell script compare strings?

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

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

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

The built-in variable explains the script name $1 on the $0 command line, the first command line argument $2, and the second command line argument. .. …… .9 ninth command line argument $# number of command line arguments $* all command line arguments, separated by spaces

QRV 18 how do I test the file in the shell script?

A: the test command can be used to test files. The basic usage is as follows:

Test usage-d file name if the file exists and is a directory, returns the true-e file name if the file exists, returns the true-f file name if the file exists and is a normal file, returns the true-r file name if the file exists and is readable, returns the true-s file name if the file exists and is not empty, returns the true-w file name if the file exists and is writable Returns the true-x file name if the file exists and is executable, returns true

QRV 19 how do I write comments in shell scripts?

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

#! / bin/bash# This is a commandecho "I am logged in as $USER"

QRV 20 how to let shell get input from the terminal on the script?

Answer: the read command reads data from the terminal (using the keyboard). The read command takes the user's input and places it in the variable you give. Examples are as follows:

# vi / tmpplash test.shrunken Please enter your name'read nameecho Bash echo 'Please enter your name'read nameecho "My Name is $name". / test.shPlease enter your nameLinuxTechiMy Name is LinuxTechi

How to cancel a variable or cancel a variable assignment in QRV 21?

Answer: the "unset" command is used to cancel variables or unassign variables. The syntax is as follows:

# unset

How does QRV 22 perform arithmetic operations?

A: there are two ways to perform arithmetic operations:

1. Use the expr command

# expr 5 + 2

two。 Use a dollar sign and square brackets ($[expression]) for example:

Test=$ [16 + 4]; test=$ [16 + 4]

What is the basic format of QRV 23 do-while statements?

A: the do-while statement is similar to the while statement, but executes the command before checking the conditional statement (LCTT translation: meaning at least once. ). Here is the syntax for using the do-while statement

Do {Command} while (condition)

QRV 24 how do you define functions in the shell script?

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

$diskusage () {df-h;}

Translation note: the following is the syntax of the shell function I gave, but not in the original text

[function] function name [()] {command; [return int;]} questions about Shell script questions are shared here. I hope the above content can be of some help to everyone. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report