Run codeception on Windows system creates an Error when done like shown in the Quickstart - composer-php

Running this: C:\Projects\HelloWorld>php vendor\bin\codecept generate:cest acceptance createTodo
Results in the Error:
if [ -d /proc/cygdrive ]; then
case $(which php) in
$(readlink -n /proc/cygdrive)/*)
# We are in Cygwin using Windows php, so the path must be translated
dir=$(cygpath -m "$dir");
;;
esac
fi
"${dir}/codecept" "$#"
I just found the answer at the bottom of some git commentsection and wanted to make it more accessible.
https://github.com/Codeception/Codeception/issues/3281
thx test1git1

The fix to run it on windows is to run the .bat file to do all the commands and drop the "php" at the start.
vendor\bin\codecept.bat generate:cest acceptance createTodo

Related

BASH: Determine if script was called from virtual machine (Ubuntu), or the W10 bash app?

Is there a way for a shell script to determine if it is called from a terminal in a virtual machine running Ubuntu, or a W10 terminal using the bash call (installed Ubuntu app in W10)?
I am working in both environments and have a lot of useful shell scripts to make my work more efficient on the virtual machine, e.g. opening specific URLs or running sets of commands. I would like them to work on the Windows side as well. However, my scripts sets up directories which will have to be different on my Windows side.
I have installed the ubuntu app from Windows Store, which allows me to open a bash window and source the files. I could just check if ~ returns an empty string, but is there a more robust way of doing it?
I am running Windows 10, version 17763 and using Ubuntu 18.04 LTS.
E.g.
C:\.sourceThis.sh
#!/bin/bash
myDir="/home/user/stuff"
cdMySub() {
cd "$myDir/$1"
}
I can run this in a Windows terminal by
C:\> bash
USER#XXXX:/mnt/c/$ source ./.sourceThis.sh
USER#XXXX:/mnt/c/$ cdMySub someSubDirectoryName
-bash: cd: /home/user/stuff/someSubDirectoryname: No such file or directory
USER#XXXX:/mnt/c/$ #Fail!
but it does not work, since the Ubuntu file system is different to Windows.
I would like to change .sourceThis.sh to something like
...
if [[ "Something that detects virtual machine" ]] ; then
myDir="/home/user/stuff"
elif [[ "Something that detects 'bash' from Windows prompt" ]] ; then
myDir="/mnt/c/user/stuff"
fi
so that the outcome is instead
C:\> bash
USER#XXXX:/mnt/c/$ source ./.sourceThis.sh
USER#XXXX:/mnt/c/$ cdMySub someSubDirectoryName
USER#XXXX:/mnt/c/stuff/someSubDirectoryName$ #Yeay, success!
EDIT:
I cannot just check for the validity of the default directory, since the scripts create the directory if it does not exist. I want it to point to another default path instead.
I use different user names, so I could check that the output from ~ is the "Windows or VM user".
USER#XXXX:/mnt/c$ echo ~
/home/USER
Thus,
tmpHome=~
if [[ "${tmpHome##*/}" == "USER" ]] ; then
# Windows user
elif [[ "${tmpHome##*/}" == "VM" ]] ; then
# VM user
fi
works for my specific user. However, I suspect that I want to use this on different users (e.g. share it with a colleague). This demands a more robust way.
I am not too experience with Linux. I do not know how to navigate the world of users, processes and tasks, which I suspect can give the answer.
I have used this for a long time successfully:
if [[ "$(uname -r)" == *Microsoft ]]; then
do stuff
fi
You could always use an if condition checking whether the path exists, and run the script from there :
if [[ -f /home/user/stuff ]]; do
script if running on linux
else
script if running on windows
fi
Here the -f flags is a bash condition checking whether the file at specified path exists, returns true if it does. You can add other validations to check whether the file also exists when running on Windows and whatnot.
Bash provides information about the system that is running it it the MACHTYPE, HOSTTYPE, and OSTYPE built-in variables.
Example values for a physical Linux system are:
MACHTYPE=x86_64-redhat-linux-gnu
HOSTTYPE=x86_64
OSTYPE=linux-gnu
Example values for a WSL Linux system are:
MACHTYPE=x86_64-pc-linux-gnu
HOSTTYPE=x86_64
OSTYPE=linux-gnu
One possible way to check if the system is WSL Linux is:
if [[ $MACHTYPE == *-pc-* ]]; then
...
fi
#Dexirian and #Michael Hoffman suggested a method that worked!
For me uname -r returns x.x.x-17763-Microsoft from the Windows prompt and x.x.x-xx-generic on my virtual machine.
Thus,
if [[ "$(uname -r)" =~ "Microsoft" ]] ; then
myDir="/mnt/c/user/stuff"
elseif [[ "$(uname -r)" =~ "generic" ]] ; then
myDir="/home/user/stuff"
fi
works like a charm!

