Skip to content
Advertisement

Copy fails in bash script even when I use `/bin/cp` and chmod 777 on the file

I wrote a bash program to setup new Debian install’s…. but I can’t figure this problem/error out…… I can’t get it to copy a .bashrc file to a new users /home directory even with 777 permissions.

Here are the declarations and setting of the variables:

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

Here are the files the program uses and they are in the same directory the program runs from:

$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

Here is the code:

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

I can get it to work if I add this line:

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

just before I enter the sudo

QUESTION:

My question is, shouldn’t I be able to copy the file because it has 777 permissions set read,write and execute even though I’m not the owner?

EDIT #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)

Advertisement

Answer

I can get it working if I change the quoting on the variable…..

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

to

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

I’m new to programming so I don’t have any explanations for this.

Advertisement