简单 bash 递归歧义
Simple bash recursion ambiguity
我正在编写非常简单的 bash 脚本来更改编码
.html 文件并希望递归处理目录。
该功能仅适用于一级目录。
你能告诉我哪里错了吗?这是我的代码。
#!/bin/bash
handleFiles () {
local REGEXP='.+\.html
测试目录结构如下:
test$ tree
.
├── test.html
└── Untitled Folder
└── test1.html
1 directory, 2 files
当我运行脚本时,我得到以下输出:
./converter.sh
test
Converting test/test.html
test/Untitled Folder
作为整个故事我post我的最终解决方案。我希望这对有类似问题的人有用。
#!/bin/bash
########################################################
# This bash script assume directory as a argument
# and convert all .html,js and xml files from
# windows-1251 encoding into utf-8 encoding.
# @author Georgi Naumov
# @email gonaumov@gmail.com for contacts and
# suggestions.
########################################################
if [[ $# -ne 1 ]] ; then
echo "Usage [=14=] <<directory to change encoding reqursively>>"
exit 1
fi
handleFiles () {
local REGEXP='.+\.(html|js|xml)
echo
for f in /*
do
if [[ -d $f ]]
then
handleFiles "$f"
elif [[ $f =~ $REGEXP ]]
then
echo "Converting $f"
enconv -L bg -x UTF-8 "$f"
fi
done
}
# The script show all .html files in test
# enter into subdirectory but not working ..
handleFiles "test"
测试目录结构如下:
test$ tree
.
├── test.html
└── Untitled Folder
└── test1.html
1 directory, 2 files
当我运行脚本时,我得到以下输出:
./converter.sh
test
Converting test/test.html
test/Untitled Folder
作为整个故事我post我的最终解决方案。我希望这对有类似问题的人有用。
#!/bin/bash
########################################################
# This bash script assume directory as a argument
# and convert all .html,js and xml files from
# windows-1251 encoding into utf-8 encoding.
# @author Georgi Naumov
# @email gonaumov@gmail.com for contacts and
# suggestions.
########################################################
if [[ $# -ne 1 ]] ; then
echo "Usage [=14=] <>"
exit 1
fi
handleFiles () {
local REGEXP='.+\.(html|js|xml)$'
for f in ""/*
do
if [[ -d "$f" ]]
then
handleFiles "$f"
elif [[ "$f" =~ $REGEXP ]]
then
echo "Converting $f"
enconv -L bg -x UTF-8 "$f"
fi
done
}
handleFiles ""
for f in ""/*
do
if [[ -d "$f" ]]
then
handleFiles "$f"
elif [[ "$f" =~ $REGEXP ]]
then
echo "Converting $f"
enconv -L bg -x UTF-8 "$f"
fi
done
}
handleFiles ""
echo
for f in /*
do
if [[ -d $f ]]
then
handleFiles "$f"
elif [[ $f =~ $REGEXP ]]
then
echo "Converting $f"
enconv -L bg -x UTF-8 "$f"
fi
done
}
# The script show all .html files in test
# enter into subdirectory but not working ..
handleFiles "test"
测试目录结构如下:
test$ tree
.
├── test.html
└── Untitled Folder
└── test1.html
1 directory, 2 files
当我运行脚本时,我得到以下输出:
./converter.sh
test
Converting test/test.html
test/Untitled Folder
作为整个故事我post我的最终解决方案。我希望这对有类似问题的人有用。
#!/bin/bash
########################################################
# This bash script assume directory as a argument
# and convert all .html,js and xml files from
# windows-1251 encoding into utf-8 encoding.
# @author Georgi Naumov
# @email gonaumov@gmail.com for contacts and
# suggestions.
########################################################
if [[ $# -ne 1 ]] ; then
echo "Usage [=14=] <>"
exit 1
fi
handleFiles () {
local REGEXP='.+\.(html|js|xml)$'
for f in ""/*
do
if [[ -d "$f" ]]
then
handleFiles "$f"
elif [[ "$f" =~ $REGEXP ]]
then
echo "Converting $f"
enconv -L bg -x UTF-8 "$f"
fi
done
}
handleFiles ""
整个方法比必要的要复杂得多。我建议使用 find
:
find test -name '*.html' -exec enconv -L bg -x UTF-8 '{}' \;
如果你想手动做,你必须把 $f
放在双引号中(即 "$f"
),否则如果目录包含空格它会中断,正如你所注意到的,因为 shell 会将其扩展为两个(或更多,视情况而定)单独的标记。
还引用 </code> 来处理空格(例如 <code>Untitled Folder
):
for f in ""/*
我正在编写非常简单的 bash 脚本来更改编码 .html 文件并希望递归处理目录。 该功能仅适用于一级目录。 你能告诉我哪里错了吗?这是我的代码。
#!/bin/bash
handleFiles () {
local REGEXP='.+\.html
测试目录结构如下:
test$ tree
.
├── test.html
└── Untitled Folder
└── test1.html
1 directory, 2 files
当我运行脚本时,我得到以下输出:
./converter.sh
test
Converting test/test.html
test/Untitled Folder
作为整个故事我post我的最终解决方案。我希望这对有类似问题的人有用。
#!/bin/bash
########################################################
# This bash script assume directory as a argument
# and convert all .html,js and xml files from
# windows-1251 encoding into utf-8 encoding.
# @author Georgi Naumov
# @email gonaumov@gmail.com for contacts and
# suggestions.
########################################################
if [[ $# -ne 1 ]] ; then
echo "Usage [=14=] <<directory to change encoding reqursively>>"
exit 1
fi
handleFiles () {
local REGEXP='.+\.(html|js|xml)
echo
for f in /*
do
if [[ -d $f ]]
then
handleFiles "$f"
elif [[ $f =~ $REGEXP ]]
then
echo "Converting $f"
enconv -L bg -x UTF-8 "$f"
fi
done
}
# The script show all .html files in test
# enter into subdirectory but not working ..
handleFiles "test"
测试目录结构如下:
test$ tree
.
├── test.html
└── Untitled Folder
└── test1.html
1 directory, 2 files
当我运行脚本时,我得到以下输出:
./converter.sh
test
Converting test/test.html
test/Untitled Folder
作为整个故事我post我的最终解决方案。我希望这对有类似问题的人有用。
#!/bin/bash
########################################################
# This bash script assume directory as a argument
# and convert all .html,js and xml files from
# windows-1251 encoding into utf-8 encoding.
# @author Georgi Naumov
# @email gonaumov@gmail.com for contacts and
# suggestions.
########################################################
if [[ $# -ne 1 ]] ; then
echo "Usage [=14=] <>"
exit 1
fi
handleFiles () {
local REGEXP='.+\.(html|js|xml)$'
for f in ""/*
do
if [[ -d "$f" ]]
then
handleFiles "$f"
elif [[ "$f" =~ $REGEXP ]]
then
echo "Converting $f"
enconv -L bg -x UTF-8 "$f"
fi
done
}
handleFiles ""
for f in ""/*
do
if [[ -d "$f" ]]
then
handleFiles "$f"
elif [[ "$f" =~ $REGEXP ]]
then
echo "Converting $f"
enconv -L bg -x UTF-8 "$f"
fi
done
}
handleFiles ""
echo
for f in /*
do
if [[ -d $f ]]
then
handleFiles "$f"
elif [[ $f =~ $REGEXP ]]
then
echo "Converting $f"
enconv -L bg -x UTF-8 "$f"
fi
done
}
# The script show all .html files in test
# enter into subdirectory but not working ..
handleFiles "test"
测试目录结构如下:
test$ tree
.
├── test.html
└── Untitled Folder
└── test1.html
1 directory, 2 files
当我运行脚本时,我得到以下输出:
./converter.sh
test
Converting test/test.html
test/Untitled Folder
作为整个故事我post我的最终解决方案。我希望这对有类似问题的人有用。
#!/bin/bash
########################################################
# This bash script assume directory as a argument
# and convert all .html,js and xml files from
# windows-1251 encoding into utf-8 encoding.
# @author Georgi Naumov
# @email gonaumov@gmail.com for contacts and
# suggestions.
########################################################
if [[ $# -ne 1 ]] ; then
echo "Usage [=14=] <>"
exit 1
fi
handleFiles () {
local REGEXP='.+\.(html|js|xml)$'
for f in ""/*
do
if [[ -d "$f" ]]
then
handleFiles "$f"
elif [[ "$f" =~ $REGEXP ]]
then
echo "Converting $f"
enconv -L bg -x UTF-8 "$f"
fi
done
}
handleFiles ""
整个方法比必要的要复杂得多。我建议使用 find
:
find test -name '*.html' -exec enconv -L bg -x UTF-8 '{}' \;
如果你想手动做,你必须把 $f
放在双引号中(即 "$f"
),否则如果目录包含空格它会中断,正如你所注意到的,因为 shell 会将其扩展为两个(或更多,视情况而定)单独的标记。
还引用 </code> 来处理空格(例如 <code>Untitled Folder
):
for f in ""/*