You are not logged in.

#1 2018-04-29 09:01:07

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,597
Website

How to pass multiple commands as a variable in a script [solved]

I'd like to have a variable in a script pass a series of command through a pipe to filter output of a command command but am unsure how to do it.

See: https://github.com/graysky2/lostfiles/c … fe7ab8d296

Last edited by graysky (2018-04-29 16:55:39)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#2 2018-04-29 09:31:51

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: How to pass multiple commands as a variable in a script [solved]

I'm not even entirely sure what you're trying to do there. BTW I'd use makepkg's "/usr/share/makepkg/util/parseopts.sh" library instead of getopt which is horrible.

I think, what you're trying to do is offer a configurable filter in a pipeline. But that won't work at all, using variables. Variables either don't do proper word splitting (use arrays instead), or refuse to do what you expected because the pipe itself cannot be added to a command line using a variable or an array. You could do it by cheating and using eval, but for obvious reasons that is ludicrous insanity.

You could define the filters in a function, though, and then pipe to the function.


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#3 2018-04-29 09:37:24

progandy
Member
Registered: 2012-05-17
Posts: 5,192

Re: How to pass multiple commands as a variable in a script [solved]

You probably need eval, but if you can define fixed functions with parameters it is cleaner.

echo foo | eval "$bar"

Edit: Forgot to refresh, Eschwartz already said it all.

Last edited by progandy (2018-04-29 09:40:41)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#4 2018-04-29 11:57:28

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,597
Website

Re: How to pass multiple commands as a variable in a script [solved]

Thanks for the replies, guys.  Basically I want to have the minimal amount of code to let the user select between:

  comm -13 \
    <(pacman -Qlq | sed -e 's|/$||' | sort -u) \
    <(find / -not \( \
    -wholename '/dev' -prune -o \
    -wholename '/home' -prune -o \
    -wholename '/lost+found' -prune -o \
    -wholename '/media' -prune -o \
    -wholename '/mnt' -prune -o \
    -wholename '/proc' -prune -o \
    -wholename '/root' -prune -o \
    -wholename '/run' -prune -o \
    -wholename '/scratch' -prune -o \
    -wholename '/srv' -prune -o \
    -wholename '/sys' -prune -o \
    -wholename '/tmp' -prune -o \
    -wholename '/var/.updated' -prune -o \
    -wholename '/var/lock' -prune -o \
    -wholename '/var/lib' -prune -o \
    -wholename '/var/log' -prune -o \
    -wholename '/var/run' -prune -o \
    -wholename '/var/spool' -prune \) | sort -u \
    ) | sed -e 's|^\t||;'

And

  comm -13 \
    <(pacman -Qlq | sed -e 's|/$||' | sort -u) \
    <(find / -not \( \
    -wholename '/dev' -prune -o \
    -wholename '/home' -prune -o \
    -wholename '/lost+found' -prune -o \
    -wholename '/media' -prune -o \
    -wholename '/mnt' -prune -o \
    -wholename '/proc' -prune -o \
    -wholename '/root' -prune -o \
    -wholename '/run' -prune -o \
    -wholename '/scratch' -prune -o \
    -wholename '/srv' -prune -o \
    -wholename '/sys' -prune -o \
    -wholename '/tmp' -prune -o \
    -wholename '/var/.updated' -prune -o \
    -wholename '/var/lock' -prune -o \
    -wholename '/var/lib' -prune -o \
    -wholename '/var/log' -prune -o \
    -wholename '/var/run' -prune -o \
    -wholename '/var/spool' -prune \) | sort -u \
    ) | sed -e 's|^\t||;' | sed -e '1d' | xargs -n1 du -s | sort -rn -k1

Note the difference is just the last line:

    ) | sed -e 's|^\t||;'

or

    ) | sed -e '1d' | xargs -n1 du -s | sort -rn -k1

CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#5 2018-04-29 16:08:05

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: How to pass multiple commands as a variable in a script [solved]

filter() {
    sed -e 's|^\t||;'
}

filter_size() {
    sed -e '1d' | xargs -n1 du -s | sort -rn -k1
}

use_filter=filter

while getopts 'sz' OPTION; do
  case "$OPTION" in
    z)
        use_filter=filter_size
[...]
        ) | $use_filter

Note, your option parsing does not seem to properly handle no arguments, which according to that change seems to be a use case which you now accept. You'll need to handle the case of '' before handling *.


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#6 2018-04-29 16:56:00

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,597
Website

Re: How to pass multiple commands as a variable in a script [solved]

@Eschwartz - Thank you!  Your implementation worked as I expected.
https://github.com/graysky2/lostfiles/c … 1c1b1b7705

Last edited by graysky (2018-04-29 16:56:20)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

Board footer

Powered by FluxBB