LinuxQuestions.org
Help answer threads with 0 replies.
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 01-20-2012, 09:50 AM   #1
pedja1
LQ Newbie
 
Registered: Jan 2012
Posts: 3

Rep: Reputation: Disabled
Creating directory with spaces in script


Hi,
Im trying to make directory that contain spaces in name inside script. For example:
Code:
mkdir /home/$dir
and variable $dir is loaded from file that contains multiple lines eg:
Code:
bla bla
thr ljlj
asd kjasd ksjad
And when i execute script it creates dir with only first part of the line in the file.
I have also tryed to comment variable $dir but then i get:
Code:
mkdir: cant create directory /home/bla bla Invalid Argument
Spaces needs to be there so please dont tell me not to use them
Thanks in advance
 
Old 01-20-2012, 10:32 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
just put the directory name in double quotes. the variable will be expanded still as long as they are double, not single, quotes.
 
Old 01-20-2012, 10:37 AM   #3
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Quote:
Originally Posted by acid_kewpie View Post
just put the directory name in double quotes. the variable will be expanded still as long as they are double, not single, quotes.
which would result in the command:
Code:
mkdir /home/"$dir"
i think,

or you can first define
Code:
DIR='"bla bla"'
 
Old 01-20-2012, 10:56 AM   #4
Janus_Hyperion
Member
 
Registered: Mar 2011
Location: /
Distribution: Fedora (typically latest release or development release)
Posts: 372

Rep: Reputation: Disabled
Alternatively, '\' could be used between the names.

Code:
 mkdir bla\ bla
 
Old 01-20-2012, 11:38 AM   #5
pedja1
LQ Newbie
 
Registered: Jan 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
I cant use
Code:
DIR='"bla bla"'
or
Code:
mkdir bla\ bla
because value bla bla is not inside script its in external file which cant be edited
quoting variable $dir works but now i have different problem

some of the lines in file have aditional space after last word and that is the problem
is there a way to solve this
 
Old 01-20-2012, 11:52 AM   #6
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
You could use echo to remove space after the last word, like
Code:
dir=$(echo $dir)
 
1 members found this post helpful.
Old 01-20-2012, 11:52 AM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by pedja1 View Post
I cant use
Code:
DIR='"bla bla"'
or
Code:
mkdir bla\ bla
because value bla bla is not inside script its in external file which cant be edited quoting variable $dir works but now i have different problem
some of the lines in file have aditional space after last word and that is the problem is there a way to solve this
Use sed in your bash script to 'sanitize' the variable before you use it:
http://tldp.org/LDP/abs/html/x22860.html#AEN22890

For example:
Code:
sed -e "s/ /\\/g" $dir
would replace the spaces with a "\", which the mkdir command would accept. You can even put another sed line after this, to remove the trailing spaces.
 
Old 01-20-2012, 12:06 PM   #8
pedja1
LQ Newbie
 
Registered: Jan 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Cedrik View Post
You could use echo to remove space after the last word, like
Code:
dir=$(echo $dir)
This worked, thanks a lot
thank you all for help
 
Old 01-20-2012, 04:52 PM   #9
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
far too much thinking going on here, I just meant
Code:
mkdir "/home/$dir"
nothing more than that.
 
  


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
Creating a script that removes the oldest files on /tmp directory danndp Linux - Newbie 6 11-18-2010 09:47 AM
Creating a script to change directory darkside720 Linux - Newbie 4 04-28-2008 04:11 PM
Creating script to change ownership directory on RHEL4U3 at startup Ferianto Linux - Enterprise 9 02-13-2008 10:01 PM
Creating an alias (name=value pair) where the name has spaces. Cubert Linux - Newbie 2 01-12-2005 06:42 AM
Help creating a directory back up shell script WarriorWarren Linux - General 6 04-06-2003 09:56 AM

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

All times are GMT -5. The time now is 05:22 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