Find the answer to your Linux question:
Results 1 to 8 of 8
I am new to linux scripting, but I have programming experience. I need to save the UUID for /dev/sda1 as a variable, lets call it id I am sure there ...
  1. #1
    Just Joined!
    Join Date
    Jul 2010
    Posts
    11

    Question [SOLVED] UUID save as a variable

    I am new to linux scripting, but I have programming experience.

    I need to save the UUID for /dev/sda1 as a variable, lets call it id

    I am sure there is a way to do this with the awk and blkid commands, but I do not know any of this well enough yet to figure it out and after a couple of hours of reading I am still lost as to exactly how I would put this together

    I need to save the uuid as a variable so that I can run an if statement with it

    if [ -f /media/$id/file ]
    then
    echo "copy successful"
    else
    echo "oh crap!"

    honestly all I need the uuid for but I cant check this by doing the same if with /dev/sda1/file so I need to be able to save the uuid into a variable

    thanks

  2. #2
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    452
    welcome to the forum

    something like this might work--

    id=`vol_id /dev/sda1 | grep ID_FS_UUID | head -n -1 | cut -b 12-`
    the sun is new every day (heraclitus)

  3. #3
    Just Joined!
    Join Date
    Jul 2010
    Posts
    11
    Quote Originally Posted by tpl View Post
    welcome to the forum

    something like this might work--

    id=`vol_id /dev/sda1 | grep ID_FS_UUID | head -n -1 | cut -b 12-`
    I tried the above exactly as typed, when I echo $id I get the entire thing as one long char string, the commands inside of that ' ' are completely ignored and considered just text.

    I took out the ' ' as well as adding the file path in front of vol_id so the compiler knows where to find it, but even with a sudo before id=........ When I echo the variable nothing happens

  4. #4
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,695
    Google: bash scripting

    Backticks

    Code:
    id=`vol_id -u /dev/sda1`

  5. #5
    Just Joined!
    Join Date
    Jul 2010
    Posts
    11
    I always get a command not found on vol_id I dont think the live linux disc I am using has that command on it.

    if I use blkid /dev/sda1 -s UUID

    I get almost what I need
    /dev/sda1: UUID="C888848E88847D26"

    anyone able to show me how to use awk or grep to just get what is in the quotes?

    I am willing to learn but the articles I find on how to use the commands dont show how to use them in the way I need to, and I don't have the time here at work to dig through 100 google search results to find the information I need.

  6. #6
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,695
    Code:
    man cut
    Code:
    blkid /dev/sda1 -s UUID | cut -d \" -f 2

  7. #7
    Just Joined!
    Join Date
    Jul 2010
    Posts
    11
    Quote Originally Posted by HROAdmin26 View Post
    Code:
    man cut
    Code:
    blkid /dev/sda1 -s UUID | cut -d \" -f 2
    THANK YOU

    This gets me exactly the 1 piece of information I need.
    The only issue I have left is that for whatever reason it is not saving it into the variable (code I am using below)

    uid= sudo blkid /dev/sda1 -s UUID |cut -d\" -f 2

    the line seems to ignore the uid= command and just displays the information, so when I try to echo the variable it is empty. If anyone has an idea let me know. otherwise I am 99% there and can probably figure it out on my own with a little tweaking

  8. #8
    Just Joined!
    Join Date
    Jul 2010
    Posts
    11

    NEVERMIND - Problem Resolved

    Quote Originally Posted by jmncee View Post
    THANK YOU

    This gets me exactly the 1 piece of information I need.
    The only issue I have left is that for whatever reason it is not saving it into the variable (code I am using below)

    uid= sudo blkid /dev/sda1 -s UUID |cut -d\" -f 2

    the line seems to ignore the uid= command and just displays the information, so when I try to echo the variable it is empty. If anyone has an idea let me know. otherwise I am 99% there and can probably figure it out on my own with a little tweaking
    Nevermind, one of my co workers pointed out that I was missing ` ` around the blkid command

    Thank you for all of your effort everyone

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...