LinuxQuestions.org
Visit Jeremy's Blog.
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 02-06-2011, 08:19 AM   #1
Susy
LQ Newbie
 
Registered: Feb 2011
Posts: 2

Rep: Reputation: 0
Exclamation Help me! Script bash - Find the right device


Hi guys,
I need help with my bash script.

I would like to find the name of my USB device, searching myFile.txt.

So using "mount" command I found all mounted devices, for example:

>mount

/dev/sda1 on /media/usb1 [...]
/dev/sdb1 on /media/usb2 [...]

I would like to verify if myFile.txt exists in /media/usb1 or in /media/usb2, but I don't know to traslate it in a bash code.

Thanks,

Susy
 
Old 02-06-2011, 08:24 AM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Hi and welcome to LQ.

The following code should get you started:

Code:
if [ -f /path/to/testfile ] 
then 
  echo "testfile exists!" 
else
  echo "testfile does not exist"
fi
If you don't know the exact location of your file, you might want to read on the 'find' command.
http://content.hccfl.edu/pollock/unix/findcmd.htm

Last edited by sycamorex; 02-06-2011 at 08:26 AM.
 
Old 02-06-2011, 09:22 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Something like (not tested) ...
Code:
while read -a my_array
do
    find ${my_array[2]} -type f -iname myFile.txt
done <<< "$( mount | grep media )"
 
Old 02-06-2011, 01:03 PM   #4
Susy
LQ Newbie
 
Registered: Feb 2011
Posts: 2

Original Poster
Rep: Reputation: 0
Thank you.

Tomorrow I'll try both solutions.
 
Old 02-06-2011, 01:55 PM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Your problem is actually locating the mounted device containing a specific file; sycamorex and catkin already answered that quite well.

I've had to solve a situation where the user should be able to select any USB storage device, whether or not it is mounted.
This is also easy to solve, without elevated privileges; you can find this out as a normal user without root access.
In case somebody reading this topic happens to need it, here's the code I've used:
Code:
#!/bin/bash
Device=()
Mounts=()
Name=()
Size=()
for node in /dev/sd[a-z] ; do
    dev="${node#/dev/}"
    removable=$[ `cat /sys/block/$dev/removable` -0 ]
    if [ $removable -eq 1 ]; then
        Device=("${Device[@]}" "$node")
        Mounts=("${Mounts[@]}" `grep -ce "^$node" /proc/mounts`)
        Name=("${Name[@]}" "`cat /sys/block/$dev/device/vendor` `cat /sys/block/$dev/device/model`")
        Size=("${Size[@]}" $[(`cat /sys/block/$dev/size`-0)/2])
    fi
done

i=0
while [ $i -lt ${#Device[@]} ]; do
    d=${Device[i]}
    m=${Mounts[i]}
    n=${Name[i]}
    s=${Size[i]}
    i=$[i + 1]
    echo "$d:"
    echo " Name: $n"
    echo " Size: $s kB"
    case $m in
        0) echo " Mounted: No" ;;
        1) echo " Mounted: Yes, 1 partition" ;;
        *) echo " Mounted: Yes, $m partitions" ;;
    esac
    echo ""
done
First part will fill in four parallel arrays: Device, Mounts, Name, and Size.
"${Device[0]}" is the device node for the first device,
${Mounts[1]} contains the number of partitions mounted on the second device, zero if none,
"${Name[0]}" is the vendor and model name for the first device, and
${Size[1]} is the size of the second device in kilobytes (1024 byte blocks).
The number of devices found is of course ${#Device[@]}.

The second part just outputs the list. Replace it with your own selection routines. I normally list one device per line, identified by a letter, and let the user select one or more devices by specifying their letters. Works quite reliably.

Hope this helps someone,
Nominal Animal

Last edited by Nominal Animal; 03-21-2011 at 07:19 AM.
 
  


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
[SOLVED] Using 'find' in a bash script Master_Ne0 Linux - Software 6 11-18-2010 01:00 PM
[SOLVED] Check if device is mounted in a bash script wael_h Slackware 5 08-01-2010 01:05 PM
[SOLVED] Detect a CD-ROM device in a bash shell script kushalkoolwal Programming 3 08-21-2009 05:41 PM
Detect if a network device is connected in a bash script jordanwb Linux - General 11 12-29-2008 11:55 AM
how to find files using bash script prernasin Linux - Newbie 6 09-26-2007 08:57 AM

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

All times are GMT -5. The time now is 01:03 PM.

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