In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Shell script refers to the basis of what, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
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
... ..
Else
If [condition]
Then
Command 1
Command 2
... .
Else
Command 1
Command 2
... ..
Fi
Fi
"$?" 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/shar
Root@localhost:~# echo $?
0
If the end state is 0, the previous command was executed successfully.
Root@localhost:~# ls / usr/bin/share
Ls: cannot access / usr/bin/share: No such file or directory
Root@localhost:~# echo $?
two
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/bash
Xerox 10
Yicheng 20
If [$x-gt $y]
Then
Echo "x is greater than y"
Else
Echo "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.
What is the syntax of the Case statement in the QR 10 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
... .
Final order
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
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 if the file exists and is a directory, return true-e if the file exists-f if the file exists and is normal-r if the file exists and is readable-s if the file exists and is not empty-w if the file exists and is writable-x if the file exists and is executable
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 command
Echo "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 / tmp/test.sh
#! / bin/bash
Echo 'Please enter your name'
Read name
Echo "My Name is $name"
#. / test.sh
Please enter your name
LinuxTechi
My 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;]
}
How do I use BC (bash Calculator) in shell scripts?
Answer: use bc in shell scripts using the following format:
Variable= `echo "options; expression" | bc`
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.