Getting Started with Linux Shell Scripting

Check what shell interpreter the system has: cat /etc/shells 

 CentOs uses bash by default: echo $SHELL

Getting Started with Shell Scripting

1. Script format:

The script should start with   #!/bin/bash   to specify the parser

Create the first script hello.sh:

#  编写脚本  也可以不写.sh指定后缀
vim hello.sh
#!/bin/bash      
echo hello world
echo "hello world bash " >> lxw.txt

The first line    #!/bin/bash    is used to specify the parser bash

The second line outputs a line hello world

The third line appends hello world bash to the lxw.txt file

Execute the script:

bash script name          

sh script name

 filename execute script ./hello.sh

It will be found that the permissions are not enough, because the current user permissions are read and write  rw- , and there is no executable permission x

We need to change the script file permissions

# 方式一hello.sh 脚本的+x 权限
chmod +x hello.sh
#  方式二   二进制更改
chmod 744 hello.sh

It is found that after changing the permissions, the script name can be used to execute the script directly

source hello.sh . hello.sh execution script 

 The . script name  here is different from the previous ./. ./ is a relative path. The . here is the shell embedded command  type source  

The difference between source and bash sh

The first two methods (bash, sh) open a subshell in the current shell to execute the script content. When the script content ends, the subshell closes and returns to the parent shell .
The third way is to add "." or source before the script path , so that the script content can be executed in the current shell without opening a subshell ! This is why we need to source it every time we modify the /etc/profile file .
The difference between opening a subshell and not opening a subshell is that the inheritance relationship of environment variables, such as the setting in the subshell
Current variable, parent shell is invisible

 

Guess you like

Origin blog.csdn.net/weixin_46310452/article/details/126450730