Joh*_*ash 5 shell search recursive busybox files
我如何搜索字符串的文件递归BusyBox中1.0如果find与grep不可用?
(我想通过 telnet 进入路由器并找出iptables存储规则的位置。)
BusyBox v1.00 (2011.01.13-12:30+0000) 内置外壳 (msh)
输入“help”以获取内置命令列表。
# 帮助
内置命令:
-------------------
. : break cd continue eval exec exit export help login newgrp
只读 readonly set shift times trap umask wait [ busybox cat chmod
cp 日期 dmesg echo expr false ftpget ftpput 主机名 ifconfig
init insmod kill killall klogd linuxrc ln 记录器 logread ls mkdir
mknod mount msh mv ping ps pwd reboot renice rm rmmod route sed
sendarp sh sysinfo syslogd 测试 tftp top traceroute true tty
卸载 vconfig wget
我滚动了这个脚本,它从当前目录进行递归模式搜索。它使用busybox的sh和sed。用busybox 1.17.1测试;您的里程可能会在 1.00 上有所不同。
#!/bin/busybox sh
sed="busybox sed"
search_in()
{
searchterm="$1"
searchdir="$2"
prefix="$3"
(
cd "$searchdir"
for file in *
do
if [ -d "$file" ]
then
# recurse into subdirectory
search_in "$searchterm" "$file" "$prefix\\/$file"
else
# use sed like grep
$sed -rn '/'"$searchterm"'/s/(.*)/'"$prefix\\/$file"': \1/gp' "$file"
fi
done
)
}
# search for command-line search term, starting in current directory (`.`)
search_in "$1" . "."
Run Code Online (Sandbox Code Playgroud)