for ((i=0; i<3 && i!=1; i++))

2015-01-12 Thread l_j_f
1. bash version
-sh-4.3# bash --version
GNU bash, version 4.3.0(1)-release (arm-hisiv200-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

2. the script
#!/bin/bash

main() {
local i
 
for ((i=0; i<3; i++)); do
local var
if [[ "${i}" == "1" ]]; then
var=1
fi 
   
echo i=$i var=$var
done
}
 
main "$@"

3. the result
-sh-4.3# ./test3.sh 
i=0 var=
i=1 var=1
i=2 var=1
# I think it should be
i=0 var=
i=1 var=1
i=2 var=




l_j_f

for ((i=0; i<3 && i!=1; i++))

2015-01-12 Thread l_j_f
1. bash version
-sh-4.3# bash --version
GNU bash, version 4.3.0(1)-release (arm-hisiv200-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

2. the script
#!/bin/bash

main() {
local idx
local current=1

for ((idx=0; idx<3 && idx != current; idx++)); do
echo $idx
done
}

main "$@"

3. the result
-sh-4.3# ./test1.sh 
0
# I think it should be 
0
2





l_j_f

use local and $?

2015-01-12 Thread l_j_f
1. bash version
-sh-4.3# bash --version
GNU bash, version 4.3.0(1)-release (arm-hisiv200-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

2. the script
#!/bin/bash

ok() {  
echo ok  
}  
   
error() {
echo error   
return 1 
}
 
 
main() { 
local err
 
local a=$(ok); err=$?
echo a=$a err=$err
 
local b=$(error); err=$?
echo b=$b err=$err
}  
main "$@" 

3. the result
-sh-4.3# ./test2.sh 
a=ok err=0
b=error err=0 #I think it should be "b=error err=1"






l_j_f