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:
?
12 |
# chsh <username> -s <new_default_shell> # 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 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:
?
1234567891011121314151617 |
if [ Condition ] then command1 command2 ….. else if [ condition ] then command1 command2 …. else command1 command2 ….. 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:
?
1234 |
[email protected]:~ # ls /usr/bin/shar /usr/bin/shar [email protected]:~ # echo $? 0 |
If the end state is 0, the previous command executed successfully.
?
1234 |
[email protected]:~ # ls /usr/bin/share ls : cannot access /usr/bin/share : No such file 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:
?
123456789 |
#!/bin/bash x=10 y=20 if [ $x -gt $y ] then echo “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:
?
1234567891011121314 |
case word in value1) command1 command2 ….. last_command !! value2) command1 command2 …… 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:
?
1234 |
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:
?
1 |
# 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:
?
1234567 |
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 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:
?
123 |
#!/bin/bash # This is a command echo “I am logged 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:
?
123456789 |
# 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 |
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:
?
1 |
# 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
?
1234 |
do { statements } 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:
?
1 |
$ diskusage () { 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:
?
1 |
variable=` echo “options; expression” | bc ` |
Source: Linuxtechi Translation Source: LCTT
Shell script face Questions