You are not logged in.

#1 2016-02-23 03:19:22

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

[bash] Exporting a var script->script->script

I'm writing a chroot build manager script (similar to and inspired by graysky's clean-chroot-manager). It calls makechrootpkg which calls arch-nspawn, which does the actual build (technically that's not even true, but nevermind that). One of the features of my script is use of overlayfs to merge a separate pkg cache dir over the system pkg cache dir to avoid "polluting" the latter.

Anyway, arch-nspawn has a flag which could direct it to make use of my merged cache, but makechrootpkg is not written to make use of that flag in its arch-nspawn calls. Initially I relied on a modified makechrootpkg in /usr/local/bin. But just now it occurred to me that I could export from my script the variable that the arch-nspawn flag would set and it would have the same effect. The major downside being that it's dependent on makechrootpkg never using that var, and arch-nspawn never setting it outside of getopts. It also affects all arch-nspawn calls made by makechrootpkg, whereas ideally it would only affect the one call that [calls systemd-nspawn which runs a script that] does the build. But that's another issue altogether.

So the question is, how awful a practice is this? If really awful, any suggestions for better alternatives, particularly if I may eventually share my script?


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 2016-02-23 13:55:02

Alad
Wiki Admin/IRC Op
From: Bagelstan
Registered: 2014-05-04
Posts: 2,407
Website

Re: [bash] Exporting a var script->script->script

I see a lot of text, but no code. Post what you have so far. smile


Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby

Offline

#3 2016-02-23 14:43:56

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

Re: [bash] Exporting a var script->script->script

This is a question of good practice (or not) rather than implementation. It doesn't seem like posting my entire 800+ line script would be practical, nor would an excerpt be practical or meaningful IMO. But if it's not clear what I'm describing:

/tmp/top-script

#!/bin/bash

export foo='bar'

/tmp/middle-script

/tmp/middle-script

#!/bin/bash

/tmp/bottom-script

/tmp/bottom-script

#!/bin/bash

echo $foo

Exported foo is seen by bottom-script:

$ /tmp/top-script
bar

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 2016-02-23 15:28:19

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: [bash] Exporting a var script->script->script

You're talking about exporting cache_dir in the "top script", right? So that arch-nspawn (when makechrootpkg calls it without passing the -c option) picks this up?

I doubt anyone would call this good practice. That variable is meant for internal use in the "bottom script". You should use external interfaces, but the problem is that makechrootpkg doesn't allow you to supply extra options for arch-nspawn and there's also no environment variable for this.

You could export a wrapper function around arch-nspawn that adds the cache dir parameter in your script, like

#!/bin/bash

arch-nspawn() {
    /usr/bin/arch-nspawn -c $my_cachedir "$@"
}
export -f arch-nspawn

# rest of script calling makechrootpkg

This only requires that makechrootpkg continues to call arch-nspawn without using a full path*. Not great, but less awful than depending on naming of internal variables. Also, while this script targets Arch, I can imagine that some people/distributions have completely removed this functionality from bash after the Shellshock scare.

Regardless of which option you use, have you submitted a feature request/patch?

* and without passing a -c option of its own, but when that happens it should also offer a way to set that (or you can filter $@)

Last edited by Raynman (2016-02-23 15:43:24)

Offline

#5 2016-02-23 16:26:38

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

Re: [bash] Exporting a var script->script->script

Yes, I'm talking about exporting cache_dir. I figured it was not good practice, but I was curious for a second opinion.

Thanks for the function idea, though in this case* I don't understand why it's less awful than exporting a var.

I may file a feature request, but didn't want to go there until I made some effort to find an alternative. OTOH, what I really want is for one particular arch-nspawn call to use my merged cache, and that's too specific to request.

* makechrootpkg and arch-nspawn are both part of devtools and I could have my script require a particular version. It probably wouldn't be a bad idea to do that regardless.

Edit: I decided to re-implement makechrootpkg as a function in my script along with slightly modified versions of its functions as necessary. Since both makechrootpkg and my script are primarily functions it was a breeze.

Last edited by alphaniner (2016-02-24 03:46:51)


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

Board footer

Powered by FluxBB