How create a bash script with another bash script?
To prevent variables from being expanded in a here-doc, put quotes around the token.
cat <<'EOT'
This is a here-doc
that contains $variable
EOT
How best to include other scripts?
I tend to make my scripts all be relative to one another.
That way I can use dirname:
#!/bin/sh
my_dir="$(dirname "$0")"
"$my_dir/other_script.sh"
How to source more than one bash script from a folder in another script so I can use functions from both of that scrips?
Try this,
#!/bin/bash
for file in ./deep/*;
do
source $file;
done
Having trouble calling a bash script from another bash script
I figured it out. The exit status is what I need, so I need to call it and set the variable to the exit status stored in $?
. The issue is that I was catching the stdout of the other script and storing it in the variable directories_are_same
as if it were a return value that I was expecting, when what I needed was its exit status. I could echo something from the other script and then treat the stdout as a returned string, but that is not how this script was designed.
Here is the working test script:
#!/bin/bash
dir_1=~/test
dir_2=~/test1
~/bin/compare_directories "$dir_1" "$dir_2"
directories_are_same="$?"
{
if [ "$directories_are_same" -eq 3 ]; then
echo "One of the directories $dir_1 and $dir_2 does not have read permissions!"
exit 1
elif [ "$directories_are_same" -eq 4 ]; then
echo "One of the directories $dir_1 and $dir_2 does not exist!"
exit 1
elif [ "$directories_are_same" -eq 5 ]; then
echo "One of the directories $dir_1 and $dir_2 is not a directory!"
exit 1
fi
} >&2
if [ "$directories_are_same" -eq 0 ]; then
echo "The directories $dir_1 and $dir_2 contain identical content"
exit 0
elif [ "$directories_are_same" -eq 1 ]; then
echo "The directories $dir_1 and $dir_2 do not contain identical content"
exit 0
else
echo "Something went wrong" >&2
exit 1
fi
Now when the directories are different I get:
The directories /home/astral/test and /home/astral/test1 do not contain identical content
and when they are the same I get:
The directories /home/astral/test and /home/astral/test1 contain identical content
bash: how _best_ to create a script from another script
Any way of outputting from the first script will work, so echo
or cat
with a heredoc should be fine:
cat << EOT
these will be
the lines of
the second script
EOT
Launch shell script from another script
You could use an ampersand (&
) and launch script.sh $1 &
ten times. This will make the script run in a fork of the main process. It is an easy way to do parallel processing but definitely not very flexible and doesn't have many features. A simple tutorial can be found here.
Related Topics
Read Data from Proc/Sys/Kernel/
How to Fix Nginx 502: Bad Gateway Error on a Digital Ocean Droplet - Ubuntu 20.04
Difference Between Source and ./ Execution of Linux Scripts
How to Add Text at the Beginning of Specific Lines Using Sed
Jupyter Lab - Suppress Console Output
Start Docker-Compose Automatically on Ec2 Startup
Sched_Fifo Higher Priority Thread Is Getting Preempted by the Sched_Fifo Lower Priority Thread
Difference Between Core and Core-File
How to Print Current Time in Kernel
Sed Not Working [Unterminated 'S' Command]
Interacting with Files from Multiple Directories via Bash Script
How to Use a Samba Server Location for Gopath
Shell Function Does Not Return Values Greater Than 255