LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to choose correct /dev/sdX for a bash script (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-choose-correct-dev-sdx-for-a-bash-script-4175573001/)

iFunction 02-23-2016 06:02 AM

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

NAME="USB_Format"; echo -en "\033]0;$NAME\a"
clear
sleep 0.75s
wmctrl -a "USB_Format"
# clears screen and keeps focus on window

echo "Enter event name >"
read -r event

event=${event^^}
event=${event// /_}

sudo umount /dev/sda1
# If the usb stick is mounted during fdisk, it needs rebooting
# before any other commands can run, so unmounting the usb helps
# the script run better.



(echo d; echo o; echo n; echo p; echo 1; echo ; echo ; echo t; echo b; echo w) |sudo fdisk /dev/sda
# this command passes the required user input:
# d - deletes partition.
# o - creates a new DOS partition table.
# n - creates a new partition formatted to 'linux' and requires the following inputs
# p - choose primary partition - default.
# 1 - choose first partition - default.
#  - default start sector of partition.
#  - default end sector of partition - we are using entire usb.
# t - changes partition format.
# b - code for W95 DOS format - 'l' lists all the formats
# w - saves and exits - 'q' exits without excecuting the formatting' you have been warned.

sudo mkdosfs -n "$event" /dev/sda1 -s 128 -F 32 -I
# This line formats the partition to FAT32 and names it.

sudo umount /dev/sda1
# again, to make sure the usb stick is unmounted before it is removed.

clear
sleep 0.75s
wmctrl -a "USB_Format"
read -p "Format complete, please remove usb and hit [enter] key to clear window. "


rtmistler 02-23-2016 06:40 AM

I would make that detail a passing argument to the script. Example:
Code:

#!/bin/bash

# Use to enable debugging
#set -xv

DRIVE=$1

And then use the variable $DRIVE in the remainder of your script. You'd invoke the script with /dev/sda or /dev/sd<something>.

Then it would not matter where the USB stick ends up mounting.

Habitual 02-23-2016 07:23 AM

Try:
Code:

if [ -d /dev/sda  ]; then echo "Mounted" ; else "Do Stuff"; fi

BW-userx 02-23-2016 08:34 AM

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
# If the usb stick is mounted during fdisk, it needs rebooting

old
should,it,not,be,so..ething,like

new
should it not be something like

Code:

sudo umount /dev/sdbx
or
sudo umount /dev/sdcx
as
/dev/sda1
old
is,Linux,install,or,swap,space???
new
is Linux install or swap space???

old:
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

rtmistler 02-23-2016 08:43 AM

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.

BW-userx 02-23-2016 09:13 AM

Quote:

Originally Posted by rtmistler (Post 5504804)
Well please fix that because the rest of your question makes little sense.

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.

what,is,throwing,your,brain,off?the....->,
just,wondering...
asapossedtothisitishardertoreadbutstillreadablejustha..etocondistiononesown..indtosortthingsoutbette risallthatisthead..antagethat..anhaso..er..achine

just,being,honest

logic...and...reason...

BW-userx 02-23-2016 09:26 AM

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..

rtmistler 02-23-2016 09:31 AM

Quote:

Originally Posted by BW-userx (Post 5504826)
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..

A suggestion is to not use text speak or severe abbreviations in your questions or comments.

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:

All member-created content should be in English. This allows our moderators to ensure all content complies with all LQ rules. In addition, we recommend you avoid sms/l33t speak in the technical fora. Avoiding sms/l33t speak will improve question clarity and increase the chance of receiving a helpful response.

BW-userx 02-23-2016 10:10 AM

Quote:

Originally Posted by rtmistler (Post 5504827)
A suggestion is to not use text speak or severe abbreviations in your questions or comments.

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.

http://www.linuxquestions.org/linux/rules.html

I'nn,about,to,post,question,on,how,the,hell,does,one,get,this..irtual,Keyboard,to,work?
called,Florence
:D

BW-userx 02-23-2016 10:42 AM

Quote:

Originally Posted by rtmistler (Post 5504827)
A suggestion is to not use text speak or severe abbreviations in your questions or comments.

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.

http://www.linuxquestions.org/linux/rules.html

it,is,english,and,what,abbreviations,are,you,talking,about??

iFunction 02-24-2016 04:39 AM

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

rtmistler 02-24-2016 07:20 AM

Quote:

Originally Posted by iFunction (Post 5505329)
This has become a rather confusing post.

My apologies iFunction. I neglected to realize that you were the original poster versus BW-userx.

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. :)

iFunction 02-25-2016 05:58 AM

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.

BW-userx 02-25-2016 06:32 AM

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:


if [[ ! what I want ]] ; then
    do not do what I wanted you to
else
    go ahead on with it
fi

Now you do know that you have two fields of logic to use, and that was only one of them that I showed you.

MOD:
Note:
As @Habitual already pointed out in post #3

rtmistler 02-25-2016 07:46 AM

Quote:

Originally Posted by BW-userx (Post 5505904)
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:


if [[ ! what I want ]] ; then
    do not do what I wanted you to
else
    go ahead on with it
fi

Now you do know that you have two fields of logic to use, and that was only one of them that I showed you.

MOD:
Note:
As @Habitual already pointed out in post #3

To second those thoughts, you may want to consider two possibilities:
  1. Issuing the mount command, piping that to a grep for the sda string and then checking the command result code, $?
  2. Issuing a umount command whether or not the stick is mounted will just guarantee that it is not mounted. However I realize that one problem is if the resource is not on sda
You may have to write a function or portion of the script to discern what "sd" resource the USB stick is attached too prior to either checking for mount or just issuing a blanket umount command


All times are GMT -5. The time now is 06:55 AM.