LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 07-20-2006, 01:39 AM   #1
branche_dude
LQ Newbie
 
Registered: Jul 2006
Posts: 7

Rep: Reputation: 0
simple question regarding shell scripting


Is there any way to find out if a given device exists?
I have a small script that asks user to type in name of a device and I want to check if the device exists in /dev dir.
So far I have:

Code:
echo -n "HDD -> "
read hdd
while [ ! -d /dev/$hdd ]
do
echo -n "Doesn't Exist. Try Again. HDD -> "
read hdd
done
The only problem is that the while statement checks for DIRECTORIES instead of devices. So if that code should work for any folder that exists in /dev. However, hda, hdb, sda are devices and therefore don't count as dir and thus don't pass that statement.

Please Help. I am very new to linux and I already did some research before turning to experts here.

Thanks.
 
Old 07-20-2006, 01:46 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
did you check the manpage for this? http://unixhelp.ed.ac.uk/CGI/man-cgi?test use -b or -c not -d
 
Old 07-20-2006, 01:49 AM   #3
prozac
Member
 
Registered: Oct 2005
Location: Australia
Distribution: slackware 12.1
Posts: 753

Rep: Reputation: 32
device files are categoried as block devices (-b) or character devices (-c) in Linux. the -d directive is for directories.
 
Old 07-20-2006, 01:50 AM   #4
branche_dude
LQ Newbie
 
Registered: Jul 2006
Posts: 7

Original Poster
Rep: Reputation: 0
Thank you very much to both of you.
That helps a lot and saved me a lot of headache.
 
Old 07-20-2006, 01:56 AM   #5
xode
Member
 
Registered: Aug 2003
Distribution: Mandrake 9.0; FC4; FC8; SUSE 10.3; SUSE 12.1; SUSE 13.2
Posts: 638
Blog Entries: 1

Rep: Reputation: 52
The find command looks like it might do just what you want. At a terminal shell prompt, type "info find" (without the "s) to pull up loads of local documentation on how find works. My understanding is that find can also work with -b and -c as listed above, as for example "find /dev -type b -name 'hd*'"

Last edited by xode; 07-20-2006 at 01:58 AM.
 
Old 07-20-2006, 02:10 AM   #6
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
if the task is to check for the existance of a known file name, why search a directory for it? ust see if it's there or not in the least intensive and most functional way, i.e. with a test.
 
Old 07-20-2006, 02:17 AM   #7
prozac
Member
 
Registered: Oct 2005
Location: Australia
Distribution: slackware 12.1
Posts: 753

Rep: Reputation: 32
acid is right, you donot need a loop for this. just check whether the file exists are not with the 'if' condition.
 
Old 07-20-2006, 09:21 PM   #8
branche_dude
LQ Newbie
 
Registered: Jul 2006
Posts: 7

Original Poster
Rep: Reputation: 0
No, I need to know the name of the primary HDD and I want to ask the user until I am provided with a proper device name. That's why the loop is there. The task is to create a new partition on that HDD using FDISK and then backup the first partition using PARTIMAGE.

Last edited by branche_dude; 07-20-2006 at 09:25 PM.
 
Old 07-21-2006, 02:35 AM   #9
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
no to what? we answered your question fine...
 
  


Reply



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
simple shell scripting problem noir911 Programming 11 03-14-2006 01:27 AM
Very simple BASH scripting question clickster Linux - Newbie 6 11-23-2005 04:28 PM
Simple Shell Scripting Question hellomynameisphil Programming 2 08-27-2005 03:41 AM
Bash Scripting--Simple Question mooreted Linux - General 4 05-10-2004 01:44 PM
simple scripting question wedgeworth Programming 6 02-04-2004 10:30 AM

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

All times are GMT -5. The time now is 04:00 PM.

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