Linux bash 脚本更改目录、子目录和文件的所有权

Linux bash 脚本更改目录、子目录和文件的所有权

我编写了一个 bash 脚本来更改文件和目录所有权。

我需要更改 nfs 区域内的文件和目录的所有者。在此区域中,文件和目录所有权属于某些用户 ID。 (nfs区域以某种方式将所有目录和文件所有者更改为相应的用户ID号。例如file1所有权属于2100,而2100是属于用户db1的ID)。我们在不知不觉中升级了服务器并更改了用户 ID。所以现在,在 nfs 区域内,我需要将所有目录和文件所有权更改为新的相应用户。

该脚本获取一个数组的目录/文件所有者(名称)以及另一个数组的目录/文件路径。当我打印数组时,我可以看到文件所有者名称与文件路径相对应。。比在 for 循环中,如果目录和文件所有者名称等于静态名称(例如 2100),我将更改新的相应用户的文件所有权。问题是当我运行脚本时,它无法更改所有目录和文件的所有权。它只会改变其中的一些。

运行脚本前的输出:

[root@eeprepo01 testscript]# ll
total 15
drwxr-xr-x 3 2100 3000    7 Nov 23 13:49 directory1
drwxr-xr-x 3 2400 3000    6 Nov 23 13:52 directory2
drwxr-xr-x 3 2300 3000    7 Nov 23 13:53 directory3
-rwxr-xr-x 1 root root 1016 Nov 23 14:01 modify.sh
-rwxr-xr-x 1 root root 1516 Nov 19 10:13 modifyzfs.sh
[root@eeprepo01 testscript]# cd directory1/
[root@eeprepo01 directory1]# ll
total 5
drwxr-xr-x 2 2100 5000 4 Nov 23 13:49 directory1_2
-rw-r--r-- 1 2100 3000 0 Nov 23 13:48 file1
-rw-r--r-- 1 2500 3000 0 Nov 23 13:48 file2
-rw-r--r-- 1 2300 3000 0 Nov 23 13:48 file3
-rw-r--r-- 1 2400 3000 0 Nov 23 13:48 file4
[root@eeprepo01 directory1]# cd directory1_2/
[root@eeprepo01 directory1_2]# ll
total 1
-rw-r--r-- 1 2100 5000 0 Nov 23 13:49 file1
-rw-r--r-- 1 2100 5000 0 Nov 23 13:49 file2
[root@eeprepo01 directory1_2]# 

