Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 17 Sep 2014, 19:58
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
spaces and variables - SOLVED
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [6 Posts]  
Author Message
trapster


Joined: 28 Nov 2005
Posts: 2003
Location: Maine, USA

PostPosted: Sat 06 Sep 2014, 12:26    Post subject:  spaces and variables - SOLVED
Subject description: How to have a script read a filename with spaces in it?
 

I have this in a script called upload:

Code:
file_name="$1"
echo "$file_name"


When I run:
Code:
#> upload artist - title.mp4

the result is:
Code:
artist


The only way I can get it to read the whole filename is by quoting it in the command:
Code:
upload "artist - title.mp4"


Can I get the script to read the full filename without me having to type the quotes in the command?

Slacko 5.2

_________________
trapster
Maine, USA

Asus eeepc 1005HA PU1X-BK
Frugal install: Puppeee4.31 + 1.0, Puppy4.10 + Lupu52
Currently using Slacko AND lupu52 w/ fluxbox

Last edited by trapster on Sun 07 Sep 2014, 06:32; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website 
step

Joined: 04 May 2012
Posts: 174

PostPosted: Sat 06 Sep 2014, 12:46    Post subject:  

The script does read the whole filename. It's the way you input the filename that makes the difference.

If you know that the command-line will never include anything else but the file name change the script to
file_name="$*".

That will do. If instead you might have other logical parameters on the same line then "$*" won't work and you will have to type the quotes in the command line. Otherwise the shell breaks each word into separate positional parameters, $0=upload $1=artist $2=- $3=title.mp4

There are ways around this.
One is to quote spaces with backslashes. You would type:
upload artist\ -\ title.mp4

Is that better? Not, probably.

Another way is to set shell variable IFS to make space a non-breaking character. But if you did then the command would become the whole line instead of just "upload".

In summary: you have to type the double quotes to keep words together.

_________________
FatDog64 631|Blue Pup 4|DebianDog+OpenMediaVault stick
Back to top
View user's profile Send private message 
Keef


Joined: 20 Dec 2007
Posts: 628
Location: Staffordshire

PostPosted: Sat 06 Sep 2014, 13:10    Post subject:  

Maybe 'read' will help:

Code:
echo Please, enter the artist:

read file_name

echo $file_name
Back to top
View user's profile Send private message 
slavvo67

Joined: 12 Oct 2012
Posts: 382
Location: The other Mr. 305

PostPosted: Sat 06 Sep 2014, 13:20    Post subject:  

Take a look at the SED command. You should be able to have the program read your input and automatically put quotes around it for you.
Back to top
View user's profile Send private message 
some1

Joined: 17 Jan 2013
Posts: 25

PostPosted: Sat 06 Sep 2014, 18:40    Post subject:  

http://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html

This may work:
Command:
upload the filename and nothing else
Code:

filename="$@"
echo "$filename" -> the filename and nothing else

..but - as a routine - better quote breakable stuff.
Back to top
View user's profile Send private message 
trapster


Joined: 28 Nov 2005
Posts: 2003
Location: Maine, USA

PostPosted: Sun 07 Sep 2014, 06:32    Post subject:  

Thanks all.

"$*" and "$@" does what I need.

_________________
trapster
Maine, USA

Asus eeepc 1005HA PU1X-BK
Frugal install: Puppeee4.31 + 1.0, Puppy4.10 + Lupu52
Currently using Slacko AND lupu52 w/ fluxbox
Back to top
View user's profile Send private message Visit poster's website 
Display posts from previous:   Sort by:   
Page 1 of 1 [6 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0531s ][ Queries: 11 (0.0051s) ][ GZIP on ]