Call one bash file from another bash file.

hi .. greetings ...i need help in bash programming. i am on linux mint.

i have two .sh files.(netcheck.sh and dlinkreboot.sh).. please note . both bash files are working perfectly well independently... but cant work together. . in short i want one bash file to call the other bash file automatically and then the control should come back to the first bash file.
the first file (netcheck.sh) detects if internet is active or gone offline. (it checks every few seconds. if internet is dead then it should call upon the second bash file.
the second file (dlinkreboot.sh) reboots the router. (rebooting the router brings the internet back online)

either you show me how to do this or

show me how both contents can be added in one single file. i tried copy pasting but it doesnt work.
both bash files have different first line (i guess its called the header)
the first one netcheck.sh has this ... #!/bin/bash

the second one dlinkreboot.sh has this ....#!/usr/bin/expect -f

these two headers dont not seem to like each other.

i dont know much about bash programming but using my rusted c++ skills that i studied 16 years back.
********************************************************************************************
This is the content of the first bash file. .. netcheck.sh to check whether net is active or not .

#!/bin/bash

downTime=0
lastAccessTime=$(date +"%s")
while [ true ]; do
if ! ping -c1 google.com >& /dev/null; then
    downTime=$(( $(date +"%s") - $lastAccessTime ))
else
    downTime=0
    lastAccessTime=$(date +"%s")
    echo "online"
    sleep 2
    date
fi

if [ $downTime -ge 10 ]; then
   echo "no internet"
   sleep 2
   echo "trying to reset modem"

/bin/bash dlinkreboot.sh

fi
done

********************************************************************************

the second bash dlinkreboot.sh is this

#!/usr/bin/expect -f
 
set timeout 20
 
# router user name and password are identical
set name "admin"
 
# router exit
set name "reboot"
 
# router IP address
set routerip "192.168.254.1"
 
# Read command as arg to this script
#set routercmd [lindex $argv 0]
 
# start telnet
spawn telnet $routerip
sleep 1
 
# send username as admin
expect "Login:"
send -- "admin\r"
sleep 1
# send password as admin
expect "Password:"
send -- "admin\r\r"

# send reboot command
sleep 1

expect *>*
send -- "\r"
expect *>*
send -- "reboot\r"
expect *>*
send -- "\r"
expect *>*
send -- "logout\r"
sleep 70
exit

******************************************************************

Hi,

The seconds file is NOT a bash file. It's an expect file.
You should call it with

/usr/bin/expect -f dlinkreboot.sh

instead of

/bin/bash dlinkreboot.sh

Regards
Santiago

1 Like

that Works .. Thanks for the guidance.