LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-08-2011, 01:32 AM   #1
bartsimpsong
LQ Newbie
 
Registered: Nov 2007
Posts: 8

Rep: Reputation: 0
A script that creates user accounts with c-shell


Ok, My problem is that everytime I try to run this script (accounts.csh)

./accounts.csh f 101 103
-103: No such file or Directory

./accounts.csh f 101 105
-105: No such file or Directory

./accounts.csh f 101 108
-108: No such file or Directory

So, this is what I have so far.

#!/bin/csh
set letter=$1
set start=$2
set end=$3
set password="foobar"
set group=10
set path=/export/home
set shell=/bin/sh

while $start<-$end
set logine=$letter
set logine=$logine$start
echo "$logine":"$password":"$start":"$group":"$logine":"$path/$logine":"$shell">>/etc/passwd

pwconv
mkdir /export/home/$logine
cp /etc/skel/local.cshrc /export/home/$logine/.cshrc
cp /etc/skel/local.profile /export/home/$logine/.profile
cp /etc/skel/local.login /export/home/$logine/.login

chown -R $start:$group /export/home/$logine

$start++

Any help is appreciated. Thank you
 
Old 10-08-2011, 11:29 AM   #2
cheesus
Member
 
Registered: Jan 2005
Location: Munich, Germany
Distribution: SuSE
Posts: 184

Rep: Reputation: 25
> while $start<-$end
this is a command to read input from the contents of a file whose name is -$end, in ths case -103.

in csh, "Expressions" are put in round panthesis ()

Cheers, Tom.
 
1 members found this post helpful.
Old 10-09-2011, 03:01 AM   #3
bartsimpsong
LQ Newbie
 
Registered: Nov 2007
Posts: 8

Original Poster
Rep: Reputation: 0
Smile Great!!!

Thank you very much Mr. Cheesus that fixed the problem and then I needed to add the "end", but for some reason it is not creating the accounts. I was thinking that there is a command that pipes the error messages into a file I am not sure about that command but I believe is something like
./accounts.csh f 101 103 >> 0 /home/user/Desktop/file.txt something like that, can anybody confirm that please? other wise I will ask google, thank you.
 
Old 10-09-2011, 08:39 AM   #4
cheesus
Member
 
Registered: Jan 2005
Location: Munich, Germany
Distribution: SuSE
Posts: 184

Rep: Reputation: 25
---DELETED BECAUSE IT WAS TERRIBLY WRONG---
But be careful, do not destroy your /etc/passwd, or you will never again log in ;-)
Better use useradd, adduser, or similar, depending on your distribution.
Cheers!

Last edited by cheesus; 10-09-2011 at 04:21 PM.
 
Old 10-09-2011, 04:19 PM   #5
cheesus
Member
 
Registered: Jan 2005
Location: Munich, Germany
Distribution: SuSE
Posts: 184

Rep: Reputation: 25
I swear I was sober when I wrote the above, but it's of course terribly wrong.
">" is write to stdout
">>" is append to stdout
"2>" is write to stderr
"2>>" is append to stderr
so actually ">>/etc/passwd" should have worked,
if you ran the script as user root
and if your system doesn't use /etc/shadow
but of course useradd, useradd or similar is still a good idea.
Cheers, Tom.
 
Old 10-10-2011, 04:03 AM   #6
bartsimpsong
LQ Newbie
 
Registered: Nov 2007
Posts: 8

Original Poster
Rep: Reputation: 0
I didn't have any problem with the passwd/shadow files. But I am wondering if I am concatenating correctly the line of
logine=$letter
logine=$logine$start
Is this correct? Thank you for your help.
 
Old 10-10-2011, 04:14 AM   #7
bartsimpsong
LQ Newbie
 
Registered: Nov 2007
Posts: 8

Original Poster
Rep: Reputation: 0
Ok I found out that in order to concatenate I just need to put one variable next to the other such as in this exmple.

There is no concatenation operator. To concatenate strings, put them one next to the other.
set newstring = aaa$string1${string2}bbb

So, what I don't understand is why when I run the script as :
./accounts f 101 103 2> errors
$cat errors
cat: f: No such file or directory
cat: 101: No such file or directory
cat: 103: No such file or directory

Not sure why these lines
 
Old 10-10-2011, 05:14 AM   #8
cheesus
Member
 
Registered: Jan 2005
Location: Munich, Germany
Distribution: SuSE
Posts: 184

Rep: Reputation: 25
Well, since f/101/103 are your params, you're probably executing $1/$2/$3.
Actually, I thougt putting the paranthesis () to while would solve that.
I any case, you need to post the current version of your script...
 
