Home  >  Article  >  Operation and Maintenance  >  What does the linux command sh mean?

What does the linux command sh mean?

藏色散人
藏色散人Original
2023-04-12 11:15:2411340browse

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