shell执行脚本的方法

执行脚本的方法
1 bash ./filename.sh (产生子进程,再运行,使用当前指定的 bash shell 去运行)
2 ./filename.sh (产生子进程,再运行,使用脚本里面指定的 shell 去运行。使用该种方式执行需要 x
权限)
3 source ./filename.sh source 命令是一个 shell 内部命令,其功能是读取指定的 shell 程序文件,并
且依次执行其中的所有的语句,并没有创建新的子 shell 进程,所以脚本里面所有创建的变量都会保存到
当前的 shell 里面)
4 . ./filename.sh (和 source 一样,也是使用当前进程执行)
[root@localhost test] # vim test2.sh
#!/bin/bash
cd /tmp
pwd
[root@localhost test] # ls -l test2.sh
-rw-r--r-- . 1 root root 24 Apr 30 20 :09 test2.sh
(1)[root@localhost test] # bash test2.sh
/tmp
(2)[root@localhost test] # ./test2.sh
-bash : ./test2.sh: Permission denied
[root@localhost test] # chmod a+rx test2.sh
[root@localhost test] # ./test2.sh
/tmp
(3)[root@localhost test] # source test2.sh
/tmp
[root@localhost tmp] #
(4)[root@localhost test] # . test2.sh
/tmp
[root@localhost tmp] #
执行 shell 脚本时,如果使用 1 2 这种方式执行会在当前的进程下产生一个新的 bash 子进程,所以子进程
切换到了 /tmp 目录,当脚本结束,子进程也就结束了,所以当前进程的目录不会发生变化; 3 4 方式执
行时,不会产生新的进程,所以脚本执行结束后当前的目录会变成 /tmp
[root@localhost test] # echo 'userdir=`pwd`' > test3.sh
[root@localhost test] # cat test3.sh
userdir = `pwd`
(1)[root@localhost test] # bash test3.sh
[root@localhost test] # echo $userdir
[root@localhost test] #
(2)[root@localhost test] # chmod a+rx test3.sh
[root@localhost test] # ./test3.sh
[root@localhost test] # echo $userdir
[root@localhost test] #
(3)[root@localhost test] # source test3.sh
[root@localhost test] # echo $userdir
/test
(4)[root@localhost test] # . test3.sh
[root@localhost test] # echo $userdir
/test
  • 45
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值