using environment variables in service script

I am facing an issue using environment variables in my service script.
In my services script, i am using an environmental variable i.e. INSTALL_DIR whose value may vary on different system. I have to get the installation directory from $INSTALL_DIR and then i have to start the service. when i am running the service script the environment variable is not sourced at all.
Is it possible to source the installation directory from INSTALL_DIR environment variable. another option i can think is dynamically creating the service script using INSTALL_DIR environment variable.
echo "INSTALL DIR: ${INSTALL_DIR}"
name=`basename $0`
pid_file="/var/run/$name.pid"
get_pid() {
cat "$pid_file"
}
is_running() {
[ -f "$pid_file" ] && ps `get_pid` > /dev/null 2>&1
}
Start()
{
echo "Starting Application"
if is_running; then
echo "[`get_pid`] Already Started"
else
if [ -z "$user" ]; then
nohup $INSTALL_DIR/bin/application 2>&1 &
else
nohup sudo -u "$user" $cmd 1> $INSTALL_DIR/bin/application 2>&1 &
fi
echo $! > "$pid_file"
if ! is_running; then
echo "Unable to start, see logs"
exit 1
fi
echo "[`get_pid`] Started"
fi
}
I am trying to run the application using following command
service application start
In my services script ... I have to get the installation directory from $INSTALL_DIR and then i have to start the service.
Your question isn't really about shell scripting, but about your system's startup. Unfortunately that process varies by Linux distribution, and tends to be poorly documented.
For example, man service says, service runs a System V init script or upstart job in as predictable an environment as possible, removing most environment variables and with the current working directory set to /., but man upstart says:
$ man -k upstart
upstart: nothing appropriate.
Not only that, but the service manpage specifically lists the environment variables a script will start with. Needless to say, yours isn't among them.
The traditional approach to parameterizing startup scripts is to put the information in a known file, normally in /etc, and reference that file in the script. In your case, you could do something like:
INSTALL_DIR=$(cat /etc/my-install-dir.cfg)
and then proceed accordingly.
There might be ways to coerce your startup to support other environment variables. But, sooner or later, the information you need has to be stored somewhere on the filesystem. It seems to me the simplest approach is to reserve a filename to hold that information, and read that file directly.
Use this below code in your script.
if [[ -z "${INSTALL_DIR}" ]]; then
echo "INSTALL_DIR is undefined"
else
INSTALL_DIR=<<your installation directory>>
fi

How to run shell script as a client side hook script for tortoiseSVN?

I have written the shell script and i am trying to put that script as a client side hook script but not getting the script engine which one i should be using to run .sh file.Usually as i have seen .js file will be used as hook script for SVN unfortunately i don't know much about jscript so please help me how to add and run the script in SVN as client side hook script.I have tried using WScipt and CScirpt but both of them are not working for my shell script.
#!/bin/bash
MAIN_DIR="/cygdrive/e/Trunk/COMMON"
FILE_NAME="/cygdrive/e/Trunk_PRE_COMMIT_HOOK/long_path.txt"
lengthy_path=`find ${MAIN_DIR} -regextype posix-extended -regex '.{500,}'| awk -F'Trunk/' '{print $2}' > ${FILE_NAME}`
if [ -f ${FILE_NAME} ]
then
if [ -s ${FILE_NAME} ]
then
echo -e "\n\n\nSorry the path of a file exceeds 256 charectors, please make it shorten and try commiting again.You can see the path in $FILE_NAME"
else
echo -e "\n\n\nPath is perfect code can be committed..........."
fi
else
echo -e "\n\n\nFile not exists............"
fi
You're trying to execute a bash script on Windows, which means you either need Cygwin installed or can use the new bash shell functionality in Windows 10. I have little experience with either, but hopefully I can get you pointed in the right direction.
If you're using Cygwin, use the following command in the Tortoise hook script configuration dialog (Fig. 4.87 in the documentation):
C:\cygwin\bin\bash C:\path\to\your_script.sh
(Sourced from this answer)
If you're using the Windows 10 bash shell, use this command:
bash -c "/mnt/c/path/to/your_script.sh"
(Sourced from this page under "Run Linux Commands From Outside Bash")
Disclaimer: I haven't tested either of these because I don't have the time or means. Try it out, and leave some feedback either way.

