Use bash to read a file and then execute a command from the words extracted

63,657

Solution 1

You can use the "for" loop to do this. something like..

for WORD in `cat FILE`
do
   echo $WORD
   command $WORD > $WORD
done

Solution 2

normally i would ask what have you tried.

while read -r line 
do 
   command ${line} > ${line}.txt
done< "file"

Solution 3

IFS=$'\n';for line in `cat FILEPATH`; do command ${line} > ${line}; done
Share:
63,657
user191960
Author by

user191960

Updated on July 09, 2022

Comments

  • user191960
    user191960 almost 2 years

    FILE:

    hello
    world
    

    I would like to use a scripting language (BASH) to execute a command that reads each WORD in the FILE above and then plugs it into a command.

    It then loops to the next word in the list (each word on new line). It stops when it reaches the end of the FILE.


    Progression would be similar to this:

    Read first WORD from FILE above

    Plug word into command

    command WORD > WORD
    
    • which will output it to a text file; with word as the name of the file.

    Repeat this process, but with next to nth WORD (each on a new line).

    Terminate process upon reaching the end of FILE above.


    Result of BASH command on FILE above:

    hello:

    RESULT OF COMMAND UPON WORD hello
    

    world:

    RESULT OF COMMAND UPON WORD world