LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 01-31-2018, 07:09 AM   #1
BinaryDemon
LQ Newbie
 
Registered: Jan 2018
Posts: 8

Rep: Reputation: Disabled
Script Help


The issue:

I'm attempting to create a LiveUSB distro and I need to identify what drive/device the LiveUSB is running on. Since it's a LiveUSB the boot device can change depending on what computer its plugged into sometimes it's sda1, or sdb1, sdd1, ect.

I noticed some LiveCD's make this easy since the root directory is usually linked to /media, but I'm modifying tinycore as a base which doesn't do this. Fortunately it will be the only device mounted automatically. I know there are a few command line commands that can list attached devices like mount but how do I grab the output in a usable string for use in a script. The device LABEL will always be the same predefined string if that helps in anyway.

Summary: What I want is to identify the boot device by script rather than relying on user input. How do I capture the deviceid in a string so I can do:

mnt/$deviceid/games


Thanks in advance for your help!
 
Old 01-31-2018, 08:01 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
What I commonly state is that anything you can type in a command line you can put into a bash script.

This process works better if you post what you do know and do have for output now so that people can help you better.
 
Old 02-01-2018, 02:56 PM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
sounds like you want UUIDs.
 
Old 02-01-2018, 06:54 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Maybe (untested on live system)
Code:
root=$(mount | awk '$3 == "/" { print $1}')
device=$(echo $root | sed 's/[0-9]*//g')
 
1 members found this post helpful.
Old 02-01-2018, 10:00 PM   #5
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,967

Rep: Reputation: 271Reputation: 271Reputation: 271
Both mount and df return the device mounted on /
 
Old 02-06-2018, 07:33 AM   #6
BinaryDemon
LQ Newbie
 
Registered: Jan 2018
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by keefaz View Post
Maybe (untested on live system)
Code:
root=$(mount | awk '$3 == "/" { print $1}')
device=$(echo $root | sed 's/[0-9]*//g')
Thanks, I'll try this soon and let you know if it works.
 
Old 02-06-2018, 04:52 PM   #7
BinaryDemon
LQ Newbie
 
Registered: Jan 2018
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thanks for the help, I think I need 1 more small tweak.

That function returns rootfs which is the 1st word in the 1st line of what the mount command returns :

rootfs on / type rootfs (blahblahblah)

but not what I want. I guess I didnt explain well when I mentioned there would only be 1 device mounted, there is a ton of listings for type squashfs mounts.

Mount -t vfat will return the line I want:

/dev/sdb1 on /mnt/sdb1 type vfat (moreinfothananyonecouldeverwant...)

Keep in mind sdb1 can change, and thats what Im trying to capture

I don't understand your string parsing magic to adapt:

root=$(mount -t vfat| awk '$3 == "/" {print $1}')
device=$(echo $root | sed 's/[0-9]*//g')

It seems to be returning null right now.
 
Old 02-09-2018, 09:28 AM   #8
BinaryDemon
LQ Newbie
 
Registered: Jan 2018
Posts: 8

Original Poster
Rep: Reputation: Disabled
Any more ideas?

Last edited by BinaryDemon; 02-09-2018 at 12:14 PM.
 
Old 02-09-2018, 09:33 AM   #9
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
wrong post...
 
Old 02-09-2018, 09:41 AM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,798

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
A tweak is to ensure that the first field starts with a / character
Code:
mount | awk '$1 ~ "^/" && $3 == "/" { print $1 }'
When awk exits before the pipe is finished then a SIGPIPE is sent to mount which will terminate. This can be faster on a big systems with many mounts:
Code:
mount | awk '$1 ~ "^/" && $3 == "/" { print $1; exit }'
 
1 members found this post helpful.
Old 02-12-2018, 06:18 PM   #11
BinaryDemon
LQ Newbie
 
Registered: Jan 2018
Posts: 8

Original Poster
Rep: Reputation: Disabled
Well lets call this solved... I eventually went with

bootdev=$(mount -t vfat | cut -c6-9)

Unfortunately shortly after that I learned you can't create Symbolic Links on Fat32 filesystems, making the whole point of this moot.

Thanks for the help anyway guys.
 
  


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
Shell script/Perl Script to remove the string until it finds special character '_' pooppp Programming 10 07-17-2012 09:36 AM
Shell script/Perl Script to remove the string until it finds special character '_' pooppp Programming 1 07-13-2012 01:03 AM
Shell script, Perl script, command or utility to convert Binary to text Perseus Programming 26 07-12-2012 06:00 AM
[SOLVED] bash and xterm: how make apps started by and for a script persist when script terminates porphyry5 Linux - General 4 06-15-2011 01:27 PM
[SOLVED] Script question: create a shell script in kde to log in on a server with ssh c4719929 Linux - Newbie 1 01-31-2011 03:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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