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 ...
- 05-02-2011 #1Just 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...
but this code saves the value into a text file. I want a code that does not save into any file whatsoever. Help? Thanks.Code:#!/bin/bash serial=10110011 echo $serial> serial.txt version= cut -c1-4 serial.txt echo $version
- 05-02-2011 #2Just Joined!
- Join Date
- Apr 2011
- Posts
- 5
- 05-02-2011 #3Linux Guru
- 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,
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:#!/bin/bash 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:serial=10110011 echo $serial version=`echo $serial | cut -c1-4` echo $version
Code:source getversion.sh
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote
