You are not logged in.
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 differLast 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
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
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
Offline