0

am having issue with grep as VESTACP is using it a lot.

i have file mysql.conf

HOST='localhost' USER='root' PASSWORD='xxxxxx' CHARSETS='UTF8,LATIN1,WIN1250,WIN1251,WIN1252,WIN1256,WIN1258,KOI8' MAX_DB='500' U_SYS_USERS='' U_DB_BASES='1' SUSPENDED='no' TIME='05:32:47' DATE='2016-03-20'

now when i run

 echo host_str=$(grep "HOST='$1'" $VESTA/conf/mysql.conf)

i get empty result , although there is HOST in mysql.conf file which i pasted above in code

so any idea whats wrong with it

UPDATE :: Vesta db connect code block

host_str=$(grep "HOST='$1'" $VESTA/conf/mysql.conf)
eval $host_str
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ]; then
    echo "Error: mysql config parsing failed"
    log_event "$E_PARSING" "$EVENT"
    exit $E_PARSING
fi

and i get

Error: mysql config parsing failed

8
  • Are you running this in a script? What do you want to do exactly? Commented Mar 25, 2016 at 14:32
  • 1
    Can you print $1 and check the value? Commented Mar 25, 2016 at 14:32
  • echo $1 empty ................ Commented Mar 25, 2016 at 14:43
  • Well if $1 is empty then you are effectively running grep "HOST=''" $VESTA/conf/mysql.conf command Commented Mar 25, 2016 at 14:46
  • thats strange $1 should contain localhost . as you can see in file its localhost Commented Mar 25, 2016 at 14:47

2 Answers 2

1

$1 is the first parameter of your script.

So, host_str=$(grep "HOST='$1'" $VESTA/conf/mysql.conf) gets the line containing some variables from your file according to your parameter, and eval $host_str sets these variables in your script.

Therefore, your script needs an argument to know which host to look for in your file, in your case, it's localhost, so run: ./yourscript.sh localhost.

Sign up to request clarification or add additional context in comments.

Comments

1

You probably don't want to use $1. Rather try this:

echo host_str=$(grep -o "HOST='[^']*'" $VESTA/conf/mysql.conf)

The [^']* expands to everything that happens to be in between the single quotes. The option -o makes sure you only get the matching string, not the whole line, if that is what you want.

3 Comments

@Aaron: Que? The only version of grep that doesn't consider the pattern as a regex is fgrep, aka grep -F these days. Adding -E makes grep run in egrep mode — with extended regular expressions instead of basic regular expressions.
vestacp need $1 for further operations
Well, but if you keep $1 in the pattern and if it does not expand exactly to what is in the file, your match result will stay empty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.