How to create a bash script file in Docker? - bash

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

Related

"sed delete" command in bash not always work

With the code:
#!/bin/bash
echo 'if [ -f /etc/skel/.bashrc ]; then . /etc/skel/.bashrc; fi' >> ~/.bashrc
echo 'enable -n echo' >> ~/.bashrc
echo "alias cls=\"echo -en '\033c\033[3J'\"" >> ~/.bashrc
echo "alias cmdlist='compgen -c | sort -b'" >> ~/.bashrc
sed -i '/if [ -f /etc/skel/.bashrc ]; then . /etc/skel/.bashrc; fi/d' ~/.bashrc
sed -i '/enable -n echo/d' ~/.bashrc
sed -i "/alias cls=\"echo -en '\033c\033[3J'\"/d" ~/.bashrc
sed -i "/alias cmdlist='compgen -c | sort -b'/d" ~/.bashrc
I expect an empty file .bashrc, instead it contains:
if [ -f /etc/skel/.bashrc ]; then . /etc/skel/.bashrc; fi
alias cls="echo -en '\033c\033[3J'"
Why?
I have solved it thanks to Poshi!
#!/bin/bash
echo 'if [ -f /etc/skel/.bashrc ]; then . /etc/skel/.bashrc; fi' >> ~/.bashrc
echo 'enable -n echo' >> ~/.bashrc
echo "alias cls=\"echo -en '\033c\033[3J'\"" >> ~/.bashrc
echo "alias cmdlist='compgen -c | sort -b'" >> ~/.bashrc
sed -i '/if \[ -f \/etc\/skel\/.bashrc \]; then . \/etc\/skel\/.bashrc; fi/d' ~/.bashrc
sed -i '/enable -n echo/d' ~/.bashrc
sed -i "/alias cls=\"echo -en '\\\033c\\\033\[3J'\"/d" ~/.bashrc
sed -i "/alias cmdlist='compgen -c | sort -b'/d" ~/.bashrc

Passing output of a command within a for Statement

