The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Add Item to Dock 0.0.8 iBot UNIX and Linux RSS News 0 06-03-2008 05:10 AM
problem with array=($(find ....) jul Shell Programming and Scripting 1 02-27-2008 04:28 PM
Add Item to Dock 0.0.7 iBot UNIX and Linux RSS News 0 01-24-2008 08:40 AM
replace item.. happyv UNIX for Dummies Questions & Answers 2 09-14-2007 12:09 AM
Skip item by using substring rwunwla Shell Programming and Scripting 2 12-16-2005 01:12 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-15-2007
fongthai fongthai is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 54
find an available item in array

Dear all,

I'm have a sorted array like this:
177
220
1001
2000
2001
2003
2005

notice that 2002 and 2004 are NOT in array.

Then user input a number INPUT, our script should return OUTPUT value like this:
if INPUT is not in array => OUTPUT=INPUT
if INPUT is in array => OUTPUT is the smallest number that larger than INPUT
and should NOT exist in array!


Example with this array:
INPUT => OUTPUT
1000 => 1000
1001 => 1002
2000 => 2002
2005 => 2006

Can anyone help me to complete this on bash shell!
  #2 (permalink)  
Old 07-15-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Code:
typeset -i mNbr
typeset -i mInput
typeset -i mCnt
echo 'Enter Number: '
read mInput
mFound='N'
while read mNbr
do
  if [ $mNbr -eq $mInput ]; then
    mCnt=$mNbr
    mFound='Y'
  fi
  if [ "$mFound" = "Y" ]; then
    if [ $mNbr -ne $mCnt ]; then
      echo 'Input = '$mInput' Output = '$mCnt
      exit
    fi
    mCnt=$mCnt+1
  else
    if [ $mNbr -gt $mInput ]; then
      echo 'Input = '$mInput' not found'
      exit
    fi
  fi
done < input_file
  #3 (permalink)  
Old 07-15-2007
fongthai fongthai is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 54
Thank you,

It works fine, except the last item!
But it's OK.
  #4 (permalink)  
Old 07-16-2007
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
Perhaps try something like this, which is at the bash prompt...
Code:
$ for v in 177 220 1001 2000 2001 2003 2005; do a[$v]=1; done
$ read -p "Input = " i; while [[ ${a[$i]} = 1 ]]; do ((i+=1)); done; echo Output = $i
Input = 2000
Output = 2002

Last edited by Ygor; 07-16-2007 at 01:41 AM..
  #5 (permalink)  
Old 07-16-2007
fongthai fongthai is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 54
Yah, it's great!
Thank Ygor!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:41 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0