Shell script use two variable in for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script use two variable in for loop
# 1  
Old 12-19-2018
Shell script use two variable in for loop

I want to create a shell script to add a user and modify its comment field mentioned in a file.

1.
File value:-
Code:
 username       comment field value

xyz123            xyztesting
abc123            abctesting
def123            deftesting


2. i am using below loop to create user corresponding to comment field mentioned in file:-


Code:
for i in `cat y | awk '{ print $2 }'`, j in `cat y | awk '{print $1}'`; do echo "useradd $j  -c "\""$i"\""";done


3. Please suggest or modify this loop to get below desired output

Code:
useradd  xyz123  -c  "xyztesting"
useradd  abc123  -c   "abctesting"
 useradd  def123   -c   "deftesting"




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 12-19-2018 at 02:01 PM.. Reason: Added CODE tags.
# 2  
Old 12-19-2018
The 'while read' loop is better in almost every way. It avoids the waste of using cat, and doesn't have the unfortunate side-effects of working behind a pipe (variable changes not preserved in most shells).

Code:
while read USERNAME COMMENT
do
        echo "USERNAME=$USERNAME, comment=$COMMENT"
done < inputfile

You can change the delimiter by changing the IFS variable, i.e. IFS=":" read var1 var2 var3
# 3  
Old 12-19-2018
Welcome to the forum.


Is that homework / classwork? If yes, we have an extra forum for these - please post there.


Your for loop - although syntactically correct - has a logical error in it; it works on this list: comment xyztesting abctesting deftesting, j in username xyz123 abc123 def123 running i through each element of it.
Using awk anyhow in your script, why not have it do ALL the work for you, in one go?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to write a Boolean variable which succeed and failed inside the if loop in shell script ?

I have if loop with multiple variable value check in if loop. How can i print only if loop satisfied variable and its value in shell script ? I dont want to check each variable in if loop. That makes my script larger. if ] then echo "Only satisfied variable with value" ... (3 Replies)
Discussion started by: prince1987
3 Replies

2. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

3. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

4. Shell Programming and Scripting

Does a variable lose its value outside the loop in shell script?

hi, when we assign a variable inside a for loop or while loop in a shell script, does it loses its value after comming out of the loop. i am facing this issue. can anyone help me?? (8 Replies)
Discussion started by: Little
8 Replies

5. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

6. Shell Programming and Scripting

Setting a variable in a while loop (.ksh script)

Hello Everyone, I'm still trying to grasp many concepts in .ksh scripting, one of them being variables inside loops. My problem is the following: * I'm trying to set a variable inside a while read loop to reuse it outside of said loop. My lines are the following :... (13 Replies)
Discussion started by: jimmy75_13
13 Replies

7. Shell Programming and Scripting

Bourne Shell - Problem with while loop variable scope.

Hello I am having issues with a script I'm working on developing on a Solaris machine. The script is intended to find out how many times a particular user (by given userid) has logged into the local system for more than one hour today. Here is my while loop: last $user | grep -v 'sshd'... (7 Replies)
Discussion started by: DaveRich
7 Replies

8. Shell Programming and Scripting

How to give a variable output name in a shell script inside a for loop

Hi all I run my program prog.c in the following way : $ ./prog 1 > output.txt where 1 is a user defined initial value used by the program. But now I want to run it for many a thousand initial values, 1-1000, and store all the outputs in different files. Like $ ./prog 1... (1 Reply)
Discussion started by: alice06
1 Replies

9. Shell Programming and Scripting

Shell script / Grep / Awk to variable and Loop

Hi, I have a text file with data in that I wish to extract, assign to a variable and process through a loop. Kind of the process that I am after: 1: Grep the text file for the values. Currently using: cat /root/test.txt | grep TESTING= | awk -F"=" '{ a = $2 } {print a}' | sort -u ... (0 Replies)
Discussion started by: Spoonless
0 Replies

10. Shell Programming and Scripting

Passing a variable to awk while in a shell for loop

I am a newbie to awk and c programming, however am not a unix newbie. However, I do need help with a kshell script I am writing. It is almost complete, the last step is killing me. Any help would be greatly appreciated. What I am trying to do is cat a text file that has usernames. Then, using... (2 Replies)
Discussion started by: synergy_texas
2 Replies
Login or Register to Ask a Question