Home >Operation and Maintenance >Linux Operation and Maintenance >What are the basic commands of shell scripts?

What are the basic commands of shell scripts?

coldplay.xixi
coldplay.xixiOriginal
2021-03-18 16:32:5827304browse

Basic commands of shell script: 1. Shell’s echo instruction is similar to PHP’s echo instruction, both are used for string output; 2. The printf command imitates the [printf()] program in the C library; 3. The test command is used to check whether a certain condition is true.

What are the basic commands of shell scripts?

The operating environment of this tutorial: linux7.3 system, DELL G3 computer.

Basic commands of shell script:

1, echo command

1.1 Basic use of

Shell The echo command is similar to PHP's echo command, both are used for string output. When outputting a string, you can omit the quotes. The echo command adds a newline character at the end by default.

username='uusama'
echo $username  # 输出 uusama
echo test   # 输出 test
echo 'test' # 输出 test
echo there is test str  # 输出 there is test str

1.2 Special output

read command reads a line from the standard input and assigns the value of each field of the input line to the shell variable. If the echo command is used on the command line, then To add the -e option, activate escape characters.

  • Use double quotes to display escape characters, such as echo "\"It is a test\""

  • Use: echo " \n”

  • To display without line breaks, use: echo “\c”

  • Use backticks to display the command execution result: echo `date` ( Backticks)

  • Use single quotes to output the string as is: echo '\n${username}\c'

2 , printf command

The printf command imitates the printf() program in the C library. It is defined by the standard, so scripts using printf are more portable than using echo.

printf uses quoted text or space-delimited parameters. You can use formatting strings in printf, and you can also specify the width, left and right alignment of the string, etc. By default printf does not automatically add newlines like echo, we can add \n manually.

2.1 How to use

printf format-string [arguments...]
# 举例
printf "%-10s %-8s %-4s\n" 姓名 性别 体重kg

3. Test command

The test command in Shell is used to check whether a certain condition is true. It can perform numerical values , characters and files.

Numerical test

Parameter Description

-eq If equal, it is true

-ne If not equal, it is true

-gt If it is greater than it is true

-ge If it is greater than or equal to it is true

-lt If it is less than it is true

-le If it is less than or equal to it it is true

Example:

num1=100
num2=100
if test [num1] -eq[num1]−eq[num2]
then
    echo '两个数相等!'
else
    echo '两个数不相等!'
fi
# [] 计算简单算术运算
echo $[num1+num2]

The [] in the code performs basic arithmetic operations.

Related learning recommendations: linux video tutorial

The above is the detailed content of What are the basic commands of shell scripts?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn