LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-26-2017, 12:35 AM   #1
Assomnia
Member
 
Registered: May 2017
Posts: 38

Rep: Reputation: Disabled
Bash script - Input command - Unable to read because error message


Hi everyone,

got a little issue with my bash script. Here is the script so you fully understand what I'm talking about.


Code:
#!/bin/bash
#By Asso'
clear
echo -e
echo -e
echo -e "Welcome\x1b[31m Asso'\x1b[0m : $(date)"
echo -e
echo -e "------------------------------------------------------------"
echo -e
echo -e "      - Press TRIANGLE ▲ - Start Kodi"
echo -e "      - Press SQUARE   ■ - Start EmulationStation"
echo -e "      - Press CIRCLE   \033[1mO\033[0m - StartX"
echo -e "      - Press START      - Perform Updates/Upgrades"
echo -e
echo -e
echo -e
echo -e "               - Press \033[1mX\033[0m - Back to shell"
echo -e
echo -e "------------------------------------------------------------"
echo -e
echo -e "   \E[5mUSE ONLY THE SHORTCUT LISTED ABOVE TO AVOID TROUBLES\E[0m"
echo -e

input() {
  read -s -N 1 answer
  case "$answer" in
    $'\e')
      echo "Kodi is starting"
      start_kodi
      ;;
    'a')
      echo "EmulationStation is Starting"
      start_emulationstation
      ;;
    $'\x7f')
      start_x
      ;;
    's')
      echo "Performing Updates and Upgrades..."
      echo "Don't switch off the PSP..."
      echo "Make sure you are online..."
      sleep 5
      start_updates
      ;;
    *)
      clear
      echo
      echo "Back to Shell !"
      start_exit
      ;;
    esac
}

start_kodi() {
  kodi
  input
}

start_emulationstation() {
  /opt/retropie/supplementary/emulationstation/emulationstation
  input
}

start_x() {
  startx
}

start_updates() {
  sudo apt-get update -y
  sudo apt-get upgrade -y
  sudo apt-get dist-upgrade -y
  sudo apt-get autoremove -y
  clear
  echo
  echo "Updates successful, rebooting..."
  sleep 2
  sudo reboot
}

start_exit() {
  exit 0
}

input

Basically this script runs when I boot my device and wait for a input. The code itself works perfectly everything is just starting perfect and when I exit any of the soft it just come back to the script and wait for the next input.
The problem I have is that for example when I start Kodi and watch something and then Exit Kodi after I have some error message (see the pic). And basically the script is still running and wait for a Input. Problem is that I can't read the script anymore (the ECHO part at the top of the script) because of this error message that take all the space if I can say so.

Click image for larger version

Name:	IMG_136556.jpg
Views:	27
Size:	275.9 KB
ID:	25571

My question is : Is there any way to clear the screen so I'm able to read the script after I exit my soft ? Or when I launch something and then exit to relaunch the script again ?

Possible to know when a process is killed and then relaunch the script ? Or maybe do a variable with the echo part and launch the variable when exit a soft

I don't really have a idea. If anyone can help

Thank you very much and sorry for the title if it is not that accurate was hard to find something to explain that...

Last edited by Assomnia; 07-26-2017 at 07:57 AM.
 
Old 07-26-2017, 12:43 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,841

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
the two functions input and start_kodi invoke each other, it is not really a good idea.
You need to invoke start_<something> from your input function, but you need to return from those funtions back to input instead of invoking it again.
You can do something like this:
Code:
input() {
while :
do
  read -s -N 1 answer
  case "$answer" in
....
done
}

start_kodi() {
  kodi
# no need to call input, just return, but probably need to reset environment
  reset
}
I'm not really sure if that solves this issue but probably helps.

Last edited by pan64; 07-26-2017 at 12:44 AM.
 
Old 07-26-2017, 12:54 AM   #3
Assomnia
Member
 
Registered: May 2017
Posts: 38

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
the two functions input and start_kodi invoke each other, it is not really a good idea.
You need to invoke start_<something> from your input function, but you need to return from those funtions back to input instead of invoking it again.
You can do something like this:
Code:
input() {
while :
do
  read -s -N 1 answer
  case "$answer" in
....
done
}

start_kodi() {
  kodi
# no need to call input, just return, but probably need to reset environment
  reset
}
I'm not really sure if that solves this issue but probably helps.

Hey there

The script is working. But when I leave Kodi the screen is empty and waits for the next input. But I can't read the echo part as everything is just cleared

Thanks


EDIT : I actually added
echo "TEST1"
echo "TEST2"
echo "TEST3"
after RESET and as I said the screen is cleared but it shows the TESTS
Can we maybe variable the echo a the beginning and send this variable after the reset ?

Last edited by Assomnia; 07-26-2017 at 12:59 AM.
 
Old 07-26-2017, 01:21 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Both solution simply call a read once the previous command has exited, ie. wait for next input.

Maybe you need to put your menu, ie the echoes at the top, into your input call so it appears each time input is called again?
 
