LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   I'm stumped - Script question (https://www.linuxquestions.org/questions/linux-newbie-8/im-stumped-script-question-96669/)

t3kn0lu5t 09-25-2003 05:37 AM

I'm stumped - Script question
 
Yes, I am a linux newbie, but I'm not stupid. First time using linux and I've managed to find comfort in Gentoo. Alas, I'm stuck on something simple, I just can't find the resource to solve my problem.

I've created a little script to setup my network for my work environment. It's called worknet.sh. I keep it in my home dir, and I run it as root so I can do emerge from work. the contents follow:

dhcpcd eth0
export http_proxy="hteeteep://ipaddress.that.it.won't.let.me.post:port"
export ftp_proxy="fteep://ipaddress:21"

(the IPs have been changed.)
My problem is that dhcpcd runs fine, and sets up eth0, but my exports never take cause I end up having to type them in manually after I run the script in order for emerge to find the proxy.
What am I doing wrong?

Thanks in advance

cadj 09-25-2003 07:47 AM

have u checked the case?

export DISPLAY=192.168.0.1:0.0

and mayb u dont need the "" marks

not sure but try it

t3kn0lu5t 09-25-2003 11:53 AM

I don't think that would matter because when I use the export command at the prompt I use all lower case and it works. It's the exact same way in the script.

unSpawn 09-25-2003 12:02 PM

Well, not wrong, but for Bash global exports are usually done from /etc/profile, /etc/bashrc and the stuff in /etc/profile.d depending on it being a login or non-interactive shell. If you don't want to have it exported globally, then you'd have to remember those exports will only live as long as you run that script. here's an outline of how things get sourced by default:
Code:

Bash invoked as "/bin/bash"
interactive login shell:
/etc/profile
        + /etc/inputrc
        + /etc/profile.d/*.sh
~/.bash_profile
        + ~/.bashrc
                + /etc/bashrc
        + ~/.bash_login
        + ~/.inputrc
~/.bash_login

interactive non-login shell or non-interactive:
        + ~/.bashrc
                + /etc/bashrc
                        + /etc/profile.d/*.sh

Bash invoked as "/bin/sh"
interactive login shell or non-interactive shell (with --login)
/etc/profile
~/.profile

If "emerge" doesn't require arguments, just tack it onto the script at the end, if "emerge" does, then you can supply them on the commandline:


#!/bin/sh
# Check if the interface is up already
/sbin/ifconfig eth0 2>/dev/null|tr -s " "|grep -qe "^ UP";
case "$?" in 0) ;; *) dhcpcd eth0;; esac
# Minimal check if exported values are available
case "${#http_proxy}" in
0) export http_proxy="hteeteep://ipaddress.that.it.won't.let.me.postort";; esac
case "${#ftp_proxy}" in
0) export ftp_proxy="fteep://ipaddress:21";; esac

case "$@" in
0) echo "Running emerge with no options"; emerge;;
*) case "$1" in
h|-h|--help) echo "$(basename $0) <emerge options>"; exit 1;;
*) echo "Running emerge with options \"$@\""; emerge "$@";;
esac;;
esac

Medievalist 09-25-2003 01:22 PM

Listen to unSpawn.

Basically, exports are only inherited by child processes, and cannot be "pushed back up the process tree".

Your shell script sets some variables, they are available in that script only. You export them, and they are available in processes initiated by that script. But regardless of what you do, once that script exits the variables go away.

When you type them in, you're setting them in the current environment space, rather than in a script's private space.
This is a fundamental design feature of *nix. It's called "inheritance" because it only goes one way... :)

Maybe what you want to do is call dhcpcd at boot time, then set your variables at login time. Or do both at login time from .profile or .bashrc .

t3kn0lu5t 09-25-2003 07:16 PM

I see.. Thanks for explaining inheritance to me, and thanks for the example script!

JZL240I-U 09-26-2003 01:26 AM

Neat piece, unSpawn :D. That's a lot explained.
Thanks also, Medievalist.


All times are GMT -5. The time now is 08:36 PM.