Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Was wondering if someone can take the time to explain the following:

I have a directory of files (PDF), which I place into an array.

shopt -s nullglob                                   # Set array to 0 is nothing found=
declare -a TotalFiles=($Prefix*.pdf)                # Current listing of files
TotalFileCount=${#TotalFiles[@]}

In my mesting the array contains the following.

Array Contents Scan-0030.pdf Scan-0140.pdf Scan-0005.pdf Scan-0006.pdf Scan-0007.pdf Scan-0008.pdf Scan-0009.pdf Scan-0010.pdf

I have created a the following functions to derive the next file to create

function NextNum {
    HighestNum =0
    echo "NextNumber Functions"

    #for index in "${TotalFiles[*]}"
    for file in ${!TotalFiles[*]}
    do
        #printf "%4d: %s\n" $index $TotalFiles ${array[$index]}
        echo $file ${TotalFiles[$file]}

        name=${TotalFiles[$file]}
        name=${name//[^0-9]/}
        name=$((10#$name))

        echo "File number in  name  - $name"
        echo $file
        TotalFiles[$file]=$name

        **((name > HighestNum)) && HighestNum=$name**
    done
}

My question is with this line in the function which i found by googleling.

((name > HighestNum)) && HighestNum=$name

How come one does not have to specify that two variables are being compared? like this,

(($name > $HighestNum)) && HighestNum=$name

thank you for the assistance.

share|improve this question
up vote 1 down vote accepted

The reason is that ((...)) is a special case. It performs arithmetic. There is no use for text inside ((...)). Thus, as a short-cut, any name inside ((...)) is assumed to refer to a shell variable.

As a result, the following two do the same thing:

$ a=1; b=2; ((c=$a+$b)); echo $c
3

$ a=1; b=2; ((c=a+b)); echo $c
3
share|improve this answer
    
Excellent, thank you. Just wanted to understand the process. – user68650 Feb 12 '15 at 3:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.