Create window into screen - linux

Is there a way to list all window names and depending on the result, creating a new window with a specific name into this (running) session.
How to create a new screen session with assigned window names is documented in the man pages, but i could find information about a solution to the problem above.

From outside the screen session, I don't think so.
But if you are starting from inside, in one of the windows of the right screen session, then yes:
for window_name in foo bar baz quux ; do ## ...
screen -t $window_name
done
You can even get fancy and run some initial commands in each window! This snipped of copy-paste bash helps me get back to work quickly after a reboot. Once I've started the screen session:
for n in $(seq 1 8) ; do ## ...
screen -t proj_$n bash -c "cd /src/foo/proj_$n*/ ;"\
' eval `set_proj_env_vars.sh` ; svn status ; make clean ; make ;'\
' exec bash --login'
done
...and as a great side effect the screen windows are numbered for the various checkouts, where each one can be working on a different bug/feature. Overkill? Totally! But it's a fun hack.

Related

gnome-terminal: How to close the original tab?

Just wrote a script that would open 3 new tabs in a window.
tab_init.sh:
#!/bin/bash
# options="--hide-menubar --geometry=140x42"
options=""
options_each=()
# tabs
cmds[1]="cd ~/a; clear"
cmds[2]="cd ~/b; clear"
cmds[3]="cd ~/c; clear"
for i in 1 2 3; do
options_each+=(--tab -e "bash -c '${cmds[i]} ; bash'" )
done
gnome-terminal $options "${options_each[#]}" &
exit 0
Current result:
After executing the script, there will be 3 more tabs opened in current window, plus the original tab, there would be 4 tabs.
Desired result:
But what I want is to open the new tabs in a standalone window, without including any other tab.
The questions are:
How to close the original tab automatically from the script, so that there would be only 3 tabs(the new ones) after executing the script?
Or, can I open the 3 new tabs in a new window, not in the original window, so that it doesn't matter whether I close the original tab.
Kind of found a way to do this.
First, make soft link of the script into $PATH.
e.g link it as tab_init somewhere,
Then, define command shortcuts in ~/.bashrc, to add the extra behavior.
e.g
alias tabinit='tab_init; exit' # init tabs, and close original tab,
alias tabinitne='tab_init' # init tabs, and keep the original tab,
Now, could use command tabinit or tabinitne to choose the behavior desired.

Display a running clock in terminal using bash without using loop

How to display a running clock in a linux terminal without using any for or while loop. Using these loops in scripts with 1 sec duration causes a significant load in the systems.
How about:
watch -n 1 date
Use watch to run a command periodically.
You can make a function which calls itself with a sleep :
#!/bin/bash
function showdate(){
printf '\033[;H' # Move the cursor to the top of the screen
date # Print the date (change with the format you need for the clock)
sleep 1 # Sleep (pause) for 1 second
showdate # Call itself
}
clear # Clear the screen
showdate # Call the function which will display the clock
If you execute this script it will run indefinitely until you hit CTRL-C
If you want to add the date to the terminal window title and your terminal supports it, you can do
#! /bin/bash
while :
do
printf '\033]0;%s\007' "$(date)"
sleep 1
done &
and it won't affect any other terminal output.
This post is ancient, but for anyone still looking for this kind of thing:
#!/bin/bash
printf '\033[;H'
while true; do date; sleep 1; done
This version avoids recursion (and stack faults!) while accomplishing the goal.
(For the record - I do prefer the watch version, but since the OP did not like that solution, I offered an improvement on the other solution.)

How to check is maximized any window in XFCE?

