If Statements in xdotool & bash - xdotool

I need make a small script in bash using xdotool. And I have problem with if statements. How to write correctly this instruction.
if [xdotool click 1]
then
./myScript.sh
fi
% If I make mouseclick I want to execute bashscript
Please help.

#/bin/bash
xdotool click 1
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
./myScript.sh
fi
exit code 0 - means that you program executed successfully
UPD:
xdotool click 1 && ./myScript.sh

There is a feature in xdotool called behave to take an action based on some event like mouse-click But...
The specific mouse-click option is not working (apparently a bug),which is:
xdotool search . behave %# mouse-click exec 'myscript.sh'
Although these commands work with mouse-move / mouse-leave option, which are:
xdotool search . behave %# mouse-move exec 'myscript.sh'
xdotool search . behave %# mouse-leave exec 'myscript.sh'
Another solution would be using echo -e like this:
echo -e "\e[?1000h"
while read -n 12; do myscript.sh; done

Related

How to map ctrl-z to fg in fish

I am trying to implement a feature from the following article:
How to boost your vim productivity
in the fish shell. The Author in the Article uses the following code to map Ctrl+Z in zsh to the "fg" command.
fancy-ctrl-z () {
if [[ $#BUFFER -eq 0 ]]; then
BUFFER="fg"
zle accept-line
else
zle push-input
zle clear-screen
fi
}
zle -N fancy-ctrl-z
bindkey '^Z' fancy-ctrl-z
The intention is to quickly switch between a vim instance in the foreground and the shell.
So Ctrl+Z backgrounds vim, and switches to the shell, then Ctrl+Z again should foreground vim again, so switching quickly is possible.
How would I replicate that in fish?
fish currently doesn't let you catch SIGTSTP, which is what Ctrl+Z sends. You can however bind another key. For example, you could write:
bind \ck 'fg %'
This makes control-K switch back to the last backgrounded process.
It looks like the zsh fancy-ctrl-z function has a separate mode where it clears the screen if there's input on the command line. I'm not sure what that's about, but that can be replicated in fish if you want, something like:
bind \ck 'if test -z (commandline) ; fg %; else ; clear; commandline ""; end'

Is it possible to open a file in an running instance of Matlab from the command line?

If I already have an instance of Matlab running is it possible to tell open a file in the Matlab editor from outside the Matlab application? I'm wondering if there it is possible do something like this.
Launch an instance of Matlab
$ ./matlab
Open a file for editing using an already running instance of Matlab:
$ matlab_open_file.sh theFile.m
The GUI variant is dragging a file from a folder and then dropping it onto Matlab icon (this actually works under OS X)
Note I know that you can launch Matlab and have it immediately execute a command (you could use this to start the editor on launch). This is not what I want.
I scripted a workaround for Linux (functional on Mint 17.1 with R2014a and R2014b), which I then associated with the .fig and .m file extensions. Note that this requires xdotool to be installed, and the keystrokes are set for Windows shortcuts (by default, MATLAB ships with Emacs shortcuts on Linux, but virtually everyone changes them in my experience). This has the limitation that any text currently on the command line is erased, and there is a small window of time where MATLAB must not lose focus. But in the absence of a more robust solution, it works well enough for me.
#!/bin/bash
# Hacky way to open a MATLAB figure in an existing instance if there is
# one, and start a new instance if not.
# What are we trying to open?
FILENAME="$#";
# Try to identify the main MATLAB window.
MLWINDOW=$( comm -12\
<(xdotool search --name MATLAB\ R | sort)\
<(xdotool search --class "com-mathworks-util-PostVMInit" | sort) )
if [ -z "$MLWINDOW" ]; then
# MATLAB isn't open; we have to open it to proceed.
matlab -desktop -r "open('$FILENAME')"
else
# We use the first existing instance since MATLAB is open
set -- $MLWINDOW
# Jump to the command line and erase it
xdotool windowactivate --sync $1 key --delay 0 "control+0" Escape
# Put the filename on the command line
xdotool type --delay 0 "$FILENAME"
# Select the filename and press ctrl-D to open, then clean up command line
xdotool key --delay 0 "shift+Home" "control+d" Escape
fi
You can type the path+filename into the command line and if a matlab session is open it will open this file in the current matlab session.
Note that this only works if you make sure matlab is the default program to open this kind of file. (Tested with .m file)
I modified Aoeuid's approach because
it did not work for me, as I had reassigned Ctrl+0 which jumps to the command line (and I don't see where I could set this to another value) → I replaced it with the “open file” dialog (Ctrl+O).
I might want to open scripts that are not on the current matlab path → I use $PWD/$filename instead of $filename. You could modify his version by using open($PWD/$FILENAME) and KP_Enter instead of $FILENAME and shift+Home/control+d.
This is the result:
#!/bin/bash
filename="$1"
# Try to identify the main MATLAB window.
MLWINDOW=$( comm -12\
<(xdotool search --name MATLAB\ R | sort)\
<(xdotool search --class "com-mathworks-util-PostVMInit" | sort) )
if [ -z "$MLWINDOW" ]; then
# MATLAB isn't open; we have to open it to proceed.
matlab -desktop -r "open('$PWD/$filename')"
else
## Activate window:
xdotool windowactivate --sync $MLWINDOW && \
## Press Ctrl+O to open the "open" dialog:
xdotool key --delay 0 "control+o" && \
## Wait for the file dialog to open:
sleep 0.5 && \
## Type the file name including the current directory
xdotool type --delay 0 "$PWD/$filename" && \
## Press enter:
xdotool key "KP_Enter"
fi
However, using key presses for an automated process might cause unwanted results.
Make sure that you added you folder to path.
Then you go to folder you need.
and just type in Matlab terminal
your_program_name
Then your program will run.

how to escape characters in xdotool

I have this script to open a new console, ssh into a server, run a deploy command.
I pass to the script the version of the deploy
xdotool key ctrl+alt+t
sleep 3
xdotool type "ssh myserver"
xdotool key Return
sleep 10
xdotool type "password"
xdotool key Return
xdotool type "sh path-to-script/deploy.sh $1"
xdotool key Return
I have several problems with this and I allready tried to google for a solution without success.
the character / its transformed to an &. when I run the script
copy&pasting in the console it works, but no if I run it as a sh file
the $1 is not evaluated
Can you give me any pointer in making this work. xdotool is not mandatory, I would use whatever it works
note: I can't by pass the ssh with a command becouse the security politics of the company and also don't know how to do it if I can't the settings in myserver
Similar problem here:
http://code.google.com/p/semicomplete/issues/detail?id=13#c29
Try:
xdotool type setxkbmap de
xdotool key Return
xdotool type "sh path-to-script/deploy.sh $1"
xdotool key Return
Maybe it works better if you write a script:
#!/bin/bash
xterm -e "ssh myserver \"sh path-to-script/deploy.sh $1\"; sleep 5"
and invoke it like this:
./theAboveScript.sh &
sleep 10
xdotool type "password"
xdotool key Return
"expect" could be a good solution.
You can start following as a script with your parameter.
#!/usr/bin/expect -f
spawn ssh myserver;
expect "Password:";
send "password\n";
expect "me#myserver:~$";
send "sh path-to-script/deploy.sh $argv\n";
interact

Running command using XARG

I have example file: test_file
//--- Test File--
**RUN_THIS
RUN_THIS00
RUN_THIS01
DONT_RUN00
DONT_RUN00
RUN_THIS02**
where RUN_THIS* & DONT_RUN* are commands.
I would like to run only RUN_THIS commands from test_file without editing the file.
I am looking for option like
cat test_file | grep RUN_THIS | xargs {Some option to be provided to run run_this}
I cannot start new shell
Something like this perhaps?
for cmd in $(grep RUN_THIS < test_file); do
$cmd --some-option-to-be-provided-to-run-this
done
That should work okay as long as there are no spaces in the commands in test_file.
eval `grep RUN_THIS test_file`
Note also the avoidance of a Useless Use of Cat.
Actually, you may have to add a semicolon to the end of each command in test_file, or change the grep to something which adds the necessary semicolons.
eval `awk '/RUN_THIS/ { print; print ";" }'`
I'm not sure I understand the requirement to not start a new shell. Under the hood, the backticks run a subshell, so this might violate that requirement (but then ultimately every external command starts a new process, which starts out as a fork of the current shell process when you run a shell script). If you are scared of security implications, you should not be using a shell script in the first place, anyhow.
To run new shells you need to incorparate "ksh" in your command.
In its simplest form
RUN_THIS00='ls'
echo $RUN_THIS00 | ksh

Can I execute a multiline command in Perl's backticks?

In Unix, I have a process that I want to run using nohup. However this process will at some point wait at a prompt where I have to enter yes or no for it to continue. So far, in Unix I have been doing the following
nohup myprocess <<EOF
y
EOF
So I start the process 'myprocess' using nohup and pipe in a file with 'y' then close the file. The lines above are effectively three seperate commands - i.e. I hit enter on the first line in UNIX, then I get a prompt where I enter 'y' and then press enter to then finally type 'EOF' and hit return again.
I want to know execute this in Perl but I am not sure how I can execute this command as it is over three lines. I don't know if the following will work....
my $startprocess = `nohup myprocess <<EOF &
y
EOF
`
Please help - thank you!
I think your proposal will work as is. If not, try replacing the redirect with a pipe:
my $startprocess = `(echo "y" | nohup myprocess) &`;
Also, depending on WHY you are doing a nohup, please look at the following pure Perl daemonizing approach using Proc::Daemon : How can I run a Perl script as a system daemon in linux?
Expect for interactive programs can be used as well.