极其简单的 bash 脚本帮助
#!/bin/sh
for i in {0..999}
do
if [$i lt 10]
then
nohup java BeginIndex ~actors/00{$i} ~/index_new/ > ~/results/actors_results/00{$i}.txt
echo "INDEXING ($i)th directory completed "
elif [$i lt 100]; then
nohup java BeginIndex ~/actors/0{$i} ~/index_new/ > ~/results/actors_results/0{$i}.txt
echo "INDEXING ($i)th directory completed "
else
nohup java BeginIndex ~/actors/{$i} ~/index_new/ > ~/results/actors_results/{$i}.txt
echo "INDEXING ($i)th directory completed "
fi
done
谁能告诉我这个脚本有什么问题吗?我需要先执行 #!/bin/bash 吗?另外,我可以按照编写的方式 nohup 这个脚本还是必须删除 if-else 语句中的 nohup ?我真的不知道 bash 的 if-else 和 for 循环的所有语法...抱歉,如果这是一个如此简单的问题。我很感激你的帮助!
编辑(发布评论更新)
我正在尝试为一个名为 actor 的目录建立索引,该目录有 1000 个子目录,其中包含文件。 java程序会运行,但是它无法处理这1000个目录(大约180万个)中的所有文件。我想编写一个脚本,一次执行子目录操作。
我正在通过 ssh 连接到服务器来执行此操作,但这需要一段时间,所以我希望它不会丢失。我不能只在整个目录上运行java程序,因为它耗尽了堆内存。这不是最有效的java代码,但我正在研究该问题的另一种解决方案,同时该脚本一次运行子目录。
#!/bin/sh
for i in {0..999}
do
if [$i lt 10]
then
nohup java BeginIndex ~actors/00{$i} ~/index_new/ > ~/results/actors_results/00{$i}.txt
echo "INDEXING ($i)th directory completed "
elif [$i lt 100]; then
nohup java BeginIndex ~/actors/0{$i} ~/index_new/ > ~/results/actors_results/0{$i}.txt
echo "INDEXING ($i)th directory completed "
else
nohup java BeginIndex ~/actors/{$i} ~/index_new/ > ~/results/actors_results/{$i}.txt
echo "INDEXING ($i)th directory completed "
fi
done
Can anyone tell me what's wrong with this script? do I need to do #!/bin/bash first? Also, can i nohup this script the way it's written or do I have to remove the nohup in the if-else statements? I don't really know all the syntax of if-else and for loops for bash... sorry if this is such a simple question. I appreciate the help!
Edit (posting an update from comments)
I'm trying to index a directory called actors, with 1000 subdirectories with files inside them. the java program will run, but it can't handle all the files in these 1000 directories (about 1.8million). I want to write a script that will do it subdirectory at a time.
I'm ssh'ing into a server to do this, but it will take a while so I want it to not drop. I can't just run the java program on the whole directory, because it runs out of heap memory. It's not the most efficient java code, but I'm working on another solution to that problem while this script runs subdirectory-at-a-time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
test 命令肯定是错误的。
请记住 bash 中的
"["
是命令,而不是语法。它期望字符串"]"
作为最后一个参数。这就是为什么在它们之前和之后需要空格。 (分号是可以的,因为它是语法的一部分)此外,正如 Kerrek 指出的,比较运算符是
-lt
而不是lt
。The test command is definitely wrong.
Just remember
"["
in bash is a command, not a syntax. It expects the string"]"
as the last argument. This is why spaces are required before and after them. (Semicolon is OK, because it is a part of the syntax)Also, as Kerrek pointed out, the comparation operator is
-lt
notlt
.测试函数在选项前需要一个连字符:
注意,
[
只是一个普通命令,通常在/usr/bin/[
中,必须对其进行处理像这样。The test functions need a hyphen in front of the option:
Note that
[
is just an ordinary command, usually in/usr/bin/[
, and it has to be treated as such.