How to choose correct /dev/sdX for a bash script
Hi there,
I have written a little script that formats a usb stick on a raspberry pi, however, it only works when I can guarentee that the usb is mounted to "dev/sda", however it is not ideal. How can I get this script to check where the USB drive is mounted? Here is the script: Code:
#!/bin/bash |
I would make that detail a passing argument to the script. Example:
Code:
#!/bin/bash Then it would not matter where the USB stick ends up mounting. |
Try:
Code:
if [ -d /dev/sda ]; then echo "Mounted" ; else "Do Stuff"; fi |
edited: per @rtmistler telling me he could not make heads or tails out of what I wrote in this post.
old I,question, new I question, Code:
sudo umount /dev/sda1 should,it,not,be,so..ething,like new should it not be something like Code:
sudo umount /dev/sdbx But,it,is,PI,so???? new: But it is PI so...? note: old: good,pratice,comments,go,on,top,then,then,code,it,is,refering,to new: good pratice comments go on top then the code it is refering to I'nn,KeyBoard,HandyCapped...:banghead: Edits made with the aid of a virtual keyboard |
Regarding the script question:
Determine the resource where the disk is, unmount it, and then perform your actions. Check out the my bash blog link in my signature for tips on bash script debugging and also there's a umount example right in there. |
Quote:
just,wondering... asapossedtothisitishardertoreadbutstillreadablejustha..etocondistiononesown..indtosortthingsoutbette risallthatisthead..antagethat..anhaso..er..achine just,being,honest logic...and...reason... |
look,sda,is,used,for,what,on,Linux?
why,would,one,want,to,u..ount,that,then,fonnate,it? but,it,is,PI,so,it,nnay,be,different,I,don't,know... just,pointing,it,out,just,in,case.. |
Quote:
Apologies, I guess I'll leave this alone because I'm unsure if you've found a solution, or if there's anything helpful I can offer here. EDIT: Further apologies, I was mistaking another member as the OP and was unclear where the question was evolving too, but have now realized my confusion. http://www.linuxquestions.org/linux/rules.html Quote:
|
Quote:
called,Florence :D |
Quote:
|
This has become a rather confusing post.
@ BMW-userx - this is a Raspberry Pi yes and thanks for pointing out about sda being for swap in most linux, my only real linux experience to date is with a raspberry pi currently. Thanks for the standard practice tip about comments, I have amended that now. @ rtmistler - Thanks for the input, I will look into this and post back if I have difficulties and will post the solution achieved when I chance upon it. And by the way, it is your bash guide for beginners that set me on this path, nice to meet the author! I've now book marked your other articles too, much nicer written that most linux beginners guides. Kind regards iFunc |
Quote:
Well, if you have made some progress or choose to ask some follow-up, please do and hopefully myself or other members will be able to offer some help. Also glad to hear that an intended helpful guide was found to be helpful. :) |
Ok,
Any alterations to this script just makes it unreliable and prone to errors. To be fair with the original script it works flawlessly so long as the raspberry pi is in a freshly rebooted state (i.e. no devices have been attached to it since it was last booted). I'm just concerned that when it comes to an event and I am not there, then is relied upon script will fall over due to the usb stick then deciding to mount on /dev/sde instead of /dev/sda. If the script could check whether it is mounted on sda I could get it to pop up a message requesting a reboot or something, but currently, it either works or does not work and all because I can't find the syntax for this issue. |
this is where you get to not only learn about error handling but get to put it into pratice.
think about it. What is it you do not want to take place? what would you have to check for to ensure that it does not? if it did then how would you personally handle it? forumulate it into an IF Statment. example: Code:
MOD: Note: As @Habitual already pointed out in post #3 |
Quote:
|
All times are GMT -5. The time now is 06:55 AM. |