LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-21-2005, 04:31 PM   #1
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Rep: Reputation: 48
can I optimize this


hi,

I have writted this pretty self-explanatory function. Is there any better way to implement this using while or for loop?

Code:
device_boot()
{
if [[ $IDE == "/dev/hda" ]]
then
BOOTABLE_DEVICE="hd0"
fi

if [[ $IDE == "/dev/hdb" ]]
then
BOOTABLE_DEVICE="hd1"
fi

if [[ $IDE == "/dev/hdc" ]]
then
BOOTABLE_DEVICE="hd2"
fi

if [[ $IDE == "/dev/hdd" ]]
then
BOOTABLE_DEVICE="hd3"
fi
echo "The bootable device is $BOOTABLE_DEVICE"
}
 
Old 10-21-2005, 05:17 PM   #2
aldimeneira
Member
 
Registered: Jun 2005
Distribution: Ubuntu
Posts: 132

Rep: Reputation: 15
I understand the function however I'm not familiar with that specific programming language (I supose it's C/C++).

Why not use a switch,

"/dev/hda" can be a String in Java:

String hd = ( the value of $IDE in a series of characters )

I don't know how to implement this in the language you're using (...maybe a vector of characters):

- changing the String
/dev/hdb
- to a character
b

So you can change it to:

char hd = 'b'

switch{
case: hd = a
BOOTABLE_DEVICE="hd0"

break;
case: hd = b
BOOTABLE_DEVICE="hd1"
break;

case: hd = c
BOOTABLE_DEVICE="hd2"
break;

case: hd = d
BOOTABLE_DEVICE="hd3"
break;

default:

break;
}

This is what I remember about the "switch" syntaxis, it may be different and I've no idea how it will look in your final function.
 
Old 10-21-2005, 05:32 PM   #3
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 48
This is basic shell scripting. Well using case statement won;t help reducing number of lines of code. By optimization I mean can I reduce the number of lines of code by putting it some for or while loop.

thanks
 
Old 10-21-2005, 07:22 PM   #4
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
You can do it with one line of code :

Code:
BOOTABLE_DEVICE=hd$(echo $IDE | cut -f 3 -d d | tr 'abcd' '0123')
Of course I wouldn't recommend such a thing (it's not extensibile and not easy to understantd). Your first implementation seems resonable.
 
Old 10-21-2005, 07:25 PM   #5
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 48
Quote:
Originally posted by vladmihaisima
You can do it with one line of code :

Code:
BOOTABLE_DEVICE=hd$(echo $IDE | cut -f 3 -d d | tr 'abcd' '0123')
Of course I wouldn't recommend such a thing (it's not extensibile and not easy to understantd). Your first implementation seems resonable.

yeah I was looking something similar to that. Wil try that....
 
Old 10-21-2005, 09:05 PM   #6
rsheridan6
Member
 
Registered: Mar 2003
Location: Kansas City
Distribution: Debian unstable
Posts: 57

Rep: Reputation: 21
This seems to scream out for a case command, which would not as short as the cryptic pipe thing posted above, but would be both shorter and more readable than what you have. For examples, if your profile is correct and you run debian, look in /etc/init.d . Most scripts in there that control daemons use the case command.
 
  


Reply


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
How to optimize the partitions of a HD dabenavidesd Linux - General 1 09-02-2005 11:40 PM
please optimize this C function ewt3y Programming 10 08-12-2005 05:48 AM
How can I optimize VNC Wynand1 Linux - Networking 4 05-17-2005 01:05 AM
Optimize/Prefix rogk Linux - Software 1 07-25-2004 05:04 AM
how could I optimize bittorrent? rcerrillo General 0 07-04-2004 06:52 AM


All times are GMT -5. The time now is 12:54 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration