2 Loops gathering input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting 2 Loops gathering input
# 1  
Old 04-05-2017
2 Loops gathering input

Greetings all,

I have came up with some code, to read an input file and generate a list, here is a sample of what I am working with. The variable $USER will be inputted by the person running the utility.

Code:
 do
 folder=${line%}
 echo "$folder $USER
 done < list.txt

sample of list.txt-

Code:
folder1
folder2
folder3

User inputs-

Code:
Tom

sample of output of running the loop-

Code:
folder1 Tom
folder2 Tom
folder3 Tom

What I want to accomplish, is insted of manually entering a user name, Tom in the example, I want to be able to input a list. Here is the sample output:

List of users-

Code:
Tom
Harry
Sally
Joe

Desired Output

Code:
folder1 Tom
folder2 Tom
folder3 Tom
folder1 Harry
folder2 Harry
folder3 Harry
folder1 Sally
folder2 Sally
folder3 Sally
folder1 Joe
folder2 Joe
folder3 Joe

What is the best way to approach this. I am thinking two loops but I am still not sure. Help is appreciated. Thanks!
# 2  
Old 04-05-2017
2 loops is the way to go ( a loop within a loop...)
# 3  
Old 04-05-2017
I am not exactly sure how to approach this. what if my list folders has a list of 4, and my list of users has 3.. thats where I got stuck doing the loop within a loop.
# 4  
Old 04-05-2017
Maybe if you shown us what you did so far, and the output, we could help you in understanding what isnt working for you and show you the way to go...
# 5  
Old 04-05-2017
Here is what I am thinking..

Code:
do
 folder=${line%}
      user=${line%}
      echo "$folder $user.txt
 done <user.txt
 done < list.txt

# 6  
Old 04-05-2017
that should do it :
Code:
for a in $(cat /tmp/folders); do
        for b in $(cat /tmp/users); do
                echo $a" "$b;
        done;
done

# 7  
Old 04-05-2017
Another thing that I came up with-

Code:
while read -u 3 -r groups.txt && read -u 4 -r folders.txt; do
		group={$line%}
		folder={$line%}
		  echo  "$folder $user"
done 3<list1..txt 4<list2.txt

How do I make sure that group and folder within the loop point to the right text file?

---------- Post updated at 01:18 PM ---------- Previous update was at 01:06 PM ----------

Quote:
Originally Posted by migurus
that should do it :
Code:
for a in $(cat /tmp/folders); do
        for b in $(cat /tmp/users); do
                echo $a" "$b;
        done;
done

is giving me-

Code:
[jeff@server:/home/jeff/test]> ./for.ksh >> for.out
[jeff@server:/home/jeff/test]> cat for.out
P.P..Pfolders.txtProups.txt P.P..Pfolders.txtProups.txt
[jeff@server:/home/jeff/test]>

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Gathering usage statistics

so i have a script that runs across many servers. i'd like to know how many times this script is being used on each server. the only straight forward, non-intrusive way i can think of doing this is to include a line in the script to make a webcall to a central server. and from that central... (9 Replies)
Discussion started by: SkySmart
9 Replies

2. Shell Programming and Scripting

Gathering data in columns from multiple files

Hello world! I need to gather all the data from different folders and copy it in an one unique text file in columns format. Let me explain, letīs say "a, b, c" are 3 data files of thousands and thousands lines (in the reality, I have nearly one hundred). "a, b, c" are located in the folders... (5 Replies)
Discussion started by: user10600
5 Replies

3. UNIX Desktop Questions & Answers

awk using 2 input files instead of while loops

Hi Friends, I have two files as input with data that looks like this: file1.txt 1 2 3 4 file2.txt a,aa b,bb c,cc d,dd e,ee f,ff instead of me doing 2 while loops to get the combinations while read line_file1 (2 Replies)
Discussion started by: kokoro
2 Replies

4. Red Hat

What is the best tools for performance data gathering and analysis?

Dear Guru, IHAC who complaint that his CentOS is getting performance issue. I have to help him out of there. Could you please tell me which tools is better to gathering the whole system performance data? -- CPU/Memory/IO(disk & Network)/swap I would like the tools could be... (6 Replies)
Discussion started by: devyfong
6 Replies

5. Shell Programming and Scripting

Kornshell gathering user input question

Alright I have a function that does a bunch of commands and then I ask the user for what type of node they are on. So determining which node they are on means they will run a different function. Whats the correct syntax for that? function everything { echo "do stuff" }... (10 Replies)
Discussion started by: Blogger11
10 Replies

6. UNIX for Advanced & Expert Users

Gathering data using SAR

Hi everyone, I would like to ask if it is possible to gather SAR data on a specified time. let say yesterdays report, i want to get data at around 12PM and 5PM. thank you. (2 Replies)
Discussion started by: cwiggler
2 Replies

7. HP-UX

HP-UX configuration gathering tool

Hello! I would like to introduce a tool for gathering information on the HP- UX operating system. I would like to hear experts opinions about this utility, its prospects and usefulness. Any feedbacks, suggestions, bug reports, feature requests etc are welcome. The tool project web page:... (0 Replies)
Discussion started by: ahaidukov
0 Replies

8. Shell Programming and Scripting

Data gathering script

I am pretty new at shell scripting, but I managed to make a primative shell script to connect from my webserver (bluehost) to an external data server via ftp or sftp or whatever I need. This script is : #!/bin/sh HOST='ftp.host.com' USER='username' PASSWD='password' FILE='file' ftp -n... (7 Replies)
Discussion started by: mesoeast
7 Replies

9. UNIX for Dummies Questions & Answers

Gathering system configuration

Hi there, I have been asked to write a script that gathers enough information on our Sun Solaris machines to be able to rebuild and configure them if they should go pop. My question is does anybody have any suggestions on the files that I need to take a copy of, to ensure that everything is... (4 Replies)
Discussion started by: hcclnoodles
4 Replies

10. Shell Programming and Scripting

Gathering info on one line

Hi all again, here is the file i am working on : GREIMBAJ00;BAN_CAV;Loader.sh;2003/06/13;17:04:04 GREIMBAJ00;BAN_CAV;Loader.sh;2003/06/13;17:04:06 GREIMBAJ00;BAN_PAK;Loader.sh;2003/06/13;17:04:11 GREIMBAJ00;BAN_PAK;Loader.sh;2003/06/13;17:04:18... (5 Replies)
Discussion started by: HowardIsHigh
5 Replies
Login or Register to Ask a Question