I am trying to use a Nested for to make updates to sub-directories within a directory Structure , the value of j is fetched based on value of i in the previous for , the script does not seem to be fetching the value of j properly , here's the script attached ,the challenge seems to be in executing the line for j in cat /tmp/echo $i`` , I have tried putting simply $i , but does not seem to picking it , any help for the problem is appreciated , Thanks in advance .
#!/bin/bash
set -xv
rm /tmp/MDMs /tmp/MDMswithBlanks
rm -rf /tmp/Tenants*
cd /images/SCWA-SaaS/latest/config/
ls -lrt | awk '{print $9;}' | grep -v "controller" >> /tmp/MDMswithBlanks
sed '/^$/d' /tmp/MDMswithBlanks >> /tmp/MDMs
for i in `cat /tmp/MDMs`
do
cd /images/SCWA-SaaS/latest/config/
cd $i
ls -lrt | grep "drwx" | awk '{print $9;}' >> /tmp/`echo $i`
**for j in `cat /tmp/`echo $i``**
do
cd /images/SCWA-SaaS/latest/config/
cd $i
cd $j
cd /tmp
mkdir $i$j
cd /images/SCWA-SaaS/latest/config/
cd $i
cd $j
cp -p configFiles.zip /tmp/`echo $i$j`
cd /tmp/`echo $i$j`
unzip configFiles.zip
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/Tivoli/TWS/GSKit32/8/lib
/usr/Tivoli/TWS/GSKit32/8/bin/gsk8capicmd -cert -add -file /tmp/DigiCertTrustedRoot.pem -label DigiCertTrustedRoot -db /tmp/`echo $i$j`/TWSClientKeyStore.kdb -pw default
/usr/Tivoli/TWS/GSKit32/8/bin/gsk8capicmd -cert -add -file /tmp/DigiCertCA2.pem -label DigiCertCA2 -db /tmp/`echo $i$j`/TWSClientKeyStore.kdb -pw default
rm configFiles.zip
zip configFiles.zip TWSClientKeyStore.kdb installAgent.properties
chown root:root configFiles.zip
chmod 544 configFiles.zip
cd /images/SCWA-SaaS/latest/config/
cd $i
cd $j
#rm configFiles.zip
cp -p /tmp/`echo $i$j`/configFiles.zip .
#rm -rf /tmp/`echo $i$j`
done
done
Regards,
Sriram.V
For iterating over dirs/subdirs, it would be better you use find command:
for i in $(find /tmp/MDMs -type d)
do
...
done
Thanks for the Suggestions everyone , I managed to execute it Successfully this way ,so suing a variable for whole path TENANT="/tmp/CERT/echo $i" :
#!/bin/bash
set -xv
rm /tmp/CERT/MDMs /tmp/CERT/MDMswithBlanks
cd /images/SCWA-SaaS/latest/config/
ls -lrt | awk '{print $9;}' | grep -v "controller" >> /tmp/CERT/MDMswithBlanks
sed '/^$/d' /tmp/CERT/MDMswithBlanks >> /tmp/CERT/MDMs
for i in `cat /tmp/CERT/MDMs`
do
cd /images/SCWA-SaaS/latest/config/
cd $i
ls -lrt | grep "drwx" | awk '{print $9;}' >> /tmp/CERT/`echo $i`
TENANT="/tmp/CERT/`echo $i`"
for j in `cat $TENANT`
do
cd /images/SCWA-SaaS/latest/config/
cd $i
cd $j
cd /tmp/CERT
mkdir $i$j
cd /images/SCWA-SaaS/latest/config/
cd $i
cd $j
TENANT_PREFIX="/tmp/CERT/`echo $i$j`"
cp -p configFiles.zip $TENANT_PREFIX
cd $TENANT_PREFIX
unzip configFiles.zip
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/Tivoli/TWS/GSKit32/8/lib
/usr/Tivoli/TWS/GSKit32/8/bin/gsk8capicmd -cert -add -file /tmp/DigiCertTrustedRoot.pem -label DigiCertTrustedRoot -db $TENANT_PREFIX/TWSClientKeyStore.kdb -pw default
/usr/Tivoli/TWS/GSKit32/8/bin/gsk8capicmd -cert -add -file /tmp/DigiCertCA2.pem -label DigiCertCA2 -db $TENANT_PREFIX/TWSClientKeyStore.kdb -pw default
rm configFiles.zip
zip configFiles.zip TWSClientKeyStore.kdb installAgent.properties
chown root:root configFiles.zip
chmod 544 configFiles.zip
cd /images/SCWA-SaaS/latest/config/
cd $i
cd $j
rm configFiles.zip
cp -p $TENANT_PREFIX/configFiles.zip .
#rm -rf $TENANT_PREFIX
done
done

Bash: execute command repeatedly and write output tab seperated to file

