warning if(ereg error first then other error - preg-match

Here is my original code listed below.
if(ereg($pattern,strtolower($this->get_domain())) && !ereg("^-|-$",strtolower($this->get_domain())) && !preg_match("/--/",strtolower($this->get_domain()))){
This is the error
Deprecated: Function ereg() is deprecated in
Then I replaced the ereg with preg_match below. I receive this error
if(preg_match($pattern,strtolower($this->get_domain())) && !preg_match("^-|-$",strtolower($this->get_domain())) && !preg_match("/--/",strtolower($this->get_domain()))){
Warning: preg_match() [function.preg-match]: No ending delimiter '^'
found in
I tried to to put / before the ^ and after the $ but still no luck. May I get some assistance from someone who might know how to fix this error.

Add delimiters to your regexes:
$pattern = "/^[a-z".$idn."0-9\-]{3,}$/";// same for "/^[a-z0-9\-]{3,}$/"
// here __^ and here __^
if(preg_match($pattern, strtolower($this->get_domain())) &&
!preg_match("/^-|-$/",strtolower($this->get_domain())) &&
// __^ __^
!preg_match("/--/", strtolower($this->get_domain()))){

Related

In bash, how to pass an optional argument with the current dir as default?

I am trying to define my customized cp function, which is something like
mycp() {
cp -r "$1" "${2:$PWD}"
}
where the second argument is optional and should be the current path by default. However, when I run that it always returns an error of "No such file or directory: ''" when there is no 2nd argument, and bash: 2: <mypath> : syntax error: operand expected (error token is "<mypath>") argument when I passed . as 2nd argument.
What did I miss here?
You can do the following (the main thing missing was the - in :- from "${2:-$PWD}":
mycp() {
cp -r "$1" "${2:-$PWD}"
}

if condition not working comparing item to __ in bash

When I do an echo $item, it gives me the result as:
__
after which I do
if [ "${item}" = "__" ]; then
$item = ""
fi
and again echo $item it still gives me __
and an error : __: command not found
and its not getting changed to empty string, can someone plz help
Hummm... why it should be changed? $item contains __. You compare x$item, which is x__ against __. The strings are different, so you don't enter the if body and the $item variable is not changed.
Code behaves correctly, why do you think it should be different?
UPDATE (after your question rewriting):
There are several errors in the code:
In the condition, you are not using the comparison operator. You are using the assignment operator. Change = for ==.
You are trying to assign a variable usng a dollar sign in the left hand side of the operation. Don't use $ for the LHS.
You are adding spaces to the sides of the assigment operator. Wrong again.
Proper code should be:
if [ "${item}" == "__" ]; then
item=""
fi

Replace javascript code between two patterns using bash

I have the following code:
this.directives.forEach(function (dir) {
var myVar = "hello";
if (control.text !== myVar) {
cleanUp(control);
if (dir)
setUpControl(myVar, dir);
control = dir;
}
});
And need to replace everything that is between: if (control !== myVar) { and }. I have followed this answer and tried the following:
sed -i 's/(if \(control\.text !== myVar\) \{).*?(\})/\1 REPLACEMENT_STRING \2/is' myFile.js
which returns
sed: 1: "s/(if \(control\.text ! ...": RE error: invalid repetition count(s)
You can try this one but it's not really efficient if some '{' appear in your function...
sed '/if (control\.text !== myVar) {/!b;p;s/.*/REPLACEMENT_STRING/p;:A;N;/}/!{s/.*//;bA};D' myFile.js

Bash script operand expected

I'm having a small problem with this following snippet and I'm not sure why. The error given is (line indicated):
*2: syntax error: operand expected (error token is "*2")
while [[ $numberServers -gt $newindex ]]; do
serverPort=$((9001+$(($newindex*2)))) <--- This line
clientPort=$(($serverPort+1))
newindex=$(($newindex+1))
localhostport=$((serverPort-2))
string=$(($string,localhost:$(($serverPort-2))))
...
Any help would be greatly appreciated.
The problem is that the variable newindex is empty, so the expression became:
$((9001+$((*2))))
check the initialization of newindex.
Example:
$ echo $((9001+$(($newindex*2))))
bash: *2: syntax error: operand expected (error token is "*2")
$ newindex=4
$ echo $((9001+$(($newindex*2))))
9009

Bash script, check numeric variable in in range

This is my script:
#!/bin/bash
JOB_NUM=4
function checkJobNumber() {
if (( $JOB_NUM < 1 || $JOB_NUM > 16 )); then
echo "pass"
fi
}
...
checkJobNumber
...
When I try to launch the script I get the message:
./script.sh line 49: ((: < 1 || > 16 : syntax error: operand expected (error token is "< 1 || > 16 ")
(Please notice the spaces in the error message)
I really don't understand what the problem is. If I do the evaluation manually at the command line, it works.
Also, I tried different evaluations like if [[ "$JOB_NUM" -lt 1 -o "$JOB_NUM" gt 16 ]];... still no success.
UPDATE: As suggested I made few more attempts in the rest of the code outside the function call, and I found the problem.
My variables declaration was actually indented this way:
JOB_NUM= 4
THREAD_NUM= 8
.....
VERY_LONG_VAR_NAME= 3
and apparently this hoses the evaulation. BAH! It always worked for me before, so why doesn’t it now?
If I delete the white spaces, the evaluation works:
JOB_NUM=4
THREAD_NUM=8
....
VERY_LONG_VAR_NAME=3
OK I officially hate bash . . . sigh :(
this will work fine
#!/bin/bash
JOB_NUM=7
function checkJobNumber() {
if [ $JOB_NUM -lt 0 ] || [ $JOB_NUM -gt 16 ] ; then
echo "true"
else
echo "false"
fi
}
checkJobNumber
And if you want to check variable during tests you can write in your bash :
set -u
this will generate a message like "JOB_NUM: unbound variable" if variable is not well set

Resources