Sending keystrokes or text to a program running with wine - bash

I'm starting a windows-program with wine in a shell script. Now I would like to pass keystrokes and texts to it. I've tried xvkbd as mentioned in this question, but the program just outputs the letter/text into the terminal and exits after that. Here's an example:
`wine BERCon.exe > output.txt &`
sleep 5;
`xvkbd -text "N"`
Is that even possible to do? I checked the man-page of xvkbd to check if I can change the focus somehow, but it seems like thats not possible.I noticed that if I do something like echo "N" | wine BERCon.exe the N actually gets passed to wine and wine puts it into the program, but gets into a loop after that and continues to input that letter.

Related

Is there a way to force "Yes" to any prompts when running command with yarn run / nx run [duplicate]

I have program, that must interact with a console program before my program can continue what it is doing. I'm trying to avoid my user from having to interact with this dos program. So, I created a .bat file that does everything I need to do except for the last step which still requires user interaction that I'm trying to avoid.
Specifically, the command I type ends up at a prompt where I need to automatically enter y and then Enter (to say yes to the prompt) and then I want to exit out.
Is there any way that I can make this happen automatically without my user having to enter y and Enter? Ideally, I'd like to have the console window NOT even pop up while this is going on.
You can pipe in a 'y' character into the program like so:
echo y | executable.exe
Multiple lines can be entered like so:
(echo y
echo n) | executable.exe
...which will pass first 'y' then 'n'.
See tip from Microsoft here.
The post from Microsoft also clearly says :
Do not type a space between the "y" and the pipe symbol (|)
and indeed, I noticed that in my case
echo y | executable.exe
doesn't work
while
echo y| executable.exe
works fine
I used the following, since "echo y | executable.exe" didn't worked for me
// Write a "Y" to the process's input
proc.StandardInput.WriteLine("Y");
// Now that we've sent the confirmation "Y" wait for the process to exit
proc.WaitForExit();
as posted here: https://www.experts-exchange.com/questions/27024185/C-ProcessStart-How-to-automatically-press-the-Y-key.html

Using terminal on mac via an executable file

Right so I wanted to automate a process which I have to do quite often for a program that I run quite often and sometimes open up multiple of.
Normally I open up Terminal and type in the following commands in order:
cd ExoClient
mono ExoClient.exe
(email)
(pass)
So I made an executable file for this called login.command on my mac.
I typed in this is order:
cd ExoClient
mono ExoClient.exe
example_email#gmail.com
example_password
So the first two lines work but the last two don't do anything. I thought that this could be because the top 2 commands are actual mac commands and the last two lines are just inputs. But I don't know how to make sure the inputs are inputted.
OR another reason why it might not work could be because I need to delay the inputs. I'd love to know what I can input here to make it wait a second before an input is inputted.
Thanks for reading, would appreciate any help.
I made a similar post on mac rumours.
The solution was to do :
cd ExoClient
mono ExoClient.exe email pass
The first thing to understand about the command-line is that there's a bunch of different things in play.
The first is the commands themselves, these are typically typed in or run from a script.
The second is "standard input" or STDIN, which is where programs take keyboard input. As far as you're concerned there's no difference when using the shell, but to the shell itself there's a huge difference. That program wants input from STDIN. The shell script is being told to run two subsequent commands.
Instead try this:
cd ExoClient
echo "example_email#gmail.com\
example_password" | mono ExoClient.exe
The | character means "send output to" in rough terms, so the output of echo goes through to the mono program. Output, or STDOUT, is another thing the shell wants to deal with.
Or have a secondary file that contains those two lines and give that as "input":
cd ExoClient
mono ExoClient.exe < login.txt
Where login.txt has the two lines and < means "take input from this file".

Is it possible to start a program from the command line with input from a file without terminating the program?

