Solved : bash script coding question

Technical issues/questions of an intermediate or advanced nature.
Post Reply
User avatar
Ed_P
Contributor
Contributor
Posts: 5999
Joined: 06 Feb 2013, 22:12
Distribution: 4.0 & 5.0rc2 Cinnamon 64 ISOs
Location: Western NY, USA

Solved : bash script coding question

Post#1 by Ed_P » 28 Mar 2021, 18:46

This code.

Code: Select all

:
:
g3=space_invaders

echo
echo 1. $g1
echo
echo 2. $g2
echo
echo 3. $g3
echo
read -p "Select 1, 2 or 3: " Nbr

Game="$""g"$Nbr
echo Game=$Game
echo $Game
echo "$g"$Nbr
echo $g3
read
The output.

Code: Select all

:
:
3. space_invaders

Select 1, 2 or 3: 3
Game=$g3
$g3
3
space_invaders
When 3 is selected how do I get the " echo $Game " or " echo "$g"$Nbr " results to be the same as the " echo $g3 " result? " echo $g$Nbr " doesn't work either. :unknown:
Ed

burdi01
Samurai
Samurai
Posts: 146
Joined: 18 Aug 2013, 12:09
Distribution: Slackware PartedMagic Xubuntu
Location: The Netherlands

bash script coding question

Post#2 by burdi01 » 29 Mar 2021, 10:22


User avatar
ncmprhnsbl
DEV Team
DEV Team
Posts: 2880
Joined: 20 Mar 2012, 03:42
Distribution: 5.0rc1-64bit all-DE+more
Location: australia
Contact:

bash script coding question

Post#3 by ncmprhnsbl » 29 Mar 2021, 13:35

that
or a different aproach:
use an array:

Code: Select all

g=(one two three)

echo
echo 1. ${g[0]}
echo
echo 2. ${g[1]}
echo
echo 3. ${g[2]}
echo
read -p "Select 1, 2 or 3: " Nbr

echo Game=${g[$Nbr-1]}
output:

Code: Select all

1. one

2. two

3. three

Select 1, 2 or 3: 3
Game=three
or to avoid the -1 part, have a 'dummy' first array entry(zero) that isn't used

Code: Select all

g=(zero one two three)

echo
echo 1. ${g[1]}
echo
echo 2. ${g[2]}
echo
echo 3. ${g[3]}
echo
read -p "Select 1, 2 or 3: " Nbr

echo Game=${g[$Nbr]}
Forum Rules : https://forum.porteus.org/viewtopic.php?f=35&t=44

User avatar
Ed_P
Contributor
Contributor
Posts: 5999
Joined: 06 Feb 2013, 22:12
Distribution: 4.0 & 5.0rc2 Cinnamon 64 ISOs
Location: Western NY, USA

bash script coding question

Post#4 by Ed_P » 29 Mar 2021, 14:51

burdi01 wrote:
29 Mar 2021, 10:22

Code: Select all

eval Game='$'g$Nbr
See e.g. https://unix.stackexchange.com/question ... nd-in-bash
:D
ncmprhnsbl wrote:
29 Mar 2021, 13:35
that
or a different aproach:
use an array:
:shock: Wow!! Thank you guys. :beer:

A related question, how can I start an app with the script and allow the terminal window to close while the app runs? Now when the terminal window closes so too does the app it starts. The app is Adobe's flashplayer.
Ed

User avatar
ncmprhnsbl
DEV Team
DEV Team
Posts: 2880
Joined: 20 Mar 2012, 03:42
Distribution: 5.0rc1-64bit all-DE+more
Location: australia
Contact:

bash script coding question

Post#5 by ncmprhnsbl » 29 Mar 2021, 23:06

Ed_P wrote:
29 Mar 2021, 14:51
how can I start an app with the script and allow the terminal window to close while the app runs?
disown:

Code: Select all

<command> &
disown
or
<command> &; disown
or
<command> & disown
or
nohup <command> & disown 
Forum Rules : https://forum.porteus.org/viewtopic.php?f=35&t=44

User avatar
Ed_P
Contributor
Contributor
Posts: 5999
Joined: 06 Feb 2013, 22:12
Distribution: 4.0 & 5.0rc2 Cinnamon 64 ISOs
Location: Western NY, USA

bash script coding question

Post#6 by Ed_P » 30 Mar 2021, 01:24

Wow! :shock:

I learn a thing or two by seeing what others post and by asking questions but to learn 4 new things in one thread is unbelievable.

eval
disown
nohup
and an easy array

:o All in a simple script to play some old games. :oops:

Thank you ncmprhnsbl and burdi01. :beer: :friends:

Added in 1 day 15 hours 18 minutes 58 seconds:
This works and I can close the terminal window and flashplayer stays open. :)

Code: Select all

nohup ./flashplayer $Game > /dev/null 2>&1 & 
disown 
echo " " 
echo " Hope you have fun."
read
But if I remove the "read" or replace it with "exit" flashplayer doesn't even start when run from a nemo Run in Terminal prompt or .desktop file. :o :( But in terminal mode it starts and the script ends as desired. :crazy:
Ed

User avatar
Ed_P
Contributor
Contributor
Posts: 5999
Joined: 06 Feb 2013, 22:12
Distribution: 4.0 & 5.0rc2 Cinnamon 64 ISOs
Location: Western NY, USA

bash script coding question

Post#7 by Ed_P » 04 Apr 2021, 04:59

This works. :good: :)

Code: Select all

nohup ./flashplayer $Game > /dev/null 2>&1 & 
disown 
echo " " 
echo " Hope you have fun."
sleep 3 
exit
Ed

Post Reply