Question related to bash. Your help needed.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question related to bash. Your help needed.
# 1  
Old 09-15-2016
Question related to bash. Your help needed.

Dear Team,

i have one question related to automation of adding new users based on txt file.

Here we go:

I have data.txt file which shows:

Code:
bang
dang
benc

One by one, after pressing enter. And now, I need scirpt which will add those users with home directory hidden within the file data.txt. I am working on redhat 7.

Code:
!#/bin/bash

for i in 'data.txt'
do
useradd $i -d /home/$i
done
echo Users created.

Could You help?


Moderator's Comments:
Mod Comment Please use CODE tags (for data as well) as required by forum rules!

Last edited by RudiC; 09-15-2016 at 06:42 PM.. Reason: Added CODE tags.
# 2  
Old 09-15-2016
Code tags for code please.

Code:
while read LINE
do
        echo useradd "$LINE" -d /home/$LINE
done < data.txt

Remove the echo once you've tested and are sure it does what you want.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 09-15-2016
Something wrong again, could You type full body of this sciprt?

I have to execute this like ./scirpt.sh data.txt right?

Very thank You for your help.

Last edited by rbatte1; 09-16-2016 at 04:19 AM.. Reason: Added ICODE tags for inline code.
# 4  
Old 09-15-2016
Code:
#!/bin/sh

while read LINE
do
        echo useradd "$LINE" -d /home/$LINE
done < data.txt

If that doesn't work please tell me in what way it didn't work, not just that it didn't.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 09-15-2016
May i ?
Code:
#!/bin/bash

while read LINE
do
  echo useradd "$LINE" -d /home/$LINE
done < $1

Execute it as
Code:
./script.sh data.txt

This User Gave Thanks to greet_sed For This Post:
# 6  
Old 09-15-2016
Code:
#!/bin/bash  while read LINE do   
echo useradd "$LINE" -d /home/$LINE
useradd "$LINE" -d /home/$LINE
 done < $1

Will work properly. Thanks, but what when i want to put "load" data from data.txt instad of keyword like LIKE???? I mean for example for $i in "here i will put name of the file etc" is that possible? or similar way like i did it within first post?

When it comes to Corona's version

Code:
while read LINE do         
echo useradd "$LINE" -d /home/$LINE
useradd "$LINE" -d /home/$LINE
 done < data.txt

Also works properly. Thank both of You!
# 7  
Old 09-15-2016
Quote:
Originally Posted by protos27
Thanks, but what when i want to put "load" data from data.txt instad of keyword like LIKE???? I mean for example for $i in "here i will put name of the file etc" is that possible?
What you are asking for is a very persistently repeated beginner shell programming mistake called "Dangerous Backticks".

Quote:
(From the URL above)

A special idiom to pay attention to, because it's basically always wrong, is this:

Code:
    	for f in `cat file`; do
    	    ...
    	done

Apart from the classical Useless Use of Cat here, the backticks are outright dangerous, unless you know the result of the backticks is going to be less than or equal to how long a command line your shell can accept. (Actually, this is a kernel limitation. The constant ARG_MAX in your limits.h should tell you how much your own system can take. POSIX requires ARG_MAX to be at least 4,096 bytes.)
It wastes time, memory, external calls, and subshells to slowly and unreliably almost do what you wanted. The read shell builtin actually reads lines, running externals in backticks do not -- they split on any spaces at all.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Bash with problems related to files

Hi unix people, i'm really a newbie and i've created a small bash to process some picture with ImageMagick. I have just some issue and i think this script, if we can help me to correct in right way, could be useful! Basically i have a Eyefi Card who puts files into a folder called "picture"... (1 Reply)
Discussion started by: riccardo
1 Replies

2. Shell Programming and Scripting

awk related question

awk -F ";" 'FNR==NR{a=$1;next} ($2 in a)' server.list datafile | while read line do echo ${line} done when i run the above, i get this: 1 SERVICE NOTIFICATION: nagiosadmin skysmart-01.sky.net .... instead of: SERVICE NOTIFICATION: nagiosadmin skysmart-01.sky.net .... can... (4 Replies)
Discussion started by: SkySmart
4 Replies

3. Shell Programming and Scripting

Idea needed regarding process related script

I want to write a script which will take two time as input parameters and display the details of running processes between the two times, any ideas about the script ? (6 Replies)
Discussion started by: sandip250382
6 Replies

4. UNIX for Dummies Questions & Answers

Question related to 'ps'

If I run a script called 'abc.sh' and then execute the following : ps -ef | grep 'abc.sh' I always get two rows of output, one for the executing script, and the other for the grep command that I have triggered after the pipe. Questions: Why does the second row turn up in the results. My... (10 Replies)
Discussion started by: jawsnnn
10 Replies

5. Shell Programming and Scripting

awk related question

awk "/^<Mar 31, 2012 : /,0" /app/blah.log can someone please help me figure out why the above command isn't pulling anything out from the log? basically, i want it to pull out all records, from the very first line that starts with the date "Mar 31, 2012" and that also has a time immediately... (4 Replies)
Discussion started by: SkySmart
4 Replies

6. Shell Programming and Scripting

having df command related question

Hi All, When i have run the below command its showing 90% which is critical for production. for this i need the answer of some below question please help me for that. 1) i want to delete some unwanted files. how can i know the unwanted files ?Is it there any way of knowing this?? 2)and... (2 Replies)
Discussion started by: aish11
2 Replies

7. UNIX for Dummies Questions & Answers

SSI related Unix In (link) support needed

I have multiple web sites and have SSI all working fine! but I want a script that allows me to place a SSI script (or .html) on any web site within my server - One problem, SSI is limited to files located within the file structure - I want to access a file outside, (but still on my server) I found... (1 Reply)
Discussion started by: johnzule
1 Replies

8. Solaris

RBAC related question.....

I am referring Bill Calkins(SCSA exam prep) for RBAC..actually i wanted to make a normal user to get the privilege to run a command through authorization, not through profile files... This is the exact steps given by Bill calkins.. 1.roleadd -m -d /export/home/adminusr -c... (11 Replies)
Discussion started by: saagar
11 Replies

9. UNIX for Advanced & Expert Users

One Question related to alias

Hello, I have created following alias in csh lab 'rlogin -l user23 complab23' but problem is complab23 does not allow automatic login by checking .rhosts file. So after typing lab on command line I have to type complicate password and if wrong password is typed thrice then account gets... (4 Replies)
Discussion started by: neerajrathi2
4 Replies

10. Programming

signals related question

Hi all, Just a little question relative to signals. I know that if an application is in the sleep state, When a signal is catched, it will be processed by the handler. But what happens if it's processing something? Does the processing stops?? The following code should illustrate this case ... (2 Replies)
Discussion started by: ninjanesto
2 Replies
Login or Register to Ask a Question