What is the 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 complete a task, and we can add all of these commands in a text file (shell script) to accomplish these day-to-day tasks.
Q:2 What is the default login shell, how to change the specified user's login shell
A: In the Linux operating system, "/bin/bash" is the default login shell, which is assigned when creating a user. Use the CHSH command to change the default shell. Examples are shown below:
# Chsh <username>-S <new_default_shell>
# Chsh linuxtechi-s/bin/sh
What types of variables can q:3 use in shell scripts?
A: In the case of a shell script, 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 value of the variable can be viewed by the command "echo $<variable name>".
Q:4 How do I redirect standard output and error output to the same location at the same time?
A: Here are two ways to achieve this:
Method One:
2>&1 (# Ls/usr/share/doc > OUT.txt 2>&1)
Method Two:
&> (# Ls/usr/share/doc &> out.txt)
How is the "if" syntax nested in the Q:5 shell script?
Answer: The basic syntax is as follows:
If [Condition]
then
command1
command2
..... else
if [condition]
then
command1
command2
...
else
Command1
Command2 ....
Fi
fi
Q:6 shell script "$?" What is the purpose of the tag??
A: When writing a shell script, if you want to check if the previous command was executed successfully, use the "$" in the IF condition. You can check the end state of the previous command. The simple example is 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 $?
2
If the end state is not 0, the command execution fails.
How does q:7 compare two digits in a shell script?
A: Use test commands (-GT, etc.) in if-then to compare two digits, as follows:
#!/bin/bash
x=10
y=20
If [$x-gt $y]
then
echo "X is greater than Y"
else
echo "Y is great ER than x "
fi
Q:8 the function of the break command 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 continue commands in a shell script?
A: The continue command differs from the break command, and it only jumps out of the current loop's iteration, not the entire loop. Continue commands are useful in many cases, such as errors, but we still want to continue the cycle.
Q:10 tell me the syntax of the case statement in the shell script?
Answer: The basic syntax is as follows:
Case word in
value1)
command1
command2
..... Last_command
!!
value2)
Command1
command2 ...
Last_command;
;
Esac
Q:11 the While loop syntax in the shell script?
Answer: As for loop, while loop repeats its command block as long as the condition is set. Unlike a For loop, the while loop continues to iterate until its condition is not true. Basic syntax:
While [test_condition]
do
commands
... Done
Q:12 How do I make a script executable?
A: 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 shebang line. Here the # symbol is called hash, and! It's called Bang. It means that the order is executed through/bin/bash.
Q:14 the For loop syntax in a shell script?
Answer: The basic syntax for A For loop:
For variables in List_of_items
do
command1
command2 ...
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 approach 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. The test command is compared by comparing each character in the string.
What are the special variables in the 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 on command line |
$ |
First command-line argument |
$ |
Second command-line argument |
..... |
....... |
$ |
Nineth Command line argument |
$# |
Number of command line arguments |
$* |
All command line arguments, separated by spaces |
Q:18 to test the files in a shell script?
q:18 How do I test a file in a shell script?
Answer: The test command can be used for testing files. The basic usage forms are 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 filename |
Returns true if the file exists and is a normal file |
-R file name |
Returns true if the file exists and is readable |
-S filename |
Returns true if the file exists and is not empty |
-W file name |
Returns true if the file exists and is writable |
-X filename |
Returns true if the file exists and can be executed |
Q:19 in a shell script, how do I write a comment?
A: Annotations can be used to describe what a script can do and how it works. Each line comment begins with a #. Examples are as follows:
#!/bin/bash
# This is a command
echo ' I am logged in as $USER '
Q:20 How do you get the shell to get input from the terminal in the script?
A: The read command can read data from the terminal (using the keyboard). The read command gets 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 ' I name is $name
#./test . sh Please
enter your name
Linuxtechi i
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 to remove a variable assignment. The syntax looks like this:
# unset <Name_of_Variable>
q:22 How do I perform arithmetic operations?
Answer: There are two ways to perform arithmetic operations:
Useexprthe command (# expr 5 + 2) 2. With a dollar sign and square brackets ($[ expression ]) such as: Test=$[16 + 4]; test=$[16 + 4]
The basic format of the Q:23 do-while statement?
A: The Do-while statement is similar to a while statement, but executes the command before checking the conditional statement (LCTT: Execute at least once.) )。 Here is the syntax for using the Do-while statement
Do
{
statements
} while (condition)
q:24 How do you define a function in a shell script?
Answer: A function is a code block that has a name. When we define a block of code, we can call the function name in our script, and the block will be executed. Examples are shown below:
$ diskusage () {df-h;}
q:25 How do i use the BC (Bash Calculator) in a shell script?
A: Use the BC in a shell script using the following format:
Variable= ' echo ' options; Expression "| BC '
Source: Linuxtechi Translation: LCTT