ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Good morning, I have a problem with the following shell script.
Do I need to ask again if the user is already registered in the system? If I put a pause after <echo "already exists"> it gives me an error, and if I put an output, it leaves the script completely. I've also tried to put a real one after <echo -n "Do you want to enter a user? (Y / N):"; read answer> but it does not work either
The purpose of the script is to request a username, if it does not exist, and then create it. But if it already exists ask for another name.
Thank you.
#! / bin / bash
echo -n "Do you want to enter a user? (s / N):"; read answer
yes [$ answer = 's']; so
echo "Enter the user:"
read USER
if grep -qi "^ $ USER:" / etc / passwd
so
echo "already exists"
plus
echo "does not exist"
fi
elif
[$ answer = 'N']; so
exit
fi
There are extra spaces in your grep query. Also, can you click on the Edit button for your post above and insert [code] [/code] tags so that the indentation is preserved. It will be much more readable then.
Good morning, I have a problem with the following shell script.
Do I need to ask again if the user is already registered in the system? If I put a pause after <echo "already exists"> it gives me an error, and if I put an output, it leaves the script completely. I've also tried to put a real one after <echo -n "Do you want to enter a user? (Y / N):"; read answer> but it does not work either
The purpose of the script is to request a username, if it does not exist, and then create it. But if it already exists ask for another name.
Thank you.
Code:
#!/bin/bash
echo -n "Do you want to enter a user? (y/N): "; read answer
if [ $answer = 'y' ]; then
echo "Enter the user: "
read USER
if grep -qi "^$USER:" /etc/passwd
then
echo "already exists"
else
echo "does not exist"
fi
elif
[ $answer = 'N' ]; then
exit
fi
One problem will be hard to spot: Uppercase variables will often reserved by the system and in those cases they are immutable. The variable $USER is one of them, the system fills that one in for you and you can't change it. Try putting "echo $USER;" at the top of your script on the line after the shebang. In general it is good practice to make all your variables lowercase and if you make your script variable lowercase, $user, then you can avoid the problem with $USER.
For your information.
The standard convention of (y/N) is if nothing was entered i.e. just the enter key was pressed the default input is n or N. You also should check to see if Y was pressed too.
Good morning, I have a problem with the following shell script.
Do I need to ask again if the user is already registered in the system? If I put a pause after <echo "already exists"> it gives me an error, and if I put an output, it leaves the script completely. I've also tried to put a real one after <echo -n "Do you want to enter a user? (Y / N):"; read answer> but it does not work either
The purpose of the script is to request a username, if it does not exist, and then create it. But if it already exists ask for another name.
Thank you.
Code:
#!/bin/bash
echo -n "Do you want to enter a user? (y/N): "; read answer
if [ $answer = 'y' ]; then
echo "Enter the user: "
read USER
if grep -qi "^$USER:" /etc/passwd
then
echo "already exists"
else
echo "does not exist"
fi
elif
[ $answer = 'N' ]; then
exit
fi
or
Code:
#!/bin/bash
#echo -n "Do you want to enter a user? (y/N): "; read answer
#default setting to N as indicated in message, so if user hits enter
#it automatically 'defaults' to N
answer=N
#eliminate echo command
read -p "Do you want to enter a user? (y/N)" answer
#no longer case sensitive.
case $answer in
'Y'|'y')
read -p "Enter the user name: " user
#USER reserved word
#read USER
#check substring
if [[ $(grep "$user" /etc/passwd) =~ "$user" ]] ;
then
echo "$user already exists"
else
echo "$user does not exist"
exit
fi
;;
'N'|'n')
echo "C'ya!"
exit
;;
esac
now you can move on to your next step,
'if it (user name) does not exist, and then create it. But if it already exists [then] ask for another [different] name.
you are going to need a loop maybe two. study up on loops.
But how should I do it to ask again the name of the user if it already exists. I do not know how to create the loop.
this is NOT a fully working script, IT has buggs in it, and not the bunny kind. SO you can examine it and analyze buggy code to fix it, so hopefully you'll gain the know how to understanding code, even bash scripting.
Code:
#!/bin/bash
#echo -n "Do you want to enter a user? (y/N): "; read answer
set -xv
#default setting to N as indicated in message
answer=N
go=p
read -p "Do you want to enter a user? (y/N) " answer
while [[ go != 'dn' ]] ;
do
case $answer in
'Y'|'y')
#check for epmty is y then get it filled
#prevents from asking again in loop
[[ ! -n "$user" ]] && { read -p "Enter the user: " user ; }
#if user not present create user
while [[ ! $(grep "^$user" /etc/passwd) =~ "$user" ]] ;
do
sudo adduser
read -p "Do you want to add another user? (y/N) " answer
[[ "${answer,,}" = "n" ]] && { exit ; } || { unset user ; continue ; }
done
#if user present
while [[ $(grep "^$user" /etc/passwd) =~ "$user" ]] ;
do
read -p "$user is already present in system.
Please enter a different user name " user
break
done
;;
'N'|'n')
echo "C'ya!"
exit
;;
esac
done
Plenty of good points covered and to be worked on, I would say that as a beginner in bash scripting this can be a very good introduction as it covers a number of techniques and easy gotchas.
First idea for me would be to get it clear in my head what the question is (my understanding):
Create a user based on name presented by the user. Should the name exist, ask for an alternative
Straight away the above leaves me with some questions:
- Create a user -- would generally mean being root/super user, do we have access to do this?
- based on name presented by the user -- are there any restrictions (ie length) or any particular format (lastname + first initial, etc)
- ask for an alternative -- how many times? ie. is there a set limit or do we expect the user to try until the task is completed
- After the successful creation of one user, should the script exit or prompt to possibly create more?
- Will we need to deal with assigning a password (albeit a default)? (again super user access required)
Now assuming we have acceptable answers to the above, we still need to look at the fact we are dealing with humans (always an issue)
So some basic questions based on the current examples provided:
- Does the user running the script have the permissions to read the necessary files? (/etc/passwd, if not, do they get an error or exit gracefully with a message to contact the admin?)
- Are we going to allow the user to correct the name if they make a typo?
- If presenting the user with questions with expected results, ie. yes / no questions, what checking is to be done on wrong input?
Using yes/no as example:
- Are we requiring full word, yes or no
- Are we limiting to the first letter? (if so, what if they enter "p")
- If limiting to the first letter, are we taking control to a single key pressed? (ie. after key press the answer is submitted without needing enter key, this alleviates the issue of user pressing more than one key (with some caveats))
My final thought would be, are we going to limit ourselves to only bash builtins (excluding creating user and password)? (ie. no grep/sed/awk, to name a few)
The above can make it a little more interesting as now you need a function to read through /etc/passwd file to check for existing users
So whilst a relatively simple task, I see this as a good learning experience --- Good luck
Thanks for catching that. I mis-remembered or else, most unlikely, things changed. Now that I check, $USER can be modified as can $LOGNAME. However, UID and EUID are read-only variables:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.