Eval set -- and more


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Eval set -- and more
# 1  
Old 01-31-2017
Question Eval set -- and more

Hello everyone,

I am taking a course on Lynda and they show this code below. I didn't fully understand some parts. Please see the questions within the code.

Thank you for your input and time.

Regards,

Code:
function usage {
   echo Options are -r -h -b -x --branch --version --help --exclude
   exit 1
}
function handleopts {
    OPTS=`getopt -o r:hb::x: -l exclude: -l branch::  -l help -l version -- "$@"`
#### Question 1: How does the "-- $@" work here ####
    if [ $? != 0 ]
    then
        echo ERROR parsing arguments >&2
        exit 1
    fi
    eval set -- "$OPTS"
#### Question 2: I understand that set -- $variable sets the content of $variable to the positional parameters, but how does it work here? What are the PP here? ####
    while true ; do
        case "$1" in
            -r ) rightway=$2 
                shift 2;;
#### Question 3: I assume that shift 2 means shift shift, but why is it needed here? ####
            --version ) echo "Version 1.2"; 
                 exit 0;;
            -h | --help ) usage; 
                 shift;;
            -b | --branch) 
                case "$2" in
                "") branchname="default"  ; shift 2 ;;
                 *) branchname="$2" ; shift 2 ;;
                esac ;;
            --) shift; break;;
        esac
    done
    if [ "$#" -ne 0 ]
    then
	echo Error extra command line arguments "$@"
	usage
    fi
}
rightway=no
handleopts $@
echo rightway = $rightway
echo branchname = $branchname


Last edited by rbatte1; 02-01-2017 at 09:16 AM.. Reason: Spelling corrections
# 2  
Old 02-01-2017
-- "$@" just means, "take the following arguments literally" and "all the following arguments, properly separated and quoted". If your arguments are "a b" "c d" "e f", "$@" will put them in as "a b" "c d" "e f", not "a" "b" "c" "d" "e" "f".

The 'shift' eats a commandline argument, i.e. converts $1=a, $2=b, $3=c, $4=d into $1=b, $2=c, $3=d. shift 2 moves it all the way to $1=c, $2=d. It's done since that particular branch uses an argument, so it has to remove two things from the list (the -r and the argument following it) not just one.

I have no idea why they're doing 'eval set', only theories. That's dangerous and dumb. They may be trying to process quotes inside quotes.



They are getting the values from 'getopt', the standard way to handle commandline arguments. I can't really explain its workings, but I'm sure others can.

Last edited by Corona688; 02-01-2017 at 11:48 AM..
# 3  
Old 02-01-2017
Quote:
Originally Posted by Corona688
-- "$@" just means, "take the following arguments literally" and "all the following arguments, properly separated and quoted". If your arguments are "a b" "c d" "e f", "$@" will put them in as "a b" "c d" "e f", not "a" "b" "c" "d" "e" "f".

The 'shift' eats a commandline argument, i.e. converts $1=a, $2=b, $3=c, $4=d into $1=b, $2=c, $3=d. shift 2 moves it all the way to $1=c, $2=d. It's done since that particular branch uses an argument, so it has to remove two things from the list (the -r and the argument following it) not just one.

I have no idea why they're doing 'eval set', only theories. That's dangerous and dumb. They may be trying to process quotes inside quotes.



They are getting the values from 'getopt', the standard way to handle commandline arguments. I can't really explain its workings, but I'm sure others can.
Hi Corona688,
I agree wholeheartedly with your 1st three paragraphs.

But the "standard" way of handing command line arguments is getopts; not getopt. In the early days of the Bourne shell, there was a getopt utility that had problems dealing with quoted option-arguments and was replaced by the getopts utility that is in the current standards. The getopts utility is processed in a loop extracting one option and its option-argument (if there is one for that option) each time through the loop. The version in the standard doesn't have any provision for processing long options (i.e., --optionname or --optionname=argument), but many implementations of the getopts utility have extensions supporting them.

The standard getopts utility can handle command lines like:
Code:
utility -r -h -b "b option argument" -x -- operand1 operand2
utility -hrx -b 'b option argument' -- operand1 operand2
utility -hrxbb" option argument" -- operand1 operand2

producing identical results for any of the above. And, if operand1's 1st character is not a <hyphen>, will also recognize the following producing identical results to any of the above:
Code:
utility -r -h -b "b option argument" -x operand1 operand2
utility -hrx -b 'b option argument' operand1 operand2
utility -hrxbb" option argument" operand1 operand2

One might guess that the getopt utility called by the script in post #1 in this thread would normalize command line arguments splitting multiple option letters following a single <hyphen> into separate arguments each with its own <hyphen> and would split an an option with an option-argument presented as a single argument into two arguments so that the while loop in that script would work properly, but I have no idea whether the getopt actually being used by this application is this flexible.
# 4  
Old 02-01-2017
Thanks for the information. So when I run the script:
Code:
./script.sh -r test

the
Code:
-- "$@"

assigns "-r" to
Code:
$1

and "test" to
Code:
$2

?
# 5  
Old 02-01-2017
You forgot the set, but: yes! However, $1 was -r before the set, and $2 was test. So...?
# 6  
Old 02-01-2017
What do you mean before the
Code:
set

. How does
Code:
set

change
Code:
$1

and
Code:
$2

?
# 7  
Old 02-01-2017
That's what it does.

Try it.
Code:
set -- a b
echo $1
echo $2

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