[英]Shell script: variable scope in functions
I wrote a quick shell script to emulate the situation of xkcd #981 (without hard links, just symlinks to parent dirs) and used a recursive function to create all the directories. 我编写了一个快速的Shell脚本来模拟xkcd#981的情况(没有硬链接,只是到父目录的符号链接),并使用了递归函数来创建所有目录。 Unfortunately this script does not provide the desired result, so I think my understanding of the scope of variable $count is wrong.
不幸的是,该脚本无法提供理想的结果,因此我认为我对变量$ count的范围的理解是错误的。
How can I properly make the function use recursion to create twenty levels of folders, each containing 3 folders (3^20 folders, ending in soft links back to the top)? 我如何正确地使函数使用递归来创建二十个级别的文件夹,每个级别包含3个文件夹(3 ^ 20个文件夹,以软链接结尾回到顶部)?
#!/bin/bash
echo "Generating folders:"
toplevel=$PWD
count=1
GEN_DIRS() {
for i in 1 2 3
do
dirname=$RANDOM
mkdir $dirname
cd $dirname
count=$(expr $count + 1)
if [ $count < 20 ] ; then
GEN_DIRS
else
ln -s $toplevel "./$dirname"
fi
done
}
GEN_DIRS
exit
Try this (amended version of the script) — it seems to work for me. 试试这个(脚本的修订版)-似乎对我有用。 I decline to test to 20 levels deep, though;
我拒绝测试到20个深度。 at 8 levels deep, each of the three top-level directories occupies some 50 MB on a Mac file system.
在8个层级的深度下,三个顶层目录中的每个目录在Mac文件系统上都占用约50 MB的空间。
#!/bin/bash
echo "Generating folders:"
toplevel=$PWD
GEN_DIRS()
{
cur=${1:?}
max=${2:?}
for i in 1 2 3
do
dirname=$RANDOM
if [ $cur -le $max ]
then
(
echo "Directory: $PWD/$dirname"
mkdir $dirname
cd $dirname
GEN_DIRS $((cur+1)) $max
)
else
echo "Symlink: $PWD/$dirname"
ln -s $toplevel "./$dirname"
fi
done
}
GEN_DIRS 1 ${1:-4}
Lines 6 and 7 are giving names to the positional parameters ( $1
and $2
) passed to the function — the ${1:?}
notation simply means that if you omit to pass a parameter $1
, you get an error message from the shell (or sub-shell) and it exits. 第6行和第7行为传递给函数的位置参数(
$1
和$2
)起了名字- ${1:?}
表示法只是意味着,如果忽略传递参数$1
,则会从shell收到错误消息(或子外壳),然后退出。
The parentheses on their own (lines 13 and 18 above) mean that the commands in between are run in a sub-shell, so changes in directory inside the sub-shell do not affect the parent shell. 括号本身(上面的第13和18行)意味着它们之间的命令在子shell中运行,因此子shell中目录的更改不会影响父shell。
The condition on line 11 now uses arithmetic ( -le
) instead of string <
comparisons; 现在,第11行的条件使用算术(
-le
)而不是字符串<
比较; this works better for deep nesting (because the <
is a lexicographic comparison, so level 9 is not less than level 10). 这对于深度嵌套更有效(因为
<
是字典比较,所以9级不小于10级)。 It also means that the [
command is OK to use instead of the [[
command (although [[
would also work, I prefer the old-fashioned notation). 这也意味着可以使用
[
命令而不是[[
命令(尽管[[
也可以,但是我更喜欢老式的表示法))。
I end up creating a script like this: 我最终创建了这样的脚本:
#!/bin/bash
echo "Generating folders:"
toplevel=$PWD
level=0
maxlevel=4
function generate_dirs {
pushd "$1" >/dev/null || return
(( ++level ))
for i in 1 2 3; do
dirname=$RANDOM
if (( level < maxlevel )); then
echo "$PWD/$dirname"
mkdir "$dirname" && generate_dirs "$dirname"
else
echo "$PWD/$dirname (link to top)"
ln -sf "$toplevel" "$dirname"
fi
done
popd >/dev/null
(( --level ))
}
generate_dirs .
exit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.