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

问题描述 投票:0回答:1

我写了一个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权限设置readwriteexecute,即使我不是主人?

编辑#1:

(root@HOST)-(15:11:52)-(/tmp/svaka)
$./system38.sh something71 something
+ for user in "$@"
+ '[' something71 = root ']'
+ chown something71:something71 /tmp/svaka/.bashrc
+ sudo -i -u something71 user=something71 CURRENTDIR=/tmp/svaka BASHRC= bash
usersBashrc
/bin/cp: -r not specified; omitting directory '/tmp/svaka/'
Couldn't cp .bashrc for user something71


(root@HOST)-(15:11:54)-(/tmp/svaka)
$ls -sail
total 136
 21  1 drwxrwxrwx  3 root        root         1024 Sep 13 15:11 .
  2  3 drwxrwxrwt 13 root        root         3072 Sep 13 15:11 ..
 34  4 -rwxrwxrwx  1 something71 something71  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 something78 something78   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        10426 Sep 13 14:32 debianConfig_awsome.3.3.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 11 -rwxrwxrwx  1 root        root        10661 Sep 13 09:06 debianConfigVersion3.1ERRORS.txt
 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 -rwxrwxrwx  1 root        root        10658 Aug  3  2016 deb-multimedia-keyring_2016.8.1_all.deb.2
398 11 -rwxrwxrwx  1 root        root        10658 Aug  3  2016 deb-multimedia-keyring_2016.8.1_all.deb.3
340 11 -rw-r--r--  1 root        root        10658 Aug  3  2016 deb-multimedia-keyring_2016.8.1_all.deb.4
 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
400  1 -rwxr-xr-x  1 root        root          793 Sep 13 15:11 system38.sh
 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
(root@HOST)-(15:12:05)-(/tmp/svaka)
linux bash shell permissions copy
1个回答
0
投票

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

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.