Directory exist if [-d "$/root/folder" ]; is not working - shell

I need to create a directory in root directory of Linux if it's not already present in it.So i am using the if [-d "$/root/folder" ]; but it's going to else part even if directory exist. Please help with this. Below is the code-
#! /bin/sh
if [ -d "$/root/folder" ];then
echo "folder file already exist"
else
echo "Settings in progress" \
&& mkdir folder \
&& chmod 7777 folder \
&& cd folder \
&& mkdir FolderConnector \
&& chmod 7777 FolderConnector \
&& cd FolderConnector \
&& mkdir ClientInput \
&& mkdir ClientOutput \
&& chmod 7777 ClientInput \
&& chmod 7777 ClientOutput \
&& cat /home/data/RTV/file2 >>/etc/exports \
&& exportfs -r \
&& exportfs \
&& echo "Settings completed successfully."
fi

What I would do :
#! /bin/sh
set -e
if [ -d "/root/folder" ]; then
echo "folder file already exist"
else
echo "Settings in progress"
mkdir folder
chmod 7777 folde
cd folder
mkdir FolderConnector
chmod 7777 FolderConnector
cd FolderConnector
mkdir ClientInput
mkdir ClientOutput
chmod 7777 ClientInput
chmod 7777 ClientOutput
cat /home/data/RTV/file2 >>/etc/exports
exportfs -r
exportfs
echo "Settings completed successfully."
fi
set -e stop the script on the first error
you had a $ character in your test

Related

Need Shell functions replacement for conditions

