即使我在文件上使用`/ bin / cp`和chmod 777,在bash脚本中复制也会失败

时间:2018-09-13 04:41:17

标签: linux bash shell permissions copy

我写了一个bash程序来设置新的Debian安装程序...。但是我无法弄清楚这个问题/错误...我无法获取将.bashrc文件复制到新用户的信息。 /home目录,即使具有777权限。

以下是变量的声明和设置:

 21 CURRENTDIR=/tmp/svaka
 22 BASHRC=.bashrc
 23 NANORC=.nanorc
 24 BASHRCROOT=.bashrcroot

以下是程序使用的文件,它们位于程序运行所在的目录中:

$ls -sail /tmp/svaka
total 110
 21  1 drwxrwxrwx  3 root        root         1024 Sep 13 05:21 .
  2  3 drwxrwxrwt 13 root        root         3072 Sep 13 05:20 ..
 34  4 -rwxrwxrwx  1 root        root         3250 Sep 11 23:27 .bashrc
 36  3 -rwxrwxrwx  1 root        root         2517 Sep 11 23:27 .bashrcroot
 31  1 -rwxrwxrwx  1 root        root          524 Sep 11 23:27 changePasswords.bash
 28  2 -rwxrwxrwx  1 root        root         1235 Sep 11 23:27 checkSystem
 42  1 -rwxrwxrwx  1 root        root         594 Sep 13 04:29 checuserpass.sh
 39  0 -rwxrwxrwx  1 root        root            0 Sep 11 23:28 cpSuccessCodes.txt
 25  8 -rwxrwxrwx  1 root        root         8048 Sep 11 23:27 debianConfig_1.bash
 32 11 -rwxrwxrwx  1 root        root        10326 Sep 13 05:21 debianConfig_awsome.3.2.sh
 33  4 -rwxrwxrwx  1 root        root         3971 Sep 11 23:27 debianConfig.sh
 35  9 -rwxrwxrwx  1 root        root         8481 Sep 11 23:27 debianConfig_version.3.0.sh
 38  7 -rwxrwxrwx  1 root        root         6970 Sep 13 05:07 debianConfigVersion3.1ERRORS.txt
340  1 -rw-r--r--  1 root        root  1024 Sep 13 05:13 .debianConfigVersion3.1ERRORS.txt.swp
 40 11 -rwxrwxrwx  1 root        root        10658 Aug  3  2016 deb-multimedia-keyring_2016.8.1_all.deb
 41 11 -rwxrwxrwx  1 root        root        10658 Aug  3  2016 deb-multimedia-keyring_2016.8.1_all.deb.1
397 11 -rw-r--r--  1 root        root        10658 Aug  3  2016 deb-multimedia-keyring_2016.8.1_all.deb.2
 43  3 drwxrwxrwx  6 root        root         3072 Sep 13 05:01 nanorc
 26  9 -rwxrwxrwx  1 root        root         8833 Sep 11 23:27 .nanorc
 30  1 -rwxrwxrwx  1 root        root          862 Sep 11 23:27 sources.list
 37  2 -rwxrwxrwx  1 root        root         1723 Sep 11 23:27 ssh_config
 27  3 -rwxrwxrwx  1 root        root         2573 Sep 11 23:27 sshd_config
 24  1 -rwxrwxrwx  1 root        root          134 Sep 11 23:27 testing3892739.sh
 22  1 -rwxrwxrwx  1 root        root          772 Sep 11 23:27 testing.sh
 29  1 -rwxrwxrwx  1 root        root           71 Sep 11 23:27 userlist.txt
 23  1 -rwxrwxrwx  1 root        root           71 Sep 11 23:27 users.txt

代码如下:

300 for user in "$@"
301 do
302     if [ "$user" = root ]
303     then
304         continue
305     fi
306     sudo -i -u "$user" user="$user" CURRENTDIR="$CURRENTDIR" BASHRC="$BASHRC" bash <<'EOF'
307     echo "usersBashrc"
308     if /bin/cp -f "$CURRENTDIR/$BASHRC" "$HOME/.bashrc"
309     then
310         echo "Copy for $user \(bashrc\) succeeded!"
311         sleep 3
312     else
313         echo "Couldn't cp .bashrc for user $user"
314         exit 127
315     fi
316     chown $user:$user "$HOME/.bashrc" || exit 127
317     chmod 644 "$HOME/.bashrc"
318     wget https://raw.github.com/trapd00r/LS_COLORS/master/LS_COLORS -O "$HOME"/.dircolors
319     echo 'eval $(dircolors -b $HOME/.dircolors)' >> "$HOME"/.bashrc
320 EOF
321 done

如果添加以下行,我可以使它工作:

chown $user:$user "$CURRENTDIR"/.bashrc

在我输入sudo

之前

问题:

我的问题是,我是否应该copy file,因为它具有777个权限集readwrite和{{1} },即使我不是所有者?

编辑#1:

execute

1 个答案:

答案 0 :(得分:0)

如果更改变量的引用,我可以使它工作.....

/bin/cp "$CURRENTDIR/$BASHRC" "$HOME/.bashrc"

/bin/cp "$CURRENTDIR"/"$BASHRC" "$HOME/.bashrc"

我是编程新手,所以对此没有任何解释。