i have 3-4 read command in sequence and i have to press enter after the execution of every read command. I want to automate that press keyboard enter key after a read command is executed in my shell script.
You mean something like that?
yes | your_script.sh
Related
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.
Suppose I editing a file that ends with a ".sh" (bash)
I select a region with ESC Shift-| which leads to the minibuffer
asking me "Shell command on region:". I don't want to say "bash" all the time but instead would like "bash" to be executed without prompt for user input. Is this possible?
Thanks
I have a bash script which shows a MENU when is executed.So, the user can select an option (1,2,3,4,...).
But now I also need to program a crontab that execute the script and automatically selects option 2 in the menu.
Any way to do that? Lice executing the script and doing and echo "2"?
You've got a few options. One is expect which allows you to expect and respond to the program. The other is simply piping into the program, for example, echo "2" | /path/to/your/script.sh. Alternately, I would simply recommend that you modify your script to simply accept parameters.
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
Example: I want to bind the F12 key to the command echo "foobar" such that every time I hit F12 the message "foobar" will be printed to screen. Ideally it could be any arbitrary shell command, not just builtins. How does one go about this?
You can determine the character sequence emitted by a key by pressing Ctrl-v at the command line, then pressing the key you're interested in. On my system for F12, I get ^[[24~. The ^[ represents Esc. Different types of terminals or terminal emulators can emit different codes for the same key.
At a Bash prompt you can enter a command like this to enable the key macro so you can try it out.
bind '"\e[24~":"foobar"'
Now, when you press F12, you'll get "foobar" on the command line ready for further editing. If you wanted a keystroke to enter a command immediately, you can add a newline:
bind '"\e[24~":"pwd\n"'
Now when you press F12, you'll get the current directory displayed without having to press Enter. What if you've already typed something on the line and you use this which automatically executes? It could get messy. However, you could clear the line as part of your macro:
bind '"\e[24~":"\C-k \C-upwd\n"'
The space makes sure that the Ctrl-u has something to delete to keep the bell from ringing.
Once you've gotten the macro working the way you want, you can make it persistent by adding it to your ~/.inputrc file. There's no need for the bind command or the outer set of single quotes:
"\e[24~":"\C-k \C-upwd\n"
Edit:
You can also create a key binding that will execute something without disturbing the current command line.
bind -x '"\eW":"who"'
Then while you're typing a command that requires a username, for example, and you need to know the names of user who are logged in, you can press Alt-Shift-W and the output of who will be displayed and the prompt will be re-issued with your partial command intact and the cursor in the same position in the line.
Unfortunately, this doesn't work properly for keys such as F12 which output more than two characters. In some cases this can be worked around.
The command (who in this case) could be any executable - a program, script or function.
You can define bash key bindings in ~/.inputrc (configuration file for the GNU Readline library). The syntax is
<keysym or key name>: macro
for example:
Control-o: "> output"
will create a macro which inserts "> output" when you press ControlO
"\e[11~": "echo foobar"
will create a macro which inserts "echo foobar" when you press F1... I don't know what the keysym for F11 is off hand.
Edit:
.inputrc understands the \n escape sequence for linefeed, so you can use
"\e[11~": "echo foobar\n"
Which will effectively 'press enter' after the command is issued.
This solution is specific to X11 environments and has nothing to do with bash, but adding the following to your .Xmodmaps
% loadkeys
keycode 88 = F12
string F12 = "foobar"
%
will send the string "foobar" to the terminal upon hitting F12.
I wanted to bind Ctrl+B to a command. Inspired by an answer above, I tried to use bind but could not figure out what series of cryptic squiggles (\e[24~ ?) translate to Ctrl+B.
On a Mac, go to Settings of the Terminal app, Profiles -> Keyboard -> + then press the keyboard shortcut you're after and it comes out. For me Ctrl+B resulted in \002 which i successfully bound to command
bind '"\002":"echo command"'
Also, if you want the command to be executed right-away (not just inserted in to the prompt), you can add the Enter to the end of your command, like so:
bind '"\002":"echo command\015"'