shell的关系
凌乱爱石头 ·2021-09-18 ·2 次阅读
段落引用shell的关系最终目的是为了操作内核(kernel),
#vim hellworld.sh 编写脚本
> (#! /bin/bash
> echo 'Hell world'
)
#ll 查看
total 8
-rw-------. 1 root root 1259 Feb 13 2021 anaconda-ks.cfg
-rw-r--r--. 1 root root 32 Sep 18 02:46 hellworld.sh
#chmod ug+x hellworld.sh
#ll
>total 8
>-rw-------. 1 root root 1259 Feb 13 2021 anaconda-ks.cfg
-rwxr-xr--. 1 root root 32 Sep 18 02:46 hellworld.sh
>#cp hellworld.sh hi.sh
#ll
total 12
-rw-------. 1 root root 1259 Feb 13 2021 anaconda-ks.cfg
-rwxr-xr--. 1 root root 32 Sep 18 02:46 hellworld.sh
-rwxr-xr--. 1 root root 32 Sep 18 02:53 hi.sh
> #chmod ug-x hi.sh
#ll
total 12
-rw-------. 1 root root 1259 Feb 13 2021 anaconda-ks.cfg
-rwxr-xr--. 1 root root 32 Sep 18 02:46 hellworld.sh
-rw-r--r--. 1 root root 32 Sep 18 02:53 hi.sh
vi hi.sh
(
#! /bin/bash
echo 'Hi world'
~
)
>#ll
total 12
-rw-------. 1 root root 1259 Feb 13 2021 anaconda-ks.cfg
-rwxr-xr--. 1 root root 32 Sep 18 02:46 hellworld.sh
-rw-r--r--. 1 root root 30 Sep 18 02:54 hi.sh
#./hi.sh 没有执行权限
-bash: ./hi.sh: Permission denied
[root@localhost ~]# sh hi.sh (确定是执行文件./不确定就sh)
Hi world
[root@localhost ~]#
source 可调用
(绝对路径,必须是可执行文件 ./hellworld.sh
(sh 普通文件脚本文件都可以 sh hellwolrd.sh
(source 两者皆可 source hellwolrd.sh
默认在当前进程执行脚本,
uname=admin 变量
echo $uname 打印变量
admin
>#! /bin/bash
>echo 'Hell world'
echo suname
echo $age
1)# ./hellworld.sh 3种方法,出现不同结果
Hell world
>2)# sh hellworld.sh
Hell world
>3)# source hellworld.sh
Hell world
admin
>修改 vim hellworld.sh
#! /bin/bash
>echo 'Hell world'
echo $uname
ping www.baidu.com
~
#ping www.baidu.com
#ps -ef 查看进程 绝对路径
./hellworld.sh ps -ef 查看进程
root 1877 1506 0 03:27 pts/0 00:00:00 -bash
root 1878 1877 0 03:27 pts/0 00:00:00 /bin/bash
root 1893 2 0 03:35 ? 00:00:00 [kworker/1:3]
root 1897 2 0 03:40 ? 00:00:00 [kworker/1:0]
root 1898 994 1 03:42 ? 00:00:00 sshd: root@pts/1
root 1902 994 1 03:42 ? 00:00:00 sshd: root@notty
root 1906 1902 0 03:42 ? 00:00:00 /usr/libexec/openssh/sftp-server
root 1913 1898 0 03:42 pts/1 00:00:00 -bash
root 1930 1913 0 03:42 pts/1 00:00:00 /bin/bash ./hellworld.sh
root 1931 1930 0 03:42 pts/1 00:00:00 ping www.baidu.com
root 1932 1878 0 03:43 pts/0 00:00:00 ps -ef
>=====================================================
sh hellworld.sh 然后ps -ef
root 1877 1506 0 03:27 pts/0 00:00:00 -bash
root 1878 1877 0 03:27 pts/0 00:00:00 /bin/bash
root 1897 2 0 03:40 ? 00:00:00 [kworker/1:0]
root 1898 994 0 03:42 ? 00:00:00 sshd: root@pts/1
root 1902 994 0 03:42 ? 00:00:00 sshd: root@notty
root 1906 1902 0 03:42 ? 00:00:00 /usr/libexec/openssh/sftp-server
root 1913 1898 0 03:42 pts/1 00:00:00 -bash
root 1933 2 0 03:45 ? 00:00:00 [kworker/1:3]
root 1934 1913 0 03:47 pts/1 00:00:00 sh hellworld.sh
root 1935 1934 0 03:47 pts/1 00:00:00 ping www.baidu.com
root 1936 1878 0 03:47 pts/0 00:00:00 ps -ef
>=======================================================
source hellworld.sh ps -ef source与前两则有区别,
root 1877 1506 0 03:27 pts/0 00:00:00 -bash
root 1878 1877 0 03:27 pts/0 00:00:00 /bin/bash
root 1898 994 0 03:42 ? 00:00:01 sshd: root@pts/1
root 1902 994 0 03:42 ? 00:00:00 sshd: root@notty
root 1906 1902 0 03:42 ? 00:00:00 /usr/libexec/openssh/sftp-server
root 1913 1898 0 03:42 pts/1 00:00:00 -bash
root 1933 2 0 03:45 ? 00:00:00 [kworker/1:3]
root 1937 2 0 03:50 ? 00:00:00 [kworker/1:0]
root 1938 1913 0 03:51 pts/1 00:00:00 ping www.baidu.com
root 1939 1878 0 03:51 pts/0 00:00:00 ps -ef
>————————————————————————————————————————
结论:绝对路径和sh 会重新打开一个进程去执行脚本文件
source 默认在当前进程执行脚本,
默认丛当前进程查询变量默认从父进程查询变量
如果用export修饰变量,那么这个变量对于子进程就是课件的
原创
shell的关系
本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。
评论交流
欢迎留下你的想法