Page 1 of 1

[Solved] bash script coding question

Posted: 28 Mar 2021, 18:46
by Ed_P
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:

bash script coding question

Posted: 29 Mar 2021, 10:22
by burdi01

bash script coding question

Posted: 29 Mar 2021, 13:35
by ncmprhnsbl
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]}

bash script coding question

Posted: 29 Mar 2021, 14:51
by Ed_P
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.

bash script coding question

Posted: 29 Mar 2021, 23:06
by ncmprhnsbl
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 

bash script coding question

Posted: 30 Mar 2021, 01:24
by Ed_P
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:

bash script coding question

Posted: 04 Apr 2021, 04:59
by Ed_P
This works. :good: :)

Code: Select all

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