LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-02-2019, 09:14 AM   #1
mirror25
LQ Newbie
 
Registered: Mar 2019
Posts: 3

Rep: Reputation: Disabled
Script users


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
 
Old 03-02-2019, 09:25 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Welcome.

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.
 
Old 03-02-2019, 09:33 AM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Your use of the words. 'so' + 'plus',
Code:
if [ true ] ; then
echo "hello world"
else
echo "good-bye world"
fi
Actually, your code is a complete cabash...

Bash Guide for Beginners

Last edited by BW-userx; 03-02-2019 at 09:37 AM.
 
Old 03-02-2019, 09:44 AM   #4
mirror25
LQ Newbie
 
Registered: Mar 2019
Posts: 3

Original Poster
Rep: Reputation: Disabled
Script users

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
 
Old 03-02-2019, 09:45 AM   #5
1nuxg33k
Member
 
Registered: Feb 2019
Location: PNW
Distribution: Debian, LFS
Posts: 87

Rep: Reputation: 33
Additionally to check your code https://www.shellcheck.net/
 
Old 03-02-2019, 09:58 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
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.
 
Old 03-02-2019, 10:00 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,734

Rep: Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920Reputation: 5920
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.

Last edited by michaelk; 03-02-2019 at 10:02 AM.
 
Old 03-02-2019, 10:36 AM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by mirror25 View Post
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.

A word on order and logic

Last edited by BW-userx; 03-02-2019 at 10:45 AM.
 
Old 03-02-2019, 11:04 AM   #9
mirror25
LQ Newbie
 
Registered: Mar 2019
Posts: 3

Original Poster
Rep: Reputation: Disabled
Unhappy Script users

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.
 
Old 03-02-2019, 11:19 AM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by mirror25 View Post
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
please read up on loops
basic understanding of loops in itself
https://www.tutorialspoint.com/cprogramming/c_loops.htm
and bash loops
https://ryanstutorials.net/bash-scri...bash-loops.php
and
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html

P.S.
some of this code may not even be needed.

Last edited by BW-userx; 03-02-2019 at 01:16 PM.
 
Old 03-02-2019, 11:25 AM   #11
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
The pattern should be delimited so that a partial match does not trigger a false response:

Code:
grep "^$user:" /etc/passwd)
The colon : is needed there for that.
 
Old 03-03-2019, 02:05 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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
 
Old 03-03-2019, 09:16 AM   #13
orbea
Senior Member
 
Registered: Feb 2015
Distribution: Slackware64-current
Posts: 1,950

Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
The variable $USER is one of them, the system fills that one in for you and you can't change it.
Code:
$ echo $USER
orbea
$ USER=foo
$ echo $USER
foo
 
1 members found this post helpful.
Old 03-03-2019, 09:26 AM   #14
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Quote:
Originally Posted by orbea View Post
Code:
$ echo $USER
orbea
$ USER=foo
$ echo $USER
foo
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:

Code:
readonly | awk '{sub(/=.*$/,"",$3); print $3}'
 
1 members found this post helpful.
Old 03-03-2019, 10:00 AM   #15
orbea
Senior Member
 
Registered: Feb 2015
Distribution: Slackware64-current
Posts: 1,950

Rep: Reputation: Disabled
Neat command, I didn't know I could check that so easily.

Code:
$ readonly | awk '{sub(/=.*$/,"",$3); print $3}'
BASHOPTS
BASH_VERSINFO
EUID
PPID
SHELLOPTS
UI
That is with bash-5.0.002 and this seems highly dependent on the specific shell and not all of them have readonly variables.

mksh/pdksh
Code:
$ readonly
KSH_VERSION
mksh the second time 'readonly' is used.
Code:
$ readonly                                                                       
KSH_VERSION
PIPESTATUS[0]
openbsd ksh
Code:
$ readonly
KSH_VERSION
PPID
posh
Code:
$ readonly
POSH_VERSION
zsh
Code:
% readonly                                      
'!'=0
'#'=0
'$'=8458
'*'=(  )
-=569XZims
'?'=130
@=(  )
ARGC=0
HISTCMD=4
LINENO=4
PPID=8457
TTYIDLE=3
ZSH_EVAL_CONTEXT=toplevel
ZSH_SUBSHELL=0
status=130
zsh_eval_context=( toplevel )
I also tried Slackware's (Old) ash, dash, ksh and yash which returned nothing with 'readonly'.

Last edited by orbea; 03-03-2019 at 10:02 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to switch users & echo script outputs using shell script rajthampi Linux - Newbie 7 06-25-2015 02:49 PM
Bash script for adding users is running but not adding users Gren Programming 2 04-27-2012 03:53 AM
Script to Auto-add Domain Users to Workstation Power Users Group doesn't work avinash.rao Ubuntu 0 08-22-2009 04:57 AM
Script to add a secondary group to all users except system default users Tekken Linux - Server 5 06-29-2009 04:02 PM
E-Mail notification to users via SMS (gateway script ok, but notification script?!?) Riku2015 Linux - Networking 10 03-08-2002 10:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration