1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# small ruby-script to change permissions recursive
# to enable all debugging messages (set via parameter "-v")
@@debug=false
### some functions
def change_perm(p_permdir, p_permfile, p_dir, rec_level)
#change into the given directory
begin
Dir.chdir(p_dir)
rescue
puts "\nError changing into directory \"#{p_dir}\"!"
exit
end
# debugging messages for this run
indent = " " * (rec_level*2)
puts "#{indent}Level of recursions: #{rec_level} (\"#{p_dir}\")" if @@debug
puts "#{indent}Current Working Directory: \"#{Dir.getwd}\"" if @@debug
# running through the active directory
begin
Dir.foreach(".") do |file|
# its important to ignore "." and ".." when using recursion
if not (file == "." or file == "..")
# split execution between files and directories
if File.directory?(file)
# execute the chmod command and remember the success/failure
file_chmod = system("chmod",p_permdir, file)
change_perm(p_permdir, p_permfile, file, rec_level+1)
puts if @@debug
puts "#{indent}Directory: #{file} (changed: \"#{file_chmod}\")" if @@debug
else
# execute the chmod command and remember the success/failure
file_chmod = system("chmod",p_permfile, file)
puts "#{indent}File: \"#{file}\" (changed: #{file_chmod})" if @@debug
end
end
end
rescue
puts "\nError while processing directory \"#{p_dir}\"!"
exit
end
# changing back to the parent directory
Dir.chdir("..")
end
### the main script
print "---------------------------------------------------\n" if @@debug
print "Programmname ($0): ", $0, "\n" if @@debug
print "ARGV[0]: ", ARGV[0], "\n" if @@debug
print "ARGV[1]: ", ARGV[1], "\n" if @@debug
print "ARGV[2]: ", ARGV[2], "\n" if @@debug
print "---------------------------------------------------\n" if @@debug
# extract the permissions from the parameters
perm_dir = ARGV[0]
perm_file = ARGV[1]
if ARGV[2].nil? or ARGV[2]=="-v" then
startdir = "."
else
startdir = ARGV[2]
end
if ARGV[2]=="-v" or ARGV[3]=="-v" then
@@debug=true
end
# only parameter for permissions need to be checked
if perm_dir.nil? or perm_file.nil? then
puts
puts "Usage: #{$0} ddd fff [pfad] [-v]"
puts " ddd ... chmod argument for direcories (755)"
puts " fff ... chmod argument for files (644)"
puts " path ... nothing for current directory"
puts " -v ... verbose listing"
puts
exit
end
change_perm(perm_dir, perm_file, startdir, 0) |