Run crontab with user input - linux

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?

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.

How to supply an input value to the prompt via shell script?

I am writing a wrapper shell script wrapper.sh to run bunch of other already available scripts owned by other people and I cannot touch those scripts.
The problem is, there is one script that runs some db specific activities - db_perf_clean.sh. That script is normally executed manually and it prompts for a password at run time. There is no way I can supply the password to it as a parameter and I cannot modify that script. As such I know the db password and I can provide it in wrapper.sh.
Please let me know how can I run that db_perf_clean.sh script inside wrapper.sh like in a silent mode.
Sometimes a script will insist that a password be read from the tty. Often, it will read from stdin. If so, try:
echo password | db_perf_clean.sh
The above has the disadvantage that the password will appear in ps. To avoid that, hide the password in a file and use that file for stdin:
db_perf_clean.sh <file_with_password
If you want the command to be silent, you can throwaway its output:
db_perf_clean.sh <file_with_password >/dev/null 2>&1
Under bash, as opposed to generic shell, that can be slightly simplified:
db_perf_clean.sh <file_with_password &>/dev/null
I found out little different approach instead of writing a password in a file and that worked too ->
db_pass="somevalue"
sh db_perf_clean.sh<<EOM
$db_pass
EOM

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

Capture all input and output from a bash script

I am trying to capture all the input and output from a bash script that i created for installing nagios. I have it creating the log file using tee right now but it only shows when there is an echo command or some output from like "service httpd restart". I mainly want to capture the input the user is entering in the log file for future reference.
The script command, run prior to your program, will capture all input and output to a file you specify. It terminates with a ctrl-D.
script -c yourprogram filename
may do what you're looking for. See the man page for 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

Resources