LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   simple question regarding shell scripting (http://www.linuxquestions.org/questions/linux-newbie-8/simple-question-regarding-shell-scripting-465792/)

branche_dude 07-20-2006 01:39 AM

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.

acid_kewpie 07-20-2006 01:46 AM

did you check the manpage for this? http://unixhelp.ed.ac.uk/CGI/man-cgi?test use -b or -c not -d

prozac 07-20-2006 01:49 AM

device files are categoried as block devices (-b) or character devices (-c) in Linux. the -d directive is for directories.

branche_dude 07-20-2006 01:50 AM

Thank you very much to both of you.
That helps a lot and saved me a lot of headache.

xode 07-20-2006 01:56 AM

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*'"

acid_kewpie 07-20-2006 02:10 AM

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.

prozac 07-20-2006 02:17 AM

acid is right, you donot need a loop for this. just check whether the file exists are not with the 'if' condition.

branche_dude 07-20-2006 09:21 PM

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.

acid_kewpie 07-21-2006 02:35 AM

no to what? we answered your question fine...


All times are GMT -5. The time now is 10:34 AM.