Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Shell Script Question Read Parameters from a textfile

  • 17-01-2011 10:25AM
    #1
    Closed Accounts Posts: 144 ✭✭


    Hi all,

    The script below is intended to help in exporting a wordpress website from a staging server to a domain.

    It accepts 2 inputs an old domain and a new domain. These are put in manually at the start of the export process.

    I would like to read in a list of old domains and new domains in the following format
    fish.staging.ie:ilovefish.ie
    cows.staging.ie:thecowssite.com
    love.staging.ie:whatislove.net
    

    and have the process run automatically.

    This is the process as it stands I prompt myself for both values.
    #!/bin/bash
    #Script to alter text in database in order to prepare a 
    #wordpress website for export
    read -p "Enter the old  domain name :   " OLD_DOMAIN
    read -p "Enter the new domain name :   " NEW_DOMAIN
    
    PATH_TO="/supermonkey/newsites/"
    
    /usr/bin/mysqldump  -f -u "DBUSER""$OLD_DOMAIN" -p[PASSWORD] "$OLD_DOMAIN" 
    >""$PATH_TO"/"$NEW_DOMAIN"/"$NEW_DOMAIN".SQL"
    SQL_FILE=""$PATH_TO"/"$NEW_DOMAIN"/"$NEW_DOMAIN".SQL"
    
    
    sed "s|$OLD_DOMAIN|$NEW_DOMAIN|g" "$PATH_TO"/"$OLD_DOMAIN"/"$OLD_DOMAIN".SQL> "$PATH_TO"/"$OLD_DOMAIN"/"$NEW_DOMAIN""01"".SQL"
    
    
    

    I tried this before and it worked but only if the file only contained 1 pair of values. Otherwise the sed happened at what looked like random.
    #!/bin/bash
    
    #Script to read a list of files and convert databases
    while IFS=: read OLD_DOMAIN NEW_DOMAIN
    do  
    
    
    done < /home/supermonkey/allsites/supmdomainlist.txt
    
    


Comments

  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    ~/src $ cat test.txt
    one:two
    three:four
    ~/src $ for i in $(cat test.txt);do first=$(echo $i|cut -d\: -f1);second=$(echo $i |cut -d\: -f2 );echo "First is $first and Second is $second";done;
    First is one and Second is two
    First is three and Second is four
    


  • Closed Accounts Posts: 144 ✭✭supermonkey


    Thanks Skrynesaver,
    The example works.
    Now I have to understand it and relate it to my own script.
    for i in $(cat test.txt);
    do first=$(echo $i|cut -d\: -f1);
    second=$(echo $i |cut -d\: -f2 );
    echo "First is $first and Second is $second";
    done;
    
    for i in $(cat test.txt);
    
    Sets up a for loop.
    How does UNIX know that the counter and the line number are the same?
    Is any file that we cat assumed to contain meaningful records?
    do first=$(echo $i|cut -d\: -f1);
    second=$(echo $i |cut -d\: -f2 );
    
    echo $i gets the counter and therefore the line number
    cut -d\: specifies that the field delimiter is :
    first is assigned to field 1 -f1
    second is assigned to field 2 -f2


  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    Thanks Skrynesaver,
    The example works.
    Now I have to understand it and relate it to my own script.

    :D

    The notion is to treat each line as a record $i then use cut to isolate specific fields using a field separator ":"
    The cut command takes a delimiter and a field to return.
    This was a quick hack, hope it can be integrated :)


  • Closed Accounts Posts: 144 ✭✭supermonkey


    The notion is to treat each line as a record $i then use cut to isolate specific fields using a field separator ":"
    The cut command takes a delimiter and a field to return.
    This was a quick hack, hope it can be integrated :)
    Is each line being a record implicit in when you cat a file? I really have to get a handle on piping!!!:p


  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    Is each line being a record implicit in when you cat a file? I really have to get a handle on piping!!!:p

    There is a shell variable $IFS which is used by the for loop to determine where each record ends.

    It's not a c style for loop, it assigns the value of the current record to $i in the loop,


  • Advertisement
  • Closed Accounts Posts: 144 ✭✭supermonkey


    There is a shell variable $IFS which is used by the for loop to determine where each record ends.
    And that variable is newline by default? Meaning that if I use newline I don't specify $IFS? I really got this wrong. Thought $IFS was the field delimiter no wonder it didn't work!


  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    And that variable is newline by default? Meaning that if I use newline I don't specify $IFS? I really got this wrong. Thought $IFS was the field delimiter no wonder it didn't work!

    IFS (Internal Field Seperator) is the field delimiter, but we're treating the field the for loop isolates as a record.


Advertisement