I'm supposed to make a script that backups some files from /etc
into a directory
that I must create called backup-confs
. The problem is that I must run the script with sudo, but when I do it doesn't create the directory. It works fine without, but I can't figure out why it doesn't work with sudo.
#!/bin/bash
mkdir /home/student/tema3-scripts-output/backup-confs 2>> err.txt
This version also doesn't work
#!/bin/bash
cd 2>> err.txt..
cd tema3-scripts-output 2>> err.txt
mkdir backup-confs 2>> err.txt
cd .. 2>> err.txt
cd tema3-scripts 2>> err.txt
CodePudding user response:
Dont do it all in one, split it up. Maybe is has problems with creating a directory and at the same time redirecting the output from ".../backup-confs"
- try using absolute references
sudo mkdir /home/some-new-dir
- give permissions to file
chmod 777 /home/some-new-dir
and a personal preference, use "copy": cp /home/student/some-config /home/some-new-dir/some-config
CodePudding user response:
I see you are working with relative directory references (like cd tema...
instead of cd /tmp/tema...
). When running under sudo
, you are changing user, which might mean that you are changing directory, therefore I would advise you always to work with absolute directory references.