How to provide auto inputs for a sub-script within a script?

Hi All,

I am writing a shell script.

#!/bin/bash
cat /etc/hosts
mkdir -p /var/tmp
mount 113.123.35.37:/vol/vol615/syb /var/tmp

In above script I am trying to add below predefined script/command (/var/tmp/db_tools)
This command in turn ask for user input, which will be always option 13 in my case.
Then User has to give '0' and at last exit

So three inputs need to give everytime whenever script/command (/var/tmp/db_tools) is running.

13
0
exit
su - sybase
/var/tmp/db_tools
 
root@host:/# su - sybase
sybase@host> /var/tmp/db_tools
*************************************************************************
            Sybase DB tools
*************************************************************************
*************************************************************************
 
      Select the action you want to perform:
 
            1.  Tempdb Setup Validation
            2.  Veritas QIO Validation
            3.  Sybase Error Logs Validation
            4.  File Validation
            5.  Sybase Database Space Usage Validation
            6.  Sybase Database & Device Usage Listing
            7.  Advanced Sybase DBA Tools
            8.  Diagnostics & Instrumentation Administration
            9.  Diagnostics & Instrumentation Reports
            10. Examine Configurable 
            11. Backup Validation
            12. Restore
            13. Healthcheck <Validation> Summary
 
            0.  Exit
 
      Enter your choice: 13
your healthcheck is okay
 
Enter your choice: 0
sybase@host> 
sybase@host> exit
sybase@host> logout
 
root@host:/# 

Please suggest how to append above.

Regards,
Madhur

Why not simply modify the db_tools script so that it no longer asks for user input but takes the defaults that you want?

You could trying using a heredoc. Or if you need to do something more complex (like verifying responses) then look at man expect ("all").

I'm confused to your requirements. :confused:

Do you want to automate running the switch-user and the db_tools? You could perhaps:-

su - sybase -c "echo \"13\n0\" | /var/tmp/db_tools" >> logfile

If you want to add a function to the db_tools scripts, we will need to see the script to know how it is laid out. Hopefully, there is a menu display, an input request and then a case statement, in which case we can add the function easily.

Can you help me out and explain what you are after?

It would be good to know the OS and version as some tools may have differences.

Robin

Hi All,

Than You for the response.

Why not simply modify the db_tools script so that it no longer asks for user input but takes the defaults that you want?
-> Because the db_tools script is the standard for all the user.
But whenever I want t use this script it should take the my inputs i.e 13, 0 and exit.

sybase@host> /var/tmp/db_tools

13
0
exit

You could trying using a heredoc. Or if you need to do something more complex (like verifying responses) then look at expect.
-> heredoc and expect, I am not sure because this is the linux server where I cannot add any expect command tools.

Do you want to automate running the switch-user and the db_tools? You could perhaps:-
1st I want to run the "su - sybase" and then the "/var/tmp/db_tools" and provide the fixed inputs in the same sequence as "13, 0 and exit."

If you want to add a function to the db_tools scripts, we will need to see the script to know how it is laid out. Hopefully, there is a menu display, an input request and then a case statement, in which case we can add the function easily.
-> I dont want to add any finction.

My basic requirement is to run script as follows example :-

I am running below script with root user

#!/bin/bash
cat /etc/hosts
mkdir -p /var/tmp
su - sybase -> upto here no problem
/var/tmp/db_tools -> this should run once I swicth to sybase user
13 -> after running dba_tools I want to enter option 13 everytime
0 -> once option 13 is completed I want to pass 0
exit -> at last I want to enter exit

Thank You all for the reply and my apologies if something is not clear.

Regards,
Madhur

Then I think that my first suggestion has a chance of succeeding. What do you get from:-

su - sybase -c "echo \"13\n0\" | /var/tmp/db_tools" >> logfile

Are there any errors? Do you get the output you need in logfile?

Regards,
Robin

Hi Robin,

When I run the below command

su - sybase -c "echo \"13\n0\" | /var/tmp/db_tools" >> logfile

it throws below message in logfile while keep on looping for the user inputs
Script aborted.......

Reason :-
First I want to do "su - sybase"
2.) Second it should run /var/tmp/db_tools
3.) Third we need to pass values in below order
13
0
exit

Thank You
Madhur

Perhaps we could try the following variation:-

su - sybase -c "/var/tmp/db_tools <<EOT
13
0
EOT" >> logfile

Does that help?

Robin

