How to input on a running process in a terminal - linux

I want to make a script in Linux, that starts a program (in the terminal), and then write what I want in the context of the already running program.
For example, I have myScript.sh :
#!/bin/bash
echo "I want to open pgcli"
pgcli -h #ARGUMENTS#
....
When I run it from a terminal, this of course get stuck when the pgcli starts. And when I manually finish it, the following statements are executed.
What I'mt trying to do is: when pgcli starts and ask me for password, the script to write the password IN the context of the program (and then, another SQL statements).
Can this be done in a script? If so, how could I do it?
Thanks!

assuming your program reads from stdin,
if you have a file containing all the needed input like so:
password
sql query1
sql query2
then you can do something like:
cat input | pgcli
in your script. Or just do
echo "input" | pgcli

Related

Can you prompt for user input in a shell script that is running remotely?

Say I have a script that will be run on a remote machine.
While running, the script computes some value.
I want to prompt the user so she can change this value if needed.
Is this possible?
I am running the script like: ssh $usr#$machine 'bash -s' < a.sh "param1" "param2"
In a.sh the read alternateValue function call seems to be ignored.
Or can anyone suggest a different approach?
The read statement reads data from stdin, but you are redirecting stdin in your command line with the < operator, so read isn't going to do anything useful.
What if you were first to copy the script over to the remote host, and then run:
ssh $usr#$machine 'bash /path/to/a.sh param1 param2'
Because there is no redirection happening here, read would work without a problem.

Running Multiple Remote Commands Consecutively Through Matlab system()

I'd like to execute multiple commands consecutively using a matlab system call. I would first like to ssh into a remote machine then run a program on that machine. After the program starts I would like to then enter another command into this program's console. Here is what I would like to do in code:
system('ssh othermachine')
system('command on other machine')
%%WAIT FOR PROGRAM TO START RUNNING ON OTHER MACHINE
system('run command on other machine')
The problem is that Matlab will hang on the first system call and won't proceed to the next system call until the process form the first is exited. Is there a way around this?
Thanks for your help!
Prologue: Your problem is general and not just related to matlab.
When you want to run remote commands via ssh, they have to be issued in the ssh call. In a (linux) shell, you'd have
$ ssh remotemachine command1
for a single command. Hence, using a matlab system call you would have
>> system('ssh remotemachine command1').
When you want multiple commands to be executed sequentially, in a shell you'd write
$ ssh remotemachine "command1; command2"
i.e., in matlab, you'd write something like
>> system('ssh remotemachine "command1; command2"').
In general, is way more elegant to group your commands in a shell script, say script.sh, and pipe it in the ssh call
$ cat script.sh | ssh remotemachine
which, in matlab shell, sounds like
>> system('cat script.sh | ssh remotemachine').
There are a number of flags you can add in order to specify which behavior you want (e.g. in terms of session detachment/background execution, output collection,... look e.g. here).

Skip to next command in shell script when user input is required

I have the following scenario. I have a shell script that is generated automatically, that I want to run. The general format of the script looks something like this:
#!/bin/sh
command_1 #something like mkdir dir1 or chmod -R 775 dir1, you get the idea
command_2
...
...
command_n
Like I said the script will be automatically generated in a way that I don't have much control of the commands that are written in the script (the purpose of the script is to use it for fuzz testing, so it makes sense). The problem is that some commands require some sort of user input (for example "chfs --some arguments" will sometimes prompt me for the root password), and therefore the script will not pass to the next command until it gets the proper input.
So, my question is: Is there a way to skip the commands that require user input when they are met in such a script, so that the script finishes and executes all the other commands? Any idea is greatly appreciated.
You can use expect script to work around this, something like this
spawn /bin/bash yourscipt.sh
expect "password:"
# Send the password, and then wait for a shell prompt.
send "xxxxx\r"
Here XXXX isyour password.
Lets say your script requires a user to enter a choice interactively.
User press y then again it askes user name.
User enter his name and then script continues.
Enter choice (y/n):_
Enter name :_
So you can pass inputs by preparing an input file with choices written in each line.
content of input file :
y
Inderdeep
And run the script as : cat inputfile | ./script

Shell script : how to output to command line?

I'm making a shell script and I want to know if it's possible to write directly to the command line when the script is executed ?
Example :
user#localhost:/home/user$./script.sh
... output
... another output
... another output
... last output
user#localhost:/home/user$I want to write here on the command line
I don't want to "echo" some text, I want to write directly at the prompt.
Thanks!
No, you can't do that. If you want user to invoke your provided command after your script is finished - why not just prompt user for confirmation?
If you just want the text to show up there, but not be able to do anything with it, you can do this.
File test.sh:
echo "Output"
./test2.sh &
File test2.sh:
echo "Output2"
Notice how the first script calls the second script with the & at the end.
In this case, "Output2" will be written to the prompt, but it can't be deleted and will have no effect on the next command at all. But if this is something you're doing to grab the user's attention, it would work.
In ksh:
print -s $(script)
will print to the command history. Wrap this in a function and you'll have something close to what you are asking for.
If you are using X environment install xclip and xdotool, then:
#!/bin/bash
your scripts....
echo -n your command to write 2>&1|xclip
xdotool click 2

Run crontab with user input

i created a crontab which will run a bash script test.sh. This test.sh file requires some input from the user, and saves the user input into a variable. How do i ensure that the user input will be saved to a variable in test.sh, and when crontab runs the script i can get the output i want?
for e.g i have 2 files, file1.sh and file2.sh. i put file2.sh in file 1.sh. i then run file1.sh, get the user input, and save it somewhere. crontab will run file2.sh, and retrieve the value from the "saved somewhere variable". is there anyway for this?
If the input is read by the script from stdin, just redirect input from a file (using a wrapper script).
#! /bin/sh
test.sh < data.in
If this does not work for you (i.e. you have your script calling some interactive shell program like telnet, you can use Expect to automate the interaction.
file1.sh gets user input and writes it to /etc/file2.dat
file2.sh reads /etc/file2.dat and does whatever it needs
This seems like a strange thing to do. Ask yourself these questions:
Do you really want a popup asking the user for an input value every time the cron runs?
What happens when there's no one at the keyboard?

Resources