Identify variables used in the shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Identify variables used in the shell script
# 1  
Old 03-28-2013
Identify variables used in the shell script

Hi,

Is there any simple way to get/identify the variables that are used in a file and print those variable names. If I have something like this in a file,

Code:
 
/$var/temp_dir/${var2}${var3}.log

I want to display the variables 'var', 'var2' and 'var3' from that file. I tried something like this,

Code:
$ echo '/$var/temp_dir/${var2}${var3}.log' | grep -v '\$\$' | sed -e 's/\$/\n\$/g' | sed -e 's/\/*//g' -e 's/\..*//g'

$var
${var2}
${var3}

It seems to be working.. but I dont think it works in all cases.

I think that we can apply a logic that identifies everything after $ sign is considered as variable name until it finds out a character that cant be a part of variable. But not sure how to implement it.

Can any one please help me?

Thanks in advance.

---------- Post updated at 06:49 PM ---------- Previous update was at 04:03 AM ----------

Anyone, please help me...
# 2  
Old 03-28-2013
Shell Command Language may be helpful, as it defines the actual format of variables (parameters).
# 3  
Old 03-28-2013
Does your shell supports grep -o ??
# 4  
Old 03-28-2013
Nope.. I am Redhat Linux and it doesn't have that
Code:
grep -o

option..
# 5  
Old 03-28-2013
Try

Code:
$ echo '/\$var/temp_dir/\${var2}\${var3}.log'  | sed -n 's/.*\(\$[a-zA-Z0-9]*\).*/\1/'

This should give you $var.

I am trying to extract others as well.
# 6  
Old 03-28-2013
Missed p? Seems to be matching too much as well (AIX):
Code:
[user@host]  echo '/\$var/temp_dir/\${var2}\${var3}.log'  | sed -n 's/.*\(\$[a-zA-Z0-9]*\).*/\1/'
[user@host]  echo '/\$var/temp_dir/\${var2}\${var3}.log'  | sed -n 's/.*\(\$[a-zA-Z0-9]*\).*/\1/p'
$

You could have something along the lines of:
Code:
 echo '/$var/temp_dir/${var2}$var3.log'  | sed -n 's/[^$]*\($[[:alnum:]]*\)[^[:alnum:]].*/\1/p'
$var

but it will only handle 1 variable per line, and probably doesn't deal correctly with shell builtins...
# 7  
Old 03-28-2013
I did this

Code:
echo "/\$var/temp_dir/\${var2}\${var3}.log" | tr '\/' '\n' | sed -n 's/.*\([\$][{]*[A-Za-z0-9]*[}]*\).*/\1/p'

I got $var and ${var3} as output! Smilie

Little tuning up of regex should do Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to create runtime variables based on the number of parameters passed in the script

Hi All, I have a script which intends to create as many variables at runtime, as the number of parameters passed to it. The script needs to save these parameter values in the variables created and print them abc.sh ---------- export Numbr_Parms=$# export a=1 while do export... (3 Replies)
Discussion started by: dev.devil.1983
3 Replies

2. Shell Programming and Scripting

How to write config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

3. UNIX for Dummies Questions & Answers

How to write Config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

4. Shell Programming and Scripting

Help with Shell Script to identify lines in file1 and write them to file2

Hi, I am running my pipeline and capturing all stout from multiple programs to a .txt file. I want to go into that .txt file and search for specific lines, and finally print those lines in a second .txt file. I can do this using grep, awk, or sed for each line, but have not been able to get... (2 Replies)
Discussion started by: hmortens
2 Replies

5. Shell Programming and Scripting

Shell script to identify the number of files and to append data

Hi I am having a question where I have to 1) Identify the number of files in a directory with a specific format and if the count is >1 we need to concatenate those two files into one file and remember that in the second file the header should not be copied. it should be form first file.... (4 Replies)
Discussion started by: pradkumar
4 Replies

6. Shell Programming and Scripting

Shell Script to identify the line number containing a particular "string"

Hi, I have a log file, where i am required to identify the line number, where a particular string/line appears in the log file. And then copy 200 lines above that line number to a new file. Can someone provide pointers on how to write this script or what command to be used ? Any... (2 Replies)
Discussion started by: kk2202
2 Replies

7. Shell Programming and Scripting

Variables in shell script

mysqldump --compact --add-drop-table -h192.168.150.80 -uroot -p somePass $combined | sed '/$combined/$table/g' | mysql $databaseThe sed part is not working from the above statement. The variables combined and table are already defined and instead of showing the actual variable, it is executing the... (4 Replies)
Discussion started by: shantanuo
4 Replies

8. Shell Programming and Scripting

test script to identify SHELL

I am new to BASH and writing a small script to identify the SHELL . #!/bin/bash BASH='/bin/bash' KSH='/bin/ksh' if then echo "it's Bash" else echo "it's not Bash" fi $ bash -x a.sh + BASH=/bin/bash + KSH=/bin/ksh + '' a.sh: line 4: where am I missing . PLease advice . (10 Replies)
Discussion started by: talashil
10 Replies

9. Shell Programming and Scripting

Accessing variables of one shell script in another shell script

Hi All, I have a shell script called sample1.sh where I have 2 variables. Now I have another shell script called sample2.sh. I want the variables in sample1.sh to be available to sample2.sh. For example. In sample1.sh I am finding the sum of 2 numbers namely a and b. Now I want to access... (2 Replies)
Discussion started by: rsendhilmani
2 Replies

10. Shell Programming and Scripting

Accessing variables of one shell script in another shell script

I have a variable $exe in a shell script file a.sh which I need to access in another shell script file b.sh. How can I do that? :rolleyes: Thanks!! (2 Replies)
Discussion started by: looza
2 Replies
Login or Register to Ask a Question