LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-15-2010, 04:25 PM   #1
lordofring
Member
 
Registered: Feb 2005
Posts: 91

Rep: Reputation: 15
a script calls another script which calls "bash" inside


Hi

I need to write a script. In which, the 2nd part commands need to run under another bash shell environment. My script looks like this:

command 1..
enter_another_bash_env.sh
command 2...


The enter_antoher_bash_env.sh will setup a new shell environment, call "bash". The rest commands need to run under the new env. (I cannot change this script too. )

If I run these command one by one manually, it works of course. If I put them into a script, enter_another_bash_env.sh won't return because it calls "bash" command inside.

I tried use "." or "exec", no luck. Any idea how to do it? Thanks.

Regards,

lordofring
 
Old 03-15-2010, 04:48 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
exec "/path/to/script.sh" should work. What is the exact output you're getting when you attempt to do this?

Edit: you could also try sourcing it, but I don't think that will give you the result you're looking for.

Last edited by rweaver; 03-15-2010 at 04:49 PM.
 
Old 03-15-2010, 05:10 PM   #3
lordofring
Member
 
Registered: Feb 2005
Posts: 91

Original Poster
Rep: Reputation: 15
I tried "exec /path/to/script.sh". the "script.sh" replaces the current process and it shows the prompt from the shell. The rest command won't be run.

By the way, this script contains line like this:
"bash --rcfile ~/.new_bash_rc"
 
Old 03-15-2010, 11:34 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Do you mean:
  1. You want to run
    Code:
    command 1..
    enter_another_bash_env.sh
    command 2...
  2. script enter_another_bash_env.sh does not return.
  3. You cannot change script enter_another_bash_env.sh
  4. Script enter_another_bash_env.sh includes bash --rcfile ~/.new_bash_rc
If so:
  1. Does enter_another_bash_env.sh return after running bash --rcfile ~/.new_bash_rc?
  2. Can you change ~/.new_bash_rc? (I think the answer has to be yes).
