Ubuntu run .sh file

1. Run the .sh file

(1)使用sh testsh执行
(2)使用bash testsh 执行
(3)使用点 执行
(4)使用source执行

./sh 文件开头***的含义:
#!/bin/sh             以下的代码由/bin/sh 来解释
#!/bin/bash           以bash shell来解释
#!/bin/csh            以csh shell来解释
#!/usr/bin/env python  以下代码由python来解释 
#! 是一个约定的标记,它告诉系统这个脚本需要什么解释器来执行,即使用哪
一种 Shell

 

(1) Use sh test.sh to execute

Use sh test.sh to execute the script file. This method indicates that the sh shell is used to execute the test.sh file. sh is already a shell replaced by bash.

Although we declared in test.sh to use #!/bin/bash to execute our file, but at this time use sh instead of bash, then #!/bin/bash has no effect.

(2) Use bash test.sh to execute

This method is actually the same as sh test.sh, except that the shell /bin/bash is used to execute our script file.

So, in fact, it is also possible to use dash test.sh', it just depends on which shell you want to use to execute the script,

However, sh, bash, and dash are slightly different. For some keywords such as let, bash supports them, but sh and dash do not. For some keywords, choose to use bash.

(3) use point. execute

Before using this method, you must add execution permissions to the file:

  chmod +x test.sh

After adding the execution permission, you can use ./test.sh to execute the script, which is the same as bash test.sh. By default, bin/bash is used to execute our script.

Only this execution method needs to add execution permission to the file, and other methods do not.

(4) Execute using source

Using source can also execute our script directly:

  source test.sh

the difference

When we use sh test.sh, bash test.sh, ./test.sh to execute the script, the test.sh running script will use a new shell environment to execute the commands in the script,

That is to say, when using these three methods, the script is actually executed in the shell of the child process. When the child process is completed, the variables and operations in the child process will end and will not be passed back to the parent process.

Can you understand? See the following example:

  1. [root@ubuntu]# bash test.sh
  2. Please input your first name: yao <==输入firstname
  3. Please input your last name: pentonBin <==输入lastname
  4. Your full name is: yao pentonBin
  5. [root@ubuntu] # echo $firstname
  6. <==no output here

What if you use the source method to execute the script?

  1. [root@ubuntu] # source test.sh
  2. Please input your first name: yao <==输入firstname
  3. Please input your last name: pentonBin <==输入lastname
  4. Your full name is: yao pentonBin
  5. [root@ubuntu] # echo $firstname
  6. yao <== output firstname here

That is to say, the execution script of the source method is executed in the parent process, and all operations of test.sh will take effect in the original shell.

This is why you need to use source ~/.bashrc instead of bash ~/.bashrc when you want to make certain settings written in ~/.bashrc take effect without logging out of the system

2. Write the .sh file

(1) Define a variable : the variable name does not add a dollar sign.
like:

Num=1
Var2="hello world!"

 Note: There cannot be a space between the variable name and the equal sign.

(2) Using variables : To use a defined variable, just add a dollar sign in front of the variable name.
For example:

#!/bin/sh
a="hello world!"
num=2
echo "a is : $a num is : ${num}nd"

Running result:
a is : hello world! num is : 2nd
Explanation: The curly braces outside the variable name are optional, you can add them or not. The curly braces are added to help the interpreter identify the boundaries of variables. We usually add curly braces .

(3) Passing parameters
We can pass parameters to the script when executing the shell script, and the format of getting the parameters in the script is: $n.
n represents a number, 1 is the first parameter to execute the script, 2 is the second parameter to execute the script, and so on... It is
worth noting that $0 gets the script path and script name, which are obtained in order Parameters,
when there are more than 10 parameters (including 10), you need to use ${10}, ${11}.... to get the parameters,
but generally there are rarely more than 10 parameters.

Create script file test.sh

#!/bin/bash
echo "脚本$0"
echo "第一个参数$1"
echo "第二个参数$2"

 It can be called when calling: sh test.sh 2 3, the output is as follows:
script test.sh
first parameter 2
second parameter 3

(4) echo command : echo is used to output strings, often used to observe system variables path.
Display common strings: echo "learn linux"
Display system library path: echo $PATH PATH is a system variable, the same as the environment variable under windows, which stores the
search path of the default library

(5) Shell script file traversal directory
problem: folder /tmp traverse

#!/bin/bash
for i in /tmp/*
do
  echo "Hello, $i"
done

 

 

Guess you like

Origin blog.csdn.net/baidu_38493460/article/details/130379413