I want my file to Log like that:
Time_Namelookup: 0,1 0,2 0,12 0,45 ...
Time_Connect: 0,34 0,23 0,23 0,11 ...
Time_Starttransfer: 0,9 0,23 0,12 ...
I want that Values are added to their specific line every n seconds.
I got a code like this:
while [ "true" ]
do
echo "Time_Namelookup:" >> $file
curl -w "%{time_namelookup}\t" -o /dev/null -s https://website.com/
echo "Time_connect" >> $file
curl -w "%{time_connect}\t" -o /dev/null -s https://website.com/
echo "Time_Starttransfer:" >> $file
curl -w "%{time_starttransfer}\t" -o /dev/null -s https://website.com/
sleep 5
done
But I get something like
Time_Namelookup: 0,1
Time_Connect: 0,34
Time_Starttransfer: 0,9
Time_Namelookup: 0,2
Time_Connect:0,23 0,23
Time_Starttransfer: 0,23
Time_Namelookup: 0,45
Time_Connect: 0,11
Time_Starttransfer: 0,12
Can you help me ?
You could put this inside your loop
if [ ! -f $file ]; then
echo "Time_Namelookup:" > $file
echo "Time_Connect:" >> $file
echo "Time_Starttransfer:" >> $file
fi
name_lookup=$(curl -w "%{time_namelookup}\t" -o /dev/null -s https://website.com/)
connect=$(curl -w "%{time_connect}\t" -o /dev/null -s https://website.com/)
starttransfer=$(curl -w "%{time_starttransfer}\t" -o /dev/null -s https://website.com/)
sed -i -e "s/\(Time_Starttransfer:.*\)/\1 $starttransfer/" \
-e "s/\(Time_Connect:.*\)/\1 $connect/" \
-e "s/\(Time_Starttransfer:.*\)/\1 $starttransfer/" \
$file
you can try this;
file=yourFile
echo "Time_Namelookup :" >> $file
echo "Time_Connect :" >> $file
echo "Time_Starttransfer:" >> $file
while [ "true" ]
do
time_namelookup=$(curl -w "%{time_namelookup}\t" -o /dev/null -s https://website.com/)
time_connect=$(curl -w "%{time_connect}\t" -o /dev/null -s https://website.com/)
time_starttransfer=$(curl -w "%{time_starttransfer}\t" -o /dev/null -s https://website.com/)
sed -i "/Time_Namelookup :/s/$/\t$time_namelookup/" $file
sed -i "/Time_Connect :/s/$/\t$time_connect/" $file
sed -i "/Time_Starttransfer:/s/$/\t$time_starttransfer/" $file
sleep 5
done
sed -i : to be edited in-place.
The following part is to find "Time_Namelookup :" in the file.
/Time_Namelookup :/
/s/$/\t$time_namelookup/"
s :substitute command
/../../ :delimiter
$ :end of line character.
\t$time_namelookup: to append tab and the value.

bsub post processing script

I was writing a script for submitting parallel jobs in cluster using bsub command
while read p; do
cd $(echo $p | tr -d '\r')
echo Submitting test: $p
bsub -P <project> -Jd <job desc> -o lsf.log "sh ./run_test.sh &> $log"
cd - &> /dev/null
done < $filename
How can I compile the results at the end of all test runs?
How about using something like this?
while read p; do
cd "$(echo "$p" | tr -d '\r')"
echo "Submitting test: $p"
bsub -P <project> -Jd <job desc> -o lsf.log \
"sh ./run_test.sh &> '$log' && cat '$log' >> /path/to/combined/log"
cd - &> /dev/null
done < "$filename"
When each job finishes successfully, the output file is concatenated with the rest.

How to optimize sed search and replace in shell script

I must launch sed up to three times because the word startpilot can occur more than once per row. I think this script could be written much better, perhaps anybody could help me to optimize that.
grep -rl "startpilot" ./* -R | xargs sed -i '' "s/startpilot/${PWD##*/}/"
#! /bin/bash
DIR="$1"
if [ $# -ne 1 ]
then
echo "Usage: $0 {new extension key}"
exit 1
fi
if [ -d "$DIR" ]
then
echo "Directory/extension '$DIR' exists already!"
else
git clone https://github.com/misterboe/startpilot.git $DIR --depth=1
echo "$DIR created."
cd $DIR && rm -rf .git && grep -rl "startpilot" ./* -R | xargs sed -i '' "s/startpilot/${PWD##*/}/" && grep -rl "startpilot" ./* -R | xargs sed -i '' "s/startpilot/${PWD##*/}/" && grep -rl "startpilot" ./* -R | xargs sed -i '' "s/startpilot/${PWD##*/}/" && grep -rl "Startpilot" ./* -R | xargs sed -i '' "s/Startpilot/${PWD##*/}/"
cd Resources/Public/ && bower install
echo "Your extension is now in $DIR."
fi
If you want to replace all occurrences of "startpilot" with ${PWD##*/}, just add the g (global) modifier to the sed command:
sed -i '' "s/startpilot/${PWD##*/}/g"
^ here
Rather than replacing the first occurrence on the line, now it will replace all of them.

Resources