If script enter_another_bash_env.sh is not secret it might help if you posted it here (or attached it if it is long). If posting it here, please use code tags (that's a link to instructions or you may prefer to use "Advanced Edit" mode which has a # button for code tags).
 
Old 03-16-2010, 08:20 AM   #5
lordofring
Member
 
Registered: Feb 2005
Posts: 91

Original Poster
Rep: Reputation: 15
Thumbs up

Hi catkin,

Your understanding is correct. enter_another_bash_env.sh does not return after bash --rcfile ~/.new_bash_rc.

I must type exit to return for that shell. Then the rest command (commands 2..) will run.

Here is an sample of enter_another_bash_env.sh
Code:
#!/bin/bash
export ...
some command ...
cat > ~/.new_bashrc << EOF
...
EOF
bash --rcfile ~/.new_bashrc
The enter_another_bash_env.sh is attached (renamed). Actually, it's the envsetup.sh from the Moblin SDK.
Attached Files
File Type: txt enter_another_bash_env.txt (1.5 KB, 26 views)
 
Old 03-16-2010, 09:19 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Thanks for the info lordofring

Why do you want to run enter_another_bash_env.sh before running command 2? If it's to get the environment variables it sets up you can grab those with
Code:
cat enter_another_bash_env.sh | grep '^export' > exports.sh
source exports.sh
 
Old 03-16-2010, 09:19 AM   #7
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
You said you ~couldn't~ modify the "enter_another_bash_env.sh"? If so that is going to be a very large problem because the existing way it's setup is not going to allow you to automate it the correct way, you might still be able to do it via expect... maybe.

The correct way would be remove the bash --rcfile ~/.new_bashrc line completely and add the following lines to your script:

Code:
export MOBLIN_TOOLCHAIN=$HOME/dragon/${SDK_VERSION}/moblin-cross-toolchain
source ~/.moblin-skdrc
Actually lets stop. Since this is a bash script calling a bash script... why aren't you just getting rid of the headache and having your script do everything you need it to do and not dealing with their external script in the first place?

eg:

Code:
#!/bin/bash
# command 1

# where you used to call external script... (using exec or whatever)
export SDK_VERSION=moblin-sdk-0.10
export MOBLIN_TOOLCHAIN=$HOME/dragon/${SDK_VERSION}/moblin-cross-toolchain

if [ ! -d ${MOBLIN_TOOLCHAIN} ]; then
    echo ""
    echo "ERROR: can NOT find Moblin build toolchain directory: ${MOBLIN_TOOLCHAIN}"
    echo ""
    echo "please edit envsetup.sh and set the value of \${MOBLIN_TOOLCHAIN} to the \"mobln-cross-toolchain\" directory in the SDK"
    echo ""
else

###################################################
# don't change the below content!!!               #
###################################################

    cat > ~/.moblin-sdkrc << EOF
## set to new value
export PATH=$MOBLIN_TOOLCHAIN/bin:$PATH

export MOBLIN_SYS=$MOBLIN_TOOLCHAIN/i586-moblin-linux/sys-root
export PKG_CONFIG_SYSROOT_DIR=\${MOBLIN_SYS}
export PKG_CONFIG_PATH=\${MOBLIN_SYS}/usr/lib/pkgconfig:\${MOBLIN_SYS}/usr/share/pkgconfig
export host_alias=i586-moblin-linux

if [ `id -u` = 0 ]; then
    export PS1="\[\e[31;1m\][$SDK_VERSION]:\[\e[35;1m\]\w# \[\e[0m\]"
else
    export PS1="\[\e[31;1m\][$SDK_VERSION]:\[\e[35;1m\]\w$ \[\e[0m\]"
fi

if [ -x /usr/bin/dircolors ]; then
    eval "\`dircolors -b\`"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    #alias grep='grep --color=auto'
    #alias fgrep='fgrep --color=auto'
    #alias egrep='egrep --color=auto'
fi

if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi

EOF

fi

source ~/.moblin-sdkrc

# command 2 etc...
Honestly, from what i can see of their script it only really ever needs ran once to generate the .moblin-sdkrc file (for each user, once generated once for your system it could be added to skel or copied to the home directories of everyone who needs it)... not every time you want to do something. Except for some exports (which are legitimately probably needed) all it's doing is changing the prompt... you could add the exports to the .bashrc and be done with your script, their script, etc.

Last edited by rweaver; 03-16-2010 at 09:24 AM.
 
Old 03-16-2010, 10:32 AM   #8
lordofring
Member
 
Registered: Feb 2005
Posts: 91

Original Poster
Rep: Reputation: 15
Hi catkin and rweaver ,

I am using rweaver's solution 1 right now: "source ~/.moblin-sdkrc".

Both of your fix serve the script well.

Let me ask one more question about "bash --rcfile". Is it used for interactive mode only?

From man bash, bash supports "-c string" or "file". So I tried
Code:
bash --rcfile ~/.moblin-sdkrc -c "some command"
bash --rcfile ~/.moblin-sdkrc scirpt_A.sh
I assume these command will start bash shell with the new rc file, execute a command and exit. But it doesn't. The command is running under the regular bash shell environment.

I prefer this solution, if it work. Since my main script will continue without any pollution from the rc file.
 
Old 03-16-2010, 10:46 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Yes, bash' --rcfile option implies an interactive shell. It's useful to run a command in a terminal emulator from a desktop "launcher" and have the window left open so you can see the command output.
 
Old 03-18-2010, 09:49 AM   #10
lordofring
Member
 
Registered: Feb 2005
Posts: 91

Original Poster
Rep: Reputation: 15
Thanks for everyone's help.
 
Old 03-18-2010, 09:59 AM   #11
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
Glad to be of service even if it wasn't from the direction you were initially expecting
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
BASH script with sed file "inside"? SilversleevesX Linux - Newbie 6 02-25-2010 10:31 PM
diff between "system calls" and "kernel threads" shariefbe Linux - Software 5 12-13-2009 10:23 AM
Is 'nice' inherited to child processes? e.g. bash script/php script that calls MySQL SirTristan Linux - Newbie 1 12-04-2008 12:57 AM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration