[Solved] Value of a variable is not recognised for commands comes from external file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Value of a variable is not recognised for commands comes from external file
# 1  
Old 11-01-2011
Question [Solved] Value of a variable is not recognised for commands comes from external file

Hi,

my script is setting a variable with value and this variable is present in my another command that is coming from external file and this command is internally called after this variable is set. but while execution of this command, the value is not retrieved properly.

say,

my script has a variable PWD and it is set to some value say abcd

export PWD=abcd
echo $PWD #here it is returinng the value abcd perfectly.

in the same shell script i am using some piece of code which will bring the unix commands from a external file and execute it one by one. one of the commands is echo $PWD
while execution at this time, it is not returning the value abcd

echo $PWD
simply it is returning $PWD instead of abcd

what is the mistake i do not understand. Please suggest.
# 2  
Old 11-01-2011
Aren't you placing "$PWD" between single quotes?

Code:
# echo '$PWD'
$PWD

# 3  
Old 11-01-2011
I am not using single quotes.

I have also tried with double quotes. still it is not working as expected.
echo $PWD
echo "$PWD" #these two i tried. no luck.

external file has line as below

PRI#echo $PWD

from this i am getting only the exact command and storing it in a variable and executing it.

export command=`echo $line | cut -d '#' -f2,3,4'
$command

PS: i am able to execute other commands properly from this file. only commands which has some variables present are not working.
# 4  
Old 11-01-2011
Here it works!
Code:
# line="PRI#echo $PWD"
# command=`echo "${line}" | cut -d '#' -f2,3,4`
# $command
/current/path

You can try to use: eval, but be careful, eval is evil!
Code:
# line="PRI#echo \$PWD"
# command=`echo "${line}" | cut -d '#' -f2,3,4`
# echo $command
echo $PWD
# eval $command
/current/path

This User Gave Thanks to felipe.vinturin For This Post:
# 5  
Old 11-01-2011
MySQL

eval is doing perfectly.
Thanks a lot for your help.

i never used eval before. why it is evil?
# 6  
Old 11-01-2011
If you are using a "configuration" file to put commands you need to execute, be careful if somente puts a "rm -rf <some path>" on it on purpose! Or some "code injection"!

Check this blog with JScript posts about it:
-----> 1. Eval is Evil, Part One - Fabulous Adventures In Coding - Site Home - MSDN Blogs
-----> 2. Eval is Evil, Part Two - Fabulous Adventures In Coding - Site Home - MSDN Blogs

In general, eval is not a good practice, but in a controled environment, it is ok!

I hope it helps.

---------- Post updated at 13:33 ---------- Previous update was at 13:33 ----------

If you are using a "configuration" file to put commands you need to execute, be careful if somente puts a "rm -rf <some path>" on it on purpose! Or some "code injection"!

Check this blog with JScript posts about it:
-----> 1. Eval is Evil, Part One - Fabulous Adventures In Coding - Site Home - MSDN Blogs
-----> 2. Eval is Evil, Part Two - Fabulous Adventures In Coding - Site Home - MSDN Blogs

In general, eval is not a good practice, but in a controled environment, it is ok!

I hope it helps.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grabbing fields without using external commands

data: bblah1_blah2_blah3_blah4_blah5 bblahA_blahB_blahC_blahD_blahE im using the following code to grab the field i want: cat data | while IFS='_' read v1 v2 v3 v4 v5; do printf '%s\n' "${v4}"; done im catting data here. in the real world, the exact content of data will be in a variable.... (4 Replies)
Discussion started by: SkySmart
4 Replies

2. Shell Programming and Scripting

File test commands with variable file name

Hi there I have the following code SDFFILE_SRC=$BACKEND_DIR/$SDFFILE_MIN_tar SDFFILE_DST=$SIM_DIR/$SDFFILE_MIN if ] .... .... .... fi Simply I need to check if $SDFFILE_SRC isa newer than $SDFFILE_DST or $SDFFILE_DST does not exist then execute the IF Body. Although the the... (2 Replies)
Discussion started by: aelhosiny
2 Replies

3. Shell Programming and Scripting

passing file extension using external variable

Hi, How can I modify the FILETYPE command ? I want to provide the file extension, like txt, root .? Thanks, #!/bin/bash FROM=$1 TO=$2 FILETYPE=$3 ... (4 Replies)
Discussion started by: nrjrasaxena
4 Replies

4. Shell Programming and Scripting

How can we assign value to an array variable from an external file?

is it possible to assign value to an array variable from an external file?? if yes then how?? I am using below code but its not working. #!bin/bash myarray < file_name echo ${mayarray} (6 Replies)
Discussion started by: mukulverma2408
6 Replies

5. Shell Programming and Scripting

storing a value from another file as a variable[solved]

Hi all, im having snags creating a variable which uses commands like cut and grep. In the instance below im simply trying to take a value from another file and assign it to a variable. When i do this it only prints the $a rather than the actual value. I know its simple but does anyone have any... (1 Reply)
Discussion started by: somersetdan
1 Replies

6. Shell Programming and Scripting

[Solved] problem - connecting code with external file

hello. this is the code #!/bin/sh total1024=0 total2048=0 total8192=0 if ; then if ; then while read variable do if ; then total1024=$(( $total1024 + 1 )) fi if ; then total2048=$((... (4 Replies)
Discussion started by: Telis
4 Replies

7. Shell Programming and Scripting

[solved] how to get specific env variable values into a file

greetings! how do i get an env variable that looks like this when echoed: into a file that looks like this: keeping in mind that the two constants are the fields from the env variable will always be in odd positions of the string that need to go into the file AND they will always start... (3 Replies)
Discussion started by: crimso
3 Replies

8. Web Development

cgi script and external UNIX commands (like swadm)

Hi, I am trying to implement a server monitoring dashboard using cgi scripting. I am planning to run the necessary unix scripts from the web page using cgi. This method works fine for standard unix commands but I am unable to run some external unix commands (like swadm show_processes, swadm... (9 Replies)
Discussion started by: jofinjoseph
9 Replies

9. Shell Programming and Scripting

Getting category when given the variable from external file to shell script

Hi, I have a script that interacts with a config file in the format: file1.txt file2.txt file3.txt file4.txt file5.txt file6.txt I would like to return the Category, when given the file name. (11 Replies)
Discussion started by: MoreCowbell
11 Replies
Login or Register to Ask a Question