You are not logged in.

#1 2014-04-18 16:30:41

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

[Solved]Bash: get positional parameter '$num'

I can't figure out how to do it without using eval:

eval param='$'{$num}

I'm pretty sure it's safe, because num will either be $# or the result of $(( $# - 1 )). But it seems like there should be a non-hackish way to do this.

XYZ: I use diff a lot to compare directories so I made the following function. If I pass -1 to the function, the "Only in <first dir>" messages should be filtered out. Likewise for '-2' and '<second dir>'. '-12' should filter out all 'Only in' messages.

ddif() {

    local param exclude diffopts

    while (( $# > 2 )); do

        if [[ $1 = '-i' ]]; then

            diffopts+="--ignore-file-name-case "
            shift

        elif [[ $1 = '-1' ]]; then

            param=$(( $# - 1 ))
            eval param='$'{$param}
            exclude="Only in ${param//\//.}"
            shift

        elif [[ $1 = '-2' ]]; then

            eval param='$'{$#}
            exclude="Only in ${param//\//.}"
            shift

        elif [[ $1 = '-12' ]]; then

            exclude="Only in"
            shift
        fi
    done

    if [[ $exclude ]]; then

        diff -rq $diffopts "$1" "$2" | sed /"$exclude"/d
    else
        diff -rq $diffopts "$1" "$2"
    fi
}
$ ddif /mnt/usb/bash /mnt/linux/bash
Only in /mnt/usb/bash/functions/bak: ddif.f.0
Only in /mnt/usb/bash/functions: ddif.f
Files /mnt/usb/bash/functions/test.f and /mnt/linux/bash/functions/test.f differ
Only in /mnt/linux/bash/: root-bash_aliases
Only in /mnt/linux/bash/: root-bashrc

$ ddif -1 /mnt/usb/bash /mnt/linux/bash
Files /mnt/usb/bash/functions/test.f and /mnt/linux/bash/functions/test.f differ
Only in /mnt/linux/bash/: root-bash_aliases
Only in /mnt/linux/bash/: root-bashrc

$ ddif -2 /mnt/usb/bash /mnt/linux/bash
Only in /mnt/usb/bash/functions/bak: ddif.f.0
Only in /mnt/usb/bash/functions: ddif.f
Files /mnt/usb/bash/functions/test.f and /mnt/linux/bash/functions/test.f differ

Last edited by alphaniner (2014-04-18 17:34:41)


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#2 2014-04-18 17:10:47

aesiris
Member
Registered: 2012-02-25
Posts: 97

Re: [Solved]Bash: get positional parameter '$num'

At the moment I can't see a better solution for your XY problem.

I can offer you these two ways to do the Y part:

#!/usr/bin/bash

i=2

input=("$@")
echo ${input[i]}

echo ${@:$i+1:1}

Offline

#3 2014-04-18 17:33:32

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: [Solved]Bash: get positional parameter '$num'

Thanks, I can definitely work with that. Here's the updated function:

ddif() {
    local exclude diffopts
    while (( $# > 2 )); do
        if [[ $1 = '-i' ]]; then
            diffopts+="--ignore-file-name-case "
            shift
        elif [[ $1 = '-1' ]]; then
            exclude="Only in ${@:$(( $# - 1 )):1}"
            shift
        elif [[ $1 = '-2' ]]; then
            exclude="Only in ${@:$#:1}"
            shift
        elif [[ $1 = '-12' ]]; then
            exclude="Only in"
            shift
        fi
    done
    if [[ $exclude ]]; then
        diff -rq $diffopts "$1" "$2" | sed /"${exclude//\//.}"/d
    else
        diff -rq $diffopts "$1" "$2"
    fi
}

Anyway, I just posted the function for context. I'm not necessarily looking for alternatives or anything.

Last edited by alphaniner (2014-04-18 17:34:05)


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#4 2014-04-19 15:42:51

Stebalien
Member
Registered: 2010-04-27
Posts: 1,239
Website

Re: [Solved]Bash: get positional parameter '$num'

If you don't mind depending on a bash-specific feature, you can use "${!num}". Also, you should consider using a case statement instead of the if-else chain.


Steven [ web : git ]
GPG:  327B 20CE 21EA 68CF A7748675 7C92 3221 5899 410C

Offline

Board footer

Powered by FluxBB