- 论坛徽章:
- 0
|
本帖最后由 little_tony 于 2014-08-27 15:02 编辑
一,我的top目录是:/home/ftp-mxy/test-dir,其目录结构为:
ftp-mxy@ubuntu:~/test-dir$ tree .
.
├── config2
├── config3
├── log
├── test-subdir1
│ └── log
└── test-subdir2
二,一个验证脚本:config2
#!/bin/sh
export CURDIR="$(/bin/pwd)"
echo "当前目录是:$CURDIR"
TOPDIR=/home/ftp-mxy/test-dir
DIR1=/home/ftp-mxy/test-dir/test-subdir1
DIR2=/home/ftp-mxy/test-dir/test-subdir2
for valdir in $DIR1 $DIR2; do
(
export CURDIR="$(/bin/pwd)"
echo "FOR语句中,IF语句之前,valdir=$valdir当前目录是:$CURDIR"
if [ -f $valdir/log ]; then
tempdir=$valdir
cd $tempdir
export CURDIR="$(/bin/pwd)"
echo "FOR语句中,IF语句之中,valdir=$valdir当前目录是:$CURDIR"
$tempdir/log
fi
export CURDIR="$(/bin/pwd)"
echo "FOR语句中,IF语句之后,valdir=$valdir当前目录是:$CURDIR"
)
done
export CURDIR="$(/bin/pwd)"
echo "FOR之后,IF语句之后,当前目录是:$CURDIR"
三,log文件脚本内容:
#!/bin/sh
echo "---------------这是test-dir1的log文件"
四:运行结果:
ftp-mxy@ubuntu:~/test-dir$ ./config2
当前目录是:/home/ftp-mxy/test-dir
FOR语句中,IF语句之前,valdir=/home/ftp-mxy/test-dir/test-subdir1当前目录是:/home/ftp-mxy/test-dir
FOR语句中,IF语句之中,valdir=/home/ftp-mxy/test-dir/test-subdir1当前目录是:/home/ftp-mxy/test-dir/test-subdir1
---------------这是test-dir1的log文件
FOR语句中,IF语句之后,valdir=/home/ftp-mxy/test-dir/test-subdir1当前目录是:/home/ftp-mxy/test-dir/test-subdir1
FOR语句中,IF语句之前,valdir=/home/ftp-mxy/test-dir/test-subdir2当前目录是:/home/ftp-mxy/test-dir
FOR语句中,IF语句之后,valdir=/home/ftp-mxy/test-dir/test-subdir2当前目录是:/home/ftp-mxy/test-dir
FOR之后,IF语句之后,当前目录是:/home/ftp-mxy/test-dir
ftp-mxy@ubuntu:~/test-dir$
五:结果:
由上面的运行结果第一次FOR循环,cd进了目录test-subdir1
可是IF语句结束之后,第二次FOR循环开始时怎么当前目录又变为顶层test-dir
请问shell脚本中有什么这方面的规律与机制吗? |
|