4

My hosting company advertises a php max upload size that can go up to 60M but, It defaults to 2M and I am not able to make changes to the htaccess file. I can override the default by changing the php.ini files but this must be done for every php.ini file in the directory tree. What I and a lot of users need and are begging for, is a BASH script that will do the following task;

  1. recursively find the php.ini files
  2. scan the file for two lines of text (new php directives)
  3. if it doesn't have those two lines of text, append the two lines of text. Save the file and continue to next file.
  4. if it does have those two lines of text, do nothing and continue to the next file.

I have figured out how to find . -ifile php.ini and append (not correctly) the two lines needed to be added.

Even with that, if I figure out how to append the lines correctly if run again it would append the lines again. Not just to the new files that need to be fixed.

if some one could help me with the

find . -ifile php.ini -exec ?????? '{}' \;

part I could figure out how to do the rest myself.

1
  • Give a before after example
    – A.B.
    Sep 2, 2015 at 10:02

1 Answer 1

2

I'd choose this approach:

#!/bin/bash
path='/path'
line1='foo'
line2='bar'
while read f; do
    if [ ! "$(grep "$line1" "$f")" ] && [ ! "$(grep "$line2" "$f")" ]; then
        echo "$line1" >> "$f"
        echo "$line2" >> "$f"
    fi
done < <(find "$path" -type f -name 'php.ini')

Some notes:

  • $path is declared / initialized internally, but it may well be passed as an argument to the script with very few / little modifications if more convenient;
  • find "$path" -type f -name 'php.ini' is executed in an external shell and its output is redirected to the while loop using a process substitution, because piping its output directly to the while loop would have left $line1 and $line2 out of the while loop's scope;
  • -type f in find "$path" -type f -name 'php.ini' might be redundant, but just to be safe;
  • The two echo commands could have been merged into a single echo command, but that would have required to add an -e option to escape the \n sequence; without knowing the exact strings to be appended (which might contain other escape sequences), not using an -e option and using two echo commands is safer;

The script searches, recursively, for any file named php.ini in the $path path; for each file, if both the grep commands into the if statement don't return 0 (i.e. if neither a line containing / matching the $line1 string nor a line containing / matching the $line2 string is found in the file), $line1 and $line2 are appended at the end of the file.

Sample output on a test hierarchy:

user@debian ~/tmp % tree -a
.
├── 1
│   ├── 2
│   │   ├── 3
│   │   │   ├── file
│   │   │   └── php.ini
│   │   ├── file
│   │   └── php.ini
│   ├── file
│   └── php.ini
└── script.sh

3 directories, 7 files
user@debian ~/tmp % find 1 -type f -exec bash -c 'echo -e "{}:\n"; cat {}; echo' \;
1/php.ini:

foo
bar
another line

1/file:

foo
bar
another line

1/2/3/php.ini:

foo
bar
another line

1/2/3/file:

foo
bar
another line

1/2/php.ini:

just one line

1/2/file:

foo
bar
another line

user@debian ~/tmp % bash script.sh                 
user@debian ~/tmp % find 1 -type f -exec bash -c 'echo -e "{}:\n"; cat {}; echo' \;
1/php.ini:

foo
bar
another line

1/file:

foo
bar
another line

1/2/3/php.ini:

foo
bar
another line

1/2/3/file:

foo
bar
another line

1/2/php.ini:

just one line
foo
bar

1/2/file:

foo
bar
another line
4
  • the above almost works perfectly and I understand most of it.
    – Larrykh465
    Sep 5, 2015 at 10:33
  • @Larrykh465 What's the "almost" about? Maybe it can be fixed, although the only flaw I can see is the fact that if one of the lines is in one of the files already the other won't be appended. This can be fixed easily tough if you want, I just thought that wasn't a possibility
    – kos
    Sep 5, 2015 at 10:39
  • Thank you for the help, but this add comet keeps timing out
    – Larrykh465
    Sep 5, 2015 at 10:43
  • @Larrykh465 What do you mean? It's too slow?
    – kos
    Sep 5, 2015 at 10:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .