Find the answer to your Linux question:
Results 1 to 3 of 3
Example, I want to extract the first four character from serial and store it into version. Code: serial= 10001100 version=...(codes).... echo $version output in the terminal: 1000 Just to show ...
  1. #1
    Just Joined!
    Join Date
    Apr 2011
    Posts
    5

    Extracting nth length of character from a variable to another variable

    Example, I want to extract the first four character from serial and store it into version.

    Code:

    serial= 10001100
    version=...(codes)....
    echo $version


    output in the terminal:
    1000

    Just to show you guys i havent been lazy...

    Code:
    #!/bin/bash
    
    serial=10110011
    echo $serial> serial.txt
    
    version= cut -c1-4  serial.txt
    
    echo $version
    but this code saves the value into a text file. I want a code that does not save into any file whatsoever. Help? Thanks.

  2. #2
    Just Joined!
    Join Date
    Apr 2011
    Posts
    5
    Quote Originally Posted by hayloiuy View Post
    Example, I want to extract the first four character from serial and store it into version.

    Code:

    serial= 10001100
    version=...(codes)....
    echo $version


    output in the terminal:
    1000

    Just to show you guys i havent been lazy...

    Code:
    #!/bin/bash
    
    serial=10110011
    echo $serial> serial.txt
    
    version= cut -c1-4  serial.txt
    
    echo $versiong
    but this code saves the value into a text file. I want a code that does not save into any file whatsoever. Help? Thanks.
    Something like this?

    Code:
    #!/bin/bash
    
    serial=10110011
    version=$(echo serial|cut -c1-4)
    
    echo $version
    Last edited by heliumhe; 05-02-2011 at 08:59 PM. Reason: fixed broken html-tag

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,970
    Actually, you can do this pretty easily by using back-quotes around the cut command so you can take the output and assign it to the $version variable. IE,
    Code:
    #!/bin/bash
    
    serial=10110011
    echo $serial
    
    version=`echo $serial | cut -c1-4`  
    
    echo $version
    Remember, however, that $version is only going to be visible inside this script. If you want to make it available in your runtime environment, then source it. IE, file getversion.sh =
    Code:
    serial=10110011
    echo $serial
    
    version=`echo $serial | cut -c1-4`  
    
    echo $version
    Now, you can see $version if you do this from your command line:
    Code:
    source getversion.sh
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

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