前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Bash脚本编程之subshell

Bash脚本编程之subshell

作者头像
枇杷李子橙橘柚
发布2019-05-26 11:06:18
8060
发布2019-05-26 11:06:18
举报
文章被收录于专栏:没有擅长的YcY没有擅长的YcY

(command1;command2;command3;...)会启动子shell。子shell可以访问父shell的变量,对父shell变量的改动只在子shell中有效;子shell中定义的变量是局部变量,外部不能访问:

代码语言:javascript
复制
#!/bin/bash
# subshell.sh

echo "We are outside the subshell."
echo "Subshell level OUTSIDE subshell = $BASH_SUBSHELL"
echo; echo

outer_variable=Outer
global_variable=

(
echo "We are inside the subshell."
echo "Subshell level INSIDE subshell = $BASH_SUBSHELL"

inner_variable=Inner
global_variable="$inner_variable"

echo "From inside subshell, \"inner_variable\" = $inner_variable"
echo "From inside subshell, \"outer\" = $outer_variable"
)

echo; echo
echo "We are outside the subshell."
echo "Subshell level OUTSIDE subshell = $BASH_SUBSHELL"
echo "From main body of shell, \"inner_variable\" = $inner_variable"
#  $inner_variable will show as blank (uninitialized)
#+ because variables defined in a subshell are "local variables".
echo "global_variable = "$global_variable""

echo

# =======================================================================

# Additionally ...

echo "-----------------"; echo

var=41                                                 # Global variable.

( let "var+=1"; echo "\$var INSIDE subshell = $var" )  # 42

echo "\$var OUTSIDE subshell = $var"                   # 41
#  Variable operations inside a subshell, even to a GLOBAL variable
#+ do not affect the value of the variable outside the subshell!


exit 0

在子shell中对目录的改变不会影响父shell:

代码语言:javascript
复制
#!/bin/bash
# allprofs.sh: Print all user profiles.

FILE=.bashrc  #  File containing user profile.

for home in `awk -F: '{print $6}' /etc/passwd`
do
  [ -d "$home" ] || continue    # If no home directory, go to next.
  [ -r "$home" ] || continue    # If not readable, go to next.
  (cd $home; [ -e $FILE ] && cat $FILE)
done

#  When script terminates, there is no need to 'cd' back to original directory,
#+ because 'cd $home' takes place in a subshell.

exit 0

程序可以在不同的子shell中并行执行:

代码语言:javascript
复制
#!/bin/bash
# subshell.sh

# 在后台运行以确保并行执行
(ping -c 10 127.0.0.1 > /dev/null) &
(ping -c 20 127.0.0.1 > /dev/null) &

# 等同于:
# ping -c 10 127.0.0.1 > /dev/null &
# ping -c 20 127.0.0.1 > /dev/null &

# 通过ps可以发现两条子命令都是当前脚本启动的子shell,拥有不同的进程ID

# 直到子shell执行完成才执行后续命令
wait

echo "finished"

I/O重定向到子shell使用管道操作符,例如ls -al | (command)

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档