正如你所看到的,文件和目录属于一些数字,这些数字是与实际用户相对应的一些ID。运行脚本后的输出如下:

    [root@eeprepo01 testscript]# bash -x modify.sh 
    + path='/nfspath/testscripts/testscript/*'
    + path_owners=($(find $path -printf '%u\n'))
    ++ find /nfspath/testscripts/testscript/directory1 /nfspath/testscripts/testscript/directory2 /nfspath/testscripts/testscript/directory3 /nfspath/testscripts/testscript/modify.sh /nfspath/testscripts/testscript/modifyzfs.sh -printf '%u\n'
    + echo 2100 2400 root 2100 2100 2300 2500 2100 2400 2500 2300 2400 2500 2300 2600 2500 2300 2500 2600 2610 2710 2610 2710 2100 2710 2400 2300 root root
    2100 2400 root 2100 2100 2300 2500 2100 2400 2500 2300 2400 2500 2300 2600 2500 2300 2500 2600 2610 2710 2610 2710 2100 2710 2400 2300 root root
    + path_owners_names=($(find $path -print))
    ++ find /nfspath/testscripts/testscript/directory1 /nfspath/testscripts/testscript/directory2 /nfspath/testscripts/testscript/directory3 /nfspath/testscripts/testscript/modify.sh /nfspath/testscripts/testscript/modifyzfs.sh -print
    + echo /nfspath/testscripts/testscript/directory1 /nfspath/testscripts/testscript/directory1/file4 /nfspath/testscripts/testscript/directory1/directory1_2 /nfspath/testscripts/testscript/directory1/directory1_2/file1 /nfspath/testscripts/testscript/directory1/directory1_2/file2 /nfspath/testscripts/testscript/directory1/file3 /nfspath/testscripts/testscript/directory1/file2 /nfspath/testscripts/testscript/directory1/file1 /nfspath/testscripts/testscript/directory2 /nfspath/testscripts/testscript/directory2/file1 /nfspath/testscripts/testscript/directory2/file2 /nfspath/testscripts/testscript/directory2/file3 /nfspath/testscripts/testscript/directory2/directory2_2 /nfspath/testscripts/testscript/directory2/directory2_2/file3 /nfspath/testscripts/testscript/directory2/directory2_2/file2 /nfspath/testscripts/testscript/directory2/directory2_2/file1 /nfspath/testscripts/testscript/directory3 /nfspath/testscripts/testscript/directory3/file3 /nfspath/testscripts/testscript/directory3/file4 /nfspath/testscripts/testscript/directory3/directory3_1 /nfspath/testscripts/testscript/directory3/directory3_1/file2 /nfspath/testscripts/testscript/directory3/directory3_1/file1 /nfspath/testscripts/testscript/directory3/directory3_1/directory3_2 /nfspath/testscripts/testscript/directory3/directory3_1/directory3_2/file2 /nfspath/testscripts/testscript/directory3/directory3_1/directory3_2/file1 /nfspath/testscripts/testscript/directory3/file2 /nfspath/testscripts/testscript/directory3/file1 /nfspath/testscripts/testscript/modify.sh /nfspath/testscripts/testscript/modifyzfs.sh
    /nfspath/testscripts/testscript/directory1 /nfspath/testscripts/testscript/directory1/file4 /nfspath/testscripts/testscript/directory1/directory1_2 /nfspath/testscripts/testscript/directory1/directory1_2/file1 /nfspath/testscripts/testscript/directory1/directory1_2/file2 /nfspath/testscripts/testscript/directory1/file3 /nfspath/testscripts/testscript/directory1/file2 /nfspath/testscripts/testscript/directory1/file1 /nfspath/testscripts/testscript/directory2 /nfspath/testscripts/testscript/directory2/file1 /nfspath/testscripts/testscript/directory2/file2 /nfspath/testscripts/testscript/directory2/file3 /nfspath/testscripts/testscript/directory2/directory2_2 /nfspath/testscripts/testscript/directory2/directory2_2/file3 /nfspath/testscripts/testscript/directory2/directory2_2/file2 /nfspath/testscripts/testscript/directory2/directory2_2/file1 /nfspath/testscripts/testscript/directory3 /nfspath/testscripts/testscript/directory3/file3 /nfspath/testscripts/testscript/directory3/file4 /nfspath/testscripts/testscript/directory3/directory3_1 /nfspath/testscripts/testscript/directory3/directory3_1/file2 /nfspath/testscripts/testscript/directory3/directory3_1/file1 /nfspath/testscripts/testscript/directory3/directory3_1/directory3_2 /nfspath/testscripts/testscript/directory3/directory3_1/directory3_2/file2 /nfspath/testscripts/testscript/directory3/directory3_1/directory3_2/file1 /nfspath/testscripts/testscript/directory3/file2 /nfspath/testscripts/testscript/directory3/file1 /nfspath/testscripts/testscript/modify.sh /nfspath/testscripts/testscript/modifyzfs.sh
    + j=0
    + for i in '$path'
    + [[ 2100 -eq 2100 ]]
    ++ chown db1:5000 /nfspath/testscripts/testscript/directory1
    + j=1
    + for i in '$path'
    + [[ 2400 -eq 2100 ]]
    + [[ 2400 -eq 2600 ]]
    + [[ 2400 -eq 2500 ]]
    + [[ 2400 -eq 2300 ]]
    + [[ 2400 -eq 2610 ]]
    + [[ 2400 -eq 2710 ]]
    + [[ 2400 -eq 2400 ]]
    ++ chown db7:5000 /nfspath/testscripts/testscript/directory1/file4
    + j=2
    + for i in '$path'
    + [[ root -eq 2100 ]]
    + [[ root -eq 2600 ]]
    + [[ root -eq 2500 ]]
    + [[ root -eq 2300 ]]
    + [[ root -eq 2610 ]]

