Problem with eval a generated and interpreted variable name and its value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with eval a generated and interpreted variable name and its value
# 1  
Old 04-16-2014
[SOLVED] Problem with eval a generated and interpreted variable name and its value

Heya

I have modified $HOME/.config/user-dirs.dirs to my like and the variables are properly 'structured' (XDG_XY_DIR="$HOME/YZ").
Now since i wrote a script to do that part, i currently stuck at the part to create a new .gtk-bookmarks file.

Regarding code snippet: (bold part beeing troublesome)
Code:
#
#	Variables for the script internaly
#
	CONFIG=$HOME/.config/user-dirs.dirs
	CONFIG_BAK=$CONFIG-$(date +'%F').bak
	BOOKMARK=$HOME/.gtk-bookmarks
	BOOKMARK_BAK=$HOME/.gtk-bookmarks-$(date +'%F').bak
	IFS_ORG="$IFS"
	IFS="="
# .... some other code ...
#
#	Prepare bookmarks
#
	source "$CONFIG"
	while read var val;do
		if [ "X" = "${var:0:1}" ] && [ "\"" = "${val:0:1}" ]
		then 	lbl="${var/XDG_/}"
			lbl="${lbl/_DIR/}"
			lbl="${lbl,,}"
			lbl="${lbl^}"
			path="${val/\"/}"	# Why do i need to call this twice?
			path="${path/\"/}"	# Why do i need to call this twice?
			RAW="\$$var"		# Line 100
			echo "file://"$RAW" ${lbl}-raw" >> "$BOOKMARK"
			echo "file://"$(echo $RAW)" ${lbl}-raw2" >> "$BOOKMARK"
			echo "file://"$(echo $(eval $RAW))" ${lbl}-raw3" >> "$BOOKMARK"
			echo "file://"$(eval $(echo $RAW))" ${lbl}-raw4" >> "$BOOKMARK"
			XDG="$(eval $(echo $RAW))" 	&& 	echo "file://"$XDG" ${lbl}" >> "$BOOKMARK"
			XDG2="$(echo $(eval \$$var))" 	&& 	echo "file://$XDG2 ${lbl}2" >> "$BOOKMARK"
			XDG3="$(echo $(eval $(echo \$$var)))" && echo "file://$XDG3 ${lbl}3" >> "$BOOKMARK"
			XDG4="$(eval \$$var)" 		&& 	echo "file://$XDG4 ${lbl}4" >> "$BOOKMARK"
			echo $? "Added $path as $lbl"	# Line 109
		fi
	done < "$CONFIG"
	IFS="$IFS_ORG"

I somehow remember (at least i think it did) that eval was the right tool to accomplish this.
Sadly, the builtin_bash manpage dont help me further:
Code:
  eval [arg ...]
              The args are read and concatenated together into a single command.
              This command is then read and executed by the shell, and its  exit
              status is returned as the value of eval.  If there are no args, or
              only null arguments, eval returns 0.

Now the .gtk-bookmarks look like:
Code:
file://$XDG_DESKTOP_DIR Desktop-raw
file://$XDG_DESKTOP_DIR Desktop-raw2
file:// Desktop-raw3
file:// Desktop-raw4
file:// Desktop2
file:// Desktop3
file://$XDG_DOCUMENTS_DIR Documents-raw
file://$XDG_DOCUMENTS_DIR Documents-raw2
file:// Documents-raw3
file:// Documents-raw4
file:// Documents2
file:// Documents3
...

As the script reports:
Code:
Save settings
Backing up /home/sea/.config/user-dirs.dirs 
0 Created backup (/home/sea/.config/user-dirs.dirs-2014-04-16.bak)
Backing up /home/sea/.gtk-bookmarks-2014-04-16.bak... 
0 Created backup (/home/sea/.gtk-bookmarks-2014-04-16.bak)
Writing /home/sea/.config/user-dirs.dirs... 
0 Written /home/sea/.config/user-dirs.dirs
/home/sea/exported-tweak_folders.sh: line 103: /home/sea/notepad: Is a directory
/home/sea/exported-tweak_folders.sh: line 104: /home/sea/notepad: Is a directory
/home/sea/exported-tweak_folders.sh: line 105: /home/sea/notepad: Is a directory
/home/sea/exported-tweak_folders.sh: line 106: /home/sea/notepad: Is a directory
/home/sea/exported-tweak_folders.sh: line 107: /home/sea/notepad: Is a directory
/home/sea/exported-tweak_folders.sh: line 108: /home/sea/notepad: Is a directory
126 Added $HOME/notepad as Desktop
/home/sea/exported-tweak_folders.sh: line 103: /home/sea/priv/docs: Is a directory
/home/sea/exported-tweak_folders.sh: line 104: /home/sea/priv/docs: Is a directory
/home/sea/exported-tweak_folders.sh: line 105: /home/sea/priv/docs: Is a directory
/home/sea/exported-tweak_folders.sh: line 106: /home/sea/priv/docs: Is a directory
/home/sea/exported-tweak_folders.sh: line 107: /home/sea/priv/docs: Is a directory
/home/sea/exported-tweak_folders.sh: line 108: /home/sea/priv/docs: Is a directory
126 Added $HOME/priv/docs as Documents

Where's my problem handling 'eval', any ideas?
Thank you in advance.
Regards
sea

EDIT, evnironement is:
Code:
uname -r;$SHELL --version
3.13.9-200.fc20.x86_64
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu) Copyright (C) 2011 Free Software Foundation, Inc. 
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> 
This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.


