LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 04-01-2015, 12:32 AM   #1
rickricky
LQ Newbie
 
Registered: Mar 2015
Posts: 7

Rep: Reputation: Disabled
Help me create a Bash Script


Hi all,

Can you guys create a script for me?

A script that will call a file name "userlist" and create user with /bin/false

take note: userlist has a list of names eg. mjordan, mjackson, hpotter, bobama.

if no file name is call it will show a error message saying "File needed"

if execute without any parameter it will display the name of the script.

newbie here in bash script

thanks!
 
Old 04-01-2015, 12:38 AM   #2
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
sounds a lot like homework, please read the forum rules. not allowed.

on the other hand, if you post your efforts and any errors you are receiving via the CLI, we will be more then happy to point out issues and directions to resolve said issues.

https://www.google.com/search?source...i65l2.2954j0j7
 
1 members found this post helpful.
Old 04-01-2015, 12:48 AM   #3
rickricky
LQ Newbie
 
Registered: Mar 2015
Posts: 7

Original Poster
Rep: Reputation: Disabled
Hi lleb,

Thanks for the reply and helpful links.

it is not actually a homework. I already created some but seems not working so I just want to request some new fresh script if anyone can provide.

but its ok.

thanks
 
Old 04-01-2015, 12:49 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
I would suggest you to post what have you tried and fix that...
 
Old 04-01-2015, 01:01 AM   #5
rickricky
LQ Newbie
 
Registered: Mar 2015
Posts: 7

Original Poster
Rep: Reputation: Disabled
here you go:
Sorry for my bad script
The part from which I don't know what to input is in useradd part.

#!/bin/bash
if [ $# -eq 0 ]; then
echo " $(basename $0) <userlist> | Create a user from a file"
exit
fi
case $1 in
userlist)
useradd -s /bin/false $1
exit
;;
*)
echo "File needed"
exit
;;
esac
 
Old 04-01-2015, 01:09 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
please use [code]here comes your code[/code] to keep formatting of your code
So $1 should be a file containing a list of users.
I would do something like this:
Code:
#!/bin/bash
if [ $# -eq 0 ]; then
echo " $(basename $0) <userlist> | Create a user from a file"
exit 1   
fi
userlist="$1" # here userlist is a variable and contains the name of the file you want to use
# check if that file exists
[[ -f "$userlist" ]] || { # in case of error (not found) print some message and exit 
    exit 1
}
# read that file and evaluate its content
Is this what you need?
 
Old 04-01-2015, 01:46 AM   #7
rickricky
LQ Newbie
 
Registered: Mar 2015
Posts: 7

Original Poster
Rep: Reputation: Disabled
Yes Similar to that.

Also can you show me how to execute this script successfully?

I'm not sure of what to add to "add" the user from the file.

Thanks
 
Old 04-01-2015, 02:04 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
how did you execute your own script?
you need to read that file and add users line by line (or at least I think this is what you want). In that case you need to construct a while loop to do the job. You can get some tips here: http://www.linuxquestions.org/questi...y-line-136784/
 
Old 04-06-2015, 12:34 PM   #9
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
you execute the file the same way you execute any file in linux.

chmod +x foo
./foo

will do the trick.
 
  


Reply

Tags
bash scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] Is it possible to create a bash script to create multiple new bash scripts Batistuta_g_2000 Linux - Newbie 6 02-19-2013 11:42 AM
Help me to create a bash script Mr. Alex Linux - Newbie 5 06-14-2011 12:51 AM
Create and run a bash script BallsOfSteel Programming 9 07-04-2008 12:48 AM
Bash Script Help - Trying to create a variable inside script when run. webaccounts Linux - Newbie 1 06-09-2008 02:40 PM
How to create a bash script to count? Alinuxnoob Programming 4 01-28-2007 10:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 08:58 PM.

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