LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Can a Linux script choose between options? (https://www.linuxquestions.org/questions/linux-newbie-8/can-a-linux-script-choose-between-options-4175612975/)

yogi_ni 08-31-2017 05:57 AM

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.

schneidz 08-31-2017 07:19 AM

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


rtmistler 08-31-2017 07:51 AM

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!"

yogi_ni 08-31-2017 07:55 AM

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

rtmistler 08-31-2017 08:46 AM

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.

schneidz 08-31-2017 09:01 AM

Quote:

Originally Posted by yogi_ni (Post 5754084)
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.

frieza 08-31-2017 09:19 AM

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.


All times are GMT -5. The time now is 11:12 PM.