Append two lines of text to php.ini in the entire directory tree.e


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Append two lines of text to php.ini in the entire directory tree.e
# 1  
Old 09-05-2015
Append two lines of text to php.ini in the entire directory tree.e

I am looking to write a script that will read the php.ini files on my web host. If the two lines do exist do nothing. If not append two lines to the end of it then move on to the next directory and open the next php.ini file.
I have the beginning of one that was given to me on another web site but the web site will only allow me to comment on the answer not ask any more, to ask more I would have to start another topic ?! oh well.
what I have almost works. I understand how most of it works but not all. Here it is;
Code:
#!/bin/bash
# path='/path'
line1='upload_max_filesize = "40M";'
line2='post_max_size = "40M";'
while read f; do
    if ! [ "$(grep "$line1" "$f")" ] && ! [ "$(grep "$line2" "$f")" ]; then
        echo "$line1" >> "$f"
        echo "$line2" >> "$f"
    fi
done < <(find -type f -name 'php.ini')
# done < <(find "$path" -type f -name 'php.ini')

it does work 99.98% but why?
I know the if ! means the first grep needs to be false and the && ! means the second needs to be false if false echo line1 and line2 but whats to determine the placement of those two lines at the end of the file? also how does the done << work ?
I kind understand the find statement. My BASH skills are pretty much limited to navigation and after playing around with the sed command I am a bit weary of diving in head first.

Last edited by Scrutinizer; 09-05-2015 at 10:14 AM.. Reason: code tags
# 2  
Old 09-05-2015
Hi
Code:
done < <(find -type f -name 'php.ini')

The first < means read stdin from a file
The construct <( command ) is known as process subtitution and it takes the output of the command and presents it as if it were a file...

A better way to do this would be:
Code:
while read f; do
  if ! grep -qFe "$line1" -e "$line2" "$f"; then
  printf "%s\n" "$line1" "$line2" >> "$f"
  fi
done < <( ... )

it can also be done with a pipe:
Code:
find ... |
while read f; do
  if ! grep -qFe "$line1" -e "$line2" "$f"; then
    printf "%s\n" "$line1" "$line2" >> "$f"
  fi
done

# 3  
Old 09-05-2015
Hello Larrykh465,

Welcome to forum, following may help you in same. But I don't have system so couldn't test it, please try it and let me know if this helps or for any queries please. Also if you are in Sunos then you could use nawk for same.
Code:
find $PATH -type f -name "php.ini" 2>dev/null > files_output_list
if [[ -s file_output_list ]]
then
	while read line
	do
	PATH=`echo $line | awk '{match($0,/\/.*\/);print substr($0,RSTART,RLENGTH)}'`;
	awk -vs1="\"" -vpath_fie="$PATH/temp_file" '{LINE1="upload_max_filesize = " s1 "40M" s1;LINE2="post_max_size = " s1 "40M" s1;if($0==LINE1){A=1};if($0==LINE2){B=1};if(A && B){NULL=1};if(!A){print LINE1 > path_file};if(!B){print LINE2 >> path_file};if(!A && !B){print LINE1 ORS LINE2 >> path_file}' $line
	###mv "$PATH/temp_file" $line
	done < "files_output_list"
else
	echo "No files found with name php.ini, please check."
fi


NOTE: I have commented mv command above, if you feel command is ok then you can use it.


Thanks,
R. Singh
# 4  
Old 09-05-2015
Probably because the number of embedded spaces is different sometimes.
ie
Code:
upload_max_filesize=40M
upload_max_filesize = 40M

even though both lines are correct syntax.
Your script might work better if you limit the value of line1 and line2 to just the variable name.
Code:
line1='upload_max_filesize'

Alternatively, what happens if the line appears twice in the .ini file?
# 5  
Old 09-05-2015
Code:
#!/bin/bash
line1='upload_max_filesize = "41M";'
line2='post_max_size = "40M";'
find -type f -iname 'php.ini' |
while read f; do
  if ! grep -qFe "$line1" -e "$line2" "$f"; then
    printf "%s\n" "$line1" "$line2" >> "$f"
  fi
done

First Thank You for your replies,
made a mockup directory tree then I ran the above code (as a welder/fitter I understand pipes Smilie) worked perfect untill I changed 50M to 40M then I wound up with a bunch of duplicate entries which means if is use it that way I am stuck with those values. So I tried to change it to deal with that. See below;
Code:
#!/bin/bash

line1='upload_max_filesize'
line2='post_max_size'
line3='upload_max_filesize = "41M";'
line4='post_max_size = "40M";'

find -type f -iname 'php.ini' |

while read f; do
  if  grep -qFe "$line1" -e "$line2" "$f"; then
       sed -i -e '/upload_max_filesize/d' 'php.ini'  -e '/post_max_size'/d 'php.ini'
#    sed -i -e '/$line1/d' "$f" -e '/$line2/d' "$f"
#    printf "%s\n" "$line3" "$line4" >> "$f"
 
  fi
done

# sed -i -e '/upload_max_filesize/d' 'php.ini'  -e '/post_max_size'/d 'php.ini'

