Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Bash script - how do I modify varible data with another varible?

  1. #1
    Join Date
    Apr 2006
    Beans
    417

    Bash script - how do I modify varible data with another varible?

    I'm building a bash script with the help of the book I bought. I can't find the syntax to accomplish one part of it. I have a variable "part" that is always 4 characters, three letters and one number at the end, example sda1 (yes it is a hard drive). I want the take the number and add it to another variable with just 3 letters. For example take variable "target" with data sdb and modify it to be sdb1, with the number from variable "part"
    what is a simple way to express this in bash syntax?

  2. #2
    Join Date
    Jul 2006
    Location
    somewhere :)
    Beans
    535
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Bash script - how do I modify varible data with another varible?

    Quote Originally Posted by bakegoodz View Post
    I'm building a bash script with the help of the book I bought. I can't find the syntax to accomplish one part of it. I have a variable "part" that is always 4 characters, three letters and one number at the end, example sda1 (yes it is a hard drive). I want the take the number and add it to another variable with just 3 letters. For example take variable "target" with data sdb and modify it to be sdb1, with the number from variable "part"
    what is a simple way to express this in bash syntax?
    target=$target`echo $part | cut -c4`
    there are 10 types of people in the world: those that understand binary and i don't know who the other F are.

  3. #3
    Join Date
    Feb 2007
    Beans
    2,339
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Bash script - how do I modify varible data with another varible?

    Code:
    a=sda1
    b=sdb
    
    b="$b"`echo "$a" | cut -c4`
    EDIT:
    Opps, late.

  4. #4
    Join Date
    Jan 2008
    Beans
    4,757

    Re: Bash script - how do I modify varible data with another varible?

    You'll be better off using sh's built-in substr function.
    Code:
    A=sda1
    B=sdb
    
    B=$B${A:3}
    edit:
    To elaborate briefly on how it works (using an example case).
    Code:
    test=abcdefghijklmnop
    
    echo ${test}     # abcdefghijklmnop
    echo ${test:1}   # bcdefghijklmnop
    echo ${test:3}   # defghijklmnop
    echo ${test:7}   # hijklmnop
    echo ${test: -1} # p
    echo ${test: -3} # nop
    echo ${test: -7} # jklmnop
    Regards
    Iain
    Last edited by ibuclaw; January 31st, 2010 at 10:46 PM.

  5. #5
    Join Date
    Jul 2006
    Location
    somewhere :)
    Beans
    535
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Bash script - how do I modify varible data with another varible?

    an interesting page on string manipulation in bash:
    http://tldp.org/LDP/abs/html/string-manipulation.html
    there are 10 types of people in the world: those that understand binary and i don't know who the other F are.

  6. #6
    Join Date
    Apr 2006
    Beans
    417

    Re: Bash script - how do I modify varible data with another varible?

    Thanks for your help. One more question, I need the opposite too, take variable data like sda1 and remove the 1, so it is only sda.

    I'm looking at this page, but I can't quite figure out the right syntax.
    http://tldp.org/LDP/abs/html/paramet...stitution.html

  7. #7
    Join Date
    Jun 2008
    Location
    California
    Beans
    2,271
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Bash script - how do I modify varible data with another varible?

    Quote Originally Posted by bakegoodz View Post
    Thanks for your help. One more question, I need the opposite too, take variable data like sda1 and remove the 1, so it is only sda
    To continue with tinivole's post, you can do this as shown below. The 0 is position and the 3 is length.

    $ test=sda1 ; echo ${test:0:3}
    sda
    Another method is to remove the last character from the string:

    $ test=sda1 ; echo ${test%?}
    sda

  8. #8
    Join Date
    Apr 2006
    Beans
    417

    Re: Bash script - how do I modify varible data with another varible?

    I tested it out and this is my result, what am I doing wrong?

    part=sda1; drive='echo ${part:0:3}'
    echo $drive
    echo ${part:0:3}

  9. #9
    Join Date
    Jan 2007
    Location
    Michigan, USA
    Beans
    1,184
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Bash script - how do I modify varible data with another varible?

    I wrote that into a script I called test.sh

    Code:
    #!/bin/bash
    part=sda1; drive='echo ${part:0:3}'
    echo $drive
    echo ${part:0:3}
    then when I run it I get
    sda

    So it's correct for getting the characters from position 0 to 3.

    Oh, and you didn't post any results.

  10. #10
    Join Date
    Jun 2008
    Location
    California
    Beans
    2,271
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Bash script - how do I modify varible data with another varible?

    Quote Originally Posted by bakegoodz View Post
    I tested it out and this is my result, what am I doing wrong?

    part=sda1; drive='echo ${part:0:3}'
    echo $drive
    echo ${part:0:3}
    If the goal is to get the string sda into the variable named drive, then you do not need to use the echo command or command substitution.

    $ part=sda1; drive=${part:0:3} ; echo $drive
    sda
    Perhaps I don't understand what you are trying to do.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

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