LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Puppy
User Name
Password
Puppy This forum is for the discussion of Puppy Linux.

Notices


Reply
  Search this Thread
Old 05-30-2015, 07:35 AM   #1
Fixit7
Senior Member
 
Registered: Mar 2014
Location: El Lago, Texas
Distribution: Ubuntu_Mate 16.04
Posts: 1,374

Rep: Reputation: 169Reputation: 169
Detect if USB drive is connected


I use a bash script to backup files to an external drive.

What line could I add to determine if sdb1 is connected, and if not, display a message ?
 
Old 05-30-2015, 07:59 AM   #2
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,057

Rep: Reputation: Disabled
Code:
if [ "`lsblk -o name|grep sdb1`" = "" ]; then echo "no device /dev/sdb1"; fi
If you get no output that doesn't mean that /dev/sdb1 is mounted, but you can mount it if you don't get the message and it's not already mounted. You can check the output of "mount" or of "lsblk -o name,mountpoint|grep sdb1 to see if it's already mounted.

Last edited by Didier Spaier; 05-30-2015 at 08:17 AM.
 
Old 05-30-2015, 08:21 AM   #3
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
That usb drive would have a filesystem and it may have a unique id. So use 'blkid' command to check presence of that uuid and do backup.
 
Old 05-30-2015, 09:30 AM   #4
Fixit7
Senior Member
 
Registered: Mar 2014
Location: El Lago, Texas
Distribution: Ubuntu_Mate 16.04
Posts: 1,374

Original Poster
Rep: Reputation: 169Reputation: 169
# is.sh
/root/Scripts/is.sh: line 6: lsblk: command not found
no device /dev/sdb1

I found an example and tweaked it. Seems to work.

[ -e /mnt/sda1/Linux_Files/TOSHIBA_SDB1 ] && echo "Toshiba drive is connected" || echo "Toshiba Drive is NOT connected !!"

I have no idea what the -e is.
 
Old 05-30-2015, 09:37 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
I don't think puppy has lsblk or blkid.

http://www.tldp.org/LDP/Bash-Beginne...ect_07_01.html
 
Old 05-30-2015, 10:34 AM   #6
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,057

Rep: Reputation: Disabled
Quote:
Originally Posted by Fixit7 View Post
I have no idea what the -e is.
In addition to the pointer michaelk gave you, try this:
Code:
man bash
But as this man page is huge and as bash is an implementation of the Shell Command Language you could begin with reading the relevant part of the POSIX specification that is easier to understand for beginners in my opinion. Just bear in mind that bash has also a lot of specific features.

PS your test checks that the USB drive is connected and mounted. I assume that's what you actually wanted.

Last edited by Didier Spaier; 05-30-2015 at 10:37 AM.
 
Old 05-30-2015, 12:27 PM   #7
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
I use it in my backup script as well. See the code snippet here: http://www.linuxquestions.org/questi...9/#post5363825

jlinkels
 
Old 05-30-2015, 12:57 PM   #8
Fixit7
Senior Member
 
Registered: Mar 2014
Location: El Lago, Texas
Distribution: Ubuntu_Mate 16.04
Posts: 1,374

Original Poster
Rep: Reputation: 169Reputation: 169
Quote:
Originally Posted by Didier Spaier View Post
In addition to the pointer michaelk gave you, try this:
Code:
man bash
PS your test checks that the USB drive is connected and mounted. I assume that's what you actually wanted.
My system is setup to mount all drives, but sometimes I do not have an external drive connected.

So I wanted to be notified when files are not being copied to that drive.
 
Old 05-30-2015, 01:11 PM   #9
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
If this is in a script, you could run the stat command on the mountpoint and verify that the "Device" field is different from that of the root filesystem.

For my own systems, my backup drives have only a subdirectory at their top level, and all backups are directed to that subdirectory, e.g., "/mnt/backups/bkups". If the drive is not mounted, that "bkups" directory won't exist, and the backup fails.
 
Old 05-30-2015, 01:29 PM   #10
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by Fixit7 View Post
...
I have no idea what the -e is.
http://linux.die.net/man/1/test
 
Old 05-30-2015, 02:19 PM   #11
Fixit7
Senior Member
 
Registered: Mar 2014
Location: El Lago, Texas
Distribution: Ubuntu_Mate 16.04
Posts: 1,374

Original Poster
Rep: Reputation: 169Reputation: 169
Interesting, but my statement does not have test, just the -e ?
 
Old 05-30-2015, 02:41 PM   #12
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
[ is a synonym for test.
 
Old 05-30-2015, 06:07 PM   #13
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
try the findmnt command, it is part of util-linux so should be available in most systems, also lsblk is part of the same package and does a similar thing to blkid also findfs

http://linux.die.net/man/8/findmnt
 
Old 05-30-2015, 07:17 PM   #14
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
I stand corrected...

http://puppylinux.org/wikka/util-linux
 
  


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
cannot detect usb drive from vmware ufmale Linux - Hardware 1 11-01-2009 11:21 PM
can't detect usb thumb drive davidstvz *BSD 5 08-27-2008 06:13 PM
How to detect wheather usb drive is connected thru script linux1 Linux - Newbie 2 06-05-2008 09:34 AM
how to detect usb devices connected to the system vijaya_svk Linux - Software 5 03-16-2008 02:21 PM
How to open external hard drive when connected to USB??? kingslax Linux - Hardware 1 09-22-2004 06:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Puppy

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