Adam P. Adam P. - 4 months ago 11
Bash Question

Why does not working bash files include?

I looked now more like a theme (e.g.: Bash: How _best_ to include other scripts?, it also tried none of them work.

I should also call "create_sample_html.sh" file in the same directory in the "test.sh" file.

But no matter what I tried, nothing happens when you run.

But in I tried to run script in Docker container return the following error message:


./create_sample_html.sh: No such file or directory

create_php_info: command not found

create_html: command not found


test.sh:

#!/bin/bash

source ./create_sample_html.sh

IP=$(ip route get 1 | awk '{print $NF;exit}')

create_php_info "<path-to-file>/index.php"
create_html ${IP} "test1" "<path-to-file>/index.html"

exit 0


create_sample_html.sh:

#!/bin/bash
create_html() {
touch $3
echo -e "<!Doctype html>" >> $3
echo -e "<html>" >> $3
echo -e "\t<head>" >> $3
echo -e "\t\t<meta charset=\"utf-8\">" >> $3
echo -e "\t\t<title>Server $1 VHost: $2</title>" >> $3
echo -e "\t</head>" >> $3
echo -e " " >> $3
echo -e "\t<body>" >> $3
echo -e "\t\t<h1>Server $1 VHost: $2</h1>" >> $3
echo -e "\t</body>" >> $3
echo -e "</html>" >> $3
}

create_php_info() {
touch $1
echo -e "<?php phpinfo(); ?>" >> $1
}
exit 0


file permissions:

-rwxrwxr-x 1 user user 480 aug 3 20:48 create_sample_html.sh
-rwxrwxr-x 1 user user 332 aug 3 20:37 test.sh

Answer

In create_sample_html.sh, remove exit 0.

Else, test.sh will execute exit 0 at the moment you are sourcing it. In the same step you can also remove #!/bin/bash from the sourced file because it's unnecessary since the functions will be executed with the shell of the parent script anyway.