search

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

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

 num=2
 echo “this is the $numnd”

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”

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 #显示用户输入,并将输入赋值给变量

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

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 # 打印结果为空
  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
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`

Process Control

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

 if ….; then
 …. elif ….; then
 …. else
 …. fi #注意是以fi结尾
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
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

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是否相等

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

  [ -f "/etc/shadow" ] && echo “This computer uses shadow passwors”
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

该脚本首先判断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
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
The 5 Core Components of the Linux Operating SystemThe 5 Core Components of the Linux Operating SystemMay 08, 2025 am 12:08 AM

The five core components of the Linux operating system are: 1. Kernel, 2. System libraries, 3. System tools, 4. System services, 5. File system. These components work together to ensure the stable and efficient operation of the system, and together form a powerful and flexible operating system.

The 5 Essential Elements of Linux: ExplainedThe 5 Essential Elements of Linux: ExplainedMay 07, 2025 am 12:14 AM

The five core elements of Linux are: 1. Kernel, 2. Command line interface, 3. File system, 4. Package management, 5. Community and open source. Together, these elements define the nature and functionality of Linux.

Linux Operations: Security and User ManagementLinux Operations: Security and User ManagementMay 06, 2025 am 12:04 AM

Linux user management and security can be achieved through the following steps: 1. Create users and groups, using commands such as sudouseradd-m-gdevelopers-s/bin/bashjohn. 2. Bulkly create users and set password policies, using the for loop and chpasswd commands. 3. Check and fix common errors, home directory and shell settings. 4. Implement best practices such as strong cryptographic policies, regular audits and the principle of minimum authority. 5. Optimize performance, use sudo and adjust PAM module configuration. Through these methods, users can be effectively managed and system security can be improved.

Linux Operations: File System, Processes, and MoreLinux Operations: File System, Processes, and MoreMay 05, 2025 am 12:16 AM

The core operations of Linux file system and process management include file system management and process control. 1) File system operations include creating, deleting, copying and moving files or directories, using commands such as mkdir, rmdir, cp and mv. 2) Process management involves starting, monitoring and killing processes, using commands such as ./my_script.sh&, top and kill.

Linux Operations: Shell Scripting and AutomationLinux Operations: Shell Scripting and AutomationMay 04, 2025 am 12:15 AM

Shell scripts are powerful tools for automated execution of commands in Linux systems. 1) The shell script executes commands line by line through the interpreter to process variable substitution and conditional judgment. 2) The basic usage includes backup operations, such as using the tar command to back up the directory. 3) Advanced usage involves the use of functions and case statements to manage services. 4) Debugging skills include using set-x to enable debugging mode and set-e to exit when the command fails. 5) Performance optimization is recommended to avoid subshells, use arrays and optimization loops.

Linux Operations: Understanding the Core FunctionalityLinux Operations: Understanding the Core FunctionalityMay 03, 2025 am 12:09 AM

Linux is a Unix-based multi-user, multi-tasking operating system that emphasizes simplicity, modularity and openness. Its core functions include: file system: organized in a tree structure, supports multiple file systems such as ext4, XFS, Btrfs, and use df-T to view file system types. Process management: View the process through the ps command, manage the process using PID, involving priority settings and signal processing. Network configuration: Flexible setting of IP addresses and managing network services, and use sudoipaddradd to configure IP. These features are applied in real-life operations through basic commands and advanced script automation, improving efficiency and reducing errors.

Linux: Entering and Exiting Maintenance ModeLinux: Entering and Exiting Maintenance ModeMay 02, 2025 am 12:01 AM

The methods to enter Linux maintenance mode include: 1. Edit the GRUB configuration file, add "single" or "1" parameters and update the GRUB configuration; 2. Edit the startup parameters in the GRUB menu, add "single" or "1". Exit maintenance mode only requires restarting the system. With these steps, you can quickly enter maintenance mode when needed and exit safely, ensuring system stability and security.

Understanding Linux: The Core Components DefinedUnderstanding Linux: The Core Components DefinedMay 01, 2025 am 12:19 AM

The core components of Linux include kernel, shell, file system, process management and memory management. 1) Kernel management system resources, 2) shell provides user interaction interface, 3) file system supports multiple formats, 4) Process management is implemented through system calls such as fork, and 5) memory management uses virtual memory technology.

See all articles

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 Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!