[英]loops inside condition shell scripting
Im beginner in shell scripting and im trying to achieve following , if i pass two numbers as argument for example = 0 5 , the console will output 1 2 3 4 5 , but if i pass 5 0 , console should output 5 4 3 2 1 , the first one works nice , but the second one does nothing in my script , it does not throw any arror. 我是shell脚本的初学者,并尝试实现以下目标,如果我将两个数字作为参数传递,例如= 0 5,则控制台将输出1 2 3 4 5,但是如果我通过5 0,则控制台应输出5 4 3 2 1 ,第一个效果不错,但是第二个效果在我的脚本中什么也不做,它不会抛出任何错误。
script 脚本
if [[ $1 -lt $2 ]]; then
for((i=$1;i<$2;i++))
do
if [[ "$i" -lt $2 ]]; then
echo -n "$i "
else
echo -n "$i"
fi
done
else
for((i=$2;i>=$1;i--))
do
if [[ "$i" -gt $1 ]]; then
echo -n "$i "
else
echo -n "$i"
fi
done
fi
as i said the first condition works - numbers are 0 5 for example but the second when first number is greater than second does not . 如我所说,第一个条件有效-例如数字是0 5,但是当第一个条件大于第二个条件时第二个条件无效。 how can i fix it? 我该如何解决?
You have a logical error in your second for loop: 您的第二个for循环中存在逻辑错误:
for((i=$2;i>=$1;i--))
# should have been:
for((i=$1;i>=$2;i--))
If you exterminate your code you will see that $1
will always be bigger or equal than $2
in your else
-statement: 如果您淘汰了代码,您将在else
-statement中看到$1
始终大于或等于$2
:
if [[ $1 -lt $2 ]]; then
# ...
else
# now $1 is equal or bigger than $2
You properly want to change your if
-statement as well: 您当然也想更改if
语句:
if [[ "$i" -gt $1 ]]; then
# change to $2 because $i will never be bigger than $1
if [[ "$i" -gt $2 ]]; then
When all of this is said you should really consider using an existing tool like seq [fisrt] [increment] last
: 说完所有这些后,您应该真正考虑使用现有的工具,例如seq [fisrt] [increment] last
:
seq 1 5 | xargs; # 1 2 3 4 5
seq 5 -1 1 | xargs; # 5 4 3 2 1
This will print "1 2 3 4 5" or "4 3 2 1 0" instead of "5 4 3 2 1", but I think that is a more reasonable output. 这将打印“ 1 2 3 4 5”或“ 4 3 2 1 0”而不是“ 5 4 3 2 1”,但是我认为这是一个更合理的输出。 If you want the non-symmetric output, it's easy enough to fix. 如果您需要非对称输出,则很容易修复。
#!/bin/bash
i=$1
test $2 -gt $1 && op=+ || op=-
while test $i != $2; do printf "%d " $(( i=$i $op 1)); done
echo
There's no need for the nested if
s here -- it's more sensible to determine the test to use and the iteration detection beforehand, and then have only one loop that's always used: 有没有必要嵌套if
在这儿-这是更明智的决定测试使用和迭代检测事前,然后只有一个循环,它总是使用:
#!/bin/bash
delta=${3:-1} # if $3 is given, treat it as number to increment/decrement by
if (( $1 < $2 )); then
delta=$(( delta < 0 ? -delta : delta)); cmp=-lt
else
delta=$(( delta > 0 ? -delta : delta)); cmp=-gt
fi
i=$1
while test "$i" "$cmp" "$2"; do
printf '%s ' "$i"
i=$(( i + $delta ))
done
printf '\n'
This has a great deal in common with the existing (excellent) answer by @William Pursell , but adds the ability to provide a skip value (for instance, to count from 0 to 100 by 10s) safely, which using an exact equality check doesn't do. 这与@William Pursell的现有(出色)答案有很多共通之处 ,但是增加了安全提供跳过值(例如,从0到100乘10s计数)的功能,使用精确的相等检查不会不行
You can farm out some of the work to external tools: 您可以将一些工作分配给外部工具:
if (( $1 > $2 )); then
seq $1 -1 $(($2+1))
else
seq $(($1+1)) 1 $2
fi | paste -sd " "
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.