How to run WP-CLI using bash script in linux - linux

I'm having issue to run bash script setting up WP-CLI by it's own. Keep on getting wp no command found error. Please help.
#!/bin/bash
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
mv wp-cli.phar /usr/local/bin/wp
exec bash
wp --info
wp plugin install taxonomy-terms-order --path=/var/www
wp plugin activate taxonomy-terms-order --path=/var/www
It's only running till exec bash line. after that its not installing any plugin. Please help.

Do not hesitate to make small experiments to understand the problem:
$ cat test.sh
#!/bin/bash
echo "Test 1"
exec bash
echo "Test 2"
$ echo $$
6506
$ ./test.sh
Test 1
$ echo $$
6548
exec bash is opening a new blocking process.
So, I think you can remove this line from your script.
If /usr/local/bin is not in your PATH, you can use the complete path of /usr/local/bin/wp instead of wp :
/usr/local/bin/wp --info
/usr/local/bin/wp plugin install taxonomy-terms-order --path=/var/www
/usr/local/bin/wp plugin activate taxonomy-terms-order --path=/var/www
Or you can add this path to the PATH:
export PATH="${PATH}:/usr/local/bin/wp"

Related

Linux shell script "unable to access jarfile"

I have a jar file in the /root directory of a debian 11 VPS. I am having trouble creating a startup shell script.
The contents of the script (/etc/init.d/runjar.sh) are as follows:
#!/bin/sh
echo "Running Jar"
java -jar /root/bot.jar
exit 0
I had ran both "chmod +x /etc/init.d/runjar.sh" and "update-rc.d runjar.sh defaults". When I restarted the VPS, the jar did not run.
I tried running the script through the terminal "sh /etc/init.d/runjar.sh" and was met with the response:
root#api:~# sh /etc/init.d/runjar.sh
: not found/runjar.sh: 2:
Running Jar
Error: Unable to access jarfile /root/bot.jar
: not found/runjar.sh: 5:
I have made sure the permissions were set using "chmod +x /root/bot.jar" and "chmod 777 /root" to no avail.
Any help would be appreciated.
Instead of using sh you can use the service command because the runjar.sh is palced in the init.d folder.
1. run a new script for a test
echo -e '#!/bin/sh\necho "Running Jar"\ndate && echo "successful"' > /etc/init.d/runjar.sh
sh /etc/init.d/runjar.sh
if output info is successful,then it proved the env of shell is fine
2. rewrite your script
echo -e '#!/bin/sh\necho "Running Jar"\njava -jar /root/bot.jar\nexit 0' > /etc/init.d/runjar.sh
chmod +x /etc/init.d/runjar.sh
sh /etc/init.d/runjar.sh
and check the output info.

"This script must be sourced, not executed. Run it like: source /bin/bash"