I have a program that users can run using the command line. Once running, it receives and processes commands from the keyboard. It's possible to start the program with input from disk like so: $ ./program < startScript.sh. However, the program quits as soon as the script finishes running. Is there a way to start the program with input from disk using < that does not quit the program and allows additional keyboard input to be read?
(cat foo.txt; cat) | ./program
I.e., create a subshell (that's what the parentheses do) which first outputs the contents of foo.txt and after that just copies whatever the user types. Then, take the combined output of this subshell and pipe it into stdin of your program.
Note that this also works for other combinations. Say you want to start a program that always asks the same questions. The best approach would be to use "expect" and make sure the questions didn't change, but for a quick workaround, you can also do something like this:
(echo y; echo $file; echo n) | whatever
Use system("pause")(in bash it's just "pause") in your program so that it does not exit immediatly.
There are alternatives such as
dummy read
infinite loop
sigsuspend
many more
Why not try something like this
BODY=`./startScript.sh`
if [ -n "$BODY" ]
then cat "$BODY" |./program
fi
That depends on how the program is coded. This cannot be achieved from writing code in startScript.sh, if that is what you're trying to achieve.
What you could do is write a callingScript.sh that asks for the input first and then calls the program < startScript.sh.

Disown, nohup or & on Mac OS zsh… not working as hoped

Hi. I'm new to the shell and am working on my first kludged together script. I've read all over the intertube and SO and there are many, MANY places where disown, nohup, & and return are explained but something isn't working for me.
I want a simpler timer. The script asks for user input for the hours, mins., etc., then:
echo "No problem, see you then…"
sleep $[a*3600+b*60+c]
At this point (either on the first or second lines, not sure) I want the script OR the specific command in the script to become a background process. Maybe a daemon? So that the timer will still go off on schedule even if
that terminal window is shut
the terminal app is quit completely
the computer is put to sleep (I realize I probably need some different code still to wake the mac itself)
Also after the "No problem" line I want a return command so that the existing shell window is still useful in the meantime.
The terminal-notifier command (the timer wakeup) is getting called immediately under certain usage of the above (I can't remember which right now), then a second notification at the right time. Using the return command anywhere basically seems to quit the script.
One thing I'm not clear on is whether/how disown, nohup, etc. are applicable to a command process vs. a script process, i.e., will any of them work properly on only a command inside a script (and if not, how to initialize a script as a background process that still asks for input).
Maybe I should use some alternative to sleep?
It isn't necessary to use a separate script or have the script run itself in order to get part of it to run in the background.
A much simpler way is to place the portions that you want to be backgrounded (the sleep and following command) inside of parentheses, and put an ampersand after them.
So the end of the script would look like:
(
sleep $time
# Do whatever
)&
This will cause that portion of the code to be run inside a subshell which is placed into the background, since there's no code after that the first shell will immediately exit returning control to your interactive shell.
When your script is run, it is actually run by starting a new shell to execute it. In order for you to get your script into the background, you would need to send that shell into the background, which you can't do because you would need to communicate with its parent shell.
What you can do is have your script call itself with a special argument to indicate that it should do the work:
#! /bin/zsh
if [ "$1" != '--run' ] ; then
echo sending to background
$0 --run $# &
exit
fi
sleep 1
echo backgrounded $#
This script first checks to see if its first argument is --run. If it is not, then it calls itself ($0) with that argument and all other arguments it received ($#) in the background, and exits. You can use a similar method, performing the test when you want to enter the background, and possibly sending the data you will need instead of every argument. For example, to send just the number of seconds:
$0 --run $[a*3600+b*60+c] &

In a bash script/command how can I make a PC beep noise, or play a sound file?

I have some long running scripts with breaks requiring input/interaction to continue but when I switch to another window I'd like to be notified (by sound) that a task is complete and now awaiting input.
I would prefer to be able to play an audio clip (*.mp3, *.ogg, etc.) but wouldn't care if the only solution is to make the PC Speaker beep noise.
Any ideas? I'm open to any CLI utilities I can install that play sounds that in turn I can execute when needed.
FYI: My System is running WinXP Pro.
UPDATE: Doh! My Windows > Control Panel > Sounds > Default Beep: was set to (none). Grrr...
Problem solved.
This will make a beep from within bash
echo -en "\007"
Try this:
echo ^G
(^G is obtained by ctrl+G).
Note: you can't copy and paste this code in a batch file, it won't work. To obtain a ^G character in a file, type in a cmd window:
echo ^G > beep.txt
(again, ^G is obtained by ctrl+G).
Then you'll have a file named beep.txt, open it with notepad, there will be a square character. This is our ^G once it is saved in a file.
You can then copy and paste it in a batch file to make a sound (don't forget to put "echo" in front of it).
spd-say
sleep 2; spd-say 'get back to work'
Infinite loop with -w if you need extra motivation:
sleep 2; while true; do spd-say -w 'get back to work'; done
or if you prefer the carrot:
sleep 2; while true; do spd-say -t female1 -w "I'm done, come back to me, darling"; done
Pre-installed on Ubuntu 14.04 via the package speech-dispatcher: http://releases.ubuntu.com/trusty/ubuntu-14.04.4-desktop-amd64.manifest for blind people I suppose?
See also: https://askubuntu.com/questions/277215/how-to-make-a-sound-once-a-process-is-complete
Also add a popup
This combo is a life saver, b stands for beep:
b() ( spd-say 'done'; zenity --info --text "$(date);$(pwd)" & )
and then:
super-slow-command;b
If I'm somewhere in the room, I'll hear it and know that the long job is done.
Otherwise, I'll see the popup when I get back to my computer.
Related:
How to show a GUI message box from a bash script in linux?
https://superuser.com/questions/345447/how-can-i-trigger-a-notification-when-a-job-process-ends
https://askubuntu.com/questions/409611/desktop-notification-when-long-running-commands-complete
Listen to your cooler
I'm joking of course, but for compilation I noticed that I use often use this queue subconsciously. When the cooler stops humming for a while, it means that the compilation is over!
I know your question was for Window but just putting this here for any Mac OSX users who come across this article. OSX 10+ comes with the say command:
say "I'm done"
For example:
sleep 5 && say "I'm done waiting 5 seconds"
By setting this variable as follows
PROMPT_COMMAND="echo -en '\a'"
then bash will beep every time it shows the prompt. When you do not need it anymore,
unset PROMPT_COMMAND
To play the system sound from Windows command line you can run:
rundll32 user32.dll,MessageBeep
It should work on all version of Windows.
copy con beep.bat [Enter]
#echo off [Enter]
echo [Ctrl+G] [Enter]
[Ctrl+Z] [Enter]
beep.bat [Enter]
Simple answer without ^G
echo -en "\007"
In my bash profile I've added a BEEP to the script using #GregReynolds solution above then added this to PS1:
GREEN="\[\033[0;32m\]"
BEEP=$(echo -en "\007")
export PS1="$GREEN : ${BEEP}"
source ~/.bash_profile - you should hear the beep after the command prompt returns
I have git-autocomplete on usually so I've provided a much simplified version above

Resources