Help for scripting sh / Add user auto


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help for scripting sh / Add user auto
# 1  
Old 05-21-2010
Help for scripting sh / Add user auto

Hi,

have to make a script that adds users and generates encrypted passwords.
When I run the script I have this error message: ". / Autouser.sh: line 29: $ 1: ambiguous redirect"

Code:
 1 #!/bin/sh
  2 #
  3 # Script de création auto d'utilisateur
  4
  5 if [ -e $1 ]
  6  then
  7
  8         while read ligne
  9                 echo $ligne
 10                 do
 11                         prenom= $(echo $ligne | ligne cut -d: f1)
 12                         nom= $(echo $ligne | ligne cut -d: f2)
 13                         login= $prenom"."$nom
 14                         echo $login
 15                         mdp= $(pwgen 4 1)
 16                         echo  $mdp
 17                         mdpcrypt= $(mkpasswd - S $mdp)
 18                         useradd -p $mdpcrypt  $login
 19                         echo "****************************************"
 20                         echo "    Creation des utilisateurs en cours .... "
 21                         echo "          Veuillez patienter "
 22                         echo "****************************************"
 23                         echo $login ":" $mdp >> comptes.txt
 24         done < $1
 25
 26         else
 27                 echo "Erreur : Le fichier n'existe pas"
 28
 29 fi

I really searched for a solution and I am a beginner ...
Your help would be welcome

Last edited by Helix_87; 05-21-2010 at 07:33 PM.. Reason: Please use code tags
# 2  
Old 05-21-2010
Might be the problem...

Code:
 
  8         while read ligne
  9                 echo $ligne
 10                 do

re-ordered to work...

Code:
 
  8         while read ligne
 10                 do
  9                 echo $ligne

# 3  
Old 05-23-2010
Thank you for your reply.
I tried as you told me but I still have the same error ...

---------- Post updated at 12:51 AM ---------- Previous update was at 12:44 AM ----------

I have this error when I execute the script :

Code:
./autouser.sh: line 31: $1: ambiguous redirect

Code:
  1 #!/bin/sh
  2 #
  3 # Script de création auto d'utilisateur
  4
  5
  6
  7 if [ -e $1 ]
  8  then
  9
 10         while read ligne
 11                 do
 12                 echo $ligne
 13                         prenom= $(echo $ligne | ligne cut -d: f1)
 14                         nom= $(echo $ligne | ligne cut -d: f2)
 15                         login= $prenom"."$nom
 16                         echo $login
 17                         mdp= $(pwgen 4 1)
 18                         echo  $mdp
 19                         mdpcrypt= $(mkpasswd - S $mdp)
 20                         useradd -p $mdpcrypt  $login
 21                         echo "****************************************"
 22                         echo "    Creation des utilisateurs en cours .... "
 23                         echo "          Veuillez patienter "
 24                         echo "****************************************"
 25                         echo $login ":" $mdp >> comptes.txt
 26         done < $1
 27
 28         else
 29                 echo "Erreur : Le fichier n'existe pas"
 30
 31 fi

# 4  
Old 05-23-2010
Hello, Helix, and welcome to the forum:

What's the value of $1? Is there a glob wildcard in it? Is it empty?

The issue almost certainly lies with the end of the while loop, "done < $1". Try double quoting the positional parameter:

Code:
done < "$1"

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 5  
Old 05-23-2010
If I were you I'd first remove/comment lines 13-25 from your last posting and see if that alone would work.
Then I'd uncomment line 13-16 and see if it still works, then 17-18, and so on...

Btw. in following two lines I wonder why you have "ligne" between pipe and cut?
Code:
----------------------------------v
13                         prenom= $(echo $ligne | ligne cut -d: f1)
14                         nom= $(echo $ligne | ligne cut -d: f2)
-------------------------------^

Besides you should avoid whitespaces when you define variables (see the nice "arrows" that I have created).
Also check lines 17 + 19 for that whitespaces.

Last edited by pseudocoder; 05-23-2010 at 08:10 PM..
This User Gave Thanks to pseudocoder For This Post:
# 6  
Old 05-23-2010
Hi Alister,

Thank you for your help.

Quote:
Hello, Helix, and welcome to the forum:

