Isla
Isla

Reputation: 3

Bash Script: for loops, read, arrays, compare two files

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

Upvotes: 0

Views: 167

Answers (2)

Kusalananda
Kusalananda

Reputation: 15613

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.

Upvotes: 1

Ronak Patel
Ronak Patel

Reputation: 3849

#!/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

Upvotes: 0

Related Questions