Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Hello to all, i have been trying to do a shell script to perform a task that seems to be simple, but it is making me pull my hair out ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2005
    Posts
    9

    script to read a file and perform some tasks.


    Hello to all, i have been trying to do a shell script to perform a task that seems to be simple, but it is making me pull my hair out lol as i cant manage to get it (i am really trying but it is going beyond my actual skills).

    the basics.

    I have this command:

    ls -la /home/httpd/vhosts/|grep ^d|grep -v " \."|cut -c 57- > domainlisting.txt

    I need to read every line of that file(every line contains a domain), and with the content of that line create a file in /path/to/newfiles/domain.tld.txt plus that file has to have the domain name inside example:

    domainlisting.txt contains:

    domain1.tld
    domain2.tld
    domani3.tld
    domain4.tld
    domain5.tld
    domain6.tld

    the script should read every line, create a txt file in a path /path/to/file/domain1.tld.txt and inside must contain domain1.tld


    i know that to create a file i need something like $VAR >$VAR.txt and >>$VAR to append the value of var to that file.

    Also i have been reading that i can read lines of the txt file with the read command (cant think on a logical way to use it).

    i would love if some one can give me a hand on this, it doesnt nessesary needs to be a shell script, it can be a command, but i am not getting luck with it.

    regards,

  2. #2
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    Code:
    #!/bin/bash
    for line in `cat domainlisting.txt`
    do
    echo $line > $line.txt
    done

  3. #3
    Just Joined!
    Join Date
    Mar 2005
    Posts
    9
    Quote Originally Posted by lakerdonald
    Code:
    #!/bin/bash
    for line in `cat domainlisting.txt`
    do
    echo $line > $line.txt
    done
    I must say I LOVE YA!

    haha thanks, i was thinking on something like that, the book "Learning the Bash shell from O'reilly" is a good tool what i think is that i was looking the issue too over-complicated which was not.

    thanks a lot man, you saved my day

    regards,

  4. #4
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    No problems, I find for-in loops to be very useful.

  5. #5
    Just Joined!
    Join Date
    Mar 2005
    Posts
    9
    Quote Originally Posted by lakerdonald
    No problems, I find for-in loops to be very useful.
    now so do i , it saved me hours of work, now i will write a script that auto generates the domainlisting.txt file, and generate the txt's on the path they should be, this is to perform selective backups on the box.

    when i have the script done i will post it here as it may help others with my same question and problem.

    regards,

  6. #6
    Just Joined!
    Join Date
    Mar 2005
    Posts
    9

    the Script is as follows

    #!/bin/bash
    ls -la /home/httpd/vhosts/|grep ^d|grep -v " \."|cut -c 57- > /root/backups/domainlisting.txt
    rm -f /root/backups/domains/*
    for line in `cat /root/backups/domainlisting.txt`
    do
    echo $line > /root/backups/domains/$line.txt
    done
    I hope that it works for any other who needs it.

  7. #7
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    This saves wasting that temporary file:
    Code:
    #!/bin/bash 
     for line in `ls -la /home/httpd/vhosts/|grep ^d|grep -v " \."|cut -c 57- ` 
     do 
         echo $line > /root/backups/domains/$line.txt 
     done
    TMTOWTDI

  8. #8
    Just Joined!
    Join Date
    May 2005
    Posts
    1
    Hi,

    I have a question.
    what if there are 2 fields in the text file?

    ie.
    20 dummy1
    25 dummy2
    .. ..

    How to read each line and assign these 2 fields into 2 variables?

    Thank you!

    anderst

  9. #9
    Linux Guru bigtomrodney's Avatar
    Join Date
    Nov 2004
    Location
    Ireland
    Posts
    6,132
    Well a while loop would be better here - though there's enough piping above and I'm knackered so I'll show it simply -
    Code:
    cat TESTFILE | while read LINE
             do echo "$LINE"
             done
    I'll let you guys do all the piping to fit the above example...

  10. #10
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    You'd then pipe $LINE into a cut statement:
    Code:
    echo $LINE | cut -d' ' -f1
    and:
    Code:
    echo $LINE | cut -d' ' -f2
    to get fields one and two respectively.
    To get the value of those, you'd enclose them in ` `'s (backticks)

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •