Extract variable name in a string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Extract variable name in a string
# 8  
Old 12-12-2013
This is a very longhand DEMO using shell builtins only...
Code:
#!/bin/sh
# Create something like your file.
> /tmp/extract.sh
chmod 755 /tmp/extract.sh
echo "#!/bin/sh" >> /tmp/extract.sh
echo "ENVIR=bin" >> /tmp/extract.sh
echo "CMND=ear" >> /tmp/extract.sh
echo "/\$ENVIR/cl\$CMND" >> /tmp/extract.sh
# Done!

# Get an array of the file using newlines for IFS...
array=($(cat /tmp/extract.sh))
# eval "${array[0]}"
# Get first variable and value...
eval "${array[1]}"
# Get second variable and value...
eval "${array[2]}"
# Now create a new variable called 'command_string'...
eval command_string="${array[3]}"

# Print these to the terminal window...
echo $ENVIR
echo $CMND
echo "$command_string"

# Now use '/' as the IFS...
ifs_str="$IFS"
IFS="/"
# Re-use 'array' for this demo...
array=($command_string)
mycommand="${array[1]}"
echo "$mycommand"
mycommand="${array[2]}"
echo "$mycommand"
# Reset IFS...
IFS="$ifs_str"
# Now execute the genuine clear command
/usr/bin/$mycommand
echo "DONE! Now scroll up your terminal window to see the results..."
# Done!

Results including THE clear screen...
Code:
Last login: Thu Dec 12 20:45:58 on ttys000
AMIGA:barrywalker~> chmod 755 variable.sh
AMIGA:barrywalker~> ./variable.sh
bin
ear
/bin/clear
bin
clear
















DONE! Now scroll up your terminal window to see the results...
AMIGA:barrywalker~> _

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search String and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

2. Shell Programming and Scripting

Extract a string from a file and store it in variable

Hi, I've a file ImageSizeDetails.txt with the following contents: Image Name: swncd 01.10.00.04 Created: Wed Jan 9 14:05:48 2013 Image Type: ARM Linux Multi-File Image (gzip compressed) Data Size: 7351011 Bytes = 7178.72 kB = 7.01 MB Load Address: 00008000 Entry Point: ... (6 Replies)
Discussion started by: Parameswaran
6 Replies

3. Shell Programming and Scripting

Search for string in a file, extract two another strings and concatenate to a variable

I have a file with <suit:run date="Trump Tue 06/19/2012 11:41 AM EDT" machine="garg-ln" build="19921" level="beta" release="6.1.5" os="Linux"> Need to find word "build" then extract build number, which is 19921 also release number, which is 6.1.5 then concatenate them to one variable as... (6 Replies)
Discussion started by: garg
6 Replies

4. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. Shell Programming and Scripting

sed - extract string before "/" from variable- linux 2.6.9-89

Hi, I have a FTP script which gets called from a wrapper script that exports a variable having value as: "/export/home/dips/logs/dipsSFTP_file1.log.YYYYMMDDHHMISS". I want to extract the file name "dipsSFTP_file1.log.YYYYMMDDHHMISS" and the dir path "/export/home/dips/logs/" from this... (4 Replies)
Discussion started by: dips_ag
4 Replies

6. Shell Programming and Scripting

sub-string extract between variable delimiters

I need to extract certain pieces from a string, wher delimiters may vary. For example A0 B0 C0 12345677 X0 Y0 Z0 A1-B1 C1 12345678 X1 Y0 Z0 A1/B2 C77 12345679 X2 Y0 Z0 I need to get C0 12345677 X0 C1 12345678 X1 C77 12345679 X2 I tried sed, see example below: echo 'A0 B0... (2 Replies)
Discussion started by: migurus
2 Replies

7. Shell Programming and Scripting

Search for string in a file and extract another string to a variable

Hi, guys. I have one question: I need to search for a string in a file, and then extract another string from the file and assign it to a variable. For example: the contents of the file (group) is below: ... ftp:x:23: mail:x:34 ... testing:x:2001 sales:x:2002 development:x:2003 ...... (6 Replies)
Discussion started by: daikeyang
6 Replies

8. UNIX for Dummies Questions & Answers

Using GREP to extract variable following a string

Hello, I'm running calculations and I need to extract a specific number from a output file. So far I've only been able to GREP entire lines containing the string: '1 F=' . I would like to go a step further and extract just the number following '1 F='. The entire line looks like: 1 F=... (10 Replies)
Discussion started by: modey3
10 Replies

9. Shell Programming and Scripting

How to extract variable number from a String

hi,I am new to shell script,I have String,like this: Number of rows exported: 5321 the numbe at end could changing,how can I extract this number and assign it to a variable,then use it later in script. thanks. (19 Replies)
Discussion started by: vitesse
19 Replies

10. UNIX for Dummies Questions & Answers

extract from string variable into new variables

I have a variable which consists of a string like this: 001 aaabc 44 a bbb12 How do I extract each substring, delimited by the spaces, into new variables - one for each substring? eg var1 will be 001, var2 will be aaabc, var3 will be 44, var4 will be a, etc? I've come up with this:... (2 Replies)
Discussion started by: Sniper Pixie
2 Replies
Login or Register to Ask a Question