Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
And i want to store this in a variable so i can update the fstab file later in the script.
I realise there's probably a few ways to do this, so please feel free to offer alternatives,but i'd be grateful if you could offer an exaplanation of how each option works.
For example, one thing I'm struggling with is where the output of blkid /dev/sdb1 gets stored when it is run within my script. Is it $?
many thanks,
Last edited by ButterSideUp; 09-23-2012 at 02:41 PM.
The output of any command goes to "standard output"---unless you tell the system otherwise.
To store something in a variable, simply set the variable using "=". If the value to be set is from another command, then this can be captured using "$(<command>)".
Try this in a terminal:
Code:
[mherring@herring_lap ~]$ a=23
[mherring@herring_lap ~]$ echo $a
23
[mherring@herring_lap ~]$ a="the quick brown fox"
[mherring@herring_lap ~]$ echo $a
the quick brown fox
[mherring@herring_lap ~]$ b=$(echo $a)
[mherring@herring_lap ~]$ echo $b
the quick brown fox
[mherring@herring_lap ~]$
Thanks for that. I maybe didn't write my post as accurately as i should have.
My question is:
1. In a script, how do i extract just the UUID string from the blkid /dev/sdb1 command?
2. I asked about assigning the output to a variable because i don't understand where information gets stored as it is derived within the script.
Commands in a script work exactly the same as on the command line. Meaning if you run "blkid /dev/sdb1" in the script, output will be sent to the screen, just as it would if you ran it from the command line. As pixellany said, if you want to capture this output you'll need to send it to a variable:
Code:
output=$(blkid /dev/sdb1)
You can then use any number of commands to extract a piece of this (just the UUID).
Thanks for that. I maybe didn't write my post as accurately as i should have.
My question is:
1. In a script, how do i extract just the UUID string from the blkid /dev/sdb1 command?
2. I asked about assigning the output to a variable because i don't understand where information gets stored as it is derived within the script.
many thanks
2. It goes to standard output!! If you don't direct it somewhere, think of it as going to the terminal screen that noone is looking at.....
1. Extract using grep:
Code:
blkid /dev/sda1 | egrep -o 'UUID[^ ]*'
many other ways to do this--eg SED. (The regex reads "UUID, followed by any number of characters that are not spaces")
I recommend running some experiments in the terminal---before trying to make a script work.
We've seen the assignment to a variable already. Another method is called "redirection"---here's an example:
Code:
[mherring@herring_lap ~]$ x="she sells seashells by the seashore"
[mherring@herring_lap ~]$ echo $x
she sells seashells by the seashore
[mherring@herring_lap ~]$ echo $x > sea_file
[mherring@herring_lap ~]$ more sea_file
she sells seashells by the seashore
[mherring@herring_lap ~]$ echo "saltwater is not good to drink" >> sea_file
[mherring@herring_lap ~]$ more sea_file
she sells seashells by the seashore
saltwater is not good to drink
[mherring@herring_lap ~]$
So---">" means "send output to <filename>"
">>" means the same thing, but the data is appended to what is already there
I've realised that /store already exists in my fstab file and that if i use this
line it will obviously append a new entry for /store to the file, which will mean
i have 2 /store entries.
So, i need to either edit my fstab entry for /store with this:
$a /store ext4 noatime,noauto,nobarrier 0 0
or,
append the new /store line and then delete the old one.
Use this to create a new version without the store line, then edit the new one as above
Code:
grep -v store /etc/fstab >/etc/fstab.new
then check the new one by eye.
If its all good, you can overwrite the original with the new.
In any case, you should always backup a critical file like this before messing with it eg
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.