2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

サブプロセスで使用する変数が与える親プロセスへの影響

Last updated at Posted at 2021-08-26

結論から言うと当然ながら影響しないのでした。
勿論、そのはずと思っていたのですが、念の為に変数あるいはワーキングディレクトリに関して、そのスコープについて確認しました。

ykishi@dezembro httpd_log % beautysh -i 2 testme.sh 
ykishi@dezembro httpd_log % cat testme.sh 
#!/bin/sh

BASE_DIR=/tmp
cd ${BASE_DIR}

n=0
i=0


echo `pwd`

(
  echo "Sub process A has started..."
  sleep 7
  echo "A done"
  for i in `seq 1 10`; do
    let n=${n}+1
  done
  echo "n=${n}"
cd /tmp/bar; echo `pwd`) &

(
  echo "Sub process B has started..."
  for i in `seq 1 20`; do
    let n=${n}+1
  done
  echo "n=${n}"
  sleep 5; echo "B done"
cd ${BASE_DIR}; echo `pwd`) &

(
  echo "Sub process C has started..."
  for i in `seq 1 30`; do
    let n=${n}+1
  done
  echo "n=${n}"
  sleep 3;
  echo "C done"
cd /tmp/foo; echo `pwd`) &

wait

echo "Completed!"
cat << EOF

n=${n}
i=${i}
`pwd`
EOF
ykishi@dezembro httpd_log % ./testme.sh 
/tmp
Sub process A has started...
Sub process B has started...
Sub process C has started...
n=20
n=30
C done
/tmp/foo
B done
/tmp
A done
n=10
/tmp/bar
Completed!

n=0
i=0
/tmp
ykishi@dezembro httpd_log % 
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?