+ [[ root -eq 2710 ]]
+ [[ root -eq 2400 ]]
+ j=3
+ for i in '$path'
+ [[ 2100 -eq 2100 ]]
++ chown db1:5000 /nfspath/testscripts/testscript/directory1/directory1_2/file1
+ j=4
+ for i in '$path'
+ [[ 2100 -eq 2100 ]]
++ chown db1:5000 /nfspath/testscripts/testscript/directory1/directory1_2/file2
+ j=5
[root@eeprepo01 testscript]# 

我回显了数组以显示数组采用所有值。我还使用 bash -x 运行脚本,发现 for 循环仅针对 6 个项目运行。

运行脚本后,输出如下:

[root@eeprepo01 testscript]# ll
total 15
drwxr-xr-x 3 db1  5000    7 Nov 23 13:49 directory1
drwxr-xr-x 3 2400 3000    6 Nov 23 13:52 directory2
drwxr-xr-x 3 2300 3000    7 Nov 23 13:53 directory3
-rwxr-xr-x 1 root root 1016 Nov 23 14:01 modify.sh
-rwxr-xr-x 1 root root 1516 Nov 19 10:13 modifyzfs.sh
[root@eeprepo01 testscript]# cd directory1/
[root@eeprepo01 directory1]# ll
total 5
drwxr-xr-x 2 db1  5000 4 Nov 23 13:49 directory1_2
-rw-r--r-- 1 2100 3000 0 Nov 23 13:48 file1
-rw-r--r-- 1 2500 3000 0 Nov 23 13:48 file2
-rw-r--r-- 1 2300 3000 0 Nov 23 13:48 file3
-rw-r--r-- 1 db7  5000 0 Nov 23 13:48 file4
[root@eeprepo01 directory1]# cd directory1_2/
[root@eeprepo01 directory1_2]# ll
total 1
-rw-r--r-- 1 db1 5000 0 Nov 23 13:49 file1
-rw-r--r-- 1 db1 5000 0 Nov 23 13:49 file2
[root@eeprepo01 directory1_2]# cd ../../
[root@eeprepo01 testscript]# cd directory2/
[root@eeprepo01 directory2]# ll
total 5
drwxr-xr-x 2 2500 3000 5 Nov 23 13:52 directory2_2
-rw-r--r-- 1 2500 3000 0 Nov 23 13:50 file1
-rw-r--r-- 1 2300 3000 0 Nov 23 13:50 file2
-rw-r--r-- 1 2400 3000 0 Nov 23 13:50 file3

该脚本仅更改directory1、directory1_2 以及文件1 和2 的所有权。

脚本如下:

#!/bin/bash

path="/nfspath/testscripts/*"

path_owners=($(find $path -printf '%u\n'))
echo ${path_owners[@]}
path_owners_names=($(find $path -print))
echo ${path_owners_names[@]}
j=0

for i in $path
do
    
        
    if [[ ${path_owners[$j]} -eq 2100 ]]; then
        `chown db1:5000 ${path_owners_names[$j]}`
        
    elif [[ ${path_owners[$j]} -eq 2600 ]]; then
        `chown db2:5000 ${path_owners_names[$j]}`
        
    elif [[ ${path_owners[$j]} -eq 2500 ]]; then
        `chown db3:5000${path_owners_names[$j]}`
        
    elif [[ ${path_owners[$j]} -eq 2300 ]]; then
        `chown db4:5000 ${path_owners_names[$j]}`
        
    elif [[ ${path_owners[$j]} -eq 2610 ]]; then
        `chown db5:5000 ${path_owners_names[$j]}`
    
    elif [[ ${path_owners[$j]} -eq 2710 ]]; then
        `chown db6:5000 ${path_owners_names[$j]}`
            
    elif [[ ${path_owners[$j]} -eq 2400 ]]; then
        `chown db7:5000 ${path_owners_names[$j]}`
        
    fi
    j=$((j+1))

done
 

相关内容