wp-cli works in windows cmd, but not in Gitbash - cmd

I've installed the wp-cli using Git-bash, created the relevant PATH variables.
I am now able type 'wp' into the Windows CMD and it works, but Git-bash doesn't recognize the command.
What must I do get this working with Git-bash, and why doesn't it work out of the box?

I run into the same issue. For example command "wp cli version" is working in cmd but not in cygwin.
Check following tutorial: https://deluxeblogtips.com/install-wp-cli-windows/
If you are using cygwin, you will need to create another wp file (without .bat) extension. Just name it wp with following content:
#!/usr/bin/env sh
dir=$(d=${0%[/\\]*}; cd "$d"; pwd)
# See if we are running in Cygwin by checking for cygpath program
if command -v 'cygpath' >/dev/null 2>&1; then
# Cygwin paths start with /cygdrive/ which will break windows PHP,
# so we need to translate the dir path to windows format. However
# we could be using cygwin PHP which does not require this, so we
# test if the path to PHP starts with /cygdrive/ rather than /usr/bin
if [[ $(which php) == /cygdrive/* ]]; then
dir=$(cygpath -m $dir);
fi
fi
dir=$(echo $dir | sed 's/ /\ /g')
"${dir}/wp-cli.phar" "$#"

Related

How to get readlink -e on M1 Macs which also works when used in bash scripts?

I am using several bash scripts for build and deployment processes which use readlink with the -e option. Since this option is not available I followed this suggest to install coreutils and create a symbolink between greadlink and readlink.
This worked perfectly on my Intel mac but when I recently switch to M1 mac I realized that the path to greadlink and readlink are changed so I tried this:
ln -s /opt/homebrew/bin/greadlink /usr/bin/readlink
Which gave me an error: Operation not permitted
I realised that this is because of the System Integrity Protection
How can I still use readlink -e in my bash scripts without deactivate the System Integrity Protection?
One approach is to create a script named readlink somewhere in your PATH with the following content.
#!/bin/sh
exec greadlink "$#"
Just make sure that the relative path of script named readlink comes before /usr/bin/ since the system readlink is in /usr/bin when you run:
declare -p PATH
or
echo "$PATH"
An example how to do it:
Create a directory in ~/, name it scripts since it will have script as contents.
mkdir -p ~/scripts
Edit ~/.bashrc to include the created directory in the PATH env variable.
if [[ :$PATH: != *:$HOME/scripts:* ]]; then
PATH=$HOME/scripts:$PATH
fi
Source ~/.bashrc
source ~/.bashrc
Create a script name readlink inside the ~/scripts directory with the following contents:
#!/bin/sh
exec /opt/homebrew/bin/greadlink "$#"
Make it executable
chmod +x ~/scripts/readlink
Check which readlink is the first in PATH
type -a readlink
Output should be something like.
readlink is /home/zlZimon/scripts/readlink
readlink is /usr/bin/readlink
Note that the current work around is for a single user, or rather the user that has scripts directory in PATH, for a system wide approach one can use the path from homebrew or /usr/local/ or whichever default is available for all users.

Why am I getting if: Expression Syntax when I try to run this script?

I am currently trying to run a Unix Executable File in terminal (my shell is TCSH) I downloaded online and I keep getting the following error:
if: Expression Syntax
Here is the script I am trying to run:
if [ -f .1 ]
then
cc -o xrdcalc .source/xrdcalc-1.1.c -lm
chmod 700 xrdcalc
./xrdcalc
else
platform=`uname`
echo
echo
echo "You are using \"xrdcalc\" for the first time on $platform , read the \"Readme.txt\" file and then proceed"
echo
echo
echo "Press enter...."
read char;
echo `date` > .1
mkdir .source
mv xrdcalc-1.1.c .source
cc -o xrdcalc .source/xrdcalc-1.1.c -lm
./xrdcalc
fi
I have little experience with running scripts and I am sure it is an easy fix.
There are other issues with this code that indicate it was coded for traditional sh or bash.
Just put #!/bin/bash as the first line (using the correct path to your system's copy of bash), and it should work without other modifications.
Of course,
chmod 755 scriptName
is also required to "mark" the file as executable and if the file is saved to a directory not in the path, you need to either cd to the correct dir, or invoke as
/full/path/to/scriptName
If you're using a reduced version of Linux that doesn't have bash installed and you can't install it for some reason, then look for other 'Bourne Shell' derived shell processors, like ash, dash, ksh, sh .
IHTH

How do I change Korn(ksh) version dynamically based on platform?

I wanted to use the /usr/bin/ksh93 interpreter on AIX and Linux wherever possible but switch to /usr/bin/ksh where it's not applicable like Mac OS X and wanted the script to be universally compatible in unix. I don't think there is any fallback mechanism in shebang
Since ksh and sh have some syntax in common, you can prefix the start of the
script with a test for ksh or ksh93 in the PATH and rerun the script with
the right interpreter. Replace the #! with the pathname to sh. (Hopefully
it is the same on both machines, or you are back where you started. You can
still try #!/usr/bin/env sh if your env will find the path for you). Add:
#!/bin/sh
if [ "$DONEIT" != true ]
then export DONEIT=true # avoid recursion
if command -v ksh > /dev/null 2>&1
then exec ksh $0 "$#"
else exec ksh93 $0 "$#"
fi
fi
... rest of your script ...
Note: command -v is the POSIX way for finding a command's path.
(Often in these situations, at the installation of a package a script goes
through the #! files and updates the interpreter path to that needed by the
target machine).
Alternatively, you could replace the #! line by any fixed path you control, eg #!/home/user/myksh, and link that file to the right ksh.
You can make a symbolic links.
if [ -f /usr/bin/ksh93 ]; then
ln -s /usr/bin/ksh93 /usr/bin/localksh
else
ln -s /usr/bin/ksh /usr/bin/localksh
fi
The shebang will be #!/usr/bin/localksh.
I would prefer using a normal shebang #!/bin/ksh, but when that one already exists and is the wrong version you will be stuck.

Directory name created with a dot ,using shell script

I am using Cygwin Terminal to run shell to execute shell scripts of my Windows 7 system.
I am creating directory , but it is getting created with a dot in name.
test.sh
#!/bin/bash
echo "Hello World"
temp=$(date '+%d%m%Y')
dirName="Test_$temp"
dirPath=/cygdrive/c/MyFolder/"$dirName"
echo "$dirName"
echo "$dirPath"
mkdir -m 777 $dirPath
on executing sh test.sh its creating folder as Test_26062015 while expectation is Test_26062015.Why are these 3 special charterers coming , how can I correct it
Double quote the $dirPath in the last command and add -p to ignore mkdir failures when the directory already exists: mkdir -m 777 -p "$dirPath". Besides this, take care when combining variables and strings: dirName="Test_${temp}" looks better than dirName="Test_$temp".
Also, use this for static analysis of your scripts.
UPDATE: By analyzing the debug output of sh -x, the issue appeared due to DOS-style line-endings in the OP's script. Converting the file to UNIX format solved the problem.

Bash script not working on a new dedicated server

Recently I have migrated to the new dedicated server which is running on the same operating system - FreeBSD 8.2. I got a root account access and all permissions have been set properly.
My problem is that, the bash script I was running on the old server doesn't works on the new machine, the only error appearing while running the script is:
# sh script.sh
script.sh: 3: Syntax error: word unexpected (expecting ")")
Here is the code itself:
#!/usr/local/bin/bash
PORTS=(7777:GAME 11000:AUTH 12000:DB)
MESSG=""
for i in ${PORTS[#]} ; do
PORT=${i%%:*}
DESC=${i##*:}
CHECK=`sockstat -4 -l | grep :$PORT | awk '{print $3}' | head -1`
if [ "$CHECK" -gt 1 ]; then
echo $DESC[$PORT] "is up ..." $CHECK
else
MESSG=$MESSG"$DESC[$PORT] wylaczony...\n"
if [ "$DESC" == "AUTH" ]; then
MESSG=$MESSG"AUTH is down...\n"
fi
if [ "$DESC" == "GAME" ]; then
MESSG=$MESSG"GAME is down...\n"
fi
if [ "$DESC" == "DB" ]; then
MESSG=$MESSG"DB is down...\n"
fi
fi
done
if [ -n "$MESSG" ]; then
echo -e "Some problems ocurred:\n\n"$MESSG | mail -s "Problems" yet#another.com
fi
I don't really code in bash, so I don't know why this happend...
Bourne shell (sh) doesn't support arrays, that's why you're running into this error when you use
sh script.sh
Use bash instead
bash script.sh
Note: I suspect that sh script.sh worked on the old server because sh is linked to bash there.
also you shouldn't need to run it through sh (that's what the
#!
on the first line is for - the OS will run the remainder of the line as a command and pass the contents of the file for it to interpret). Just make the script executable:
chmod +x script.sh
and then you can just run it directly without the sh in front of the name.
It's possible that the default shell is not bash and so by running it through sh you're interpreting it with a different shell which is then giving the error
The code looks good. It is likely that your new dedicated server is running older version of Bash than your last server. Or maybe /usr/local/bin/bash is pointing towards older version.
Run
$ which bash
if the output is other than /usr/local/bin/bash then change the first shebang line to the newer path, if it still does not work
Try replacing third line:
PORTS=(7777:GAME 11000:AUTH 12000:DB)
with
PORTS=('7777:GAME' '11000:AUTH' '12000:DB')
and rerun the script.
If it still does not work then post the BASH version here by running
$ bash --version
try with facing and trailing spaces
PORTS=( 7777:GAME 11000:AUTH 12000:DB )

Resources