A
A
AmdInDrive2020-11-04 20:19:01
linux
AmdInDrive, 2020-11-04 20:19:01

Ubuntu bash scripts and inserting commands into them?

Hello, those who are interested in this issue. After two days of fighting bash'em, I still couldn't figure it out. what and how to do. The bottom line is this, I have a task to write code in file.sh that will check the user in the /etc/passwd directory, if it exists, then write about it, and if not, then write that it does not exist, therefore create it and give him a password. Next, it is necessary that there is a command to check the home directory of this user (/home/our_user), if it exists, then write about it, if not, then create it and assign it the owner in the person of our new user.
My problems that arose when running this script, by the way, I ran it through the sudo bash file.sh command. And I have an assumption that this is a little wrong.
1) It constantly gives me that there is no /etc/passwd file. Although I have a question how this can be, if it stores information about all users.
2) A command with a user variable does not always work, and I don’t understand the syntax of writing it at all, since the standard $ command does not work, and why it’s so unclear.
3) When switching to the user that the script should create, I get just the $ icon and that's it, I guess because the directory is not assigned to it.
I am attaching a picture with the code, please, if it’s not difficult for anyone, disassemble it completely, where there are errors and how to fix them, it is very important for me to understand where I am wrong. And a small request to explain this in simple language, since even I am not familiar with the syntax of Ubuntu (Linux), I will not say anything about the commands.
Here is the code itself:

#!/bin/bash
user=dan
    if 
grep $user /etc/passwd/
    then 
echo "The user $user Exists"
else 
echo "The user $user does not Exists"
useradd $user -m
passwd $user
fi
     if 
grep $user /home/$user
     then 
echo "The directory $user Exists"
     else 
echo "The directory user $user  home dont Exists"
mkdir /home/$user
chown $user:$user /home/$user
fi

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
CityCat4, 2020-11-05
@AmdInDrive

Okay, I'm not too lazy, I'll write an answer, although I usually don't answer such questions.
This is not a code.
This is not a script.
It's not bash.
This is some kind of crazy porridge from an incomprehensible what. Although the problem is quite simple. But your problems start right from the staging.
- Home directory - not necessarily in /home, it is specified in the user entry in /etc/passwd
- Rights to the home directory - not necessarily $user:$user, usually $user:$usergroup
The script might look something like this (used commands I will not explain - there is a man for each, and more voluminous manuals). Yes, I always use Bash version 1.x

#!/bin/sh

user=$1 # Это некорректно, здесь нужна проверка на то, передан ли аргумент, только для примера
  uinfo=`getent passwd $user`
  bla=`echo $uinfo | awk 'BEGIN {FS=":"} \
        {printf "uname=%s;uid=%s;ugid=%s;uhome=%s",$1,$3,$4,$6}'`
  eval $bla
# После этого блока у нас есть переменные uname, uid, ugid и uhome, которые
# если юзер есть, заполнены данными, а если нет - то пустые

if [ ${#uname} -ne 0 ]; then
  echo "Username exist"
  # Мы предположили, что uhome заполнен, но на самом деле это нужно проверять
 else
   echo "No username"
   # Здесь команды на создание юзера
fi

if [ -d $uhome ]; then
  echo "Homedir exist"
 else
   echo "No homedir"
   mkdir $uhome
   chown $uid:$ugid $uhome
fi

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question