14 Commits

Author SHA1 Message Date
Clemens Schwaighofer
68b18f497a Screen name allow - next to letters and numbers 2025-12-25 11:20:10 +09:00
Clemens Schwaighofer
729124e985 change string for init line 2025-05-30 09:52:52 +09:00
Clemens Schwaighofer
d9ca21e807 Fix not needed screen pos in run command output 2025-05-30 09:52:03 +09:00
Clemens Schwaighofer
1517103c60 Forgot line break at the end of printf calls 2025-05-30 09:49:33 +09:00
Clemens Schwaighofer
f61e7e55bb fix right align in printf 2025-05-30 09:48:50 +09:00
Clemens Schwaighofer
369dcb7329 screen init output run update 2025-05-30 09:47:16 +09:00
Clemens Schwaighofer
e068a6c659 Update info text for create/skip 2025-05-30 09:40:11 +09:00
Clemens Schwaighofer
7ca5f973d4 Fix the -n -> -z 2025-05-30 09:37:28 +09:00
Clemens Schwaighofer
298dc19a9a Merge branch 'master' of git.tokyo.tequila.jp:ScriptsCollections/ScreenInit 2025-05-30 09:37:15 +09:00
Clemens Schwaighofer
95a5cda81c Move the skip check for lines in the past first row check
Add info on skipped line
2025-05-30 09:36:21 +09:00
Clemens Schwaighofer
9293f6cd00 Fix wrong empty check
used -n instead of -z
2025-05-30 09:35:09 +09:00
ff9e95ff9a Add ignore line and skip line, more name checks
lines starting with ";" are skipped, empty lines are ignored
if the first line is empty (no name set) an error is thrown
check that title is alphanumeric only

Add a config folder for the config files (ignored)

update readme file
2025-05-30 09:29:06 +09:00
Clemens Schwaighofer
3771fd987c Markdown fix in Readme file 2024-11-20 23:50:29 +09:00
Clemens Schwaighofer
88d955aab8 Add shellcheckrc, clean up code 2024-09-03 12:47:27 +09:00
4 changed files with 48 additions and 30 deletions

2
.shellcheckrc Normal file
View File

@@ -0,0 +1,2 @@
shell=bash
external-sources=true

View File

@@ -6,11 +6,16 @@ Init screens from config files
## Config file layout
```
```txt
<screen session name>
<window name>#<command>
<window name>#<command>;<command>;
#
;<ignored line>
```
`screen session name` is the name you use for gstting the screen session name, see `screen -ls`. Must be Alphanumeric without spaces.
`window name` is the name for this window
`command` can be any comamnd sequence separated by `;`. If nothing given default `^C` shell is used
- `screen session name` is the name you use for gstting the screen session name, see `screen -ls`. Must be Alphanumeric without spaces and cannot be empty
- `window name` is the name for this window
- `command` can be any comamnd sequence separated by `;`. If nothing given default `^C` shell is used
- If there is only a `#` an empty unnamed window is opened
- if a line starts with `;` it is skipped
- empty lines are ignored

2
configs/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
*
!.gitignore

View File

@@ -25,8 +25,7 @@
set -e
ERROR=0;
if [ ! -f "$1" ];
then
if [ ! -f "$1" ]; then
echo "Cannot find screen init config file '$1'";
ERROR=1;
else
@@ -35,11 +34,10 @@ else
fi;
# check if we are in a screen one, if yes, exit
if [ -z "$STY" ];
then
if [ -z "$STY" ]; then
# check if the "work" screen exists
if [ ! -z "$SCREEN_NAME" ] && [[ ! -z $(screen -ls | grep ".$SCREEN_NAME\t") ]];
then
# if [ -n "$SCREEN_NAME" ] && [[ -n $(screen -ls | grep ".$SCREEN_NAME\t") ]];
if [ -n "$SCREEN_NAME" ] && screen -ls | grep -q ".$SCREEN_NAME\t"; then
echo "Screen '$SCREEN_NAME' already exists";
ERROR=1;
fi;
@@ -48,11 +46,9 @@ else
ERROR=1;
fi;
if [ $ERROR -eq 1 ];
then
if [ $ERROR -eq 1 ]; then
exit;
fi;
# termcap (echo $TERMCAP)
# http://web.mit.edu/gnu/doc/html/screen_15.html#SEC89
# in screen ^-a . to dump current termcap
@@ -79,43 +75,56 @@ export SCREENCAP="SC|screen.xterm-256color|VT 100/ANSI X3.64 virtual terminal:\
# read the config file and init the screen
pos=0;
cat "$1" |
while read line;
while read -r line;
do
if [ $pos -eq 0 ];
then
if [ $pos -eq 0 ]; then
# should I clean the title to alphanumeric? (well yes, but not now)
SCREEN_NAME=$line;
if [ -z "$SCREEN_NAME" ]; then
echo "[!] No screen name set";
exit;
fi;
if [[ ! $SCREEN_NAME =~ ^[A-Za-z0-9-]+$ ]]; then
echo "[!] Screen name must be alphanumeric (letters, numbers, and hyphens only): ${SCREEN_NAME}";
exit;
fi;
# check that we do not create double entries
if screen -list | grep -q "${SCREEN_NAME}"; then
echo "Screen with ${SCREEN_NAME} already exists";
echo "[!] Screen with ${SCREEN_NAME} already exists";
exit;
fi;
else
# screen number is pos - 1
SCREEN_POS=$(( pos-1 ));
# extract screen title and command (should also be cleaned for title)
SCREEN_TITLE=$(echo "$line" | cut -d "#" -f 1);
SCREEN_CMD=$(echo "$line" | cut -d "#" -f 2);
# screen number is pos - 1
SCREEN_POS=$[ $pos-1 ];
# skip lines that start with ";" these are comments, we do not use # as they are separators
if [[ $line =~ ^\; ]]; then
printf "[%2s] [SKIP] '%s' with command '%s'\n" $SCREEN_POS "$SCREEN_TITLE" "$SCREEN_CMD";
continue;
fi;
# skip empty lines
if [ -z "$line" ]; then
continue;
fi;
# for the first screen, we need to init the screen and only set title
# for the rest we set a new screen with title
if [ $pos -eq 1 ];
then
echo "Init screen with title '$SCREEN_NAME'";
if [ $pos -eq 1 ]; then
printf "===> * INIT screen with title '%s'\n" "$SCREEN_NAME";
screen -dmS "$SCREEN_NAME";
# set title for the first
screen -r "$SCREEN_NAME" -p $SCREEN_POS -X title "$SCREEN_TITLE";
else
screen -r "$SCREEN_NAME" -X screen -t "$SCREEN_TITLE" $SCREEN_POS;
fi;
echo "[$SCREEN_POS] Set title to '$SCREEN_TITLE'";
printf "[%2s] + ADD window with title '%s'\n" $SCREEN_POS "$SCREEN_TITLE";
# run command on it (if there is one)
if [ ! -z "$SCREEN_CMD" ];
then
echo "[$SCREEN_POS] Run command '$SCREEN_CMD'";
if [ -n "$SCREEN_CMD" ]; then
printf " > RUN command '%s'\n" "$SCREEN_CMD";
# if ^M is garbled: in vim do: i, ^V, ENTER, ESCAPE
screen -r "$SCREEN_NAME" -p $SCREEN_POS -X stuff $"$SCREEN_CMD^M";
fi;
fi;
pos=$[ $pos+1 ];
done;
pos=$(( pos+1 ));
done <<<"$(cat "${1}")";