LinuxQuestions.org
Review your favorite Linux distribution.
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 08-31-2017, 05:57 AM   #1
yogi_ni
LQ Newbie
 
Registered: Aug 2017
Posts: 2

Rep: Reputation: 0
Question Can a Linux script choose between options?


Don't ask me why but we have a script running to read from a specific model of usb device. However the particular device used is now end of life so we now need to use a different model.

I have been able to update the script to use this newer model of usb, however my problem arises because some people still have the old model USB too. So while we integrate the new usb devices and phase out the old ones we need the script to be able to read and run from both.

Unfortunately my Linux experience is not quite strong enough to know how to change this script to be able to do this.
I realise it is probably extremely simple but I have not come across anything like this before.

Any help will be massively useful.

this is the script we are running:


disk_id ()
{
# Checking if /media/usbdisk is connected
df -h | grep -o "/media/usbdisk" > /dev/null 2> /dev/null
if [ "$?" = "0" ]
then
umount /media/usbdisk > /dev/null 2> /dev/null
fi

# Checking if USB HDD is connected
log "Checking if $DEVICE is connected"
/usr/sbin/lsusb 2>&1| grep -o "BUFFALO" > /dev/null
if [ "$?" != "0" ]
then
log "USB drive is not connected! Disconnect and reconnect the USB disk!"
unlock_key
exit 1
fi



Basically we want this script to be able to read from both BUFFALO and FREECOM.
 
Old 08-31-2017, 07:19 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-30
Posts: 5,290

Rep: Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916
probably this would be sufficient:
Code:
disk_id ()
{
# Checking if /media/usbdisk is connected
df -h | grep -o "/media/usbdisk" > /dev/null 2> /dev/null
if [ "$?" = "0" ]
then
umount /media/usbdisk > /dev/null 2> /dev/null
fi

# Checking if USB HDD is connected
log "Checking if $DEVICE is connected"
/usr/sbin/lsusb 2>&1| egrep -o "whatever-floats-your-boat|whatever-grinds-your-gears" > /dev/null
if [ "$?" != "0" ]
then
log "USB drive is not connected! Disconnect and reconnect the USB disk!"
unlock_key
exit 1
fi

Last edited by schneidz; 08-31-2017 at 07:22 AM.
 
1 members found this post helpful.
Old 08-31-2017, 07:51 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,314
Blog Entries: 13

Rep: Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371
Hi yogi_ni and welcome to LQ!

I do feel that schneidz' answer is fully correct and I thank them for their expertise. I think it may benefit with the relevant strings which you cited in your original post:
Code:
/usr/sbin/lsusb 2>&1| egrep -o "BUFFALO|FREECOM" > /dev/null
Unfortunately I have seen people literally copy the exact recommended post without substituting their correct search strings, whereupon they write back, "It didn't work!"
 
1 members found this post helpful.
Old 08-31-2017, 07:55 AM   #4
yogi_ni
LQ Newbie
 
Registered: Aug 2017
Posts: 2

Original Poster
Rep: Reputation: 0
Hi Schneidz,

Thanks for the reply. Im not sure how this would work. We would need to add a second device type in the variables surely?

What I was thinking of doing albeit fairly rudimental was inserting a second if statement. Would this work too?


# Checking if USB HDD is connected
log "Checking if $DEVICE is connected"
/usr/sbin/lsusb 2>&1| grep -o "FREECOM" > /dev/null
if [ "$?" != "0" ]
then
/usr/sbin/lsusb 2>&1| grep -o "BUFFALO" > /dev/null
if [ "$?" != "0" ]
then
log "USB drive is not connected! Disconnect and reconnect the USB disk!"
unlock_key
exit 1
fi
fi
 
Old 08-31-2017, 08:46 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,314
Blog Entries: 13

Rep: Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371
A couple of comments:
  1. Code tags help retain the formatting for the code, they are [code][/code] or use the # in the advanced editor mode, this is what Schneidz has done to segregate the code in their response
  2. Comparing the return value is potentially suspect because it may not always be the output of the grep
  3. I might consider not repeatedly calling lsusb but instead checking the files in the /dev tree over this option, or getting the output of lsusb once in a variable and then searching that variable
  4. You can use elif to do if-elseif-else in bash, and I would suggest that to make it mutually exclusive and more correctly classify your result
  5. When debugging bash scripts, just after the !#/bin/sh line, recommend you add "set -xv" to enable extra debug, and later you can comment out that line. It will show you much greater debug information for what the variable assignments are, as well as the result of the commands and the grep
Note that if you choose the elif option, the problem is that $? is no longer valid from the first call.

Last edited by rtmistler; 08-31-2017 at 08:49 AM. Reason: Added info about debugging bash scripts
 
1 members found this post helpful.
Old 08-31-2017, 08:46 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,314
Blog Entries: 13

Rep: Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371Reputation: 4371
A couple of comments:
  1. Code tags help retain the formatting for the code, they are [code][/code] or use the # in the advanced editor mode, this is what Schneidz has done to segregate the code in their response
  2. Comparing the return value is potentially suspect because it may not always be the output of the grep
  3. I might consider not repeatedly calling lsusb but instead checking the files in the /dev tree over this option, or getting the output of lsusb once in a variable and then searching that variable
  4. You can use elif to do if-elseif-else in bash, and I would suggest that to make it mutually exclusive and more correctly classify your result
  5. When debugging bash scripts, just after the !#/bin/sh line, recommend you add "set -xv" to enable extra debug, and later you can comment out that line. It will show you much greater debug information for what the variable assignments are, as well as the result of the commands and the grep
Note that if you choose the elif option, the problem is that $? is no longer valid from the first call.

Last edited by rtmistler; 08-31-2017 at 08:49 AM. Reason: Added info about debugging bash scripts
 
1 members found this post helpful.
Old 08-31-2017, 09:01 AM   #7
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-30
Posts: 5,290

Rep: Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916
Quote:
Originally Posted by yogi_ni View Post
Hi Schneidz,

Thanks for the reply. Im not sure how this would work. We would need to add a second device type in the variables surely?

What I was thinking of doing albeit fairly rudimental was inserting a second if statement. Would this work too?


# Checking if USB HDD is connected
log "Checking if $DEVICE is connected"
/usr/sbin/lsusb 2>&1| grep -o "FREECOM" > /dev/null
if [ "$?" != "0" ]
then
/usr/sbin/lsusb 2>&1| grep -o "BUFFALO" > /dev/null
if [ "$?" != "0" ]
then
log "USB drive is not connected! Disconnect and reconnect the USB disk!"
unlock_key
exit 1
fi
fi
sure, multiple ways to skin this cat.
 
Old 08-31-2017, 09:19 AM   #8
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
a couple of thoughts on this, first of all, maybe UDEV might be a more appropriate tool? and instead looking for a specific UUID for the USB drive instead of a more generic means.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] what options to choose in kernel configuration (make config) hayf Programming 6 08-21-2011 07:58 AM
What kernel options should I choose for a pcmcia wireless ethernet card to work? elconde Linux - Laptop and Netbook 3 03-18-2004 09:16 PM
updated kernel now has 3 options to choose on boot centr0 Linux - General 1 02-26-2003 11:39 PM

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

All times are GMT -5. The time now is 03:48 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