Old 10-10-2011, 04:37 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,258

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Is there a reason why it HAS to be csh ?
http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/
http://www.grymoire.com/Unix/CshTop10.txt
 
Old 10-11-2011, 12:39 AM   #10
bartsimpsong
LQ Newbie
 
Registered: Nov 2007
Posts: 8

Original Poster
Rep: Reputation: 0
It has to be ksh, csh, or any other BUT bash. Rules of assignment. Yes this is the latest script.

#!/bin/csh
set letter=$1
set start=$2
set end=$3
set password="foobar"
set group=10
set path=/export/home
set shell=/bin/sh
set logine=$letter$start

while ( $start -le $end )
##set logine=$letter$start
echo "$logine":"$password":"$start":"$group":"$logine":"$path/$logine":"$shell">>/etc/passwd

pwconv
mkdir /export/home/$logine
cp /etc/skel/local.cshrc /export/home/$logine/.cshrc
cp /etc/skel/local.profile /export/home/$logine/.profile
cp /etc/skel/local.login /export/home/$logine/.login

chown -R $start:$group /export/home/$logine

$start++
end

As I have been working on the script I found this command which tells me what lines work.
So now when I type: csh -x ./accounts.csh f 101 103
shows:
set letter=f
set start=101
set end=103
set password=foobar
set group=10
set path=/export/home
set shell=/bin/sh
set logine=f101
while ( 101 -le 103 )
while: Expression Syntax.

Which means that there is a problem with the syntax of the while loop, but I have been looking and I see no problem with the while loop. I will try to do it with ksh and see if it works.
 
Old 10-11-2011, 02:36 AM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,258

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
You'll find this useful http://kornshell.com/doc/
 
Old 10-11-2011, 03:25 AM   #12
cheesus
Member
 
Registered: Jan 2005
Location: Munich, Germany
Distribution: SuSE
Posts: 184

Rep: Reputation: 25
Hmm, after all that posts, you have not even looked up the syntax of a csh while loop !?!?

http://lmgtfy.com/?q=csh+while+loop+example+

See e.g. Example 3 at
http://www.bo.infn.it/alice/alice-do...de/node36.html

Cheers, Tom.
 
Old 10-12-2011, 12:14 AM   #13
bartsimpsong
LQ Newbie
 
Registered: Nov 2007
Posts: 8

Original Poster
Rep: Reputation: 0
Well, I have checked every line of the script and browse the internet to look up for the syntax of the while loop for csh and I couldn't find any thing that would help me. Yes I found many places that showed me how the syntax goes but nothing different to what I had. So, what I decided to do is to re-do it in korn shell and it seems that it worked now. I just have to wait to go to the class room and run it, but it works in my machine. Thank you for your help cheesus & Chrism01. I will post my script as soon as I finish the test of Calculus tomorrow.

Last edited by bartsimpsong; 10-12-2011 at 12:28 AM.
 
Old 10-13-2011, 01:55 AM   #14
bartsimpsong
LQ Newbie
 
Registered: Nov 2007
Posts: 8

Original Poster
Rep: Reputation: 0
Ok this works. Copy and paste to create accounts. To execute the script would be: ./script.ksh f 101 105

#!/bin/ksh
letter=$1
start=$2
end=$3
password="Kz5kAMNh47sHk"
group=10
path=/export/home
shell=/bin/sh

while [[ $start -le $end ]]; do
logine=$letter$start
echo "$logine":"$password":"$start":"$group":"$logine":"$path/$logine":"$shell">>/etc/passwd
pwconv
mkdir /export/home/$logine
cp /etc/skel/local.cshrc /export/home/$logine/.cshrc
cp /etc/skel/local.profile /export/home/$logine/.profile
cp /etc/skel/local.login /export/home/$logine/.login

chown -R $start:$group /export/home/$logine
((start+=1))
done

Cheers!!!
 
  


Reply


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
Shell script to find enabled system accounts logicalfuzz Linux - General 3 01-29-2010 09:52 AM
bash/php script which creates linux user... tifoso Linux - General 1 11-05-2009 07:18 AM
Create user accounts from shell script? maheshkodamati Linux - Newbie 1 02-26-2008 12:46 PM
Looking to Create User Accounts Using Batch Load Script wlue Linux - Newbie 3 12-17-2007 02:51 PM
Request : set passwords for many users [user accounts exist] using a shell script bv_uma Linux - Software 3 08-19-2006 10:01 AM


All times are GMT -5. The time now is 03:30 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration