You are not logged in.

#1 2022-02-06 16:34:07

sleekmason
zoom
Registered: 2018-05-22
Posts: 873
Website

[Solved] How can I keep terminal open for commands during script?

How do I script this so the terminal won't close? I would rather not use more than one script if I can help it.

In this script, I'm checking for updates, popping up a yad dialog if no updates are to be found, and/or starting a proper terminal to continue the upgrade if there are.

As it is, after the terminal opens, pressing enter just shuts the whole window as the first command is completed.

What can I do to fix this?

#!/bin/bash
## Simple script to update, list, and then upgrade your system.
## If no updates exist, exit.

# updates
updates=$(apt-get dist-upgrade -s --quiet=2 | grep ^Inst | wc -l)

# If 0
if [[ "$updates" -eq 0 ]];
then yad --title "Simple Apt Updater" --window-icon=/usr/share/icons/ld-icons/upgrade.png \
--width=310 --height=180 --center \
--text-info --justify=center --wrap < /usr/share/lilidog/simple-apt-updater.txt --button="OK" --fontname="Dejavu Sans 12" && exit

else
x-terminal-emulator -T 'Simple Apt Updater' -e bash -c "read -p '


                ------ SIMPLE APT UPDATER ------
                

   A small script to update and upgrade your system using apt.
   
   
   
   -----------------------------------------------------------
   
                  Please press Enter to begin.
   
   -----------------------------------------------------------
   
   
   
   Or close this window to quit.
   
   -----------------------------'";
echo ""  
echo ""
sudo apt update && sudo apt list --upgradeable
echo ""
echo "   ------------------------------------------------------------"
echo ""
read -p "   Scroll Above to see the list of all packages to be upgraded.
   If no packages are shown, then there are none to upgrade.

   ------------------------------------------------------------


   Press Enter to continue with the upgrade, or close this
   window to quit.
   
   ------------------------------------------------------------" ;
echo ""
echo ""
sudo apt upgrade
echo ""
echo ""

fi

Last edited by sleekmason (2022-02-08 16:20:05)

Offline

#2 2022-02-06 20:04:42

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,464
Website

Re: [Solved] How can I keep terminal open for commands during script?

I. I would put the script into file and run that like

urxvt -hold -e bash -c "yourscript"

Specific switches may only work with specific terminal.
edit: Due to interactive nature of the script this -hold thing is probably moot.

II. Smarter version would be to script put the exit behavior into the script, for example:
a. if no errors, wait 10 seconds and close (sleep 10)
b. if errors, wait a lot longer with some error message
(In this case omit -hold)
edit: Due to interactive nature of the script this 2nd example is probably moot.

Last edited by brontosaurusrex (2022-02-06 20:17:35)

Online

#3 2022-02-06 20:58:40

sleekmason
zoom
Registered: 2018-05-22
Posts: 873
Website

Re: [Solved] How can I keep terminal open for commands during script?

Yep, this has come up a few times now where I wanted to just isolate a portion of the script to be called separately in order to keep things all in one script. Well, no worries, as two scripts works fine, but sure would like to find out the answer:)

Would certainly broaden things a bit I think.  Interactive stuff is fun.

Last edited by sleekmason (2022-02-06 21:28:16)

Offline

#4 2022-02-07 02:49:56

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 8,971
Website

Re: [Solved] How can I keep terminal open for commands during script?

How about calling back the original script with an option? Like:

#....
else
x-terminal-emulator -T 'Simple Apt Updater' -e "$0" --upgrade

And your script looks for --upgrade eg:

case $1 in
'')
    # do the early stuff
    ;;
--upgrade)
    # do the upgrade stuff in the terminal
    ;;
esac

Haven't tested this at all though.


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#5 2022-02-07 14:32:40

sleekmason
zoom
Registered: 2018-05-22
Posts: 873
Website

Re: [Solved] How can I keep terminal open for commands during script?

I am understanding the concept a bit, though not sure where I should be adding case $1 in, whether before or after 'if [[ "$updates" -eq 0 ]];', and it seems I may be missing something else as well?  I looked for examples and tried to follow them but wound up right where I am now.

#!/bin/bash

## Simple script to update, list, and then upgrade your system.
## If no updates exist, exit.

# updates
updates=$(apt-get dist-upgrade -s --quiet=2 | grep ^Inst | wc -l)

case $1 in
# If 0
if [[ "$updates" -eq 0 ]];
then yad --title "Simple Apt Updater" --window-icon=/usr/share/icons/ld-icons/upgrade.png \
--width=310 --height=180 --center \
--text-info --justify=center --wrap < /usr/share/lilidog/simple-apt-updater.txt --button="OK" --fontname="Dejavu Sans 12" && exit
else
x-terminal-emulator -T 'Simple Apt Updater' -e "$0" --upgrade)
;;