Nagios custom plugin calling python Openstack Swift client

I want to check with NAGIOS whether my server can connect to Openstack Swift container. I wrote a simple script where I use Swift Python client to get stat of the container
Script looks like that
#!/bin/bash
set -e
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4
if ! which /usr/bin/swift >/dev/null 2>&1
then
echo "Swift command not found"
exit $STATE_UNKNOWN
fi
my_swift="/usr/bin/swift -V 2.0 -A http://my-swift-domain.com:5000/v2.0/ --insecure --os-username my-user-name --os-password my-password --os-tenant-name tenant-name stat container"
output=`$my_swift | grep Objects | sed 's/Objects:\s*\([0-9]*\).*/\1/'`
if [ "$output" -eq "$output" ] 2>/dev/null
then
echo "successfully connected to swift. Number of objects in container $output";
exit $STATE_OK
else
echo "Number of container objects is not correct";
exit $STATE_CRITICAL
fi
Script has right permissions and NAGIOS is able to run it properly. The script itself called from bash works and returns something like:
successfully connected to swift. Number of objects in container 4973123
But it doesn't when I run it via nrpe. I checked it by running /usr/lib64/nagios/plugins/check_nrpe -H 127.0.0.1 -c check_swift
I just get Number of container objects is not correct
After debugging I'm pretty sure that the command
output=`$my_swift | grep Objects | sed 's/Objects:\s*\([0-9]*\).*/\1/'`
is not even called.
I tried to put swift --version there just to see if it will give me some output and it does. So, it let me think that there is something wrong with parameters but I really don't know what, because the command itself called in a shell works perfectly fine.
Any help appreciated :)
Try to change de first line for this:
#!/usr/bin/env bash
Turns out that it was SELinux (on CentOS) blocking the execution of the command because of the wrong context of the file. I copied the file from home directory to Nagios' plugins directory.
restorecon check_swift_container -v helped

In Mac, how do I determine in a script (sh/bash/applescript) if currently running via Apple Remote Desktop?

I am creating an application installer for Mac. The installer involves getting a code from the user on install. I used an Installer Plugin for the Code Input screen.
I have read (from this link) and verified that plugins do not work in the command line and Apple Remote Desktop. I can check if the installer is running from the command line using a variable ("$COMMAND_LINE_INSTALL").
My questions is, how can I programmatically check if it is running via Apple Remote Desktop?
If pstree is available, you can get a quick ancestry of the current process and see if Apple Remote Desktop is in it, something like pstree -p $$ in bash. Unfortunately, I installed pstree using brew, so this is most likely not going to be available for you, unless you distribute a binary yourself.
The other approach is to walk up the parent yourself. Here is a sample that I tested to be working to check if I am running inside iTerm (I used iTerm as a sample because I don't know what the pstree output would look like when running inside Apple Remote Desktop).
pid=$$
running_in_iterm=0
while [ $pid -ne 1 ]; do
command=$(ps -o command= -p $pid)
case "$command" in
*iTerm*)
running_in_iterm=1
break;;
esac
pid=$(ps -o ppid= -p $pid)
done
if [ $running_in_iterm -eq 1 ]; then
echo "Running in iTerm"
else
echo "Not running in iTerm"
fi
You can try running this script from both the built-in Terminal app as well as iTerm and see the difference.

Resources