Bash command returns string to shell [duplicate] - bash

This question already has an answer here:
How do I place stdout on edit line?
(1 answer)
Closed 3 years ago.
I run multiple bash script for FFmpeg. However, since I have several windows open, I lose track of what file I ran since I execute the file directly from the script there is no history of what I ran. The only history there is the first file I called
for example:
enter tv number i.e 19: 19
stream key: key
Press [enter] to execute tv19
I press enter, and it runs tv19 but there is no record I ran tv19 in that window, so how can I echo the command to the $
like this
[ibrod ~]$ ./tv19
than I can press enter and use the up arrow than I know what file I ran in that putty window.

Thanks for the help, I figured another way. I change the prompt like [tv10 ~]$
just made a file changetv i put inside
PS1="[$1 \W]\$ "
so I run the file and call it as
. ./changetv tv10
so now I know what window is what. :)

Related

Unable to run xdotool commands from within a bash script [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 11 months ago.
I've recently been having some trouble with xdotools and bash scripting. I've dipped my toes into make my Linux install look a little bit nicer, and so I decided to have a bash script open up a window upon login to my DE displaying htop. I have managed to automate the process of opening the window, but I am unable to move the windows because I cannot get the proper window ID as the terminal reports this;
./htop.sh: line 5: search: command not found
Obviously "htop" is the name of the file and "search" is the command I am trying to run to get the window id. Also, to provide some context to this with my code:
#!/bin/bash
# displays 'htop' in the bottom right corner of the screen
xfce4-terminal --hide-borders --hide-toolbar --hide-menubar --title=HTOP --command="bash -c 'clear;htop;$SHELL'" &
WINDOWID=xdotool search --name "HTOP" &
xdotool windowmove $WINDOWID 4526 850 &
Anyways, whenever I run the line,
xdotool search --name "HTOP" # HTOP is the title of the terminal window I open
within my terminal everything works just fine, and as long as the window that the script opens is actually open, it spits out the window ID that I need to further preform the "windowmove" command. So I guess my question is; is this just a bug of xdotool that you cannot preform the functions from withing a bash script? or did I just mess up my syntax somewhere?
WINDOWID=xdotool search --name "HTOP" &
What you are doing here is assigning the string "xdotool" to the variable $WINDOWID.
As the string is followed by a space, your shell interprets everything after the space as a separate command.
If you want to assign the output of a command to a variable you can do that like this:
WINDOWID=&(xdotool search --name "HTOP")
Or by using the deprecated way with backticks:
WINDOWID=`xdotool search --name "HTOP"`
Also note that it makes no sense to run the commands in your script in background (&). Each command relies on the previous to produce a correct result, so what you actually want to do is run them in series, meaning without the trailing &.

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.

Emacs: Shell command on region without prompt for user input

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

Can a bash script prepopulate the prompt with a command to run when it exits? [duplicate]

This question already has an answer here:
How do I place stdout on edit line?
(1 answer)
Closed 3 years ago.
I run multiple bash script for FFmpeg. However, since I have several windows open, I lose track of what file I ran since I execute the file directly from the script there is no history of what I ran. The only history there is the first file I called
for example:
enter tv number i.e 19: 19
stream key: key
Press [enter] to execute tv19
I press enter, and it runs tv19 but there is no record I ran tv19 in that window, so how can I echo the command to the $
like this
[ibrod ~]$ ./tv19
than I can press enter and use the up arrow than I know what file I ran in that putty window.
Thanks for the help, I figured another way. I change the prompt like [tv10 ~]$
just made a file changetv i put inside
PS1="[$1 \W]\$ "
so I run the file and call it as
. ./changetv tv10
so now I know what window is what. :)

How can I visually select, then execute shell commands directly in vim? [duplicate]

This question already has answers here:
Execute current line in Bash from Vim
(7 answers)
Closed 7 years ago.
When writing bash scripts in vim, it would be useful to be able to selectively run only a few lines from the script, e.g. if I have some script
#! /bin/bash
# more commands
mkdir /tmp/test_dir
echo "some output to STDOUT"
touch /tmp/test_dir/new_file
# more commands
say I just want to execute this part of the script
mkdir /tmp/test_dir
echo "some output to STDOUT"
touch /tmp/test_dir/new_file
how can I highlight and execute it in vim?
I have tried
v, select target text, :
which gives a prompt with :'<, '>
then tried :! w
but its not working properly.
I dont need to write the STDOUT of the shell commands to a file or anything, but I would like to be able to at least see the output.
From the answer of pacholik, you can extrapolate:
in visual mode, hit : and write w !bash
so that your whole command is :'<,'>w !bash
You will get the output as a result (but it won't change the file
If you remove the w , it will instead replace the line by the output of the buffer.
I have for example mapped r to "run command" in visual mode.
Like this:
vnoremap r :w !bash<cr>
I also have this in normal mode (to run the current line), with yr (you run)
nnoremap yr :.w !bash<cr>
You can use Quickrun plugin : http://vimawesome.com/plugin/quickrun-vim
It can run many languages on the fly (bash, python, c++...), and you can run only a selected area.
Everything you need is :
Set the filetype of your file (normally it's automatically detected, unless you created a new file, in this case just do for example : :set ft=sh for bash)
Select a part of your file with V.
Run :'<,'>QuickRun
The output opens in a new window.
You can tweak the plugin on many points, see the help.

Resources