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 ...
- 12-03-2010 #1Just 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:
The part that parses the file for the information is this: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`
And when run it gives this error:Code:PASS=$(egrep 'DB_PASSWORD' $PATH/wp-config.php | awk -F "'" '{print $4}')
./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).
- 12-04-2010 #2Linux Enthusiast
- Join Date
- Apr 2004
- Location
- UK
- Posts
- 658
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 onTo be good, you must first be bad. "Newbie" is a rank, not a slight.
- 12-05-2010 #3Just 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?
- 12-05-2010 #4
You can definitely pass the output of one of those commands in to a loop to process. For example:
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.Code:IFS=$'\012' # only split on newlines for file in $(find . -name '*foo*'); do # do something with each file done
If you need arrays, check out this page:
ArraysDISTRO=Arch
Registered Linux User #388732
- 12-08-2010 #5Just 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:
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 '2,$ d' /home/rob/wp-config.php
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:[~]# 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 can provide an strace if needed. The basics of the code is this:Code:root@ast [~]# find / -name "wp-config.php" | sed '1,4 d' | sed '2,$ d' /home/user/public_html/blog/wp-config.php
Then it echos it out before plugging it in to mysql.Code:DIR=$(find / -name "wp-config.php" | sed '2,$ d') DBUSER=$(egrep 'DB_USER' $DIR | awk -F "'" '{print $4}')


Reply With Quote