0

I have a doubt about running multiple scripts from a third one:

first.sh

#!/bin/bash
echo "script 1"
#... and also download a csv file from gdrive

second.sh

#!/bin/bash
echo "script 2"

third.awk

#!/usr/bin/awk -f 

BEGIN {
    print "script3"
}

I would like a 4th script that run them in order, I've tried the following but only runs the first script.

#!/bin/bash

array=( first.sh second.sh )
for i in "${array[@]}"
do
   chmod +x $i
   echo $i
   . $i 
done

But only runs the first script and nothing else.

Thank you very much for the support! Santiago

3
  • Loop through the index or the array, something like: for i in "${!array[@]}"; do echo "${array[i]}}"; done Commented Jan 12, 2023 at 15:11
  • 1
    Do you want to run the scripts, or to source them? Commented Jan 12, 2023 at 15:18
  • 2
    You're probably looking for./"$i" instead of . "$i" Commented Jan 12, 2023 at 15:30

1 Answer 1

1

You can't source an awk script into a shell script. Run the script instead of sourcing it.

. (aka source) executes commands from the file in the current shell, it disregards the shebang line.

What you need instead is ./, i.e. path to the script, unless . is part of your $PATH (which is usually not recommended ).

#!/bin/bash

array=( first.sh second.sh )
for i in "${array[@]}"
do
   chmod +x "$i"
   echo "$i"
   ./"$i"  # <---
done

Why is the second script not running? I guess the first script contains an exit, which when sourced exits the shell, i.e. it doesn't continue running the outer wrapper.

Sign up to request clarification or add additional context in comments.

3 Comments

This "$i" is not working at all. The dot is needed but with a "/", like that ./"$i", see?)
@Ivan: It depends on the setting of $PATH, but generally, you're right. Fixed.
But ./ is a path, while . with a space is a builtin command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.