Solved

Bash Shell Script - Help with conditional evaluation of a variable integer

Posted on 2008-09-29
10
1,230 Views
Last Modified: 2013-12-26
Hello, first off really new to bash, so not sure how this works.

Both of these values should be 15, but it always prooves false.

I believe $hd_size might be stored as a LONG (15.55)

how do can I change this expression to evaluate it as a whole number instead.






if [ $hd_size -le $part_size ]; then

Open in new window

0
Comment
Question by:WinPE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 4

Expert Comment

by:Ara-
ID: 22598752
You could try to print them to see the value. You can typecast a variable like this:
declare -i var_name
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22598762
there is no concept of data types in bash. Can you show the preceding code that assigns values to these variables?
0
 

Author Comment

by:WinPE
ID: 22598815
Sure: =)

hd_size="$(sfdisk -s /dev/$chosen_hd)"
cylinders="$(sfdisk -g /dev/$chosen_hd | awk '{ print $2; }')"
hd_size_convert="$(echo "scale=0; $hd_size / 1048576" | bc -l)"
 
 vol_size_tmp="$(mktemp /tmp/vol_size_tmp.XXXXXX)"
  trap "[ -f "$vol_size_tmp" ] && rm -f $vol_size_tmp" HUP INT QUIT TERM EXIT
  while [ "$ASK_VOL_SIZE" -ne 0 ]; do
    $DIA --backtitle "$msg_nchc_free_software_labs" --title  \
    "Partition Size" --inputbox "Please enter the primary partition size in gigabytes:" \
    0 0 $hd_size_convert\
    2> $vol_size_tmp
	nfs_srv="$(cat $vol_size_tmp)"
    if [ -n "$(cat $vol_size_tmp | grep -iE "[^[:digit:]]")" ] || [ -z "$nfs_srv" ] || [ $hd_size_convert -le $nfs_srv ]; then
      ASK_VOL_SIZE=1
      $DIA --backtitle "$msg_nchc_free_software_labs" --title "$msg_nchc_clonezilla" \
      --msgbox "$msg_enter_digits_only\n$msg_please_do_it_again!!!" 0 0 
    else
      ASK_VOL_SIZE=0
    fi
  done

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 45

Expert Comment

by:sunnycoder
ID: 22598840
okay .. I see hd_size getting assigned but I dont see part_size ...

Add this line just before the comparison        
echo $hd_size  $part_size

if [ $hd_size -le $part_size ]; then
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22598925
>I believe $hd_size might be stored as a LONG (15.55)
Umm .. did you mean that $hd_size is 15.55 and _part_size is 15? If yes and you wish to compare only the part before decimal place then use

$hd_size_new=`echo $hd_size|sed 's:\..*::'`
if [ $hd_size_new -le $part_size ]; then

If you wish to strip off the decimal part from part_size then use a similar line
$hd_part_new=`echo $part_size|sed 's:\..*::'`
if [ $hd_size_new -le $part_size_new ]; then

If however both are numbers with decimal points, e.g. 15.55, and you wish to compare them in entirety then compare them as strings
if [ "$hd_size" = "$part_size" ]; then
0
 

Author Comment

by:WinPE
ID: 22598992
I tried the following but same problem.

sorry I meant $hd_size_convert not $part_size

I can enter
14 and the line will proove true.
but if I enter 15 it prooves false for some reason.

$hd_size_convert echos as 15 in my test case so it should work right??
  if [ -n "$(cat $vol_size_tmp | grep -iE "[^[:digit:]]")" ] || [ -z "$nfs_srv" ] || [ "$hd_size_convert" -le "$nfs_srv" ]; then

Open in new window

0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22599027
Can you pls echo both the values and post the result here?
0
 

Author Comment

by:WinPE
ID: 22599039
both values echo as 15
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
ID: 22599094
Then -le should return true. Since you echo'ed the values just before comparison and the test condition is a bunch of conditions ORed together, the expression would evaluate to true. How did you determine it was evaluating to false?
0
 

Author Comment

by:WinPE
ID: 22599158
I'm an idiot... you cleared it out...

I was processing it backwards... I should of been doing -lt all this time... was looking at my whole stament backwards :(

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sed question 2 122
Problem logging tar errors 11 69
Call Shell Script from Perl Script 6 163
batch file problem 5 75
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question