LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Assigning a value to a variable also creates an empty file named as the value in bash (https://www.linuxquestions.org/questions/linux-newbie-8/assigning-a-value-to-a-variable-also-creates-an-empty-file-named-as-the-value-in-bash-4175592679/)

StorageDon 11-01-2016 02:47 PM

Assigning a value to a variable also creates an empty file named as the value in bash
 
I have a small bash file that when I run it, several empty files appear after the script is ran.

The file contains several records:
Release 438
Release 448
Release 453
Release 466

When done, the directory has 5 new files,
000
438
448
453
466

Code:

#!/bin/bash
#
#  This sub will check if a parm is numeric
#  (Note: $1 here is not $1 in main routine
#
isnum() {
    awk -v a="$1" 'BEGIN {print (a == a + 0)}';
    }
#
#  Extract last build number BBB from "Release BBB" format
#
BMAX=000
while read dname
do
    if [[ ! -z "$dname" ]]; then
        bnum=`echo $dname | cut -f 2 -d " "`
        if [[ `isnum "$bnum"` == "1" ]]; then
            if [ "$bnum" > "$BMAX" ]; then
                BMAX=$bnum
            fi
        fi
    fi
done < /tmp/releasedirectories.txt


keefaz 11-01-2016 02:57 PM

Code:

if [ "$bnum" > "$BMAX" ]; then
You need -gt test here, or use (($bnum > $BMAX)) syntax

StorageDon 11-01-2016 05:42 PM

Thanks!
 
Quote:

Originally Posted by keefaz (Post 5625805)
Code:

if [ "$bnum" > "$BMAX" ]; then
You need -gt test here, or use (($bnum > $BMAX)) syntax

That was the bug-a-boo!
Thank you keefaz

grail 11-01-2016 08:52 PM

There is also no reason to use an external command like awk or even where you use cut:
Code:

#
#  Extract last build number BBB from "Release BBB" format
#
BMAX=000
reg='^[0-9]+$'

while read _ bnum
do
  if [[ "$bnum" =~ $reg ]]; then
    (( bnum > BMAX )) && BMAX=$bnum
  fi
done < /tmp/releasedirectories.txt



All times are GMT -5. The time now is 02:20 AM.