Table of Contents
Process Control" >Process Control
Home Operation and Maintenance Linux Operation and Maintenance What does the linux command sh mean?

What does the linux command sh mean?

Apr 12, 2023 am 11:15 AM
linux

The linux command sh is the command to run the shell in Linux and is the interpreter of the shell. The shell script is the shell and command line interface in Linux. Users can enter commands in the shell script to perform various tasks. .

What does the linux command sh mean?

## The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

What does the linux command sh mean?

Linux sh command brief description

1. Possible execution methods

Methods to execute .sh files under linux

.sh files are text files. If To execute, you need to use chmod a x xxx.sh to give executable permissions.

2. Beginning: #!/bin/sh

The shell program must start with "#!/bin/sh". # in the shell generally means a comment, so many people think that "#!" is also a comment, but in fact it is not.

"#!/bin/sh" is a declaration of the shell, indicating what type of shell you are using and its path.

#!/bin/ means this script is executed using .bin/sh.

#! is a special identifier, followed by the path of the shell that interprets this script. If not declared, the script will be executed in the default shell, which is defined by the system where the user is located. In order to execute a shell script, if the script is written to run in Kornshell ksh, and the default running shell script is C shell csh, the script is likely to fail during execution. Therefore, it is recommended that everyone treat "#!/bin/sh" as the main function of C language. It is necessary to write a shell to make the shell program more rigorous.

3. Variables

Variables must be used in other programming languages. In shell programming, all variables are composed of strings, and there is no need to declare variables . To assign a value to a variable, you can write like this:

#!/bin/sh
 #对变量赋值:
 a=”hello world”# 现在打印变量a的内容:
 echo “A is:” echo $a
Copy after login

Sometimes variable names are easily confused with other words, such as:

 num=2
 echo “this is the $numnd”
Copy after login

This will not print out "this is the 2nd", And just print "this is the ", because the shell will search for the value of the variable numnd, but this variable has no value. Therefore, you can use curly braces to tell the shell that what we want to print is the num variable:

 num=2
 echo “this is the ${num}nd”
Copy after login

In this way, "this is the 2nd"

4. Shell command And process control

The following commands can be used in shell scripts:

Unix commands

Although any unix command can be used in shell scripts, But there are still some relatively more commonly used commands. These commands are usually used for file and text operations.
Such as:

 echo "some text" #将文字内容打印在屏幕上
 ls #文件列表
 cp sourcefile destfile #文件拷贝
 mv oldname newname #重命名文件或移动文件
 rm file #删除文件
 grep 'pattern' file #在文件内搜索字符串,如:grep 'searchstring' file.txt
 cat file.txt #输出文件内容到标准输出设备(屏幕)上
 read var #显示用户输入,并将输入赋值给变量
Copy after login

Concept: pipe, redirection and backtick (backslash)

  1. Pipeline| Will a The output of a command serves as input to another command.
grep "hello" file.txt | wc -l
Copy after login

The above code is expressed as: search for lines containing "hello" in file.txt and count the number of lines. Here the output of the grep command is used as the input of the wc command.

It should be noted that the command after the pipe is a subcommand and will not appear in the next command (a bit like C in {} and {}The difference between external assignment), such as the following command:

#!/bin/shecho 1 2 3 | { read a b c ; echo $a $b $c ; } # 打印结果为: 1 2 3echo $a $b $c # 打印结果为空
Copy after login
  1. Redirection: Output the results of the command to a file instead of the standard output (screen).
    >Write the file and overwrite the old file
    >>Append to the end of the file, retaining the old file content.

  2. Inverse dash"`": Use inverse dash to output the output of one command as another command A command line parameter .

  3.  find . -mtime  -1  -type  f  -print
    Copy after login
The above statement is used to find files that have been modified in the past 24 hours (-mtime -2 means the past 48 hours). If you want to package all the found files, you can use the following linux script:

 #!/bin/sh
 # The ticks are backticks (`) not normal quotes (‘):
 tar -zcvf  lastmod.tar.gz `find . -mtime -1 -type f -print`
Copy after login

Process Control

if
if Expression, if the condition is true, execute then The following part:

 if ….; then
 …. elif ….; then
 …. else
 …. fi #注意是以fi结尾
Copy after login
In most cases, you can use the test command to test the condition. For example, you can

compare strings, determine whether files exist and whether they are readable, etc. ...

while
while Syntax of loop The structure is:

# expression 1# while循环:当expresssion成立的时候,执行cmdwhile (expresssion)do
  cmddone# expression 2,可以直接使用truewhile true(或 :)do 
	cmddone
Copy after login
This command can be used with pipelines, such as:

# 寻找 ${path} 路径下唯一首字母为‘E’的子目录,并 cd 到该目录find ${path}/E* -type d | while read corresp_pathdo
	cd ${corresp_path}done
Copy after login

Test conditions Usually use
"[ ]" to represent the test conditions. Note that the spaces here are very important, make sure there are spaces in the square brackets.

 [ -f "somefile" ] #判断文件是否存在
 [ -d "testResults/" ] #判断目录testResults/是否存在
 [ -x "/bin/ls" ] #判断/bin/ls文件是否存在并有可执行权限
 [ -n "$var" ] #判断$var变量是否有值
 [ "$a" = "$b" ] #判断$a和$b是否相等
Copy after login

Shortcut operator If you are familiar with C language, you may like the expression:

  [ -f "/etc/shadow" ] && echo “This computer uses shadow passwors”
Copy after login
Here

"&&" is a shortcut operation symbol, if the expression on the left is true, the statement on the right is executed. Of course, the above expression can also be considered as the AND operation in logical operations.

The same OR operation

"||"is also available in shell programming:

 #!/bin/sh
 mailfolder=/var/spool/mail/james [ -r "$mailfolder" ]‘ ‘{ echo “Can not read $mailfolder” ; exit 1; } #感觉这里的‘’应该是||
 echo “$mailfolder has mail from:” grep “^From ” $mailfolder
Copy after login

该脚本首先判断mailfolder是否可读。如果可读则打印该文件中的”From” 一行。如果不可读则或操作生效,打印错误信息后脚本退出。这里有个问题,那就是我们必须有两个命令:
◆打印错误信息
◆退出程序
我们使用花括号以匿名函数的形式将两个命令放到一起作为一个命令使用。一般函数将在下文提及。
不用‘与’和‘或’操作符,我们也可以用if表达式作任何事情,但是使用与或操作符会更便利很多。

推荐学习:《linux视频教程

The above is the detailed content of What does the linux command sh mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Clair Obscur: Expedition 33 - How To Get Perfect Chroma Catalysts
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1676
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

git software installation git software installation Apr 17, 2025 am 11:57 AM

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

How to set important Git configuration global properties How to set important Git configuration global properties Apr 17, 2025 pm 12:21 PM

There are many ways to customize a development environment, but the global Git configuration file is one that is most likely to be used for custom settings such as usernames, emails, preferred text editors, and remote branches. Here are the key things you need to know about global Git configuration files.

See all articles