LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-02-2017, 04:05 PM   #1
bastl
Member
 
Registered: Sep 2003
Location: Germany/BW
Distribution: My own
Posts: 237

Rep: Reputation: 22
Bash script: sored string with variables


Hello
I have a strings stored in a array with
Code:
readarray -t usr_opt < usr.file
so I do now assign and check these options:
Code:
...
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?
 
Old 05-02-2017, 06:41 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
check your format
http://tldp.org/LDP/abs/html/ivr.html

or try this
Code:
#!/bin/bash

for (( i = 0 ; i < 10 ; i++ ))
do

if [[ $(( $i % 2 )) -eq 0 ]] ;
then
 array[i]="no quotes"
else
 array[i]="\"quotes"\"

fi

done

for (( j = 0 ; j < ${#array[@]} ; j++ ))
{

  if [[ ! ${array[j]} =~ ^\".*\"$ ]]; then
    array[j]="\"${array[j]}"\"
  fi

}


for (( a = 0 ; a < ${#array[@]} ; a++ )) {
  echo "${array[a]}"
}
 echo "${array[@]}"
is this what you wanted?
Let me know if that works for you.

if no quotes on the ends of string in each element then adds quotes on the both ends of each string in each element.

Last edited by BW-userx; 05-02-2017 at 07:48 PM.
 
Old 05-03-2017, 02:13 AM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
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'.
 
Old 05-03-2017, 02:19 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
copt is an array, you should print it:
Code:
echo "${copt[@]}"
echo $copt # will print only the first element of the list
 
Old 05-03-2017, 01:40 PM   #5
bastl
Member
 
Registered: Sep 2003
Location: Germany/BW
Distribution: My own
Posts: 237

Original Poster
Rep: Reputation: 22
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 ...
 
Old 05-03-2017, 02:06 PM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by bastl View Post
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

}

}
a array[@]

Something like that.

Last edited by BW-userx; 05-03-2017 at 02:12 PM.
 
Old 05-03-2017, 02:12 PM   #7
bastl
Member
 
Registered: Sep 2003
Location: Germany/BW
Distribution: My own
Posts: 237

Original Poster
Rep: Reputation: 22
O.K. BW-userx that works fine, many thanks:
Code:
if [[ ! ${usr_opt[42]} =~ ^\".*\"$ ]]
then
   eval copt="\"${usr_opt[42]}"\"
else
   eval copt="${usr_opt[42]}"
fi

Last edited by bastl; 05-03-2017 at 02:15 PM.
 
Old 05-03-2017, 02:20 PM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by bastl View Post
O.K. BW-userx that works fine, many thanks:
Code:
if [[ ! ${usr_opt[42]} =~ ^\".*\"$ ]]
then
   eval copt="\"${usr_opt[42]}"\"
else
   eval copt="${usr_opt[42]}"
fi
you can shorten that you know.

think about what it is you are really checking for.

If not then make it so.
If yes then do nothing.

go back and look at my one line of code.

Code:
[[ ! ${usr_opt[42]} =~ ^\".*\"$ ]] &&  eval copt="\"${usr_opt[42]}"\"
What is 'eval' suppose to be doing for you?
Checking what you've already checked for?

Last edited by BW-userx; 05-03-2017 at 02:25 PM.
 
Old 05-03-2017, 02:33 PM   #9
bastl
Member
 
Registered: Sep 2003
Location: Germany/BW
Distribution: My own
Posts: 237

Original Poster
Rep: Reputation: 22
Yes, and I'll shorten it one more, because not quoting is the standard in the option file. It was only for testing all possibilities and for learning.
 
Old 05-03-2017, 03:16 PM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
keew, have fun.
 
Old 05-04-2017, 12:28 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Code:
> usr_opt[42]='--opt1=$idir --opt2 --opt3="hello"'
> declare -a copt
> copt=( ${usr_opt[42]} )
> echo "${copt[0]}"
--opt1=$idir
> echo "${copt[1]}"
--opt2
> echo "${copt[2]}"
--opt3="hello"
I do not really understand why do you need to overcomplicate this?
 
Old 05-04-2017, 08:22 AM   #12
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by pan64 View Post
Code:
> usr_opt[42]='--opt1=$idir --opt2 --opt3="hello"'
> declare -a copt
> copt=( ${usr_opt[42]} )
> echo "${copt[0]}"
--opt1=$idir
> echo "${copt[1]}"
--opt2
> echo "${copt[2]}"
--opt3="hello"
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.

http://www.softpanorama.org/Utilities/eval.shtml
 
Old 05-04-2017, 09:18 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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).
 
1 members found this post helpful.
Old 05-04-2017, 09:41 AM   #14
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by pan64 View Post
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.

Thanks for your input.
 
Old 05-05-2017, 07:35 PM   #15
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
What is readarray? What language are you using? You're only looking at your 6th and 42nd command-line option?

Looks like code gibberish to me!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] need help matching string variables in a bash script slockna Linux - General 6 10-26-2015 05:48 AM
Bash Script - Passing command string between variables Devcon Programming 13 01-10-2011 10:13 AM
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 06:09 AM
Assigning variables and string parsing in bash JDska55 Linux - Newbie 9 06-16-2009 10:51 AM
bash - pull out variables from a | deliminated string elinenbe Programming 11 02-15-2008 11:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration