LinuxQuestions.org
Review your favorite Linux distribution.
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 09-23-2012, 02:40 PM   #1
ButterSideUp
LQ Newbie
 
Registered: Sep 2012
Location: Holywood
Distribution: RHEL
Posts: 12

Rep: Reputation: Disabled
How to extract a string into a variable


Although i can do a lot of the things i need to do, I'm new to bash, so please bear with me.

I have a script, in which I need to extract just the the UUID for my iscsi device partition;

blkid /dev/sdb1:

UUID="5a34315a-e9e9-4d01-823b-d07b8c6195d8" TYPE="ext4"

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.
 
Old 09-23-2012, 02:50 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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 ~]$
 
Old 09-23-2012, 03:00 PM   #3
ButterSideUp
LQ Newbie
 
Registered: Sep 2012
Location: Holywood
Distribution: RHEL
Posts: 12

Original Poster
Rep: Reputation: Disabled
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
 
Old 09-23-2012, 03:06 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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).
 
Old 09-23-2012, 03:16 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by ButterSideUp View Post
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")
 
Old 09-23-2012, 03:24 PM   #6
ButterSideUp
LQ Newbie
 
Registered: Sep 2012
Location: Holywood
Distribution: RHEL
Posts: 12

Original Poster
Rep: Reputation: Disabled
Ok that's great, thanks!, but i'm still being stupid here.

I kinda understand what you are saying, but how do i access the standard output value within the script?

Do i assign the entire command to a variable, if so maybe it would be easier to just see the syntx. Sorry, i'm being dumb!!!

I tried to use "$(<command>)" as you suggested, but can't quite get it working.

Last edited by ButterSideUp; 09-23-2012 at 03:36 PM.
 
Old 09-23-2012, 03:40 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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
 
Old 09-23-2012, 03:51 PM   #8
ButterSideUp
LQ Newbie
 
Registered: Sep 2012
Location: Holywood
Distribution: RHEL
Posts: 12

Original Poster
Rep: Reputation: Disabled
Thanks that's really helpful. So, i have this:

[root@qradar tim]# blkid /dev/sdb1 | egrep -o 'UUID[^ ]*'
UUID="5a34315a-e9e9-4d01-823b-d07b8c6195d8"

I'm trying to assign the command to a variable as below but not working:

[root@qradar tim]# <variable>= blkid /dev/sdb1 | egrep -o 'UUID[^ ]*'

Also, i don't need "UUID" just the number.
 
Old 09-23-2012, 03:53 PM   #9
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by ButterSideUp View Post
Thanks that's really helpful. So, i have this:

[root@qradar tim]# blkid /dev/sdb1 | egrep -o 'UUID[^ ]*'
UUID="5a34315a-e9e9-4d01-823b-d07b8c6195d8"

I'm trying to assign the command to a variable as below but not working:

[root@qradar tim]# <variable>= blkid /dev/sdb1 | egrep -o 'UUID[^ ]*'

Also, i don't need "UUID" just the number.
You missed the $()
Code:
<variable>=$(blkid /dev/sdb1 | egrep -o 'UUID[^ ]*')
 
Old 09-23-2012, 04:07 PM   #10
ButterSideUp
LQ Newbie
 
Registered: Sep 2012
Location: Holywood
Distribution: RHEL
Posts: 12

Original Poster
Rep: Reputation: Disabled
Ah brilliant thanks, i get it now.

My script will do 2 things to the fstab file:

1 change /store to /store_old
2.set the UUID and file options for /store

The value derived into the variable in your comment above:

[root@qradar tim]# echo $a
UUID="5a34315a-e9e9-4d01-823b-d07b8c6195d8"

is different from the fstab entry because it includes " "

UUID=240ecbe0-6e44-4e0d-8d5d-efd8ba4f867a /store ext4 defaults,noatime,nobarrier 1 2

##### 6: Edit the /etc/fstab file. ##########################################################################

echo "%%%%% Step6: Edit the etc/fstab file. %%%%%"

sed -i 's/LABEL=\/store.*\/store /LABEL=\/store \/store_old/' /etc/fstab
echo "/dev/$DEVICE /store ext4 noatime,nobarrier 0 0" >> /etc/fstab
 
Old 09-23-2012, 04:11 PM   #11
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
You can remove the quotes with a simple bash string replace
Code:
$ a='UUID="5a34315a-e9e9-4d01-823b-d07b8c6195d8"'
$ echo $a
UUID="5a34315a-e9e9-4d01-823b-d07b8c6195d8"
$ echo ${a//\"/}
UUID=5a34315a-e9e9-4d01-823b-d07b8c6195d8
 
Old 09-23-2012, 04:55 PM   #12
ButterSideUp
LQ Newbie
 
Registered: Sep 2012
Location: Holywood
Distribution: RHEL
Posts: 12

Original Poster
Rep: Reputation: Disabled
Again, very helpful indeed!

My final question is about this line:

echo "/dev/$DEVICE /store ext4 noatime,nobarrier 0 0" >> /etc/fstab

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.

Stuck again!!!
 
Old 09-23-2012, 05:53 PM   #13
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
It's time for this:
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html

To answer your specific question:

You can of course edit the fstab file directly...

To do a replacement, look at SED also---for example, to edit a file "in place", replacing "fred" with "ralph", you would do this:
Code:
sed -i 's/fred/ralph/g' filename
The ultimate SED tutorial here:
http://www.grymoire.com/Unix/Sed.html
 
Old 09-23-2012, 05:58 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
Code:
cp /etc/fstab /etc/fstab.<YYYYMMDD>
 
Old 09-23-2012, 07:42 PM   #15
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
for
Quote:
cp /etc/fstab /etc/fstab.<YYYYMMDD>
I prefer
Quote:
mv /etc/fstab /etc/fstab.<YYYYMMDD>
cp /etc/fstab.<YYYYMMDD> /etc/fstab
since it retains the original time stamp on the backed up - original - file (/etc/fstab.<YYYYMMDD>) and is useful to restore back if needed.

OK
 
  


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
how to extract from a string Owais.Ahmad Linux - Newbie 13 07-27-2012 06:13 AM
extract a string within a string using a pattern adshocker Linux - Newbie 1 11-04-2010 10:44 PM
Extract value from string ewingtux Programming 1 12-27-2008 07:19 PM
Help: removing a variable substring from a string variable in sh script gnparsons Programming 2 06-04-2008 05:21 PM
extract string from file to variable [BASH] NikosNL Programming 13 05-07-2008 09:43 AM

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

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