Find the answer to your Linux question:
Results 1 to 5 of 5
I have a script that reads from a wordpress config file, and displays the connection settings for the database. It's for troubleshooting issues, like if they have a db connection ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2010
    Posts
    10

    Bash script help - locate command and variables


    I have a script that reads from a wordpress config file, and displays the connection settings for the database. It's for troubleshooting issues, like if they have a db connection error, just makes it faster to get that password from the config file. With this it gives an error:

    Code:
    PATH=$(locate -n 1 wp-config.php | sed 's/wp-config.php/\n/g' | sed '/wp-config.php/ d' | sed '/^$/ d')
    
    cd $PATH
    echo `pwd`
    The part that parses the file for the information is this:

    Code:
    PASS=$(egrep 'DB_PASSWORD' $PATH/wp-config.php | awk -F "'" '{print $4}')
    And when run it gives this error:

    ./db_portable_2.sh: line 41: egrep: command not found
    ./db_portable_2.sh: line 41: awk: command not found

    I have another version that just uses the path to wp-config.php as a parameter to the script and it works:

    Code:
    PASS=$(egrep 'DB_PASSWORD' $1 | awk -F "'" '{print $4}')

    Any ideas why the one using locate doesn't work? With the locate version, using CD and echoing the pwd command displays the correct path, and with or without it I get that error (of course changing the egrep command to use the correct path with and without using the cd command).

  2. #2
    Linux Enthusiast
    Join Date
    Apr 2004
    Location
    UK
    Posts
    682
    Hi there,

    The PATH variable is used to tell your shell where to look for programs. Check it out by running "echo $PATH" before you run any of your scripts.

    Anyway, once you start your script you are overwriting this path with a reference to your wordpress directory which does not contain any copies of egrep or awk. Because your shell no longer knows anywhere else to look you get the command not found errors.

    Simplest solution is to not use PATH as your variable. "env" should show you all of the variables that are currently used in your environment. Make up anything new and you should be fine.

    Let us know how you get on
    To be good, you must first be bad. "Newbie" is a rank, not a slight.

  3. #3
    Just Joined!
    Join Date
    Dec 2010
    Posts
    10
    That worked, can't believe I forgot about that environment variable. Thanks! Have one more question though, is there a way to use a loop and read to read in from locate (or find) and then put that into an array?

  4. #4
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    You can definitely pass the output of one of those commands in to a loop to process. For example:
    Code:
    IFS=$'\012' # only split on newlines
    
    for file in $(find . -name '*foo*'); do
        # do something with each file
    done
    As for arrays, well, Bash does technically support arrays, but not very well. If at all possible, you should try to do your processing in the loop itself.

    If you need arrays, check out this page:
    Arrays

  5. #5
    Just Joined!
    Join Date
    Dec 2010
    Posts
    10
    Awesome, I'll have to try some of that. However I am having one more problem. The ones I got working are using locate, or using input from the user to find the file. When I did one with find it just hangs after running. Running an strace doesn't really help me as I still can't read it very well.

    Running the first part to get the path to the wp-config.php does work when just run in shell:

    Code:
    [~]# find / -name "wp-config.php" | sed '2,$  d'
    /home/rob/wp-config.php
    However when it's run in the script, it's not outputting anything and running the commands separately doesn't work either for some reason, but I can't figure it out.

    Code:
    [~]# find / -name "wp-config.php" | sed '1,4  d' | sed '2,$ d' | egrep 'DB_USER'
    [~]# egrep 'DB_USER' /home/user/public_html/blog/wp-config.php
    define('DB_USER', 'user_wdps1');
    I did the sed like that as the first result find was finding doesn't have real values in it, but the line itself does work.

    Code:
    root@ast [~]# find / -name "wp-config.php" | sed '1,4  d' | sed '2,$ d'
    /home/user/public_html/blog/wp-config.php
    I can provide an strace if needed. The basics of the code is this:
    Code:
    DIR=$(find / -name "wp-config.php" | sed '2,$  d')
    DBUSER=$(egrep 'DB_USER' $DIR | awk -F "'" '{print $4}')
    Then it echos it out before plugging it in to mysql.

Posting Permissions

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