0

I'm trying to do something like this.

I need every entry/user in file1 to be queried against every entry in file2 independently.

> cat file1
john 
james
mark
adam
luke
chris
scott

> cat file2
check if **user** has account
check if **user** has permission
check if **user** has website
check if **user** has root

So basically read line from file1 one by one, but execute against all entries in file2. So john is checked against all four entries, then james and so on.

Do i assign variables to every user? then how will I define that them in file2 also the lists/files are likely to fluctuate in content/size so would like to accommodate that change..

Thanks guys! Isl

Ronak Patel
  • 3,819
  • 1
  • 16
  • 29
Isla
  • 3
  • 2
  • Given the two files, as they are currently presented, what is your expected output? – Kusalananda Jul 28 '16 at 17:25
  • Been looking at these: [link](http://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash) but not what I'm looking for :( – Isla Jul 28 '16 at 17:25
  • Hey Kusalananda, basically Ive tried to simplify it. file1 contains a host list, whereas file2 will be a series of commands/scripts that will be executed for each host. I need to read every host - line in file1 against every script in file2. Hope that helps! Isla – Isla Jul 28 '16 at 17:28
  • i.e users will need to be variables I guess and run against every entry statement in file2 – Isla Jul 28 '16 at 17:34

2 Answers2

1

Put the collection of commands that you need to run for each word from the first file into a script, read the file with words line by line and execute the commands for the currently read word:

while read -r word; do
   some command using "$word"
   some other command using "$word"
   # etc.
done <file_with_words

What's basically happening here is that I'm asking you to turn your second file into a script with a loop.

As per your comment, the first file actually contains hostnames, and the second file contains commands to run against those hostnames. What you're asking in the question is to create and execute a new script for each hostname in the first file. This makes little sense as the script is, well, already a script (it also sounds terribly fragile and might pose a security risk if the input is not properly handled). Instead, modify it to read in the hostnames as per my code above.

Kusalananda
  • 14,885
  • 3
  • 41
  • 52
  • Thank you for your quick response Kusalananda, on my way home and will let you know how I get on! I cant upvote but will here ^^^ :) – Isla Jul 28 '16 at 17:47
  • Hey, can you let me know what you mean by "second file into a script with a loop" please. like where do I add "for i in `cat file2`; do "? – Isla Jul 28 '16 at 17:57
  • @Isla You don't, that's my point. You already have a script, your second file. Just modify it by adding a loop reading the words from the first file. Replace the hostnames with `"$word`". – Kusalananda Jul 28 '16 at 18:02
  • @Isla I'm assuming that what you were trying for was to _create a new script_ for each hostname read from the first file, but that doesn't make sense. It's a script already, just make it read the hostnames from the first file and do things with them. – Kusalananda Jul 28 '16 at 18:04
  • Thanks Kusalananda, you are a good teacher, will try later and let you know. Do you recommend any reading material? Isl – Isla Jul 28 '16 at 18:07
  • @Isla Well, thanks. I'd recommend reading questions and answers here and on the [Unix & Linux](http://unix.stackexchange.com/) forum, especially the ones tagged with the `shell` and `bash` tags. You also learn a lot by trying to solve people's problems for yourself and then comparing with the accepted answers. – Kusalananda Jul 28 '16 at 18:14
0
#!/bin/bash

while read user
do
        while read action
        do
                ## do your stuff here
                echo "$user:  ${action//user/$user}"

        done <  /home/user123/file2.txt

done <  /home/user123/file1.txt

After running this script as:

> ./test.sh
john: check if **john** has account
john: check if **john** has permission
john: check if **john** has website
john: check if **john** has root
james: check if **james** has account
james: check if **james** has permission
james: check if **james** has website
james: check if **james** has root
mark: check if **mark** has account
mark: check if **mark** has permission
mark: check if **mark** has website
mark: check if **mark** has root
adam: check if **adam** has account
adam: check if **adam** has permission
adam: check if **adam** has website
adam: check if **adam** has root
luke: check if **luke** has account
luke: check if **luke** has permission
luke: check if **luke** has website
luke: check if **luke** has root
chris: check if **chris** has account
chris: check if **chris** has permission
chris: check if **chris** has website
chris: check if **chris** has root
scott: check if **scott** has account
scott: check if **scott** has permission
scott: check if **scott** has website
scott: check if **scott** has root
Ronak Patel
  • 3,819
  • 1
  • 16
  • 29