LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-01-2016, 02:47 PM   #1
StorageDon
LQ Newbie
 
Registered: May 2013
Posts: 10

Rep: Reputation: Disabled
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
 
Old 11-01-2016, 02:57 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Code:
if [ "$bnum" > "$BMAX" ]; then
You need -gt test here, or use (($bnum > $BMAX)) syntax
 
Old 11-01-2016, 05:42 PM   #3
StorageDon
LQ Newbie
 
Registered: May 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thanks!

Quote:
Originally Posted by keefaz View Post
Code:
if [ "$bnum" > "$BMAX" ]; then
You need -gt test here, or use (($bnum > $BMAX)) syntax
That was the bug-a-boo!
Thank you keefaz

Last edited by StorageDon; 11-01-2016 at 05:43 PM.
 
Old 11-01-2016, 08:52 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
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
 
  


Reply

Tags
bash, programing, touch, variable


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
wget -O creates a empty file on failure cpjain Linux - Newbie 2 02-01-2013 07:37 AM
BASH: Assigning a string containing pipes to a variable gearge Programming 5 11-27-2011 05:03 AM
[SOLVED] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 09:57 AM
[SOLVED] Cron - mysqldump job creates an empty sql file novice32 Linux - Newbie 5 04-17-2010 12:12 PM
aplay -l > message,txt creates empty file milindlokde Programming 5 06-24-2007 01:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration