Passing output of a command within a for Statement - shell

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

Related

bash - how to grep a string within a file and index with the file name

I have a few thousand files each within their own folder and I want to link a specific string ID with the file name.
So for example here I have a couple of different folders :
Folder_1
file_abc.txt
Sample-001-abc
Folder_2
file_efg.txt
Sample-002-efg
Folder_3
file_hig.txt
Sample-003-hig
and I would like to grep based on the string "Sample" and link that string to the filename each is located in, to ensure I have the correct sample/filename order. Essentially, I would like to output a separate file that looks like this:
Filename_sample_linked.txt
file_abc.txt Sample-001-abc
file_efg.txt Sample-002-efg
file_hig.txt Sample-003-hig
...etc
I can grab all the IDs with the following:
find dir/*.txt -type f -exec grep -H 'Sample' {} + >> List_of_samples.txt
but am not quite sure how to link that with the file name that the string came from. Thanks in advance for your help!
Maybe sed.
If you actually meant for that filename to be line 1:
$: sed -E '1{s/.*/Filename_sample_linked.txt/;p;d}; /[.]txt/{N;s/\n/ /}; /^$/{N;d}' file
Filename_sample_linked.txt
file_abc.txt Sample-001-abc
file_efg.txt Sample-002-efg
file_hig.txt Sample-003-hig
Otherwise, replace 1{s/.*/Filename_sample_linked.txt/;p;d}; with just 1d; maybe.
I will assume you did NOT mean for the last line to literally be ...etc.
TLDR;
generate a script via "meta programming"
run it
save the output
Sometimes, if you can solve a problem by doing simple steps repetitively, then all you need to do is figure out the steps and repeat them. The advantage of this approach is that it's not very sophisticated and relatively easy to do.
go to the folder
list the two files side by side
save that to a list
In shell-speak, that could look like this:
cd Folder_1; echo file_* Sample*; cd ..
cd Folder_3; echo file_* Sample*; cd ..
I can show you how to do this many times, but first let's have some fun.
setup.sh
#!/bin/bash
md () { mkdir -p "$1"; cd "$1"; pwd; }
ran3() { tr -dc '[[:alnum:]]' </dev/urandom | dd bs=${1:-3} count=1 status=none ; }
w80() { echo $(( 20 + 80 * RANDOM / 65536 )); }
w20() { echo $(( 3 + 20 * RANDOM / 65536 )); }
# Initial setup
md Folder_1
touch file_abc.txt
touch Sample-001-abc
cd ..
md Folder_2
touch file_efg.txt
touch Sample-002-efg
cd ..
md Folder_3
touch file_hig.txt
touch Sample-003-hig
cd ..
# More folders with 3-char comparison patterns
s=0
s=$( find -name Sample\* | wc -l )
(( s++ ))
max=$(( s + 20 ))
while [[ $s -lt $max ]]; do
md Folder_$s
str=`ran3`
printf -v ss '%03d' $s
touch file_$str.txt
touch Sample-$ss-$str
(( s++ ))
cd ..
done
# More folders with random names
max=$(( max + 20 ))
while [[ $s -lt $max ]]; do
fstr=`ran3 $(w80)`
md ${fstr}_$s
str=`ran3 $(w20)`
printf -v ss '%03d' $s
touch file_$str.txt
touch Sample-$ss-$str
(( s++ ))
cd ..
done
I ran that a few times and now I have:
./Folder_143/file_nhE.txt
./Folder_143/Sample-143-nhE
./yHb9aSWkRDXqQfITkrqplQQMH[cNdMTYt_144/file_W9eDdDIEN.txt
./yHb9aSWkRDXqQfITkrqplQQMH[cNdMTYt_144/Sample-144-W9eDdDIEN
./ClauM57QCXPCqLPBHUMERI6Vxc_145/file_vhTSZfTh.txt
./ClauM57QCXPCqLPBHUMERI6Vxc_145/Sample-145-vhTSZfTh
./kpXndK8eapnUJKf9XvFZgnY31kUVNmUkHDp1ey[Q3IsY53EOQ_146/file_b[rj3ft.txt
./kpXndK8eapnUJKf9XvFZgnY31kUVNmUkHDp1ey[Q3IsY53EOQ_146/Sample-146-b[rj3ft
./mx]Yrj5eEa4PlEL2snmYOttZwc]Vi4rSCJ_147/file_]OyWwrVd0RN.txt
./mx]Yrj5eEa4PlEL2snmYOttZwc]Vi4rSCJ_147/Sample-147-]OyWwrVd0RN
./Zpi1sip87UmM85gd[dh]9sQn5ZE5rjBGA[[9ae_148/file_uQPZU[Tn[.txt
step 1 (generate the meta script)
find -name Sample\* -type f \
-printf "cd $PWD"';s="%f"; cd %h ; echo file_${s##*-}.txt %f\n'
Here we use find to make a bunch of commands for us, that we can run. Mixing quotes like "ABC"'123' into a single string ABC123 is just a clever way to interpolate some variables, while passing others to -printf for later.
"cd $PWD"
Go to the base folder before each sub-folder.
';s="%f"; cd %h ;
End the cd-pwd and asign the basename to s.
Go to the dirname of each found Samlple* file.
echo file_${s##*-}.txt %f\n'
Print the two files side by side.
${s##GLOB} means remove everything from the start of the s string that matches GLOB. So effectively remove everything up to and with the last dash.
The %f and %h macros are available when using find -printf, if that was unclear.
cd /home/jaroslav/tmp/so-link;s="Sample-138-Lj]"; cd ./Folder_138 ; echo file_${s##*-}.txt Sample-138-Lj]
cd /home/jaroslav/tmp/so-link;s="Sample-139-pad"; cd ./Folder_139 ; echo file_${s##*-}.txt Sample-139-pad
cd /home/jaroslav/tmp/so-link;s="Sample-140-ImN"; cd ./Folder_140 ; echo file_${s##*-}.txt Sample-140-ImN
cd /home/jaroslav/tmp/so-link;s="Sample-141-nxr"; cd ./Folder_141 ; echo file_${s##*-}.txt Sample-141-nxr
cd /home/jaroslav/tmp/so-link;s="Sample-142-4Di"; cd ./Folder_142 ; echo file_${s##*-}.txt Sample-142-4Di
cd /home/jaroslav/tmp/so-link;s="Sample-143-nhE"; cd ./Folder_143 ; echo file_${s##*-}.txt Sample-143-nhE
cd /home/jaroslav/tmp/so-link;s="Sample-144-W9eDdDIEN"; cd ./yHb9aSWkRDXqQfITkrqplQQMH[cNdMTYt_144 ; echo file_${s##*-}.txt Sample-144-W9eDdDIEN
cd /home/jaroslav/tmp/so-link;s="Sample-145-vhTSZfTh"; cd ./ClauM57QCXPCqLPBHUMERI6Vxc_145 ; echo file_${s##*-}.txt Sample-145-vhTSZfTh
cd /home/jaroslav/tmp/so-link;s="Sample-146-b[rj3ft"; cd ./kpXndK8eapnUJKf9XvFZgnY31kUVNmUkHDp1ey[Q3IsY53EOQ_146 ; echo file_${s##*-}.txt Sample-146-b[rj3ft
Keep in mind that this solution is fragile and relies on the regularity of the names of the input files / folders. If you are working with unpredictable data, you may need to think harder...
step 2 (run the meta script)
$ find -name Sample\* -type f \
-printf "cd $PWD"';s="%f"; cd %h ; echo file_${s##*-}.txt %f\n' \
| bash -x
cd /home/jaroslav/tmp/so-link;s="Sample-001-abc"; cd ./Folder_1 ; echo file_${s##*-}.txt Sample-001-abc
cd /home/jaroslav/tmp/so-link;s="Sample-002-efg"; cd ./Folder_2 ; echo file_${s##*-}.txt Sample-002-efg
cd /home/jaroslav/tmp/so-link;s="Sample-003-hig"; cd ./Folder_3 ; echo file_${s##*-}.txt Sample-003-hig
(...)
+ cd './v0a7F1K5P[fKL5NSYXaMZdFdGV7UCK_154'
+ echo file_R0IZENm2ni.txt Sample-154-R0IZENm2ni
file_R0IZENm2ni.txt Sample-154-R0IZENm2ni
+ cd /home/jaroslav/tmp/so-link
+ s=Sample-155-MEuAFsvztX
+ cd ./tKUlAFlPy2zq2xiZhruhS9U2VDnNQc7LiwYkUxL_155
+ echo file_MEuAFsvztX.txt Sample-155-MEuAFsvztX
file_MEuAFsvztX.txt Sample-155-MEuAFsvztX
+ cd /home/jaroslav/tmp/so-link
+ s='Sample-156-M7e]VOOHFz'
+ cd './3zVn7Z9ltN2MpmS[lo]6DCgv4RFEdX9XDoskFY0p_156'
+ echo 'file_M7e]VOOHFz.txt' 'Sample-156-M7e]VOOHFz'
file_M7e]VOOHFz.txt Sample-156-M7e]VOOHFz
(...)
step 3 (save the output)
$ find -name Sample\* -type f \
-printf "cd $PWD"';s="%f"; cd %h ; echo file_${s##*-}.txt %f\n' \
| bash | column -t \
| tee /tmp/list
(...)
file_ImN.txt Sample-140-ImN
file_nxr.txt Sample-141-nxr
file_4Di.txt Sample-142-4Di
file_nhE.txt Sample-143-nhE
file_W9eDdDIEN.txt Sample-144-W9eDdDIEN
file_vhTSZfTh.txt Sample-145-vhTSZfTh
file_b[rj3ft.txt Sample-146-b[rj3ft
file_]OyWwrVd0RN.txt Sample-147-]OyWwrVd0RN
(...)
From here you can sort the output or whatever else you need
$ cat /tmp/list | sed 's/\ \+/ /' | sort -b -k2.9,2.14n --debug
_________________________________________
file_M7e]VOOHFz.txt Sample-156-M7e]VOOHFz
___
_________________________________________
file_ftb8ifOfYt.txt Sample-157-ftb8ifOfYt
___
_________________________________________
file_tWOa]ZbTA.txt Sample-158-tWOa]ZbTA
___
_______________________________________
file_n4s2Znk.txt Sample-159-n4s2Znk
___
___________________________________
file_mjMox[P.txt Sample-160-mjMox[P
___
___________________________________
file_tQ3.txt Sample-161-tQ3
___
___________________________
file_G6VFcFHYH.txt Sample-162-G6VFcFHYH
___
_______________________________________
file_4mXCFTZaC.txt Sample-163-4mXCFTZaC
___
_______________________________________

moving files to their respective folders using bash scripting

I have files in this format:
2022-03-5344-REQUEST.jpg
2022-03-5344-IMAGE.jpg
2022-03-5344-00imgtest.jpg
2022-03-5344-anotherone.JPG
2022-03-5343-kdijffj.JPG
2022-03-5343-zslkjfs.jpg
2022-03-5343-myimage-2010.jpg
2022-03-5343-anotherone.png
2022-03-5342-ebee5654.jpeg
2022-03-5342-dec.jpg
2022-03-5341-att.jpg
2022-03-5341-timephoto_december.jpeg
....
about 13k images like these.
I want to create folders like:
2022-03-5344/
2022-03-5343/
2022-03-5342/
2022-03-5341/
....
I started manually moving them like:
mkdir name
mv name-* name/
But of course I'm not gonna repeat this process for 13k files.
So I want to do this using bash scripting, and since I am new to bash, and I am working on a production environment, I want to play it safe, but it doesn't give me my results. This is what I did so far:
#!/bin/bash
name = $1
mkdir "$name"
mv "${name}-*" $name/
and all I can do is: ./move.sh name for every folder, I didn't know how to automate this using loops.
With bash and a regex. I assume that the files are all in the current directory.
for name in *; do
if [[ "$name" =~ (^....-..-....)- ]]; then
dir="${BASH_REMATCH[1]}"; # dir contains 2022-03-5344, e.g.
echo mkdir -p "$dir" || exit 1;
echo mv -v "$name" "$dir";
fi;
done
If output looks okay, remove both echo.
Try this
xargs -i sh -c 'mkdir -p {}; mv {}-* {}' < <(ls *-*-*-*|awk -F- -vOFS=- '{print $1,$2,$3}'|uniq)
Or:
find . -maxdepth 1 -type f -name "*-*-*-*" | \
awk -F- -vOFS=- '{print $1,$2,$3}' | \
sort -u | \
xargs -i sh -c 'mkdir -p {}; mv {}-* {}'
Or find with regex:
find . -maxdepth 1 -type f -regextype posix-extended -regex ".*/[0-9]{4}-[0-9]{2}-[0-9]{4}.*"
You could use awk
$ cat awk.script
/^[[:digit:]-]/ && ! a[$1]++ {
dir=$1
} /^[[:digit:]-]/ {
system("sudo mkdir " dir )
system("sudo mv " $0" "dir"/"$0)
}
To call the script and use for your purposes;
$ awk -F"-([0-9]+)?[[:alpha:]]+.*" -f awk.script <(ls)
You will see some errors such as;
mkdir: cannot create directory ‘2022-03-5341’: File exists
after the initial dir has been created, you can safely ignore these as the dir now exist.
The content of each directory will now have the relevant files
$ ls 2022-03-5344
2022-03-5344-00imgtest.jpg 2022-03-5344-IMAGE.jpg 2022-03-5344-REQUEST.jpg 2022-03-5344-anotherone.JPG

Copying files with specific name to respective directories

I have a source directory in UNIX hiving below files
20180401abc.txt,20180402acb.txt,20180402def.txt
and in target having directories like 20180401,20180402
How can i move 20180401abc.txt to 20180401 & 20180402acb.txt,20180402def.txt to 20180402 directories respectively.
using below code ,
ls /home/source/ > filelist.txt
for line in `cat filelist.txt`
do
dir_path=`echo $line|cut -c1-8`
mkdir -p "/home/target/${dir_path}"
find /home/source/ -type f -exec cp {} /home/target/${dir_path} \;
done
#rm filelist.txt
Just use below script, it will solve your issue:-
ls /home/source/ > filelist.txt
while read filename
do
dir_name=$(echo $line | cut -c1-8 )
dir_path="/home/target/"$dir_name
mkdir $dir_path
chmod 666 $dir_path
mv $filename $dir_path
done < filelist.txt
rm -rf filelist.txt
$ touch 20180401abc.txt 20180402acb.txt 20180402def.txt
$ for F in *.txt; do
DIR=$(echo $F | sed -r 's/[a-z]{3}.txt$//');
mkdir -p $DIR;
mv $F $DIR/$F;
done
$ find
> ./20180402
> ./20180402/20180402acb.txt
> ./20180402/20180402def.txt
> ./20180401
> ./20180401/20180401abc.txt
Code listed below is working:
for filename in `ls /home/source/`
do
dir_name=$(echo $filename | cut -c1-8)
dir_path="/home/target/${dir_name}"
mkdir -p $dir_path
cp /home/source/$filename $dir_path
done

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

Unable to mute unzip output in script

I wrote a script that unzips certificates from zips and tests the certs against one of our servers:
#!/bin/bash
WORKINGDIR=$(pwd)
if [ ! -f ./users.zip ]; then
echo "users.zip not found. Exiting."
exit 1
else
unzip users.zip -d users
echo "users.zip extracted."
fi
cd ./users/client
echo "Extracting files..."
for file in `ls *.zip`; do
unzip -j $file -d `echo $file | cut -d . -f 1` &> /dev/null
done
echo "name,result" > $WORKINGDIR/results.csv
i=0 # Total counter
j=0 # Working counter
k=0 # Failed counter
for D in `ls -d */`; do
cd "$D"
SHORT=`find *.p12 | cut -f1 -d "."`
openssl pkcs12 -in `echo $SHORT".p12"` -passin file:./password -passout pass:testpass -out `echo $SHORT".pem"` &> /dev/null
echo "Trying: "$SHORT
((i++))
curl --cert ./`echo $SHORT".pem"`:testpass https://example.com -k &> /dev/null
OUT=$?
if [ $OUT -eq 0 ];then
((j++)) ; echo -e $(tput setaf 2)"\t"$SHORT": OK $(tput sgr0)" ; echo $SHORT",OK" >> $WORKINGDIR/results.csv
else
((k++)) ; echo -e $(tput setaf 1)"\t"$SHORT": FAILED $(tput sgr0)" ; echo $SHORT",FAILED" >> $WORKINGDIR/results.csv
fi
rm `echo $SHORT".pem"`
cd ..
done
echo "Test complete:"
echo "Tested: "$i
echo "Working: "$j
echo "Failed: "$k
echo "Results saved to "$WORKINGDIR"/results.csv"
exit 0
When it gets to the unzipping part I always get this output:
Archive: users.zip
creating: users/keys/
inflating: users/keys/user1.zip
inflating: users/keys/user2.zip
inflating: users/keys/user3.zip
inflating: users/keys/user4.zip
inflating: users/keys/user5.zip
inflating: users/keys/user6.zip
inflating: users/keys/user7.zip
inflating: users/keys/user8.zip
inflating: users/keys/user9.zip
inflating: users/keys/user10.zip
inflating: users/keys/user11.zip
I've tried to pipe the output to /dev/null in different ways:
&> /dev/null
1>&- 2>&-
2>&1
etc.
Nothing works. What's weird is that if I put just the unzipping part of the script into a separate script file:
#!/bin/bash
for file in `ls *.zip`; do
unzip -j $file -d `echo $file | cut -d . -f 1` &> /dev/null
done
It works no problem. Any thought on why this is happening?
The /dev/null behavior is really odd. It’s probably better to just use unzip’s -q (quiet) option.
I figured it out and feel like quite the simpleton.
I was getting the output from the first time I call unzip:
unzip users.zip -d users
and not from the loop:
for file in `ls *.zip`; do
unzip -j $file -d `echo $file | cut -d . -f 1` &> /dev/null
done
I added -qq to the first unzip:
unzip -qq users.zip -d users
and it works as expected.
In the script you posted there is no redirection for users.zip specifically.
unzip users.zip -d users

Resources