I am running a docker container, in which I am trying to source a .sh file.
To reproduce the experience, if you have docker, it's very easy:
$ docker run -i -t conda/miniconda3 /bin/bash
# apt-get update
# apt-get install git
# git clone https://github.com/guicho271828/latplan.git
# cd latplan
# source ./install.sh
Doing this, gives this error:
This script must be sourced, not executed. Run it like: source /bin/bash
I have looked on other posts but I could not find a solution.
Any idea?
Many thanks!
[EDIT]
This is the begining of the install.sh file:
#!/bin/bash
env=latplan
# execute it in a subshell so that set -e stops on error, but does not exit the parent shell.
(
set -e
(conda activate >/dev/null 2>/dev/null) || {
echo "This script must be sourced, not executed. Run it like: source $0"
exit 1
}
conda env create -n $env -f environment.yml || {
echo "installation failed; cleaning up"
conda env remove -n $env
exit 1
}
conda activate $env
git submodule update --init --recursive
ok, I just removed the 1st checking, and it's working fine

Shell Script won't run from NodeJS because it has "set -o pipefail" [duplicate]

The below mentioned line of code used to work for me all the time on a Ubuntu 16.04 distribution, but suddenly option-name pipefail is an illegal option:
set -eu -o pipefail
returns:
set: Illegal option -o pipefail
Why does this happen? I run the command on a completely new installed system and as part of a shell script. The code is placed right at the beginning:
myscript.sh:
1 #!/bin/bash
2 set -eu -o pipefail
3 ...
The script is run as sudo:
sudo sh ./myscript.sh
You are running bin/sh, on Ubuntu it is a symbolic link pointing to /bin/dash, but pipefail is a bashism.
Make the script executable:
chmod +x myscript.sh
and then run the script as follows:
sudo ./myscript.sh
I had the same error when running script from zsh and the script began with incorrect shebang.
WRONG, missing ! after #:
#/bin/bash
rest-of-the-script
Correct:
#!/bin/bash
rest-of-the-script

How to make a bash script to run fakeroot and exit from fakeroot?

I have following bash script, I want to enter to fakeroot environment, then run other commands, then exit, which I can run my laptop ubuntu terminal.
$ cat test_root.sh
#!/bin/bash
set -u
set -e
fakeroot
echo "my $UID"
exit
But that script won't work, appreciate your clues to fix it.
$ ./test_root.sh
root\#
root\# exit
exit
my 1000
Thank you.
Kind regards,
From man fakeroot it's fakeroot -- command
$ fakeroot sh -c 'echo "my $UID"'
my 0

some error with the 'ln' scripts

anybody is familiar with the etcd project? Or we'd better forget the project when talk about this issue. The issue is
$ build
ln: `gopath/src/github.com/coreos/etcd': cannot overwrite directory
when exec the build shell
and the content is:
#!/bin/sh -e
if [ ! -h gopath/src/github.com/coreos/etcd ]; then
mkdir -p gopath/src/github.com/coreos/
ln -s ../../../.. gopath/src/github.com/coreos/etcd
fi
export GOBIN=${PWD}/bin
export GOPATH=${PWD}/gopath
export GOFMTPATH="./bench ./config ./discovery ./etcd ./error ./http ./log main.go ./metrics ./mod ./server ./store ./tests"
# Don't surprise user by formatting their codes by stealth
if [ "--fmt" = "$1" ]; then
gofmt -s -w -l $GOFMTPATH
fi
go install github.com/coreos/etcd
go install github.com/coreos/etcd/bench
Some addition:
My system is windows 7
I run the shell on git bash.
issue reproduce:
step1: open the git bash
step2: git clone git#github.com:coreos/etcd.git
step3: cd etcd
step4: build
As mentioned in "Git Bash Shell fails to create symbolic links" (since you are using the script in a git bash on Windows 7)
the ln that shipped with msysGit simply tries to copy its arguments, rather than fiddle with links. This is because links only work (sort of) on NTFS filesystems, and the MSYS team didn't want to reimplement ln.
A workaround is to run mklink from Bash.
This also allows you to create either a Symlink or a Junction.
So 'ln' wouldn't work as expected by default, in the old shell that ships with Git for Windows.
Here's solution. Tbh it is a workaround, but since you're on Windows, I don't see another way.
Start a command line, and enter there to the directory with the script. There should be a path gopath/src/github.com/coreos/ (if no such a path, you must create it). Next issue a command
mklink /D "gopath/src/github.com/coreos/etcd" "../../../../"
Next you should edit the build script to delete a lines with creation symlink and a directory. E.g.
#!/bin/sh -e
export GOBIN=${PWD}/bin
export GOPATH=${PWD}/gopath
export GOFMTPATH="./bench ./config ./discovery ./etcd ./error ./http ./log main.go ./metrics ./mod ./server ./store ./tests"
# Don't surprise user by formatting their codes by stealth
if [ "--fmt" = "$1" ]; then
gofmt -s -w -l $GOFMTPATH
fi
go install github.com/coreos/etcd
go install github.com/coreos/etcd/bench
Note, that I am just removed 4 lines of code. Next you run the script, and this should work.
You shouldn't be using git clone and the build sh script. Use the go get command. For example, on Windows 7,
Microsoft Windows [Version 6.1.7601]
C:\>set gopath
GOPATH=C:\gopath
C:\>go version
go version go1.3 windows/amd64
C:\>go get -v -u github.com/coreos/etcd
github.com/coreos/etcd (download)
github.com/coreos/etcd/third_party/bitbucket.org/kardianos/osext
github.com/coreos/etcd/pkg/strings
github.com/coreos/etcd/error
github.com/coreos/etcd/third_party/github.com/coreos/go-etcd/etcd
github.com/coreos/etcd/http
github.com/coreos/etcd/third_party/github.com/coreos/go-log/log
github.com/coreos/etcd/third_party/github.com/rcrowley/go-metrics
github.com/coreos/etcd/mod/dashboard/resources
github.com/coreos/etcd/log
github.com/coreos/etcd/third_party/github.com/gorilla/context
github.com/coreos/etcd/third_party/github.com/gorilla/mux
github.com/coreos/etcd/mod/dashboard
github.com/coreos/etcd/discovery
github.com/coreos/etcd/pkg/btrfs
github.com/coreos/etcd/pkg/http
github.com/coreos/etcd/third_party/code.google.com/p/gogoprotobuf/proto
github.com/coreos/etcd/mod/leader/v2
github.com/coreos/etcd/mod/lock/v2
github.com/coreos/etcd/metrics
github.com/coreos/etcd/third_party/github.com/mreiferson/go-httpclient
github.com/coreos/etcd/mod
github.com/coreos/etcd/third_party/github.com/BurntSushi/toml
github.com/coreos/etcd/third_party/github.com/goraft/raft/protobuf
github.com/coreos/etcd/third_party/github.com/goraft/raft
github.com/coreos/etcd/store
github.com/coreos/etcd/server/v1
github.com/coreos/etcd/server/v2
github.com/coreos/etcd/store/v2
github.com/coreos/etcd/server
github.com/coreos/etcd/config
github.com/coreos/etcd/etcd
github.com/coreos/etcd
C:\>

Resources