listing current working directory when seeing all open shells in a screen session - gnu-screen

I use screen and start a couple of shells within it. Now, if I list all shells using Ctrl-a " , then, all the windows are listed with a generic "bash" label. How can I change this, for e.g. so that they also list the current working directory of that window also.

Never mind, I figured it out. What is displayed via the Ctrl-A " sequence is the xterm window name, so you have to set that to something else.
So, for bash, put this in the .bashrc
PROMPT_COMMAND='
if [ $TERM = "screen" ]; then
echo -n -e "\033k$MYPWD\033\\"
fi
'

Related

Revisited: How to launch a KDE konsole with multiple tabs running various progs?

As the title strongly suggests, I have already read How to launch a KDE konsole with multiple tabs running various progs?, and the answer is just what I need - except, I want to set the remote title, and I don't find any options for that. I can see elsewhere that it should be possible to do from the command line, but I'd rather use this config-file method, if possible. Is there an option for that?
This works for me cmd;
konsole --tabs-from-file `/settings/s-tab-file
cat /settings/s-tab-file
title: Pay6 1 ;; command: ~/bin/pay_index # scripts that call functions
title: Sal6 2 ;; command: ~/bin/sales_index # <---[ scripts fails unless contains #! /bin/sh ]---
title: Led6 3 ;; command: ~/bin/ledger_index
above is 3 separate lines, i had to put a space line because post
turned them into one long line.
worked great right up to kubuntu 18.04 3 konsole tabs as expected
problem I'm having now is Kubuntu 20.04 is giving me a 4th trailing empty konsole
that looks stupid and has to be removed to look proper.

How to automate opening multiple ssh servers with distinct session name in iterm2?

I am automating my developing setup and found an appplescript that opens multiple terminals and ssh into different servers. It works great but I want to change the tab names so that I can distinguish between tabs.
First make sure that the profile that you are using has the following "Terminal" option set:
Now you are set the tab and/or window title:
tell current session
write text "echo -ne \"\\033]1; A Tab Title \\007\""
write text "echo -ne \"\\033]2; A Window Title \\007\""
end tell

Zsh trouble when using echo with color/formatting characters

I'm just switch to zsh and now adapting the alias in which was printing some text (in color) along with a command.
I have been trying to use the $fg array var, but there is a side effect, all the command is printed before being executed.
The same occur if i'm just testing a echo with a color code in the terminal:
echo $fg_bold[blue] "test"
]2;echo "test" test #the test is in the right color
Why the command print itself before to do what it's supposed to do ? (I precise this doesn't happen when just printing whithout any wariable command)
Have I to set a specific option to zsh, use echo with a special parameter to get ride of that?
Execute the command first (keep its output somewhere), and then issue echo. The easiest way I can think of doing that would be:
echo $fg[red] `ls`
Edit: Ok, so your trouble is some trash before the actual output of echo. You have some funny configuration that is causing you trouble.
What to do (other than inspecting your configuration):
start a shell with zsh -f (it will skip any configuration), and then re-try the echo command: autoload colors; colors; echo $fg_bold[red] foo (this should show you that the problem is in your configuration).
Most likely your configuration defines a precmd function that gets executed before every command (which is failing in some way). Try which precmd. If that is not defined, try echo $precmd_functions (precmd_functions is an array of functions that get executed before every command). Knowing which is the code being executed would help you search for it in your configuration (which I assume you just took from someone else).
If I had to guess, I'd say you are using oh-my-zsh without knowing exactly what you turned on (which is an endless source of troubles like this).
I don't replicate your issue, which I think indicates that it's either an option (that I've set), or it's a zsh version issue:
$ echo $fg_bold[red] test
test
Because I can't replicate it, I'm sure there's an option to stop it happening for you. I do not know what that option is (I'm using heavily modified oh-my-zsh, and still haven't finished learning what all the zsh options do or are).
My suggestions:
You could try using print:
$ print $fg_bold[red] test
test
The print builtin has many more options than echo (see man zshbuiltins).
You should also:
Check what version zsh you're using.
Check what options (setopt) are enabled.
Check your ~/.zshrc (and other loaded files) to see what, if any, options and functions are being run.
This question may suggest checking what TERM you're using, but reading your question it sounds like you're only seeing this behaviour (echoing of the command after entry) when you're using aliases...?

Prevent bash from forwarding stdin to stderr

I'm trying to implement a simple terminal GUI using bash's interactive mode. I successfully invoked bash, get its stdout and print everything to a text view. I forward the user input from the text view to bash's stdin, to be able to run commands. It works great, except I don't get any error messages.
However, when I proceeded to print bash's stderr to my text view, I noticed something strange. In addition to now receiving error messages, bash seems to pass everything from stdin to stderr. Because of this, every character I type is printed twice (once normally because I enter it, and once because I print everything from stderr).
It also seems to print the prompt via stderr (bash-3.2$). Is this the expected behavior? Can this be suppressed?
I also tried to just capture use input (and not let the user type directly into the text view) and rely on bash to print the user input. This is almost working, except the order of the output via stdout and stderr is random:
If I type a command like echo test and hit enter, sometimes I get this:
(the second test is the output, I didn't type testtest)
bash-3.2$ echo testtest
bash-3.2$
Sometimes I get:
bash-3.2$ echo test
bash-3.2$ test
The order in which I receive the final \n, the output and the next bash-3.2$ is obviously mixed up.
There is no way to read stdout and stderr in the "correct" order, because there is no notion of order between different pipes. But you can ensure that both are sent to the same pipe (i.e. same file descriptor) instead of having each one go to a separate pipe. To do that, look on the options of whatever you use to start the bash subprocess; or maybe start a command line like bash -c 'bash 2>&1'.

running parts of shell script in background

I have a shell script which asks for user input and depending on the input opens db connection using sqlplus and run some sql querys like drop table /create table/select/update. Is it possible that the sql part be run as background job,so that even if i lose vpn connectivity to the network,all the sql queries gets executed.
Also ,when the sql parts gets completed and user is prompted with another input the shell script comes to foreground and after getting the input again goes to background?
I have found some questions which tell us how to run the script in background,but i want to run ONLY some parts of the same script in background if possible(and come to foreground for user input).Though i can make multiple scripts too handle it(dividing the scripts in parts which needs to be called in background and calling them though another script),i would rather do it in a single script if possible.
You can break your main script up into functions / smaller scripts to achieve the desired behavior of a mix of background processes and foreground processes.
For example, in your main script:
#!/bin/sh
echo "Starting script..."
# do so more stuff here, maybe ask user for input
./run_background_process_1 &
# ask the user for some more input
./run_background_process_2 &
...
Use the & symbol at the end of script calls to denote that they should be run in the background.
(Updated) If you'd like to keep everything in 1 script, use functions to break up / encapsulate the parts of logic that you would like to run in the background. Call these functions by suffixing the call with &, same as above.
You can try the following example to see that it works:
#!/bin/sh
hello() {
condition="yes"
while [[ $condition== "yes" ]]
do
echo "."
sleep 1
done
}
# Script main starts here
echo "Start"
hello &
echo "Finish"
Remove the & after hello and you'll see that it behaves differently.
There are tools which allow you to keep scripts running despite loss of connection. For example, check out http://www.gnu.org/software/screen/ - one of its features is Programs continue to run when their window is currently not visible and even when the whole screen session is detached from the users terminal.
After search on internet i found out i can use three methods to make the script background :
1) using bg: How do I put an already-running process under nohup? .but unfortunately ,this didnt worked for me in ksh shell.
2) using coprocesses
3) using nohup
I decided to go with nohup as it was easier to implement. The sqlplus part which needed to be run in background ,i made another script of it and called it from the main script using nohup
nohup script-name.ksh ${parameter1} ${paramter2} &
This worked for me.