LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell arguments in variable vs direct string (https://www.linuxquestions.org/questions/linux-newbie-8/shell-arguments-in-variable-vs-direct-string-4175593215/)

jpollard 11-10-2016 04:06 AM

The entire thing gets simpler with Perl... as you are under control of how variables get used at each step.

At one time I used a perl script to interpret a network configuration file... and made the configuration file executable, using a "#!/etc/network.p" in the configuration file to invoke the Perl script.

Much easier than trying to get bash to do it - and it shortened the configuration file.

break_da_funk 11-10-2016 04:13 AM

Quote:

Originally Posted by jpollard (Post 5628974)
The entire thing gets simpler with Perl... as you are under control of how variables get used at each step.

At one time I used a perl script to interpret a network configuration file... and made the configuration file executable, using a "#!/etc/network.p" in the configuration file to invoke the Perl script.

Much easier than trying to get bash to do it - and it shortened the configuration file.

In my case configuration file analysis is done by some awk script which outputs options for pppd. Then pppd is executed with fixed options + options read by awk script. I tried to combine fixed options and from configuration file with awk but I realized that this is user arguments analysis - I don't want to do that.
Anyway thank You for the answer :)

break_da_funk 11-10-2016 06:44 AM

Quote:

Originally Posted by jpollard (Post 5628729)
This is not obvious... and may not be the best way...

If you replace the following line in the test.sh script
Code:

./arguments_count.sh $my_args
with

Code:

eval ./arguments_count.sh $my_args
You get the same answer as the direct string returns. This happens because you are forcing an additional evaluation of the strings... and that converts the '4 and 5' parameters into the string '4 5'.

This approach may not even work in all cases - as the space character between the 4 and 5 will have been evaluated, so if you expected to pass a tab character it won't be there as previous evaluations removed it.

This will not help if have multiple nested structures...

Easier to use perl or python..

Eval also solve the problem.

pan64 11-10-2016 11:33 AM

do not use eval
http://stackoverflow.com/questions/1...-i-use-instead

jpollard 11-10-2016 12:17 PM

Quote:

Originally Posted by pan64 (Post 5629086)

Is also another way of saying "don't be portable". If you are using the Bourne shell you can't use most of those recommendations as the facilities don't exist.

Most UNIX like systems do have bash available... but not necessarily provided by the distribution.


All times are GMT -5. The time now is 02:09 AM.