Trying to figure out operating system to run a command in my package. Found this :
case "$OSTYPE" in
solaris*) echo "SOLARIS" ;;
darwin*) echo "OSX" ;;
linux*) echo "LINUX" ;;
bsd*) echo "BSD" ;;
msys*) echo "WINDOWS" ;;
*) echo "unknown: $OSTYPE" ;;
esac
I receive : The term 'case' is not recognized as the name of a cmdlet, function, script file, or operable program
But if I run this within a file "sh test.sh" it shows the correct system to terminal.
My operating system is windows. The question is why is the error being received when i run in the terminal and not from the file? And is there any way a case command to run in a script to figure out the operating system?
It looks like that comes out of a UNIX shell script.
You are running windows.
if you are trying to run that thing with cmd, you're gonna have a bad time.
However, you could install a copy bin /bin/sh on your windows box. Such miracles happen these days. Even the crippled operating systems can have a decent shell!
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
I'm using emacs under Ubuntu
If I want to edit a.txt then I either click on the emacs icon or use
$ emacs --geometry 10x10 --fullscreen --no-splash ~/a.txt &
from bash. My .emacs file starts the emacs server.
If I then want to edit another file from the command line, I use
$ emacsclient -n ~/b.txt
to load the file into the existing emacs.
but I keep getting it wrong, and all hell breaks loose in various ways.
How would I make a bash command 'e', that checks whether the emacs server is already running, and executes the appropriate command?
Attempts to use the emacsclient -a switch invariably produce undesirable and ill-determined behaviour.
Extra points if it can 'do the right thing' when run on a console too.
So far this function definition in .bashrc seems to be a perfect solution:
function e #open emacs in background and disown it, or if already running use emacsclient
{
echo "emacs function backgrounds and disowns emacs, or calls client if server already running; see .bashrc";
local FLUFFY="$#";
local SERVERSOCKET=/tmp/emacs${UID}/server ;
echo "trying to open: " $FLUFFY
echo " checking: " $SERVERSOCKET "for emacs server " ;
# test for existence of emacs server socket
if [ -e $SERVERSOCKET ]; then
echo "using emacsclient"
emacsclient -n $FLUFFY;
else
echo "starting emacs: make tea..."
emacs --geometry 10x10 --fullscreen --no-splash $FLUFFY & disown ;
fi;
}
which was derived from this incantation:
FLUFFY=~/b.txt ; if [ -e /tmp/emacs1000/server ]; then emacsclient -n $FLUFFY; else emacs --geometry 10x10 --fullscreen --no-splash $FLUFFY & fi;
which does what I want by checking for the existence of the emacs server socket for user 1000.
I have a bash script which calls another bash script, like so:
#!/bin/bash
echo "Hi"
./script-two.sh
echo "Hello!"
The problem that I have is that it never makes it to printing "Hello!"
I think this is because ./script-two.sh (Which I did not write) is somehow exiting or changing the shell. I have included this script at the end of this post.
Is there a way I can gurentee that my execution will continue after script-two.sh executes?
I have looked into using the trap command, but I don't fully understand its use properly.
Thanks,
Casey
Here is the contents of what would be script-two.sh
#!/bin/sh
# This file is part of the DITA Open Toolkit project hosted on
# Sourceforge.net. See the accompanying license.txt file for
# applicable licenses.
# (c) Copyright IBM Corp. 2006 All Rights Reserved.
export DITA_HOME=cwd
if [ "${DITA_HOME:+1}" != "1" ]; then
echo "DITA_HOME environment variable is empty or not set";
exit 127;
fi
echo $DITA_HOME
cd "$DITA_HOME"
# Get the absolute path of DITAOT's home directory
DITA_DIR="`pwd`"
echo $DITA_DIR
if [ -f "$DITA_DIR"/tools/ant/bin/ant ] && [ ! -x "$DITA_DIR"/tools/ant/bin/ant ]; then
chmod +x "$DITA_DIR"/tools/ant/bin/ant
fi
export ANT_OPTS="-Xmx512m $ANT_OPTS"
export ANT_OPTS="$ANT_OPTS -Djavax.xml.transform.TransformerFactory=net.sf.saxon.TransformerFactoryImpl"
export ANT_HOME="$DITA_DIR"/tools/ant
export PATH="$DITA_DIR"/tools/ant/bin:"$PATH"
NEW_CLASSPATH="$DITA_DIR/lib:$DITA_DIR/lib/dost.jar:$DITA_DIR/lib/commons-codec-1.4.jar:$DITA_DIR/lib/resolver.jar:$DITA_DIR/lib/icu4j.jar"
NEW_CLASSPATH="$DITA_DIR/lib/saxon/saxon9.jar:$DITA_DIR/lib/saxon/saxon9-dom.jar:$NEW_CLASSPATH"
NEW_CLASSPATH="$DITA_DIR/lib/saxon/saxon9-dom4j.jar:$DITA_DIR/lib/saxon/saxon9-jdom.jar:$NEW_CLASSPATH"
NEW_CLASSPATH="$DITA_DIR/lib/saxon/saxon9-s9api.jar:$DITA_DIR/lib/saxon/saxon9-sql.jar:$NEW_CLASSPATH"
NEW_CLASSPATH="$DITA_DIR/lib/saxon/saxon9-xom.jar:$DITA_DIR/lib/saxon/saxon9-xpath.jar:$DITA_DIR/lib/saxon/saxon9-xqj.jar:$NEW_CLASSPATH"
if test -n "$CLASSPATH"
then
export CLASSPATH="$NEW_CLASSPATH":"$CLASSPATH"
else
export CLASSPATH="$NEW_CLASSPATH"
fi
"$SHELL"
It looks like script-two.sh is setting up an ant build environment.
I think the author intended that it sets up the build environment, then you type your build commands in manually, then type exit to leave the build environment.
I say this because the bottom line of script-two.sh is:
"$SHELL"
which starts a new shell.
Try running your script, then type exit. I think you will see it print Hello! after you type exit.
I'm guessing you're trying to do something like:
#!/bin/bash
echo "Hi"
./script-two.sh
ant <some args>
To do that, what you really want to do is source it, by changing:
./script-two.sh
to
. script-two.sh
e.g.
#!/bin/bash
echo "Hi"
. script-two.sh
ant <some args>
But, you will need to edit script-two.sh and change:
"$SHELL"
to:
case $0 in *script-two.sh)
# executed, start a new shell with the new environment
"$SHELL"
;;
*)
# sourced, don't start a new shell
;;
esac
so that it only starts a shell if the script is being run like ./script-two.sh, but not if it is being sourced like . script-two.sh.
Or if you absolutely can't change script-two.sh, then you could do:
#!/bin/bash
echo "Hi"
. script-two.sh </dev/null
ant <some args>
which will trick "$SHELL" into exiting because it has no input.
Also
export DITA_HOME=cwd
doesn't seem right to me.
It should probably be
export DITA_HOME=$(pwd)
or
export DITA_HOME=`pwd`
(both are equivalent)
I had a similar problem today, up on digging I finally found the answer.
The script I was calling (from within my script) actually had an exit 0 in the end. Removing that fixed my issues.
Just leaving this here as someone may find it useful.
Well for starters, you can execute your bash script with the -x switch to see where it is failing:
bash -x script-one.sh
Secondly, if you call the second script like this:
#!/bin/bash
echo "Hi"
var=$(bash script-two.sh)
echo "Hello!"
It will continue, as long as script-two.sh exits cleanly. Again, you can run the -x script against that script find any problems.
And as Mikel mentioned, always make sure to have exit at the bottom of your scripts.
I want to have my .bashrc detect if running emacsclient would work in lieu of running emacs. Basically:
if emacs_server_is_running; then
EDITOR=emacsclient
VISUAL=emacsclient
else
EDITOR=emacs
VISUAL=emacs
fi
What would I put in the emacs_server_is_running function to accomplish this aim?
You're making this too hard. From the the emacsclient(1) man page:
-a, --alternate-editor=EDITOR
if the Emacs server is not running, run the specified editor instead. This can also be specified via the `ALTERNATE_EDITOR' environment variable.
If the value of EDITOR is the empty string, then Emacs is started in daemon mode and emacsclient will try to connect to it.
From a shell script, you could do something like this:
emacsclient -ca false -e '(delete-frame)'
This will open a new emacs window then quickly close it, returning true on success. If no emacs server exists, the alternate editor is /bin/false, which returns false.
#madumlao came up with a great idea in his answer. We may do slightly better, however:
emacsclient -a false -e 't'
This doesn't require opening a new frame and works in a case when:
sh-4.4$ emacsclient -a false -e '(delete-frame)'
*ERROR*: Terminal type "dumb" is not powerful enough to run Emacs
I read the answers, but none of this did fit my use case, where I did not want to start an server if not running and also wanted to avoid blocking forever.
Looking at the server-force-stop function in emacs, it simply deletes the server file in either server-auth-dir or server-socket-dir which in my case are /tmp/emacs1000/server or ~/.emacs.d/server.
Knowing this, to test if a server is running we can simply do:
test -e "/tmp/emacs1000/server" || test -e "~/.emacs.d/server"
I can think of a couple of ways, neither of them foolproof:
function emacs_server_is_running()
{
ps -ef | grep emacsserver | grep -v grep > /dev/null
}
function emacs_server_is_running()
{
netstat -a | grep esrv > /dev/null
}
Or instead of setting this up when the shell starts (after all, the server could die or the server could start after you have logged in), why not make the check at the time you need to edit something:
EDITOR=$HOME/emacs_try_client_first
VISUAL=$HOME/emacs_try_client_first
where $HOME/emacs_try_client_first could be as simple as
#!/bin/sh
# emacs_try_client_first
emacsclient $# || emacs $#
Try this.
emacs_server_is_running(){
lsof -c emacs | grep server | tr -s " " | cut -d' ' -f8
}
I find it convenient to redefine the emacs command by adding the following to ~/bin/emacs (which must be in your path):
#!/bin/sh
# if emacsclient can't connect to the daemon, try to launch it first
emacsclient -c "$#" || (/usr/bin/emacs --daemon; emacsclient -c "$#")
After this is in place, one can simply type emacs to start emacs, and a server will be automatically started if needed.
I had the same problem. When I launch emacs, it checks if there is already a server. If this is the first instance of emacs, it starts in server mode.
Then, when I view/edit a file, I want to connect to the server. This way the file is opened very quickly since no new emacs instance is started. Reusing the results of #mob above, I came up with the following working solution:
(1) .emacs
I have the following snippet in my .emacs file:
;; start emacs-server if not running
(add-hook 'after-init-hook
(lambda ()
(require 'server)
(unless (server-running-p)
(server-start))))
If this is the first emacs instance, it starts in server mode.
(2) .bashrc
#########################
# START: Emacs settings #
#########################
export EDITOR=/home/jabba/Dropbox/home/jabba/bin/emacs_try_client_first
export VIEWER=$EDITOR
alias vi=$EDITOR
alias e=vi # i.e., $EDITOR
alias em=vi # same as above
export TERM="xterm-256color"
#######################
# END: Emacs settings #
#######################
I've used vim for 15+ years, so when I want to edit a file, I write automatically vi file. Since I want to switch to emacs, I defined this alias :) The commands "vim" and "emacs" start a new instance. Using the aliases "vi", "e", and "em" you can connect to a running emacs instance (which is the server). If there is no running emacs server, a new instance will be opened (in server mode).
(3) emacs_try_client_first
#!/usr/bin/env bash
function emacs_server_is_running()
{
ps ux | grep "\bemacs\b" | grep -v grep >/dev/null
}
if emacs_server_is_running; then
emacsclient -n "$#" 2>/dev/null &
else
emacs "$#" 2>/dev/null &
fi
It also works nicely with Midnight Commander, where F3 is view, F4 is edit. In MC, if you want to use emacs, go to Configuration and switch off the internal view and internal edit.
Edit: the after macro was added. Edit #2: it was not necessary after all.
Here is my setup
#!/bin/sh
emacsclient -s IRC -c $* || (emacs --daemon=IRC && emacsclient -s IRC -c --eval "rcirc" $*)
It connects to the IRC server and opens a frame; if the server if off, it starts it, runs the elisp code and then tries again.
my solution is based in the first solution exposed by Jabba, but using emacs --script, this is useful for me to have the emacs server status (running or not running) within a bash script. I use this for multiple emacs servers, that is why I have a variable for the server name
server_ts="servername"
tmp_file=`mktemp --tmpdir=/tmp emacs-manager.XXXXXX`
echo ";;
(require 'server)
(progn (setq server-name \"$server_ts\")
(if (server-running-p)
(princ \"1\")
(princ \"0\")))
" > $tmp_file
isrunning=$(emacs --script $tmp_file 2>/dev/null)
rm $tmp_file
echo "is running: $isrunning"
before doing this I tried looking for the socket files, and parsing the output of the ps command. The former fails sometimes when the computer crashes, the socket files still there but there is no emacs process. The latter is not easy since emacs server could be started in a variety of ways.
In emacs check the value of server-socket-dir.
Check if there are some files in this directory. If does, then the server is running.
Here an example I use to get org-pomodoro timer to my xfce panel:
#!/bin/bash
if [ -z "$(ls -A /run/user/1000/emacs/)" ]; then
echo "Emacs server is down"
else
emacsclient --eval '(pomodoro-string-for-menu-bar)'| sed 's/["()]//g'
fi