ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
...
eval i_dir=("${usr_opt[6]}") # holds a directory
eval copt=("${usr_opt[42]}") # are user options: --opt1=$idir --opt2 --opt3="hello"
now
eval copt=("${usr_opt[42]}")
truncates copt after --opt1
echo $copt # --opt1=/home/user1/work ; and nothing more
eval copt="${usr_opt[42]}"
gives an error
echo $copt # <nothing> # error: --opt2: command not found
Does the user quotes his string in the file then all is perfect:
options: "--opt1=$idir --opt2 --opt3="hello""
eval copt=${usr_opt[42]}
echo $copt # --opt1=/home/user1/work --opt2 --opt3="hello"
So is there an easier way as to check for the qotes in the string and if not quoted quote it?
bastl, why are you using eval there?
is this a bash script?
from what i see, 'eval' is useless there and everything should work if you just remove all occurences of 'eval'.
It's bash-4.x script!
echo does'n do the job you need eval or source.
But i have too many relations between these options that source isn't realy useful and is the reason of that option file.
So BW-userx your script looks very promising I'll test it and let you now here.
But you see how much lines you need only because the user could forget the quotes.
I hoped of something shorter like () or ...
It's bash-4.x script!
echo does'n do the job you need eval or source.
But i have too many relations between these options that source isn't realy useful and is the reason of that option file.
So BW-userx your script looks very promising I'll test it and let you now here.
But you see how much lines you need only because the user could forget the quotes.
I hoped of something shorter like () or ...
so become a programmer then you will gain an understanding the even the mv command has more then one line of code behind that one liner.
Code:
mv file file
In other words you could write a function that does what you want then call it a yeah 'a'
that short enough for you? Because that is how it works.
Code:
a()
{
for (( j = 0 ; j < ${#array[@]} ; j++ ))
{
if [[ ! ${array[j]} =~ ^\".*\"$ ]]; then
array[j]="\"${array[j]}"\"
fi
}
}
I do not really understand why do you need to overcomplicate this?
I know I am completely lost on this usage of eval when using it to check if something has quotes on both ends of a string. From what I read up on it. it is to run a command within it. I didn't piddle with it a lot. Not seeing a need for it. then you do this. it all looks Greek to me. And I do not wnat to over write this post. But I am putting in that link explaining this eval command.
eval must not be used, it is unsafe and usually the situation can be solved (easily) without eval too.
There can be a few cases when eval is really required, but this is not the one.
The link you sent is more or less outdated, there are much better alternatives, just for example:
Code:
variable indirection:
abc=10 x=abc
echo ${!x}
last parameter in the list
Code:
set One Two Three Four
echo "${@:$#}"
what is a bit complicated the evaluation of string entered at the command prompt.
First bash will evaluate it, (it will also look for built-ins, functions, aliases) next it will pass the result to the command found and that command will also process the arguments. And you can protect the arguments using ' or ", but also you may need to pass " or any other strange chars as arguments.
The command eval will produce additional evaluation steps which will require additional protections which will definitely increase the complexity of argument passing (not to speak about remote ssh executions and other tricks, like pipe and redirection).
eval must not be used, it is unsafe and usually the situation can be solved (easily) without eval too.
There can be a few cases when eval is really required, but this is not the one.
The link you sent is more or less outdated,
That explains why when I googled eval I kept getting hits that the pages it sent me to where people were using everything but the eval command to do what was needed to be done.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.