what I am now trying to do is if grep finds line1 and line 2 to be true then have sed remove them.
the bottom sed command works on a single file in the directory from the command line but I cant figure out how to get to work in the script. when I run it it doesn't error but doesn't remove the lines either.
also what is the $f ?? I see it in scripts I think it is a variable for the file name but if so how is it assigned ?

Last edited by Larrykh465; 09-05-2015 at 03:22 PM..
# 6  
Old 09-05-2015
$f is the name of each file that is processed.
The find command creates a list of file name, that are then read one at a time into the $f variable by the do loop.
You might experiment with the following code to clean up your files.
Code:
grep -v upload_max_filesize $f |grep -v post_max_size >temp.ini
echo $line3 >>temp.ini
echo $line4 >>temp.ini
mv temp.ini $f

This will remove all the existing lines and replace them with whatever is in $line3 and $line4.
Be sure to do a backup before starting.
# 7  
Old 09-05-2015
Code:
#!/bin/bash

# phpupdate.sh

line1='upload_max_filesize'
line2='post_max_size'
line3='upload_max_filesize = "61M";'
line4='post_max_size = "60M";'

find -type f -iname 'php.ini' |

while read f; do


 if  grep -qFe "$line1" -e "$line2" "$f"; then
      sed -i -e '/upload_max_filesize/d' $f  -e '/post_max_size'/d $f
 fi
        printf "%s\n" "$line3" "$line4" >> "$f"
done

I got the above to work and it works correctly even when I change the values for $line3 and $line4

question? if I put the printf statement inside the if fi clause it gets bypassed completely and how do I pass variables $line1 and $line2 to sed?
Thanks for explaining the $f to me it help a lot to know where to put echo commands so I could see what was going on.
I am going to run it on a copy of my real directory tree via virtualbox see how it goes.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to find and get a file in an entire directory with an excluded directory specified?

How to get a file 'zlib.h' in an entire directory with an excluded directory specified lives under that starting directory by using find command, as it failed on: $ find . -name 'zlib.h' -a -ipath 'CHROME.TMP' -prune -o -print it'll just list entirely up (2 Replies)
Discussion started by: abdulbadii
2 Replies

2. Shell Programming and Scripting

Insert new line of text into Odbc.ini file

I am new to Perl. I wrote a Perl program that inserts text "EnableScrollableCursors=3" after a section of contexts in the odbc.ini file matches a variable in an array list. "EnableScrollableCursors=3" is added to a newline before whitespaces separate each section of contexts in the odbc.ini. ... (0 Replies)
Discussion started by: dellanicholson
0 Replies

3. Shell Programming and Scripting

append text to column in all files of directory

Hi, I want to append "chr" to all col 2 values of all files in a particular folder. This is what I came up with but isnt working. Please help. ls -1 * | ( while read line do awk 'BEGIN {FS=OFS=":"} {$2="chr"$2;print $0}' $line > $line_new done ) Another question is, how to delete... (7 Replies)
Discussion started by: alpesh
7 Replies

4. Ubuntu

php.ini

Hi, I have installed ubuntu server lamp. When I used the command locate to find the php.ini file I found two location. /etc/php5/cli/php.ini /etc/php5/apache2/php.ini When I want to change the setting of the php, which one of them should I change and why I have this file twice? (3 Replies)
Discussion started by: programAngel
3 Replies

5. UNIX for Dummies Questions & Answers

Count Number Of lines in text files and append values to beginning of file

Hello, I have 50 text files in a directory called "AllFiles" I want to make a program that will go inside of the "AllFiles" Directory and count the number of lines in each individual text file. Then, the program will calculate how many more lines there are over 400 in each text file and... (7 Replies)
Discussion started by: motoxeryz125
7 Replies

6. Shell Programming and Scripting

Append text to end of line on all lines

Hi, I've spent some time researching for this but can't seem to find a solution. I have a file like this 1234|Test|20101111|18:00|19:00There will be multiple lines in the file with the same kind of format. For every line I need to make it this 1234|Test|20101111|18:00|19:00||create... (5 Replies)
Discussion started by: giles.cardew
5 Replies

7. UNIX for Dummies Questions & Answers

Trying to show lines in INI files until the comment character (#)

I have a working directory on a server with over 100 INI files. For the most part, they are configured the same way. Each line will contain 1 or none variables listed from the first character in the line such as VariableName=0. Unfortunately there are comments everywhere using the... (4 Replies)
Discussion started by: hindesite
4 Replies

8. Web Development

about php.ini and mysql linux

I can't use mysql .How should i config the file of php.ini . the message: Fatal error: Call to undefined function mysql_errno() in /var/www/html/inc/dv_clssql.php on line 129 how should i do? please help me (2 Replies)
Discussion started by: fang_xiaoan
2 Replies

9. UNIX for Dummies Questions & Answers

Move all files in a directory tree to a signal directory?

Is this possible? Let me know If I need specify further on what I am trying to do- I just want to spare you the boring details of my personal file management. Thanks in advance- Brian- (2 Replies)
Discussion started by: briandanielz
2 Replies
Login or Register to Ask a Question