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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-21-2005, 04:31 PM
|
#1
|
|
Senior Member
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249
Rep:
|
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"
}
|
|
|
|
10-21-2005, 05:17 PM
|
#2
|
|
Member
Registered: Jun 2005
Distribution: Ubuntu
Posts: 132
Rep:
|
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.
|
|
|
|
10-21-2005, 05:32 PM
|
#3
|
|
Senior Member
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249
Original Poster
Rep:
|
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
|
|
|
|
10-21-2005, 07:22 PM
|
#4
|
|
Member
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196
Rep:
|
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.
|
|
|
|
10-21-2005, 07:25 PM
|
#5
|
|
Senior Member
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249
Original Poster
Rep:
|
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....
|
|
|
|
10-21-2005, 09:05 PM
|
#6
|
|
Member
Registered: Mar 2003
Location: Kansas City
Distribution: Debian unstable
Posts: 57
Rep:
|
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.
|
|
|
|
| Thread Tools |
Search 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
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:09 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|