Running a script from script (bash)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Running a script from script (bash)
# 1  
Old 12-23-2008
Running a script from script (bash)

Hello

I am a UNIX noob, attempting to use Cygwin on Windows XP to build a program. I am trying to get a few scripts together that can automate some tasks and I have run into a problem.

Basically this is related to calling one script from another. I found this thread in my search but am too noob to know if it answers my problem or not, so I will explain here.

I am calling one script (say: myscript.sh) from my home directory (the script itself is in the cygwin/usr/local/bin directory). Now on one line of this script it calls another script that is on a different drive and folder (say: cygdrive/d/dev/project) and it is called winenv.set.sh.

Now within winenv.set.sh a number of environmental variables are set. But what I notice is that after calling 'myscript.sh' the environmental variables have not been set. I know it works properly if I change into that directory and run the file using: . winenv.set.sh. And that is the same line I am using in 'myscript.sh'.

If I run: bash -v myscript.sh I end up seeing the text of the winenv.set.sh file in the console.

Hopefully my explanation is clear but if not please let me know how I can clarify, and thank you for any help you can provide. Smilie
# 2  
Old 12-26-2008
Here is the solution for those who come across this thread in the future.

The problem in a nutshell is that a script itself runs in a sub-shell, so setting environment variables in that script only sets them in the sub-shell (which is destroyed when the script finishes). The solution is to run the script using the 'source' command which allows the script to change the environment variables in the calling shell.

In my case, I am calling a script that calls a script that sets environment variables:

source myscript.sh

- and myscript.sh will contain a line: source winenv.set.sh


note that a period can be used instead of 'source'.

. myscript.sh

. winenv.set.sh


More information here: http://www.cygwin.com/ml/cygwin/2005-05/msg01011.html

Last edited by bobban; 12-26-2008 at 02:23 AM.. Reason: more info
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Running nmap via bash script

Hello fellow scripters... I am trying to set up a script for work that reads the contents of a file of IP address and network names, takes these and runs a given nmap scan against them and then saves the output to a file with the filename of the network name. Such as... IP file looks like... (4 Replies)
Discussion started by: hawkeyesc72
4 Replies

2. Shell Programming and Scripting

Running options in bash script

Hello UNIX & Linux Forums community! Long time Linux daily user hobbyist, new to shell scripting.... I'm working on a script that does all the "work" in one script, and makes calls to a second script to display info to the user via mostly expanding variables in heredocs. I'm contemplating... (6 Replies)
Discussion started by: Cody Learner
6 Replies

3. Shell Programming and Scripting

Bash shell script to check if script itself is running

hi guys we've had nagios spewing false alarm (for the umpteenth time) and finally the customer had enough so they're starting to question nagios. we had the check interval increased from 5 minutes to 2 minutes, but that's just temporary solution. I'm thinking of implementing a script on the... (8 Replies)
Discussion started by: hedkandi
8 Replies

4. Shell Programming and Scripting

script running by sh but not by bash

execute the attached script as, bash -x INFA_MAP_GEN_XML.sh "/export/home/e120945/INFORMATICA_MAPPING_GENERATOR/SOURCE" "INFA_TEMPLATE.csv" "/export/home/e120945/INFORMATICA_MAPPING_GENERATOR/TARGET" "UNIX" "/export/home/e120945/INFORMATICA_MAPPING_GENERATOR/SETUP"... (5 Replies)
Discussion started by: 120945
5 Replies

5. UNIX for Dummies Questions & Answers

Help Running a Check in Bash Script

Hey guys, so I wrote a small script that pretty much just takes in two numbers and counts from the first to the second, e.g. unknown-hacker|544> count.sh 1 3 1 2 3 My problem is I want to make it so that if you input invalid parameters, such as non-numerical characters, more than 2... (2 Replies)
Discussion started by: Duo11
2 Replies

6. UNIX for Dummies Questions & Answers

Running Executable in Bash Script

Hey guys, so I've been trying to write a bash script called runSorter.sh that runs an executable that also takes in some parameters and outputs the results to a text file. The executable, sorter, takes in a number parameter. I want to make it so that you can input as many number parameters into... (4 Replies)
Discussion started by: Duo11
4 Replies

7. Shell Programming and Scripting

Running matlab script from within bash script

Hi guys, I'm new to scripting and I'm wondering if I can run a matlab script from within a bash script. Basically, at the minute I have a bash script which creates a file "bottfile.xyz" I then import "bottfile.xyz" into matlab using importdata('bottfile.xyz'). Then I just run the matlab... (0 Replies)
Discussion started by: jhunter87
0 Replies

8. Shell Programming and Scripting

Validate BASH script before running it...

Hello, Is there some way to validate a bash script before running it. I want to make sure all of the syntax and everything is good so that I dont get a false return code. Thanks, tom (7 Replies)
Discussion started by: tjones1105
7 Replies

9. Shell Programming and Scripting

Script running in bash 3.0 not in 3.2

I have wasted one working day writing this scripts. It compares two folders and make a good tabbed report about their differences. #!/bin/bash function DRAW_DEPTH () { ROUND=$1 while do printf %s " " ROUND=`expr $ROUND - 1` done printf %s "|- " } function MAIN () {... (9 Replies)
Discussion started by: trutoman
9 Replies

10. UNIX for Advanced & Expert Users

debugging an already running bash script

# ps -ef | grep rc root 13903 1 0 08:56 ? 00:00:00 /usr/sbin/automount --timeout=60 /archive file /etc/auto_archive root 10706 1 0 08:55 ? 00:00:00 /bin/bash /etc/rc.d/rc 3 root 2933 20071 0 19:38 pts/1 00:00:00 grep rc Is there any way to debug the... (1 Reply)
Discussion started by: marcpascual
1 Replies
Login or Register to Ask a Question