LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash script with environment variables -Application launcher (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-with-environment-variables-application-launcher-740246/)

Offman 07-15-2009 09:58 AM

Bash script with environment variables -Application launcher
 
Hi

I have a bash script where I use EXPORT to set some environment variable. I know if I source it or call it like this ". ~/shellscript.sh" it will set these variables globally.
But is there a way of creating an application launcher for this so it sets the variables properly?

cheers

catkin 07-15-2009 10:13 AM

Hello Offman :)

Almost certainly. What do you mean by "properly"?

Best

Charles

Offman 07-15-2009 10:14 AM

well if i start it with an application launcher the variables are not set globally. so in another terminal on export they are not listed.

onebuck 07-15-2009 10:17 AM

Hi,

Welcome to LQ!

Quote:

Originally Posted by Offman (Post 3608239)
Hi

I have a bash script where I use EXPORT to set some environment variable. I know if I source it or call it like this ". ~/shellscript.sh" it will set these variables globally.
But is there a way of creating an application launcher for this so it sets the variables properly?

cheers

Maybe you should look at creating a '.bashrc' & '.bash_profile' for your user.

Do a search here on LQ as I know this very subject has been covered many times. :)

Offman 07-15-2009 10:20 AM

@onebuck

thanks for the reply.
However, I do not want to include the variables in the bashrc. I am using a virtual machine on different networks and machines. And I wrote a script that mounts the shared folders and sets the network setting according to where it is used. The script already gets called by bashrc but I really prefer to have an application launcher so I can set it up myself. If everytime I open a terminal the script runs it will be quiet annoying with the scripts...

best
marc

onebuck 07-15-2009 10:25 AM

Hi,

Thanks for clearing up as to what you want to do. You could include the call as a 'alias' within your '.bashrc'. :)

Offman 07-15-2009 10:44 AM

So I simply make an alias to source script.sh and than I put the alias as the command in the application launcher?

onebuck 07-15-2009 10:57 AM

Hi,

Quote:

Originally Posted by Offman (Post 3608314)
So I simply make an alias to source script.sh and than I put the alias as the command in the application launcher?

Quote:

#.bashrc
#08-30-06 12:20

# Add bin to path
export PATH="$PATH:$HOME/bin"

# Dynamic resizing
shopt -s checkwinsize

# Custom prompt
#PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

#08-29-06 11:40

if [ `id -un` = root ]; then
PS1='\[\033[1;31m\]\h:\w\$\[\033[0m\] '
else
PS1='\[\033[1;32m\]\h:\w\$\[\033[0m\] '
fi
#
# Add color
eval `dircolors -b`

# User defined aliases
alias cls='clear'
alias clls='clear; ls'
alias ll='ls -l'
alias lsa='ls -A'
alias lsg='ls | grep'
alias lsp='ls -1 /var/log/packages/ > package-list'
alias na='nano'
alias web='links -g -download-dir ~/ www.google.com'

#07-15-09 10:55
alias some='export PATH="$PATH:$HOME/bin/some"' <<<<- is this OK?

#08-29-06 11:50

#To clean up and cover your tracks once you log off
#Depending on your version of BASH, you might have to use
# the other form of this command
trap "rm -f ~$LOGNAME/.bash_history" 0

#The older KSH-style form
# trap 0 rm -f ~$LOGNAME/.bash_history

What about setting the 'alias' as exampled above? You could tweak and include a conditional but the general idea is to have the path set to a specific environment. You could clear that same 'some' at completion. Just a simple thought. :)

Offman 07-15-2009 11:35 AM

Well I really want to have an icon at the taskbar to quickly run the script. don't want to include all the code in the bashrc.
Is there a way?

onebuck 07-15-2009 12:36 PM

Hi,

If that's all then just create the script and 'chmod +x' then just point to it with your file manager of choice.

Offman 07-15-2009 12:41 PM

@onebuck thanks for the help by the way...
I tried this but then it won't set the variables globally....

onebuck 07-15-2009 12:53 PM

Hi,

When you say globally are you wanting system wide settings for all users?

Let's be specific. I keep getting bits of information from your posts. Layout exactly what you are attempting. What exactly are you launching? Is this a trade secret? :)

Really it may be something as simple as setting up your profiles.

catkin 07-15-2009 12:56 PM

Hello Offman :)

If I understand correctly what you want to do it's impossible, based on how *n*x processes work. Each child process inherits environment variables from its parent, thus it is not possible (and not desirable!) for a child process to somehow inject environment variables into all (you did say "global") processes.

I guess there's something you want to do and you have assumed that the solution is to set environment variables globally. Maybe better to re-wind and tell us what you want to achieve.

Best

Charles

Offman 07-15-2009 01:05 PM

hi catkin and onebuck,

I managed to rewrite my script to be a bit more clever. so now it is really only doing the work if necessary and thus it is ok that it runs each time in bashrc.

...what I wanted to do:
I have a vmware virtual machine with ubuntu on a small portable drive. I use this vm on a Windows Vista and a fedora core box. One is at home and needs no proxy etc the other at work need specific settings and proxys. also the paths to the shared folders are different on both systems. so now i have written a script which checks whether mounting is ok and if not it does it according to the system the vm is running on. also it checks whether it is the work or home network and does the job. at the beginning it was very simple and did it's job without checking whether there is anything to do and thus it was annoying to have it run each time the .bashrc is read. no it is ok. also one problem was that sometimes I just resume the vm and thus I have to find an easy way to adapt the settings. so now i simply have to bring up a terminal and it does the trick...
thats it!


thanks everyone for the help!

onebuck 07-15-2009 01:21 PM

Hi,

It would have been much easier if you had explain what you were attempting to begin with. Read the drift of the thread and you will see how bits of information was provided.

Now you are speaking about a 'VM'. Important information there.

I'm glad you got things working for you. :)

Please mark the thread solved via the thread tools.

The second link below would be helpful to you for future posts;


All times are GMT -5. The time now is 05:53 AM.