Old 07-26-2017, 01:27 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,841

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I'm sorry, but I do not understand what did you ask.
From the other hand your script probably works, or more-or-less does what you expect, but not really correct. cyclic calling functions into each other is not a good idea and may cause difficulties. Also returning back (instead of further invocations) may solve some issues.
Kodi is very drastic, that means it grabs all the resources, reconfigures them for its own needs so probably you cannot simply continue afterward, you may need to restore everything.
 
Old 07-26-2017, 01:43 AM   #6
Assomnia
Member
 
Registered: May 2017
Posts: 38

Original Poster
Rep: Reputation: Disabled
@PAN64

Maybe I wasn't clear actually. I modified the script like you wrote it. with the reset and everything. The script works but once I exit Kodi the screen is cleared and the script wait for the next input and the problem remains. I can't see the menu (echo command at the top).
So for some test I added some echo command to see if the text would appear and it actually does appear. So what I did also is that I put the menu text into a .txt file and I use a variable like this :

menu=$(<loading_text.txt)
echo "$menu"

So now the script looks like this :

Code:
#!/bin/bash
#By Asso'
clear

menu=$(<loading_text.txt)
echo "$menu"

input() {
while :
do
  read -s -N 1 answer
  case "$answer" in
    $'\e')
      echo "Kodi is starting"
      start_kodi
      ;;
    'a')
      echo "EmulationStation is Starting"
      start_emulationstation
      ;;
    $'\x7f')
      start_x
      ;;
    's')
      echo "Performing Updates and Upgrades..."
      echo "Don't switch off the PSP..."
      echo "Make sure you are online..."
      sleep 5
      start_updates
      ;;
    *)
      clear
      echo
      echo "Back to Shell !"
      start_exit
      ;;
    esac
done
}

start_kodi() {
  kodi
  reset
echo "$menu"
}

start_emulationstation() {
  /opt/retropie/supplementary/emulationstation/emulationstation
  reset
echo "$menu"
}

start_x() {
  startx
}

start_updates() {
  sudo apt-get update -y
  sudo apt-get upgrade -y
  sudo apt-get dist-upgrade -y
  sudo apt-get autoremove -y
  clear
  echo
  echo "Updates successful, rebooting..."
  sleep 2
  sudo reboot
}

start_exit() {
  exit 0
}

input

Sounds correct now ? What do you think ?



@grail

Where in the input should I put the menu ?
 
Old 07-26-2017, 02:34 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,841

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I think you need to put the echo "$menu" line before the read command, inside the while loop.
 
1 members found this post helpful.
Old 07-26-2017, 07:56 AM   #8
Assomnia
Member
 
Registered: May 2017
Posts: 38

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
I think you need to put the echo "$menu" line before the read command, inside the while loop.
Well ! That works just like a charm !
Thank you very much for everything. I will keep trying but it looks like its ok
 
Old 07-26-2017, 08:59 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,841

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
you are welcome
glad to help you
 
Old 07-27-2017, 12:09 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Here is a simple change I would have made to make it a little cleaner:
Code:
#!/usr/bin/env bash

clear

menu()
{
  echo -e "$(cat<<-EOF


    Welcome\x1b[31m Asso'\x1b[0m : $(date)

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

          - Press TRIANGLE ▲ - Start Kodi
          - Press SQUARE   ■ - Start EmulationStation
          - Press CIRCLE   \033[1mO\033[0m - StartX
          - Press START      - Perform Updates/Upgrades



               - Press \033[1mX\033[0m - Back to shell

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

    \E[5mUSE ONLY THE SHORTCUT LISTED ABOVE TO AVOID TROUBLES\E[0m

  EOF
  )"
} #menu

input() {
  read -s -N 1 answer
  case "$answer" in
    $'\e')
      echo "Kodi is starting"
      start_kodi
      ;;  
    'a')
      echo "EmulationStation is Starting"
      start_emulationstation
      ;;  
    $'\x7f')
      start_x
      ;;  
    's')
      cat<<-MSG
        Performing Updates and Upgrades...
        Don't switch off the PSP...
        Make sure you are online...
      MSG
      sleep 5
      start_updates
      ;;  
    *)  
      clear
      echo -e "\nBack to Shell !"
      start_exit
      ;;  
    esac
}

start_kodi() {
  kodi
}

start_emulationstation() {
  /opt/retropie/supplementary/emulationstation/emulationstation
}

start_x() {
  startx
}

start_updates() {
  sudo apt-get update -y
  sudo apt-get upgrade -y
  sudo apt-get dist-upgrade -y
  sudo apt-get autoremove -y
  clear
  echo -e "\nUpdates successful, rebooting..."
  sleep 2
  sudo reboot
}

start_exit() {
  exit 0
}

while :
do
  menu
  input
  reset
done
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash Script - Reading User Input while Processing output from Command within Bash cleeky Linux - General 5 05-27-2014 02:57 PM
Bash script for reading output of linux command and make input Black187 Linux - Newbie 8 09-08-2013 03:25 PM
bash script to read multiple user input prasunjit Programming 1 03-31-2013 02:09 PM
[SOLVED] BASH Script. Result of command input into variable bcyork Linux - Newbie 4 12-06-2011 12:57 PM
[SOLVED] bash scripting - read command and input echo armandino Linux - General 2 01-24-2011 10:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration