0

I need to write a bash-script that will create a new user named user1 and an empty file my_txt.txt in ~user1/tmp/ . The problem is terminal asks the password for new user and i don't know how to input it along su - user1

Axmed
  • 1
  • 2
    I sense a homework exercise... A hint: you don't have to su to the new user in order to be able to create files for them. – telcoM Apr 14 '22 at 13:52
  • 1
    this looks like a homework question, so I'll just ask you a question that might point you in the right direction - why do you think you need to use su? does the question itself require it? Also, to create a new user, you're either already running as root or you used sudo or similar. – cas Apr 14 '22 at 13:52
  • 1
    Please [edit] your question and show your complete script. Hint: chown and chgrp might be useful. – Bodo Apr 14 '22 at 13:58

1 Answers1

0

Thanks to the commentators I've written this code, and it seems to work:

sudo useradd -m -s "/bin/bash" user1

sudo bash -c "echo user1:password | chpasswd"

sudo -u user1 bash -c "cd /home/user1 && touch my_txt"

Axmed
  • 1