Last edited by sea; 04-16-2014 at 07:26 PM..
# 2  
Old 04-16-2014
eval is almost never the answer. The same usually goes for dynamic variable names. If you find yourself using either, you've taken a wrong turn somewhere.

You can use a here-document to safely turn your variables into strings. Much better than eval.

Code:
echo "cat <<EOF" >> /tmp/$$
# copy into /tmp/$$ removing backticks and () to prevent accidental/malicious execution of commands
tr -d '`()' < configfile >> $/tmp/$$
echo "EOF" >> /tmp/$$

. /tmp/$$ > /tmp/$$-1

rm /tmp/$$

It works by creating a file like this:

Code:
cat <<EOF
bookmark
bookmark
bookmark
EOF

...and running it in your shell to create another file.

/tmp/$$-1 should now contain the same text as your GTK file with $VARIABLE names substituted.

TIP: You can do ${VARNAME}stuff instead of $VARNAME stuff in your config file. The shell will look for the { } so you won't need spaces to separate them out.

Last edited by Corona688; 04-16-2014 at 07:04 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 04-16-2014
This worked, thank you Smilie

Code:
BOOKMARK=$HOME/.gtk-bookmarks
CONFIG=$HOME/.config/user-dirs.dirs
DEST=$HOME/gtk-bookmarks.txt
. $CONFIG
echo "cat <<EOF" >> /tmp/$$
# copy into /tmp/$$ removing backticks and () to prevent accidental/malicious execution of commands
tr -d '`()' < $BOOKMARK >> /tmp/$$
echo "EOF" >> /tmp/$$

. /tmp/$$ > $DEST

rm /tmp/$$
cat $DEST

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Safe way to eval variable declarations?

Is there a safe way to evaluate variable declarations within a script whether they come from a .conf file, user input, or stdin? Example .conf file: server=ftp.xxxx.com port=21 user="$USER" # Hopefully allow this type of substitution domain="$DOMAIN" server="$(malicious... (4 Replies)
Discussion started by: Michael Stora
4 Replies

2. Shell Programming and Scripting

'eval' used in variable assignment

pattern1=book { x=1 eval echo \$pattern$x } book (this is the output) But when I assign a variable to the output of the eval it doesn't work unless I prefix 2 times backslash before $ as shown below. { a=`eval echo \\$pattern$x` echo $a } book Why here twice "\" has to be... (3 Replies)
Discussion started by: ravisingh
3 Replies

3. Shell Programming and Scripting

assign multiple rows value to a variable using eval

background : Solaris, ksh metresult="ooo > pp" ts=89 eval append_${ts}="$metresult" bash: pp: command not found I want to create a variable which has in a part of its name a dynamically-established number (stored in another variable) usually I do this with eval command. The problem I... (5 Replies)
Discussion started by: black_fender
5 Replies

4. Shell Programming and Scripting

assignment to variable from eval command

Hi Gurus, I am having 2 parameters as below parm1=value1 parm2=parm1 I want to evaluate parm1 value using eval echo \$$parm2 and later i want to assign this value to other variable which i will be using in if statement like : if ]; then do this....... fi could you please suggest... (5 Replies)
Discussion started by: k_vikash
5 Replies

5. Shell Programming and Scripting

eval and variable assignment

Hi, i have an issue with eval and variable assignment. 1) i have a date value in a variable and that date is part of a filename, var1=20100331 file1=${var1}-D1-0092.xml.zip file2=${var2}-D2-0092.xml.zip file3=${var3}-D3-0092.xml.zip i am passing the above variables to a script via... (11 Replies)
Discussion started by: mohanpadamata
11 Replies

6. Shell Programming and Scripting

Help with eval usage for string containing Environment Variable

Help !! First, Thanks in Advance Here is what I have I have an environment Variable, let's call it v_VALUE. v_VALUE="\$ORACLE_HOME/bin" Hence, the location is ORACLE_HOME is not evaluated. ORACLE_HOME happens to be /app/oracle/product/10.1.2 I need a method of returning the... (1 Reply)
Discussion started by: dhangliter
1 Replies

7. Shell Programming and Scripting

Passing eval value to a variable

Hello, I have a script that does an scp to a server and then gets the number of process running on that server, the o/P should be stored in a variable for further processing eval `echo "ssh -q $Infa_user@$host 'csh -c $CMD '"` where CMD="ps -ef | grep -i ${INFA_REPO} | grep -v grep | wc... (2 Replies)
Discussion started by: amit1_x
2 Replies

8. Shell Programming and Scripting

bin/sh eval variable assignment

Why can't I do this? eval "TEST=5;echo $TEST;"; THIS WORKS!! TEST=5;echo $TEST; (2 Replies)
Discussion started by: blasto333
2 Replies

9. Shell Programming and Scripting

How to assign eval value as Variable..

Im facing problem in assigning value of eval array variable as normal variable.. x=0 eval DATA${x}="FJSVcpcu" x=`expr $x + 1` eval DATA${x}="FJSVcsr" if x=0, type -> eval echo \$DATA$x , its give me FJSVcpcu i want assign this value into an variable as variable=`eval echo... (3 Replies)
Discussion started by: neruppu
3 Replies

10. Shell Programming and Scripting

eval a variable that has a .

Hi, Is there any way that I can eval the following - eval abc.csv=def.csv I am getting the - bash: command not found error. thanks. (3 Replies)
Discussion started by: ttshell
3 Replies
Login or Register to Ask a Question