• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

How to check if /dev/sdk* exists

I need an if statement that checks if /dev/sdk* exists this would includes /dev/sdk1 /dev/sdk2.
The reason for this is I need to know if there's any devices located at /dev/sdk*

I tried the following
if [ -d /dev/sdk* ]; then echo "it exists"; fi

But it outputs:
-bash: [: /dev/sdk1: binary operator expected


0
TotallyMe
Asked:
TotallyMe
1 Solution
 
evilrixSenior Software Engineer (Avast)Commented:
Something like this?
if [ -e /dev/null ] ; then
   echo "exists"
fi

Open in new window

0
 
evilrixSenior Software Engineer (Avast)Commented:
Oops, try again...
if [ -e /dev/nul* ] ; then
   echo "exists"
fi

Open in new window

0
 
evilrixSenior Software Engineer (Avast)Commented:
OR as an alternative, you could try this (it's a bit of a hack)...
dummy=`ls /dev/tty* 2>&1 > /dev/null`
if [ $? -eq 0 ]; then
        echo "exists"
fi

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TintinCommented:

if ls /dev/sdk* >/dev/null 2>&1
then
   echo exists
else
   echo does not exist
fi

Open in new window

0
 
omarfaridCommented:
try

ls /dev | grep /dev/dsk
if [ $? -eq 0 ]
then
    echo exists
else
    echo does not exist
fi
0
 
TotallyMeAuthor Commented:
Thanks to Tintin I'm currently using :
if ls /dev/sdk* >/dev/null 2>&1
then
   echo "exists"
fi

not sure if there's anything better than this?
0
 
Duncan RoeSoftware DeveloperCommented:
I would say it's pretty close to as good as you'll get. If you want to set a variable for later testing, you could put:

ls /dev/sdk* >/dev/null 2>&1 && exists=true || exists=false

later you can put

if $exists; then ...

or

$exists && ...
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now