Eval set -- and more


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Eval set -- and more
# 8  
Old 02-01-2017
I agree with the above and again:eval if used this way on external, user controlled parameters is especially dangerous. It should not be used this way!

Further observations:
  • the while true loop is faulty. There is no default case and the loop only ends when there is the option --. If that is left out, no shifting takes place, so the script will enter an infinite loop. A while true loop is not the way one would normally do option handling with getopts anyway.
  • The unquoted function parameter $@ may alter the script's command line parameters when it passes it to the function. There should be double quotes around it..
  • The test for the number of non-option parameters is never reached [ "$#" -ne 0 ] when there are no options either..
# 9  
Old 02-01-2017
We haven't been told what operating system or shell are being used for these tests, but if we use bash, ksh, sh, or zsh on macOS with the BSD based getopt utility available on that system (which says it was designed to behave the same as the Bell Labs getopt utility I described in post #3 in this thread), the command:
Code:
OPTS=`getopt -o r:hb::x: -l exclude: -l branch::  -l help -l version -- "$@"`

would set OPTS to:
Code:
 -- r:hb::x: -l exclude: -l branch:: -l help -l version -- -r test

if the script is invoked with the command:
Code:
./script.sh -r test

and after the command:
Code:
eval set -- "$OPTS"

is run, the positional parameters will be set to:
Code:
$1="--"
$2="r:hb::x:"
$3="-l"
$4="exclude:"
$5="-l"
$6="branch::"
$7="-l"
$8="help"
$9="-l"
${10}="version"
${11}="--"
${12}="-r"
${13}="test"

There are lots of things wrong with the script that has been posted for discussion in this thread many of which have already been mentioned at least once.

Instead of what this getopt utility is doing, to make the following while loop work correctly getopt with the shown arguments would have to return the string:
Code:
-r test --

instead of:
Code:
 -- r:hb::x: -l exclude: -l branch:: -l help -l version -- -r test

Rather than trying to understand how this script works, you would be much better off taking the time to learn how to rewrite the script using getopts (perhaps using suggestions in the thread How to Use getopts with longoptions? from a couple of years ago as a starting point).
# 10  
Old 02-01-2017
Quote:
Originally Posted by mohca2020
What do you mean before the
Code:
set

. How does
Code:
set

change
Code:
$1

and
Code:
$2

?
I mean that set -- "$@" doesn't change an iota in the positional parameters
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getopt eval set parameters not happening when the script is called through an application!

Hi, There is an Informatica tool through which unix scripts can be called. Now the requirement in my project is that a parent script calls a child script but this parent script has to be called through the Informatica tool. In Parent script I'm using TEMP=`getopt -o x:y: -l area:,volume:... (1 Reply)
Discussion started by: Panna
1 Replies

2. Shell Programming and Scripting

Eval

thank you (35 Replies)
Discussion started by: ratnalein88
35 Replies

3. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (13 Replies)
Discussion started by: vivek d r
13 Replies

4. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (1 Reply)
Discussion started by: vivek d r
1 Replies

5. Shell Programming and Scripting

Strange result of eval, how does eval really work with ssh?

Hi all, some small script with eval turned me to crazy. my OS is linux Linux s10-1310 2.6.16.53-0.8.PTF.434477.3.TDC.0-smp #1 SMP Fri Aug 31 06:07:27 PDT 2007 x86_64 x86_64 x86_64 GNU/Linux below script works well #!/bin/bash eval ssh remotehost date eval ssh remotehost ls below... (1 Reply)
Discussion started by: summer_cherry
1 Replies

6. Shell Programming and Scripting

eval

hi all, Am trying to add some code to a ksh script and i dont understand how an eval function is used : _var=$1 _conceal=$2 eval _val=\$${_var} can someone shed some light on what the eval function in the above context means/does ?? thanks. (4 Replies)
Discussion started by: cesarNZ
4 Replies

7. Shell Programming and Scripting

eval help

I am trying to expand the variable $user in my alias command and tried several variations of eval but can't seem to get it to work. The end result should be either: oracle_user='sudo su - oracle ' or oracle_user='sudo su - oracle1 ' user=$(grep '^oracle:' /etc/passwd | cut... (5 Replies)
Discussion started by: BeefStu
5 Replies

8. Shell Programming and Scripting

KSH script eval(?) to set variable

first of all, thanks to all on this board, it has been a huge resource to answer most of my questions! I am stuck on something that should really be simple, and was looking for some help.. I am using KSH on solaris and working on a script to move containers from server to server. Where i am... (4 Replies)
Discussion started by: tksol
4 Replies

9. Shell Programming and Scripting

EVal

Hi All, I'm running some encrypted data through a script I wrote. In order to do this, I'm using eval to resolve some of my variables. At the moment, when I use eval to resolve, it strips out some of my encrypted values, and totally drops some others. For example if I have the value ab1"3 it drops... (1 Reply)
Discussion started by: Khoomfire
1 Replies

10. Shell Programming and Scripting

using eval with set, wc, and expr

Okay this is a mess, I'm trying to assign variables with variables in a for-loop. Here is what i have for code. The syntax is not good. Given the following script: #! /bin/csh foreach site (ABC DEF GHI) eval set \t$${site}sf = ``wc -l \$${site}.sf | awk '{print $1}'`` eval set... (2 Replies)
Discussion started by: wxornot
2 Replies
Login or Register to Ask a Question