How pressing the enter key automatically in the shell script? - shell

I have trying to make auto install shell script for Sun java web server.
But, from the start, have some trouble.
when I start up the setup manually, I need to press enter key several times to agree licenses.
but I don't know how to replace the enter key automatically on the shell script.
could you someone help me?

Use echo and pipes. Something like:
(echo; echo; echo) | my_setup

Related

Put command string on the bash prompt

Say I have bash prompt in the terminal:
host:~/dir $
how can I write a command to the prompt that the user can choose to run? Maybe there is a way to use readline(3) to put a command in the shell prompt?
In other words, I am looking to write a command here:
host:~/dir $ <write some command here>
I tried:
echo "write some command here" > /dev/stdin
but that didn't quite work - it doesn't seem to put it on the prompt, is there a way to do that?
What I am trying to do - When you hit up/down arrow keys with bash, your previous command shows up in the prompt, I am trying to read from another history file and put it on the prompt.
Without knowing more about what your use case is, I'd start by pointing you in the direction of whiptail. It's part of the base install of most Linux systems and it allows you to present an input box to the user, even allowing for the box to be pre-filled with a default value. A very simple example would look roughly like this:
whiptail --input "Want to run this?" 8 78 "<write command here>" --title "Dialog box title here"
There's a primer available on WikiBooks that does an adequate job of introducing most of its basic features, if you want to dive deeper.

Request user input in bash_profile alias command

Is there anyway to stop in a bash_profile alias command and prompt for a user input? I'm fairly new to writing terminal commands.
I'm making a custom command that will take 2 inputs and edit my host files and set up vhosts for me on my local machine, at the moment i'm just passing the arguments into the command
addSite mywebsite.co.uk
But ideally I would like to be able to just run addSite, then the command stops and prompts 'Please enter the domain for your new site'.
Is this possible? If not, can someone point me in the right direction to be able to write a custom terminal command that can do this?
Thanks
You can ask for user input with read and pass a prompt with the -p flag. After the command, you specify the variable name. Then access it like any other variable.
read -p 'Please enter the domain for your new site: ' domain
echo Your domain name: $domain
EDIT: as pointed out by #tripleee in the comments, it's worth noting that this will have unintended side effects if a shell is launched from another source e.g. if you launch an executable file from Finder.

How to open a command-line application with a command without closing it?

I am trying to write a script that opens a command-line application (sagemath in this case) which on start up will send a certain command down the pipe (attach a script) without closing the application at the end.
I tried something like:
#!/bin/bash
echo "load(\"script.sage\")" | sage
This, of course, opens sage load the script print the output of the script and closes sage. Adding & at the end of the last line didn't work.
I know that technically I can add this script to the list of scripts which are loaded on startup always but this is not what I want. I thought that it might be done be making a dynamic link at some directory to my script, but not sure if there is such a directory and where it is.
Any suggestions?
Edit:
I didn't know about Expect (I'm a youngster in linux). Reading about, following Mark's suggestion, it a bit I managed to solve this. If this is of any interest to anyone in the future then this does the trick:
#!/usr/bin/expect
set timeout 20
spawn sage
expect "sage:"
send "load(\"script.sage\")\n"
interact
#!/usr/bin/expect
set timeout 20
spawn sage
expect "sage:"
send "load(\"script.sage\")\n"
interact
You could use 'screen' depending on how dynamically you need this script to run. See http://linux.die.net/man/1/screen for info on how to use screen.
You can either:
Use nohup to start the program E.g., nohup "load(\"script.sage\")" | sage.
Or, you can use the disown command.

how user can respond to system prompts using shell script

I am trying to automate something using shell script.
My question is when there is a system prompt, for e.g. "Do you want to continue (y/n):"
And I need to input y using my shell script, how can it be done ? So at the end when the system prompt displays on the shell then my shell script should be automatically able to enter y.
Thanks in advance.
You can try an expect script
It is used to automate control of interactive applications such as
telnet, ftp, passwd, fsck, rlogin, tip, ssh, and others. Expect uses
pseudo terminals (Unix) or emulates a console (Windows), starts the
target program, and then communicates with it, just as a human would,
via the terminal or console interface.
It is really quite easy using redirection, and there are several ways to do it. This uses a here string. First to generate that system prompt:
more one.sh
read -p "Do you want to continue (y/n): " ans
echo "Answer was $ans"
Now the script to run the program:
bash one.sh <<< 'y'
Output:
Answer was y
Warning! This works by redirecting the process's standard input stream. Some programs, particularly when getting passwords, access the terminal driver at a low level for security reasons.

Obfuscating a command within a shell script

There are a lot of tips (and warnings) on here for obfuscating various items within scripts.
I'm not trying to hide a password, I'm just wondering if I can obfuscate an actuall command within the script to defeat the casual user/grepper.
Background: We have a piece of software that helps manage machines within the environment. These machines are owned by the enterprise. The users sometimes get it in their heads that this computer is theirs and they don't want "The Man" looking over their shoulders.
I've developed a little something that will check to see if a certain process is running, and if not, clone it up and replace.
Again, the purpose of this is not to defeat anyone other than the casual user.
It was suggested that one could echo an octal value (the 'obfuscated' command) and use it as a variable within the script. e.g.:
strongBad=`echo "\0150\0157\0163\0164\0156\0141\0155\0145"`
I could then use $strongBad within the shell script to slyly call the commands that I wanted to call with arguments?
/bin/$strongBad -doThatThingYouDo -DoEEET
Is there any truth to this? So far it's worked via command line directly into shell (using the -e flag with echo) but not so much within the script. I'm getting unexpected output, perhaps the way I'm using it?
As a test, try this in the command line:
strongBad=`echo -e "\0167\0150\0157"`
And then
$strongBad
You should get the same output as "who".
EDIT
Upon further review, the addition of the path to the echo command in the variable is breaking it. Perhaps that's the source of my issue.
You can do a rotate 13 on any command you want hidden beforehand, then just have the the obfuscated command in the shell script.
This little bash script:
#!/bin/bash
function rot13 {
echo "$#" | tr '[a-m][n-z][A-M][N-Z]' '[n-z][a-m][N-Z][A-M]'
}
rot13 echo hello, world!
`rot13 rpub uryyb, jbeyq!`
Produces:
rpub uryyb, jbeyq!
hello, world!

Resources