I am very new to shell scripting and i need to write a script, the conditions are pretty straight fwd:
i have 2 dir location
First Dir:
$ABC_123/cert
which contains 3 files:
file1.pem
file2.pem
file3.pem
Second Dir:
$ABC_123/private
which contains 2 files:
file4.pem
file5.pem
these files needs to be replaced/appended with files from a user input dir location depending on below conditions
if file3 is present in the user input dir location then take back of all files under the above two dir and then replace file1,file2,file4,file5 and append file3
if file3 is not present in the user input dir location then take back up and replace only file 1,file2,file4,file5 only
i want to write a shell function for back up and replace/append conditions.
Below is the shell i wrote:
#!/bin/sh
$ABC_123=/ABC/123/XCOM
export ABC_123
# input the change number
printf 'Enter the NUMBER number\n'
read NUMBER
file1="$ABC_123/certs/fil2cert.pem"
file2="$ABC_123/certs/fil2cert.pem"
file3="$ABC_123/certs/fil3cert.pem"
file4="$ABC_123/private/fil4key.pem"
file5="$ABC_123/private/fil5key.pem"
file6="$ABC_123/private/fil6key.pem"
file7="/tmp/$NUMBER/fil3cert.pem"
# Begining of the implementation
if [ -f $file1 ] && [ -f $file2 ] && [ -f $file3 ] && [ -f $file4 ] && [ -f $file5 ] && [ ! -f $file6 ] && [ -f $file7 ]
then
#
#BACKUP
#
cp -p $file1 $file1.$(date +%Y-%m-%d).bkp
cp -p $file2 $file2.$(date +%Y-%m-%d).bkp
cp -p $file3 $file3.$(date +%Y-%m-%d).bkp
cp -p $file4 $file4.$(date +%Y-%m-%d).bkp
cp -p $file5 $file5.$(date +%Y-%m-%d).bkp
#
#REPLACE
#
cp /tmp/$NUMBER/*key.pem $ABC_123/private/
cp /tmp/$NUMBER/*cert.pem $ABC_123/certs/
cat /tmp/$NUMBER/fil3cert.pem >> $ABC_123/certs/fil3cert.pem
#
#CHANGE PERMISSIONS
#
chmod 644 $ABC_123/private/*key.pem
chmod 644 $ABC_123/certs/*cert.pem
chmod 644 $ABC_123/certs/fil3cert.pem
chown root:root $ABC_123/private/*key.pem
chown root:root $ABC_123/certs/*cert.pem
chown root:root $ABC_123/certs/fil3cert.pem
elif [ -f $file1 ] && [ -f $file2 ] && [ -f $file3 ] && [ -f $file4 ] && [ -f $file5 ] && [ ! -f $file6 ] && [ ! -f $file7 ]
then
#
#BACKUP
#
cp -p $file1 $file1.$(date +%Y-%m-%d).bkp
cp -p $file2 $file2.$(date +%Y-%m-%d).bkp
cp -p $file3 $file3.$(date +%Y-%m-%d).bkp
cp -p $file4 $file4.$(date +%Y-%m-%d).bkp
#
#REPLACE
#
cp /tmp/$NUMBER/*key.pem $ABC_123/private/
cp /tmp/$NUMBER/*cert.pem $ABC_123/certs/
#
#CHANGE PERMISSIONS
#
chmod 644 $ABC_123/private/*key.pem
chmod 644 $ABC_123/certs/*cert.pem
chown root:root $ABC_123/private/*key.pem
chown root:root $ABC_123/certs/*cert.pem
elif [ ! -f $file1 ] && [ ! -f $file2 ] && [ ! -f $file3 ] && [ ! -f $file4 ] && [ ! -f $file5 ] && [ ! -f $file6 ]
then
#
#REPLACE
#
cp /tmp/$NUMBER/*key.pem $ABC_123/private/
cp /tmp/$NUMBER/*cert.pem $ABC_123/certs/
cp /tmp/$NUMBER/fil3cert.pem $ABC_123/certs/
#
#CHANGE PERMISSIONS
#
chmod 644 $ABC_123/private/*key.pem
chmod 644 $ABC_123/certs/*cert.pem
chmod 644 $ABC_123/certs/fil3cert.pem
chown root:root $ABC_123/private/*key.pem
chown root:root $ABC_123/certs/*cert.pem
chown root:root $ABC_123/certs/fil3cert.pem
elif [ -f $file6 ]
then
#
# BACKUP
#
find $ABC_123/certs/* -type f -exec mv {} {}.$(date +%Y-%m-%d).bkp \;
find $ABC_123/private/* -type f -exec mv {} {}.$(date +%Y-%m-%d).bkp \;
#
# REPLACE
#
cp /tmp/$NUMBER/*key.pem $ABC_123/private/
cp /tmp/$NUMBER/*cert.pem $ABC_123/certs/
cp /tmp/$NUMBER/fil3cert.pem $ABC_123/certs/
#
# CHANGE PERMISSIONS
#
chmod 644 $ABC_123/private/*key.pem
chmod 644 $ABC_123/certs/*cert.pem
chmod 644 $ABC_123/certs/fil3cert.pem
chown root:root $ABC_123/private/*key.pem
chown root:root $ABC_123/certs/*cert.pem
chown root:root $ABC_123/certs/fil3cert.pem
elif [ ! -f "$file1" ] || [ ! -f "$file2" ] || [ ! -f "$file3" ] || [ ! -f "$file4" ] || [ ! -f "$file5" ] && [ ! -f "$file6" ] ; then
# At least one file is missing, writing the list of missing file(s):
echo -n "Missing file(s): "
for i in "$file1" "$file2" "$file3" "$file4" "$file5" ; do
if [ ! -f "$i" ] ; then
echo -n "$i "
fi
done
echo
fi
i want to use shell function instead of repeating the BACKUP,REPLACEMENT and CHANGE PERMISSION command again and again
Your shell function could be something like
all_present() {
for file
do
[ -f $file ] || return 1
done
return 0
}
and it would be used like this:
if ! all_present "$file1" "$file2" "$file3" "$file4" "$file5" "$file6"
then
echo at least one is missing
...
BTW: In case you can switch from shell to bash or zsh or ksh, you can hold all files in an array and the whole expression would be simpler.

Getting error at the time of run .sh script from DockerFile

My DockerFile is :
FROM postgres:latest
VOLUME /var/lib/postgresql/data
ARG PG_POSTGRES_PWD=123qwe
ARG DBUSER=postgres
ARG DBUSER_PWD=123qwe
ARG DBNAME=docker_pg_sh
ARG DB_DUMP_FILE=gtma_latest.sql
ENV POSTGRES_DB docker_pg_sh
ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD ${PG_POSTGRES_PWD}
ENV PGDATA /pgdata
COPY wait-for-pg-isready.sh /tmp/wait-for-pg-isready.sh
COPY ${DB_DUMP_FILE} /gtma_latest.sql
RUN set -e && \
nohup bash -c "docker-entrypoint.sh postgres & " && \
/tmp/wait-for-pg-isready.sh && \
psql -U postgres -c "CREATE USER ${DBUSER} WITH SUPERUSER CREATEDB CREATEROLE ENCRYPTED PASSWORD '${DBUSER_PWD}';" && \
psql -U ${DBUSER} -d ${POSTGRES_DB} -c "CREATE DATABASE ${DBNAME} TEMPLATE template0;" && \
pg_restore -v --no-owner --role=${DBUSER} --exit-on-error -U ${DBUSER} -d ${DBNAME} </gtma_latest.sql && \
psql -U postgres -c "ALTER USER ${DBUSER} WITH NOSUPERUSER;" && \
rm -rf /gtma_latest.sql
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
CMD pg_isready -U postgres -d docker_pg_sh
and wait-for-pg-isready.sh file is :
#!/bin/bash
set -e
get_non_lo_ip() {
local _ip _non_lo_ip _line _nl=$'\n'
while IFS=$': \t' read -a _line ;do
[ -z "${_line%inet}" ] &&
_ip=${_line[${#_line[1]}>4?1:2]} &&
[ "${_ip#127.0.0.1}" ] && _non_lo_ip=$_ip
done< <(LANG=C /sbin/ifconfig)
printf ${1+-v} $1 "%s${_nl:0:$[${#1}>0?0:1]}" $_non_lo_ip
}
get_non_lo_ip NON_LO_IP
until pg_isready -h $NON_LO_IP -U "postgres" -d "docker_pg_sh"; do
>&2 echo "Postgres is not ready - sleeping..."
sleep 4
done
>&2 echo "Postgres is up - you can execute commands now"
Both file are in same folder.
But when I run the command docker build -t gmta-test-sh-vol:1.0.0 . , I am getting error
like this :
Step 14/15 : RUN set -e && nohup bash -c "docker-entrypoint.sh postgres & " && /tmp/wait-for-pg-isready.sh && psql -U postgres -c "CREATE USER ${DBUSER} WITH SUPERUSER CREATEDB CREATEROLE ENCRYPTED PASSWORD '${DBUSER_PWD}';" && psql -U ${DBUSER} -d ${POSTGRES_DB} -c "CREATE DATABASE ${DBNAME} TEMPLATE template0;" && pg_restore -v --no-owner --role=${DBUSER} --exit-on-error -U ${DBUSER} -d ${DBNAME} </gtma_latest.sql && psql -U postgres -c "ALTER USER ${DBUSER} WITH NOSUPERUSER;" && rm -rf /gtma_latest.sql
---> Running in a791e734c6df
/bin/sh: 1: /tmp/wait-for-pg-isready.sh: not found
The command '/bin/sh -c set -e && nohup bash -c "docker-entrypoint.sh postgres & " && /tmp/wait-for-pg-isready.sh && psql -U postgres -c "CREATE USER ${DBUSER} WITH SUPERUSER CREATEDB CREATEROLE ENCRYPTED PASSWORD '${DBUSER_PWD}';" && psql -U ${DBUSER} -d ${POSTGRES_DB} -c "CREATE DATABASE ${DBNAME} TEMPLATE template0;" && pg_restore -v --no-owner --role=${DBUSER} --exit-on-error -U ${DBUSER} -d ${DBNAME} </gtma_latest.sql && psql -U postgres -c "ALTER USER ${DBUSER} WITH NOSUPERUSER;" && rm -rf /gtma_latest.sql' returned a non-zero code: 127
But there is no error when the line for copy the file wait-for-pg-isready.sh is running .
How can the problem will be solved?
Thanks in advance.

Issue accessing external exadata database from docker

I am having a problem accessing external exadata database from docker.
Docker File:
FROM centos:7.3.1611
WORKDIR /tmp
ADD . /tmp
ENV http_proxy=<added>
ENV https_proxy=<added>
ENV RHEL_FRONTEND=noninteractive
ENV ORACLE_INSTANTCLIENT_MAJOR=12.2
ENV ORACLE_INSTANTCLIENT_VERSION=12.2.0.1.0
ENV ORACLE=/usr
ENV ORACLE_HOME=$ORACLE/lib/oracle/$ORACLE_INSTANTCLIENT_MAJOR/client64
ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib
ENV C_INCLUDE_PATH=$C_INCLUDE_PATH:$ORACLE/include/oracle/$ORACLE_INSTANTCLIENT_MAJOR/client64
RUN yum update && yum install -y libaio1 \
curl rpm2cpio cpio \
&& mkdir $ORACLE && TMP_DIR="$(mktemp -d)" && cd "$TMP_DIR" \
&& oracle-instantclient$ORACLE_INSTANTCLIENT_MAJOR-basic-$ORACLE_INSTANTCLIENT_VERSION-1.x86_64.rpm -o basic.rpm \
&& rpm2cpio basic.rpm | cpio -i -d -v && cp -r usr/* $ORACLE && rm -rf ./* \
&& ln -s libclntsh.so.12.1 $ORACLE/lib/oracle/$ORACLE_INSTANTCLIENT_MAJOR/client64/lib/libclntsh.so.$ORACLE_INSTANTCLIENT_MAJOR \
&& ln -s libocci.so.12.1 $ORACLE/lib/oracle/$ORACLE_INSTANTCLIENT_MAJOR/client64/lib/libocci.so.$ORACLE_INSTANTCLIENT_MAJOR \
&& oracle-instantclient$ORACLE_INSTANTCLIENT_MAJOR-devel-$ORACLE_INSTANTCLIENT_VERSION-1.x86_64.rpm -o devel.rpm \
&& rpm2cpio devel.rpm | cpio -i -d -v && cp -r usr/* $ORACLE && rm -rf "$TMP_DIR" \
&& echo "$ORACLE_HOME/lib" > /etc/ld.so.conf.d/oracle.conf && chmod o+r /etc/ld.so.conf.d/oracle.conf && ldconfig \
&& rm -rf /var/lib/apt/lists/* && apt-get purge -y --auto-remove curl rpm2cpio cpio
RUN pip --no-cache-dir install Flask==0.12.2
ENV SHELL /bin/bash
EXPOSE 80
# WORKDIR /docker
ENTRYPOINT ["python"]
CMD ["app.py"]
Error:returned a non-zero code: 1
Can anyone help me to resolve this?
The build command fails when executing the below command:
RUN yum update && yum install -y libaio1 \
curl rpm2cpio cpio \
&& mkdir $ORACLE && TMP_DIR="$(mktemp -d)" && cd "$TMP_DIR" \
&& oracle-instantclient$ORACLE_INSTANTCLIENT_MAJOR-basic-$ORACLE_INSTANTCLIENT_VERSION-1.x86_64.rpm -o basic.rpm \
&& rpm2cpio basic.rpm | cpio -i -d -v && cp -r usr/* $ORACLE && rm -rf ./* \
&& ln -s libclntsh.so.12.1 $ORACLE/lib/oracle/$ORACLE_INSTANTCLIENT_MAJOR/client64/lib/libclntsh.so.$ORACLE_INSTANTCLIENT_MAJOR \
&& ln -s libocci.so.12.1 $ORACLE/lib/oracle/$ORACLE_INSTANTCLIENT_MAJOR/client64/lib/libocci.so.$ORACLE_INSTANTCLIENT_MAJOR \
&& oracle-instantclient$ORACLE_INSTANTCLIENT_MAJOR-devel-$ORACLE_INSTANTCLIENT_VERSION-1.x86_64.rpm -o devel.rpm \
&& rpm2cpio devel.rpm | cpio -i -d -v && cp -r usr/* $ORACLE && rm -rf "$TMP_DIR" \
&& echo "$ORACLE_HOME/lib" > /etc/ld.so.conf.d/oracle.conf && chmod o+r /etc/ld.so.conf.d/oracle.conf && ldconfig \
&& rm -rf /var/lib/apt/lists/* && apt-get purge -y --auto-remove curl rpm2cpio cpio

How to create a bash script file in Docker?

I wrote a script that the task is to create a text file and write into two lines.
This has been done, but more than once and I do not understand why because the script file does not contain any cycle and once the file is executed.
#!/bin/bash
<other, non-relevant variables and commands>
PHP_ROOT_DIR=/etc/php-5.6.22
PHP_CGI_WRAPPER=php-5622
mkdir -p $(dirname $0)/cgi-bin
touch $(dirname $0)/cgi-bin/$PHP_CGI_WRAPPER
echo -e "#!/bin/bash" >> $(dirname $0)/cgi-bin/$PHP_CGI_WRAPPER
echo -e "exec ${PHP_ROOT_DIR}/bin/php-cgi" >> $(dirname $0)/cgi-bin/$PHP_CGI_WRAPPER
<other, non-relevant variables and commands>
exit 0
output file content:
#!/bin/bash
exec /etc/php-5.6.22/bin/php-cgi
#!/bin/bash
exec /etc/php-5.6.22/bin/php-cgi
#!/bin/bash
exec /etc/php-5.6.22/bin/php-cgi
Update:
This result occurs when run Docker container.
If I reduce my script to only of above part (removed non-relevant code parts), then working correctly on host OS and Docker in a container equally.
But the problem is still not fixed.
full version script file is this:
#!/bin/bash
source $(dirname $0)/create_vhost_config.sh
source $(dirname $0)/create_sample_html.sh
IP=$(ip route get 1 | awk '{print $NF;exit}')
SRC_DIR=/usr/local/src
HTTPD_VERSION=2.4.20
HTTPD_ROOT_DIR=/etc/apache2
HTTPD_CONF_FILE=/etc/apache2/conf/apache2.conf
PHP_VERSION=5.6.22
PHP_ROOT_DIR=/etc/php-5.6.22
PHP_CGI_WRAPPER=php-5622
for i in 1 2
do
mkdir -p /var/www/vhosts/test$i/public
mkdir -p /var/www/vhosts/test$i/log
create_html ${IP} "test$i" "/var/www/vhosts/test$i/public/index.html"
create_php_info "/var/www/vhosts/test$i/public/index.php"
create_simple_vhost "test$i" "$HTTPD_ROOT_DIR/conf/conf.d/test$i.home.conf"
done
# add PHP handler to Apache configuration
sed -i -e 's# DirectoryIndex index.html# DirectoryIndex index.php index.html#g' $HTTPD_CONF_FILE
echo -e "\n<FilesMatch \"\.php$\">" >> $HTTPD_CONF_FILE
echo -e "\tSetHandler application/x-httpd-php" >> $HTTPD_CONF_FILE
echo -e "</FilesMatch>" >> $HTTPD_CONF_FILE
$HTTPD_ROOT_DIR/bin/apachectl restart
# *************************************** Creating PHP-CGI wrappers ***************************************
mkdir -p /var/www/cgi-bin
touch /var/www/cgi-bin/$PHP_CGI_WRAPPER
echo -e "#!/bin/bash" >> /var/www/cgi-bin/$PHP_CGI_WRAPPER
echo -e "exec ${PHP_ROOT_DIR}/bin/php-cgi" >> /var/www/cgi-bin/$PHP_CGI_WRAPPER
#restart apache
chmod -R +x /var/www/cgi-bin/
# *************************************** Configure with mod_cgi/mod_cgid ***************************************
sed -i -e 's##LoadModule cgid_module modules/mod_cgid.so#LoadModule cgid_module modules/mod_cgid.so#g' $HTTPD_CONF_FILE
sed -i -e 's##LoadModule actions_module modules/mod_actions.so#LoadModule actions_module modules/mod_actions.so#g' $HTTPD_CONF_FILE
echo -e "\nScriptAlias /php/ /var/www/cgi-bin/" >> $HTTPD_CONF_FILE
echo -e "<Directory /var/www/cgi-bin/>" >> $HTTPD_CONF_FILE
echo -e "\tRequire all granted" >> $HTTPD_CONF_FILE
echo -e "</Directory>" >> $HTTPD_CONF_FILE
rm -rf $HTTPD_ROOT_DIR/conf/conf.d/*
for i in 1 2
do
create_mod_cgi_conf "test$i" "/php/php-5622" "$HTTPD_ROOT_DIR/conf/conf.d/test$i.home.conf"
done
$HTTPD_ROOT_DIR/bin/apachectl restart
# *************************************** Configure with mod_fcgid ***************************************
cd $SRC_DIR
wget http://xenia.sote.hu/ftp/mirrors/www.apache.org//httpd/mod_fcgid/$(wget -O- http://xenia.sote.hu/ftp/mirrors/www.apache.org//httpd/mod_fcgid/ | egrep -o 'mod_fcgid-[0-9\.]+.tar.gz' | sort -V | tail -1)
tar -xf mod_fcgid-[0-9].[0-9].[0-9].tar.gz
cd mod_fcgid-[0-9].[0-9].[0-9]
APXS=/etc/apache2/bin/apxs ./configure.apxs
make -j"$(nproc)"
make install
rm -rf $HTTPD_ROOT_DIR/conf/conf.d/*
for i in 1 2
do
create_mod_fcgid_conf "test$i" "php-5622" "$HTTPD_ROOT_DIR/conf/conf.d/test$i.home.conf"
done
$HTTPD_ROOT_DIR/bin/apachectl restart
# Configure with FPM
find /var/www/vhosts/test*/public/ -exec chown daemon:daemon {} +
sed -i -e 's##LoadModule proxy_module modules/mod_proxy.so#LoadModule proxy_module modules/mod_proxy.so#g' $HTTPD_CONF_FILE
sed -i -e 's##LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so#LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so#g' $HTTPD_CONF_FILE
echo "include=etc/fpm.d/*.conf" >> $PHP_ROOT_DIR/etc/php-fpm.conf
cp $SRC_DIR/php-$PHP_VERSION/sapi/fpm/init.d.php-fpm $PHP_ROOT_DIR/sbin/php-fpmd
chmod 0700 $PHP_ROOT_DIR/sbin/php-fpmd
find /var/www/vhosts/test*/public/ -exec chown www-data:www-data {} +
rm -rf $PHP_ROOT_DIR/etc/fpm.d/*
rm -rf $HTTPD_ROOT_DIR/conf/conf.d/*
for i in 1 2
do
create_php_fpmd_conf "test$i.home" "$IP" 5000$i "$PHP_ROOT_DIR/etc/fpm.d/test$i.home.conf"
create_fpm_conf "test$i" "$IP" 5000$i "$HTTPD_ROOT_DIR/conf/conf.d/test$i.home.conf"
done
$HTTPD_ROOT_DIR/bin/apachectl restart
$PHP_ROOT_DIR/sbin/php-fpmd start
# install Composer for PHP
cd /tmp
$PHP_ROOT_DIR/bin/php -r "readfile('https://getcomposer.org/installer');" | $PHP_ROOT_DIR/bin/php
mv composer.phar /usr/local/bin/composer
$HTTPD_ROOT_DIR/bin/apachectl restart
$PHP_ROOT_DIR/sbin/php-fpmd restart
#install PHP-PEAR
cd $SRC_DIR && \
wget http://pear.php.net/go-pear.phar -O go-pear.phar
$PHP_ROOT_DIR/bin/php go-pear.phar
$HTTPD_ROOT_DIR/bin/apachectl restart
$PHP_ROOT_DIR/sbin/php-fpmd restart
#install XDebug
setterm -bold on
cd $SRC_DIR
wget https://xdebug.org/files/$(wget -O- https://xdebug.org/files/ | egrep -o 'xdebug-[0-9\.]+.tgz' | sort -V | tail -1)
tar -xvf xdebug-[0-9].[0-9].[0-9].tgz
cd xdebug-[0-9].[0-9].[0-9]
$PHP_ROOT_DIR/bin/phpize
./configure --enable-xdebug --with-php-config=$PHP_ROOT_DIR/bin/php-config
make -j"$(nproc)"
make install
echo $(find / -type d -name no-debug-zts*)/xdebug.so >> $PHP_ROOT_DIR/etc/php.ini
$HTTPD_ROOT_DIR/bin/apachectl restart
$PHP_ROOT_DIR/sbin/php-fpmd restart
exit 0
Change this:
echo -e "#!/bin/bash" >> $(dirname $0)/cgi-bin/$PHP_CGI_WRAPPER
to this:
echo -e "#!/bin/bash" > $(dirname $0)/cgi-bin/$PHP_CGI_WRAPPER
io-redirection

Makefile (running outside scripts that have makefile macros)

I have some .PHONY targets such as 'clean', 'backup', and 'help'
the rule for some of these targets is very large.
For example:
.PHONY: backup
backup:
#$(GREEN)
#mkdir -p backup/include #make an backup include folder if it doesn't already exist
#mkdir -p backup/src #make a backup src folder if it doesn't already exist
#mkdir -p backup/docs #make a backup docs folder if it doesn't already exist
#total=0; headerCount=0; sourceCount=0; documentCount=0; \
for file in $(HEADER_PATH)*; do \
if ls $$file[~] >/dev/null 2>&1; then \
mv -fu $$file[~] backup/$$file; \
let "headerCount+=1"; \
echo $(DATE)[Backed Up] $$file~ >> $(LOG); \
fi; \
done; \
for file in $(SOURCE_PATH)*; do \
if ls $$file[~] >/dev/null 2>&1; then \
mv -fu $$file[~] backup/$$file; \
let "sourceCount+=1"; \
echo $(DATE)[Backed Up] $$file~ >> $(LOG); \
fi; \
done; \
for file in $(DOC_PATH)*; do \
if ls $$file[~] >/dev/null 2>&1; then \
mv -fu $$file[~] backup/$$file; \
let "documentCount+=1"; \
echo $(DATE)[Backed Up] $$file~ >> $(LOG); \
fi; \
done; \
let "total= headerCount + sourceCount + documentCount"; \
echo -n $(OUTPUT_PROMPT)" "; \
if test $$total -eq 0; then \
echo Nothing To Back up; \
else \
if test $$headerCount -eq $$total; then \
echo -n $$total" "; \
echo -n "Header"; \
if test $$total -ge 2; then \
echo -n "s"; \
fi; \
echo " Backed Up"; \
elif test $$sourceCount -eq $$total; then \
echo -n $$total" "; \
echo -n "Source"; \
if test $$total -ge 2; then \
echo -n "s"; \
fi; \
echo " Backed Up"; \
elif test $$documentCount -eq $$total; then \
echo -n $$total" "; \
echo -n "Document"; \
if test $$total -ge 2; then \
echo -n "s"; \
fi; \
echo " Backed Up"; \
else \
$(UNDERLINE); echo $$total " Files Backed Up"; $(UNUNDERLINE); \
if test $$headerCount -eq 1; then \
echo $(OUTPUT_PROMPT)" "$$headerCount header; \
elif test $$headerCount -ge 2; then \
echo $(OUTPUT_PROMPT)" "$$headerCount headers; \
fi; \
if test $$sourceCount -eq 1; then \
echo $(OUTPUT_PROMPT)" "$$sourceCount source; \
elif test $$sourceCount -ge 2; then \
echo $(OUTPUT_PROMPT)" "$$sourceCount sources; \
fi; \
if test $$documentCount -eq 1; then \
echo $(OUTPUT_PROMPT)" "$$documentCount document; \
elif test $$documentCount -ge 2; then \
echo $(OUTPUT_PROMPT)" "$$documentCount documents; \
fi; \
fi; \
fi;
#$(DEFAULT_TEXT)
what the code does is not important, but I wanted to illustrate that it has macros in which 'make' must expand, and that it also performs shell code (bash), and that some indication on what the script did, is displayed in the terminal.
I want to put this script outside of 'make' in another directory, and turn that code into something like this:
.PHONY: backup
backup:
#run scripts/backup.scr
#or something similar to that
How can I put the rule of my target (which is makefile/bash code) into a separate file, and have make practically paste it in so that it runs how I had it originally?
I thought I might be able to use the "include" command inside 'make'.
It looks like it is used to include other makefiles though..
maybe I should just paste the entire target/rule into another makefile, and then include that makefile into my main one?
Would that be the best way?
In your case, you have quite few output variables. It might be worth the hassle to separate the generation and execution, like:
clean : clean-script
sh clean-script
rm -f clean-script
clean-script : clean-script.in
sed -e 's:[#]HEADER_PATH[#]:$(HEADER_PATH):g' $<.in > $#
And write clean-script.in as a clean sh script with a few substitutions.
If you use GNU make, you can of course build a list of output varables like:
clean-script : clean-script.in
sed $(foreach var,$(SUBSTVARS),-e 's:[#]$(var)[#]:$($(var)):g') $<.in > $#
I don't know if it can help you, but you can run make inside some Makefile usually with a command (inside a rule) e.g.
$(MAKE) subtarget
See the section Recursive use of Make in the GNU make documentation.
I tend to dislike using make for complex projects (but unfortunately, I have to). If you are free to chose some other tool, you might consider omake and many others (cmake, scons, bake, ...)

Resources