I want to change top panel color and alpha when any window is maximized.
For now I have something like this:
#!/bin/bash
while [ 1 = 1 ]
do
if window_is_maximized
then
xfconf-query -c xfce4-panel -p /panels/panel-0/background-alpha -s 100
else
xfconf-query -c xfce4-panel -p /panels/panel-0/background-alpha -s 50
fi
done
Maximized windows in X do not have a special state that you can test reliably. From a script, you can use xwininfo:
You can check if the window happens to be the same size as the root (main) window, and its position is the upper-left corner.
If you happen to be using a window manager which supports certain EMWH properties (_NET_WM_STATE_FULLSCREEN, _NET_WM_STATE_MAXIMIZED_VERT, _NET_WM_STATE_MAXIMIZED_HORZ), your script could check for those. But in a quick check for window managers which might do that, I found none.

bash script loses focus when displaying images

I am trying to figure out how to not lose focus in a shell script while simultaneously displaying an image. User input may come at any time, but seeing the photo taken, would seem important.
To Clarify, I have no problem outputing an image. display works fine, as does animate, and feh, etc.. what i need is for the shellscript to still process user input, (in this example, "t") while displaying the last image taken, for an undefined amount of time.
I'm writing in bash, in Linux.
Heres an example of what I'm trying:
#!/bin/bash
i=0
capture() {
cd ~/Desktop/ani
streamer -c /dev/video0 -s 800x600 -o outfile$i.jpeg
display outfile$i.jpeg &
let i++
}
while true; do
#clear
read -rsn1 input
if [ "$input" = "t" ]; then
capture
else
exit
fi
done
In the actual script I may continue to take photos, so I want to continue listening for user input. I can imagine a couple ways to do this, but I cannot figure it out.
To continue listening user input. you can do like
while true; do
#clear
read -p "Your input: " input
if [ "$input" == "t" ]; then
capture
fi
done
A rather ugly way to solve this: install the utility wmctrl (in debian/Ubuntu, sudo apt-get install wmctrl). Then, after your display command, add:
sleep 1
wmctrl -i -a "$WINDOWID"
This will sleep for one second (to leave some time to the display command to finish loading—tune this value to whatever feels right to you). Then, wmctrl will use the value of the variable WINDOWID (that is hopefully set by your terminal emulator) as a numeric value (-i) and raise the window and give it focus (-a).

current directory doesn't appear in title bar when running under screen

My xterm $prompt variable in my .tcshrc is:
set prompt="%{\033]0;%m:%~\007%}%{^[[;37;1m%}%B%{^[[;34;1m%}%m%{^[[;34;1m%}:%b%c%# "
The highlighted part above (%{\033]0;%m:%~\007%}) puts the hostname (%m) and the current directory (%~) in the title bar. (At least I think that that's what puts it in the title bar; it's been a while since I fiddled with this prompt).
When I run screen, however, the current directory stops getting updated when I change directories.
My questions:
How can I make this prompt work in screen?
Is there a better way to display the current directory in the title bar?
I am running linux with xterm and tcsh.
I think there is no direct way, because of the way screen works. However screen can display its own status bar, that you can define in .screenrc. Here's mine for instance :
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%=%{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]'
Firstly, to make it work you must check where exactly is the line with set prompt=blah-blah in your .tcshrc. For example, the code below that perfectly works in plain xterm would not work under screen in xterm:
switch ($TERM)
case "xterm*":
set prompt="%{\033]0;${HOME:t}#%m:%l:%c08\007%}%{\033[36m%}%l:%c02%#%{\033[0m%} "
# update xterm title to display current cmd in it
alias postcmd 'echo -n "\033]0;${HOME:t}#`hostname`:${tty} \!#:q\007"'
...
because screen by default sets $TERM variable to screen and not xterm! So you must add:
case "screen":
# lame, but prevents an error in screen after 'su - root'
if (! $?WINDOW) setenv WINDOW 1
set prompt="%{\033]0;${HOME:t}#%m:${WINDOW}:%c08\007%}%{\033[36m%}%c02%#%{\033[0m%} "
alias postcmd 'echo -n "\033]0;${HOME:t}#`hostname`:${WINDOW} \!#:q\007"'
...
Secondly, make sure yo have this line in ~/.screenrc:
termcapinfo xterm* 'hs:ts=\E]2;:fs=\007:ds=\E]2;\007'

Resources