Jinsi ya kuongeza kutofautisha katika bash?


Nenda kwa jibu lililokubaliwa


Nimejaribu kwa nyongeza variable numeric kutumia zote mbili var=$var+1 na var=($var+1) bila mafanikio. Tofauti ni nambari, ingawa bash inaonekana inaisoma kama kamba.

Toleo la Bash 4.2.45 (1) -tafura (x86_64-pc-linux-gnu) juu ya Ubuntu 13.10.


626





2013-12-03




Idadi ya majibu: 8


Kuna zaidi ya njia moja ya kuongeza kutofautisha kwa bash, lakini kile ulijaribu sio sawa.

Unaweza kutumia kwa mfano upanuzi wa hesabu :

 var=$((var+1))
((var=var+1))
((var+=1))
((var++))
 

Au unaweza kutumia let :

 let "var=var+1"
let "var+=1"
let "var++"
 

Tazama pia: http://tldp.org/LDP/abs/html/dblparens.html .


978


2013-12-03

 var=$((var + 1))
 

Hesabu katika bash hutumia $((...)) syntax.


165


2013-12-03

Chaguzi anuwai za kuongeza na 1, na uchambuzi wa utendaji

Shukrani kwa jibu la Radu Rădeanu ambalo hutoa njia zifuatazo za kuongeza kutofautisha kwa bash:

 var=$((var+1))
((var=var+1))
((var+=1))
((var++))
let "var=var+1"
let "var+=1" 
let "var++"
 

Kuna njia zingine pia. Kwa mfano, angalia majibu mengine juu ya swali hili.

 let var++
var=$((var++))
((++var))
{
    declare -i var
    var=var+1
    var+=1
}
{
    i=0
    i=$(expr $i + 1)
}
 

Kuwa na chaguzi nyingi husababisha maswali haya mawili:

  1. Je! Kuna tofauti ya utendaji kati yao?
  2. Ikiwa ndivyo, ni ipi inayofanya vizuri zaidi?

Nambari ya jaribio la kuongezeka kwa utendaji:

 #!/bin/bash

# To focus exclusively on the performance of each type of increment
# statement, we should exclude bash performing while loops from the
# performance measure. So, let's time individual scripts that
# increment $i in their own unique way.

# Declare i as an integer for tests 12 and 13.
echo > t12 'declare -i i; i=i+1'
echo > t13 'declare -i i; i+=1'
# Set i for test 14.
echo > t14 'i=0; i=$(expr $i + 1)'

x=100000
while ((x--)); do
    echo >> t0 'i=$((i+1))'
    echo >> t1 'i=$((i++))'
    echo >> t2 '((i=i+1))'
    echo >> t3 '((i+=1))'
    echo >> t4 '((i++))'
    echo >> t5 '((++i))'
    echo >> t6 'let "i=i+1"'
    echo >> t7 'let "i+=1"'
    echo >> t8 'let "i++"'
    echo >> t9 'let i=i+1'
    echo >> t10 'let i+=1'
    echo >> t11 'let i++'
    echo >> t12 'i=i+1'
    echo >> t13 'i+=1'
    echo >> t14 'i=$(expr $i + 1)'
done

for script in t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14; do
    line1="$(head -1 "$script")"
    printf "%-24s" "$line1"
    { time bash "$script"; } |& grep user
    # Since stderr is being piped to grep above, this will confirm
    # there are no errors from running the command:
    eval "$line1"
    rm "$script"
done
 

Matokeo:

 i=$((i+1))              user    0m0.992s
i=$((i++))              user    0m0.964s
((i=i+1))               user    0m0.760s
((i+=1))                user    0m0.700s
((i++))                 user    0m0.644s
((++i))                 user    0m0.556s
let "i=i+1"             user    0m1.116s
let "i+=1"              user    0m1.100s
let "i++"               user    0m1.008s
let i=i+1               user    0m0.952s
let i+=1                user    0m1.040s
let i++                 user    0m0.820s
declare -i i; i=i+1     user    0m0.528s
declare -i i; i+=1      user    0m0.492s
i=0; i=$(expr $i + 1)   user    0m5.464s
 

Hitimisho:

Inaonekana bash ina kasi sana kutekeleza i+=1 wakati $i inatangazwa kama nambari. let taarifa zinaonekana polepole, na expr ni polepole zaidi kwa sababu sio kujengwa kwa bash.


87



Kuna hii pia:

 var=`expr $var + 1`
 

Zingatia kwa uangalifu nafasi na pia ` sio '

Wakati majibu ya Radu, na maoni, ni ya nguvu na inasaidia sana, ni maalum. Najua uliuliza mahsusi juu ya bash, lakini nilidhani nitaingiza bomba tangu nilipopata swali hili wakati nilikuwa nikitazama kufanya kitu kimoja nikitumia sh kwenyeboxbox chini ya eCLinux. Hii inaweza kusongeshwa zaidi ya bash.


18


2015-07-31

Ikiwa unatangaza $var kama nambari, basi kile ulijaribu mara ya kwanza kitafanya kazi:

 $ declare -i var=5
$ echo $var
5
$ var=$var+1
$ echo $var
6
 

Rejea: Aina za vigezo, Mwongozo wa Bash kwa Kompyuta


10


2016-08-22

Kuna njia moja inakosekana katika majibu yote - bc

 $ VAR=7    
$ bc <<< "$VAR+2"
9
$ echo $VAR
7
$ VAR=$( bc <<< "$VAR+1" )
$ echo $VAR
8
 

bc imeainishwa na kiwango cha POSIX , kwa hivyo inapaswa kuwapo kwenye toleo zote za Ubuntu na mifumo ya POSIX-inayotumia. <<< Redirection inaweza kubadilishwa kwa echo "$VAR" | bc ajili ya urahisi wa kubeba, lakini tangu swali anauliza kuhusu bash - ni sawa na matumizi tu <<< .


7


2015-12-06

Kurudi code 1 suala ni sasa kwa variants wote chaguo-msingi ( let , (()) , nk). Hii mara nyingi husababisha shida, kwa mfano, kwenye hati zinazotumia set -o errexit . Hii ndio ninayotumia kuzuia msimbo wa makosa 1 kutoka misemo ya hesabu ambayo inatathmini kwa 0 ;

 math() { (( "[email protected]" )) || true; }

math a = 10, b = 10
math a++, b+=2
math c = a + b
math mod = c % 20
echo $a $b $c $mod
#11 12 23 3
 

6


2017-02-23

Hii lazima iwe njia mbaya zaidi ya kukamilisha kazi rahisi kama hiyo lakini nilitaka tu kuishughulikia kwa raha nadhani (tofauti kabisa ya gofu ya kificho).

 $ var=0
$ echo $var
0
$ var="$(python -c 'print('$var'+1)')"
$ echo $var
1
 

au

 $ var="$(printf '%s\n' $var'+1' | bc)"
$ echo $var
1
 

Tumia moja kwa moja chaguo zuri zaidi hapa.


0


2019-10-04