William Falcon
William Falcon

Reputation: 9823

How to run shell script from bash script

I get an error (on line: sh up.sh) running the following:

#!/bin/bash

# Install angular components 
echo "Installing Angular Components..."
cd angApp
npm install

# Install Server components
echo "Installing Backend Components..."
cd ..
cd APIServer

# go back to main dir
cd ..

# ask to see if we should launch server
echo "Do you want to launch the server now? Enter (yes/no)  "
read shouldLaunch

# Launch if requested. Otherwise end build
if [ "$shouldLaunch" == "yes" ]; then
    echo "Great! Launching the servers for you..."
    sh up.sh
else
    echo "No problem..."
    echo "you can launch the server by doing ./up.sh"
    echo "bye!"
fi

How do I run the up.sh script?

Upvotes: 0

Views: 157

Answers (2)

clt60
clt60

Reputation: 63972

To avoid cd-ing mess, simply run the parts in subshells, like:

#!/bin/bash

(
# Install angular components - in shubshell
echo "Installing Angular Components..."
cd angApp
npm install
)

(
# Install Server components - again in subshell
echo "Installing Backend Components..."
cd APIServer
#do something here
)    

# go back to main dir
#cd .. #not needed, you're now in the parent shell...

# ask to see if we should launch server
echo "Do you want to launch the server now? Enter (yes/no)  "
read shouldLaunch

# Launch if requested. Otherwise end build
if [ "$shouldLaunch" == "yes" ]; then
    echo "Great! Launching the servers for you..."
    sh up.sh
else
    echo "No problem..."
    echo "you can launch the server by doing ./up.sh"
    echo "bye!"
fi

Upvotes: 1

If the up.sh file is in the same directory as the file containing the code above then you can do

echo "Great! Launching the servers for you..."
$(dirname $0)/up.sh

The variable $0 is the path of the current script, dirname strips off the last segment of the path, and $(...) turns the output of dirname into a string.

Upvotes: 2

Related Questions