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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
08-31-2017, 05:57 AM
|
#1
|
LQ Newbie
Registered: Aug 2017
Posts: 2
Rep:
|
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.
|
|
|
08-31-2017, 07:19 AM
|
#2
|
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326
|
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.
|
08-31-2017, 07:51 AM
|
#3
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,894
|
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.
|
08-31-2017, 07:55 AM
|
#4
|
LQ Newbie
Registered: Aug 2017
Posts: 2
Original Poster
Rep:
|
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
|
|
|
08-31-2017, 08:46 AM
|
#5
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,894
|
A couple of comments: - 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
- Comparing the return value is potentially suspect because it may not always be the output of the grep
- 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
- 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
- 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.
|
08-31-2017, 08:46 AM
|
#6
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,894
|
A couple of comments: - 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
- Comparing the return value is potentially suspect because it may not always be the output of the grep
- 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
- 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
- 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.
|
08-31-2017, 09:01 AM
|
#7
|
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326
|
Quote:
Originally Posted by yogi_ni
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.
|
|
|
08-31-2017, 09:19 AM
|
#8
|
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
|
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.
|
All times are GMT -5. The time now is 09:42 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|