Saturday, January 8, 2022

[SOLVED] I wrote a script that's supposed to make a directory. It works without sudo, but doesn't with it. THe script is required to be called with sudo

Issue

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

Solution

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"

  1. try using absolute references sudo mkdir /home/some-new-dir
  2. 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



Answered By - eDonkey