LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to make the Read command handles spaces (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-make-the-read-command-handles-spaces-251582/)

Kilahchris 11-05-2004 03:44 PM

how to make the Read command handles spaces
 
I wrote a simple script for TuxNes emulator (Nintendo emulator).

The script works with loading files with names where there is no spaces in between the characters.


for example (Because i dont have access to my computer my script is much longer and doesnt look exactly like this)


!#Bin Bash

echo Which game would you like to play?

Read X;

if [-n, x]

tuxnes --video=1024x768 -- <some other options> $X;

else

echo No such game.






when I enter a game like Superman the game properly loads from the script. However when i enter a game like Super Mario Bros i get either file not found or

when I get the prompt which Game you would like to play?
sometimes I enter Super/ Mario/ Bros/ 3* and i get an error too many arguments.???


Does anyone know how to make the bash read command accept spaces that are in what you input????

homey 11-05-2004 04:04 PM

Try read "X"

Kilahchris 11-05-2004 04:18 PM

thank you

when i get home i will try it. Just curious what does the quotation marks do to the Read Command?

homey 11-05-2004 04:30 PM

I don't know about your particular situation but I use the quotes to make a command include spaces.

Kilahchris 11-05-2004 04:35 PM

"" didnt work. Here is my script file.



Quote:

#!/bin/bash

ls
echo "Which game would you like to play"


read -r "X"

if [ -n "$X" ];

then


tuxnes --renderer=auto --geometry=1024x768 --format=16 --sound=/dev/audio --soundrate=44100 --js1=/dev/js0 --enlarge=12 --joystick-map=B1,B0,B10,B9,A0,A1,A2,A3 $X

else

echo "no such file found"

fi;


Does anyone know which bash command will accept input with spaces between characters?
Is there a way to make the Read command accept spaces such as in file names like
Super Mario Bros (U).nes



Darin 11-06-2004 07:31 AM

I don't belive that was where he was talking about using the quotes, try it when you input the name:

$> ./tuxnes.sh
Which game would you like to play?
"Super Mario Brothers"

If that doesn't work you may have to insert surrounding quotes or escape the spaces inside the variable X. My guess is that if X is "super mario" then the command:

tuxnes $X

would try to run on both words like two seperate options, as if you typed:

tuxnes super
tuxnes mario

If you could do something like $X = quotes + $X + quotes then the command would work although I can't for the life of me think of the right syntax that would add the quote symbol into the beginning and end of a variable.

LasseW 11-06-2004 10:19 AM

Yes, the quotes do work, but you need to use them in the argument list of the tuxnes command as well, ie the end of the command should read

joystick-map ... "$X"

Kilahchris 11-06-2004 12:10 PM

ok thanks for all your input.


He was right.

All i needed to do was type put quotes around Read "X" variable, quotes does not need to be around the what input

and once I copy and paste the filename it ran. I was simply able to type Super Mario Bros 3 (E) and everything worked.


The reason why i didnt work before because i was trying to make thinks easier by using the asterix sign like

Super Mario Bros 3*
instead of typing the full name of the file.


All times are GMT -5. The time now is 04:48 PM.