What's the value of $1? Is there a glob wildcard in it? Is it empty?

The issue almost certainly lies with the end of the while loop, "done < $1". Try double quoting the positional parameter:


Code:
done < "$1"
Regards,
Alister
If I add the double quotes have this error message:
Code:
./autouser.sh: line 30: : any file or directory of this type



---------- Post updated at 01:42 AM ---------- Previous update was at 01:31 AM ----------

Hi pseudocoder,
Thank you for your answer.
I tried what you told me but it does not work, always the same error ( ./autouser.sh: line 30: : any file or directory of this type)
# 7  
Old 05-23-2010
How are you invoking autouser.sh? Exactly what are you typing? It looks like $1 is empty. Are you not passing an argument to the script?

Also, pseudocoder's advice regarding the whitespace is correct. The only reason you aren't seeing errors from that is because the redirection error aborts the script before it has a chance to execute those statements. (Nice catch, pseudocoder Smilie).

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Red Hat

Auto Increasing Quota for User.

Hi, I would like to know wheather we can increase the quota for particular user automatically? I am having requirement to increase the quota only for 2 days in a week. but that shoule be automatically instead of manualy modification. is it possible? (3 Replies)
Discussion started by: manoj.solaris
3 Replies

2. OS X (Apple)

Scripting User Account Removal

Ok, so I've been looking all over the place for how exactly to do this and I've become so bombarded with information I finally decided I'll pose the question here. I'm not a programmer or anything that hardcore, but if I see things already implemented and working examples I can easily learn and... (17 Replies)
Discussion started by: panacea
17 Replies

3. Shell Programming and Scripting

Auto answer option in scripting.

Hi masters, How to get auto answer option in scripting. for example in my script i uses the gpg command , in that beginning it will ask passpharse : ( in that i need to type my passpharase) and once i typed it will ask Use this key anyway? ( for that answer will be yes) for... (1 Reply)
Discussion started by: anishkumarv
1 Replies

4. UNIX for Dummies Questions & Answers

Scripting - process and user ids...Help please

Hello all: Working on a job I was asked get a simple script to perform the following task and would like to ask for some help. I'm looking forward to learning more and diving deeper into the World of Open Source servers. I need a script for a Unix server, using as few lines as possible, that... (4 Replies)
Discussion started by: moahten
4 Replies

5. UNIX for Dummies Questions & Answers

scripting for user accounts info in UX

Hi guys, May I know how do I list out in UX for - all existing user accounts; sorted by activated/disabled - primary/sec groups of each user - date when the user accounts are created, - user last logon date - last password change date Anyone had done scripting for these info?... (0 Replies)
Discussion started by: doom10
0 Replies

6. Shell Programming and Scripting

auto login to Super User account

i want to write to script which will login to su account without hving user interaction.( i know Super user password) i wrote following script its also able to log into su account. but seesion gets terminates soon. what can be done ??? or is there any other solution. i don't want to use expect... (6 Replies)
Discussion started by: anup13
6 Replies

7. AIX

AIX; Auto clearing of 'too many invalid login attempts by user'

Does anyone have a good script / cron job that handles this? I have looked in smit and see it is clearing this count with: chsec -f /etc/security/lastlog -a "unsuccessful_login_count=0" -s '{userid}' However when I looked around to find ways to automate this I have not found an easy... (0 Replies)
Discussion started by: Keith Johnson
0 Replies

8. Solaris

Auto Locking user accounts

Hi, I am trying to setup account locking in Solaris 9. I have made the changes in /etc/default/login where RETRIES=5 and SYSLOG_FAILED_LOGINS=5 and in /etc/user_attr I am having: test_user::::lock_after_retries=yes Still I am not able to lock test_user after successive unsuccessful... (1 Reply)
Discussion started by: run_time_error
1 Replies

9. UNIX for Dummies Questions & Answers

freebsd auto login user at boot / i'm stuck

I run freebsd 4.8 and try to find the way or 'a' way to log in a certain user i created automaticaly at pc boot. I have searched the net, the manuals, but found nothing... could anyone please get me going by showing me an example or what file(s) i need to take a look at. i am not working... (6 Replies)
Discussion started by: termiEEE
6 Replies
Login or Register to Ask a Question