LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 04-19-2014, 08:26 AM   #1
L1nuxn00b703
Member
 
Registered: Sep 2009
Posts: 117

Rep: Reputation: 15
scripting question (using if)


hello,

I have the following script to check if drives exist.

Code:
#!/bin/bash

SDB=/dev/sdb
SDC=/dev/sdc
SDD=/dev/sdd
SDE=/dev/sde
SDF=/dev/sdf

if [ -b $SDB ]; then
  echo "$SDB  drive present.  continuing."
else
  exit 1
fi

if [ -b $SDC ]; then
  echo "$SDC  drive present.  continuing."
else
  exit 1
fi

if [ -b $SDD ]; then
  echo "$SDD  drive present.  continuing."
else
  exit 1
fi

if [ -b $SDE ]; then
  echo "$SDE  drive present.  continuing."
else
  exit 1
fi

if [ -b $SDF ]; then
  echo "$SDF  drive present.  continuing."
else
  exit 1
fi
Is there a better method (one or two liner) that would do the same thing instead of using multiple if statements?

Thanks,
 
Old 04-19-2014, 09:07 AM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
This is untested, so you might have to adapt it.

Code:
for td in b c d e f
do
  if [ -b /dev/sd${td} ]; then
    echo "/dev/sd${td}  drive present.  continuing."
  else
    exit 1
  fi
done
jlinkels
 
  


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
scripting question bino25 Linux - General 1 03-01-2012 08:56 AM
scripting question. mrbutabara Linux - Newbie 8 01-27-2009 09:43 AM
Scripting question jim.thornton Linux - Newbie 10 03-24-2008 08:12 AM
Scripting (?) question ladio Linux - Newbie 3 05-09-2007 12:03 PM
Yet another scripting question kinetik Linux - General 4 04-01-2006 12:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:45 AM.

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