--upgrade)

"read -p '


                ------ SIMPLE APT UPDATER ------
                

   A small script to update and upgrade your system using apt.
   
   
   
   -----------------------------------------------------------
   
                  Please press Enter to begin.
   
   -----------------------------------------------------------
   
   
   
   Or close this window to quit.
   
   -----------------------------'" ;
echo ""  
echo ""
sudo apt update && sudo apt list --upgradeable
echo ""
echo "   ------------------------------------------------------------"
echo ""
read -rp "   Scroll Above to see the list of all packages to be upgraded.
   If no packages are shown, then there are none to upgrade.

   ------------------------------------------------------------


   Press Enter to continue with the upgrade, or close this
   window to quit.
   
   ------------------------------------------------------------" ;
echo ""
echo ""
sudo apt upgrade
echo ""
echo ""
fi
;;
esac

Here is the shellcheck as well.

Line 10:
case $1 in
^-- SC1009 (info): The mentioned syntax error was in this case expression.
 
Line 12:
if [[ "$updates" -eq 0 ]];
^-- SC1073 (error): Couldn't parse this case item. Fix to allow more checks.
   ^-- SC1072 (error): Expected ) to open a new case item. Fix any mentioned problems and try again.
   ^-- SC1085 (error): Did you forget to move the ;; after extending this case item?

Offline

#6 2022-02-08 00:40:47

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 8,971
Website

Re: [Solved] How can I keep terminal open for commands during script?

Case doesn't work like that - read 'help case'. smile

OK here's my take on how it might work with your script, but it'll need testing on your end and probably debugging:

#!/bin/bash
## Simple script to update, list, and then upgrade your system.
## If no updates exist, exit.

# check arguments

case $1 in
'') # no argument

   # updates
   updates=$(apt-get dist-upgrade -s --quiet=2 | grep ^Inst | wc -l)

   # If 0
   if [[ "$updates" -eq 0 ]];
   then
      yad --title "Simple Apt Updater" --window-icon=/usr/share/icons/ld-icons/upgrade.png \
      --width=310 --height=180 --center \
      --text-info --justify=center --wrap < /usr/share/lilidog/simple-apt-updater.txt --button="OK" --fontname="Dejavu Sans 12" && exit
   else
      x-terminal-emulator -T 'Simple Apt Updater' -e "$0" --upgrade # $0 is this script
   fi
   ;;
--upgrade) # now running in terminal
   echo '
                ------ SIMPLE APT UPDATER ------


   A small script to update and upgrade your system using apt.



   -----------------------------------------------------------

                  Please press any key to begin.

   -----------------------------------------------------------



   Or close this window to quit.

   -----------------------------'
   read -srn1
   echo ""
   echo ""
   sudo apt update && sudo apt list --upgradeable
   echo ""
   echo "   ------------------------------------------------------------"
   echo ""
   read -r -p "   Scroll Above to see the list of all packages to be upgraded.
   If no packages are shown, then there are none to upgrade.

   ------------------------------------------------------------


   Press Enter to continue with the upgrade, or close this
   window to quit.

   ------------------------------------------------------------"
   echo ""
   echo ""
   sudo apt upgrade
   echo ""
   echo ""
   ;;
esac

This passed shellcheck OK but I haven't actually tried running it. As bronto said, terminals don't all honour the x-terminal-emulator spec properly so you might hit glitches depending on what you're actually using.

You're not using BL I guess, but we ship a function called terminalCheck() in /usr/lib/bunsen/common/bl-includes that tries to work around some issues and pop up a terminal when needed by a script.

EDIT
I usually use a snippet like this to offer the user a chance to exit:

echo "Press enter to continue,
any other key to exit"
read -srn1
[[ -n $REPLY ]] && { echo "bye" ; exit 1;}

Last edited by johnraff (2022-02-08 01:01:11)


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#7 2022-02-08 01:27:56

sleekmason
zoom
Registered: 2018-05-22
Posts: 873
Website

Re: [Solved] How can I keep terminal open for commands during script?

Wow yeah, I wasn't even close on the placement.  Yours works though:) This should allow me to go back and fix a couple of other scripts as well. Nice!

I am enthusiastically looking at the terminalcheck portion of the bl-includes script and also found a few other sites that look very interesting.  More enlightenment.  Love it.

Thank you for all your help:)

Offline

Board footer

Powered by FluxBB