bash 如何在android shell中制作一行for循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22343856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make one line for loop in android shell
提问by xdan
This works on bash
这适用于 bash
for i in {1..5}; do echo $i; done
the out put is 1 2 3 4 5 But on android shell the output is {1..5}
输出为 1 2 3 4 5 但在 android shell 上输出为 {1..5}
回答by j1s1e1
i=0; while [ $(($i)) -le 5 ]; do i=$(($i + 1)); echo $i; done;
Tested on adb shell in Galaxy Tab 4 7" -- Thanks to Lynch for most of the answer. 'expr fails in default shell.
在 Galaxy Tab 4 7" 中的 adb shell 上测试--感谢 Lynch 提供的大部分答案。'expr 在默认 shell 中失败。
回答by Sahil Mahajan Mj
Have you tried,
你有没有尝试过,
for ((i=1; i<=5; i++)) do echo $i; done
Edit-
编辑-
or you can use sequence,
或者你可以使用序列,
for i in `seq 1 5`; do echo $i; done
回答by scottt
If your sequence isn't too large, you can just list out the values of interest. So in the default Android ADB shell:
如果您的序列不是太大,您可以只列出感兴趣的值。所以在默认的 Android ADB shell 中:
for i in 1 2 3; do echo $i; done
Returns:
1
2
3
or:
或者:
for i in 5 11 7; do echo $i; done
Returns:
5
11
7
or even:
甚至:
for i in apple orange banana; do echo $i; done
Returns:
apple
orange
banana
回答by Lynch
If your shell is sh then use the following command:
如果您的 shell 是 sh,则使用以下命令:
for i in `seq 1 5`; do echo $i; done
Edit
编辑
You probably dont have the seq
program. Try this command instead:
你可能没有这个seq
程序。试试这个命令:
i=1; while [ $i -le 5 ] ; do echo $i; i=`expr $i + 1`; done