Or, perhaps using printf in place of echo?

Hi,

No below also not working it throws "Script aborted......."

su - sybase -c "/var/tmp/db_tools <<EOT
13
0
EOT" >> logfile

Else can we change the original script as suggested earlier in the thread.
My request are as follows :-

su - sybase

Whenever executed "/var/tmp/db_tools"
it should always run option "13. Healthcheck <Validation> Summary"
after this "0. Exit"
atlast "quit"

We can edit the attached db_tools.

Thank You

Regards,
Madhur

Okay, let's try to do one thing at a time. Can you become user sybase and then run:-

echo "13\n0" | /var/tmp/db_tools

What output do you get then?

Do you have to CNTL-C to cancel it still? Perhaps scott's suggestion would work better:-

printf "13\n0" | /var/tmp/db_tools

Robin

Hi,

I used below

su - sybase
printf "13\n0" | /var/tmp/db_tools

Yes, I have to CNTL-C to cancel it.

Regards,
Madhur

Ctrl-C, or would Enter do? Try adding another \n after the 0. If that doesn't work, run the command manually and trace your steps.

Hi Scott,

Initially the below code works and shows the output.

printf "13\n0\n" | /var/tmp/db_tools

But once it displays -> All checks completed!

It again loops for input and displays menu.
I guess it not taking "0" as exit and keep on loop

Press <Return> to continue:
Select the action you want to perform:

        1.  Tempdb Setup Validation
        2.  Veritas QIO Validation
        3.  Sybase Error Logs Validation
        4.  File Validation
        5.  Sybase Database Space Usage Validation
        6.  Sybase Database & Device Usage Listing
        7.  Advanced Sybase DBA Tools
        8.  Sybase Diagnostics & Instrumentation Administration
        9.  Sybase Diagnostics & Instrumentation Reports
        10. Examine Configurable Shared Memory Dump \(CSMD\) Setup
        11. Sybase Backup Validation
        12. Restore Individual or All Application Databases from DB Dumps
        13. Healthcheck &lt;Validation&gt; Summary

        0.  Exit

  Enter your choice: 

^C
Script aborted.......

sybase@arr>

Regards,
Madhur

---------- Post updated at 09:36 PM ---------- Previous update was at 09:13 PM ----------

Hi,

This one works fine.

printf "13\n\n0\n\nEXIT\n" | /var/tmp/db_tools

But how to perform below

#!/bin/sh
su - sybase
printf "13\n\n0\n\nEXIT\n" | /var/tmp/db_tools
exit

OR

#!/bin/sh
su - sybase -c "printf "13\n\n0\n\nEXIT\n" | /var/tmp/db_tools"
exit

Regards,
Madhur

So is EXIT an entry to the db_tools? :eek:

:confused: I'm confused :confused:

Let's try this as root:-

su - sybase -c "printf \"13\n\n0\n\nEXIT\n\" | /var/tmp/db_tools"

Does that get there?

Robin

Hi Robin,

Perfect perfect perfect...works fine as required.

su - sybase -c "printf \"13\n\n0\n\nEXIT\n\" | /var/tmp/db_tools"

Thank You very much :):):slight_smile:

Regards,
Madhur

So on the menu, where is the EXIT option, and why is it different to option zero?

I should suggest that /var/tmp is not a good place to be storing code. It's the default area used by vi and other tools to store temporary files. Something like your db_tools code would be better under /usr/local/scripts, /usr/local/bin or perhaps /opt/our-apps/scripts and well away from /var

Anyway, glad to be of help.

Robin
Liverpool/Blackburn
UK

Hi Robin,

The menu looks like below

Select the action you want to perform:

        1.  Tempdb Setup Validation
        2.  Veritas QIO Validation
        3.  Sybase Error Logs Validation
        4.  File Validation
        5.  Sybase Database Space Usage Validation
        6.  Sybase Database & Device Usage Listing
        7.  Advanced Sybase DBA Tools
        8.  Sybase Diagnostics & Instrumentation Administration
        9.  Sybase Diagnostics & Instrumentation Reports
        10. Examine Configurable Shared Memory Dump \(CSMD\) Setup
        11. Sybase Backup Validation
        12. Restore Individual or All Application Databases from DB Dumps
        13. Healthcheck &lt;Validation&gt; Summary

        0.  Exit

  Enter your choice:

Yes, I accept your advice and will surely apply it in /usr/local/scripts

Regards,
Madhur