Operating System - HP-UX
1829516 Members
1304 Online
109991 Solutions
New Discussion

Re: create directory name (with blank spaces) using a script

 
SOLVED
Go to solution
Dario_1
Trusted Contributor

create directory name (with blank spaces) using a script

I am trying to write a script that will be syncronizing the directory structure of a Samba share from production to DR. The directories are created by Window users and most of them with blank spaces. the problem I am experiencing is that if I use mkdir "file name" from command line, the directory gets created but if I use a script to create the directory, the script creates two directories (file and name).

I am using the find command to create files from both servers and then the diff command to stripe the directories that must be created and deleted based on the DR server based onthe production environment. I am also using the sed command to add double quotes (") to the begining and end of each line of the file and an if statement is used to read the lines and execute the mkdir command for each line.

The problem we are experiencing is that the system is not interpreting the double quotes and multiple directories are getting created. We also tried using double quotes within the FOR I statement (mkdir "$i")as well as backslashes before the blank space (file\ name)with the same results.

Thank you in advance for any help and/or suggestion.
15 REPLIES 15
Steven E. Protter
Exalted Contributor

Re: create directory name (with blank spaces) using a script

Shalom,

Linux example with special chars

Quantum\ Leap\ -\ Season\ 3\ -\ Dvdrip/Quantum\ Leap\ -\ \[3x05\]\ -\ The\ Boogieman\ -\ \(DVDRip\)\ -\ \[Medieval\].avi


Double quote the directory name including spaces.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Steven Schweda
Honored Contributor

Re: create directory name (with blank spaces) using a script

td176> x='name with spaces'
td176> mkdir "$x"
td176> ls -l
total 8
drwxrwxrwx 2 antinode 513 4096 Jun 4 13:02 name with spaces

td176> cat md.sh
#!/bin/sh
x='name with spaces in script'
mkdir "$x"

td176> ./md.sh

td176> ls -l
total 24
-rwxrwxrwx 1 antinode 513 52 Jun 4 13:06 md.sh
drwxrwxrwx 2 antinode 513 4096 Jun 4 13:02 name with spaces
drwxrwxrwx 2 antinode 513 4096 Jun 4 13:06 name with spaces in script

It might help if you showed a simple example
of what you're doing. Vague descriptions are
not as good.
OldSchool
Honored Contributor

Re: create directory name (with blank spaces) using a script

w/o seeing the actual script, I'd have to guess that you are doing something like

for i in $somevar
do
mkdir $i
done

the i will be the first word "file" the first time thru, "name" the second in this case.

as snippet of the script, and its input would be helpful
Sandman!
Honored Contributor

Re: create directory name (with blank spaces) using a script

Seems like most folks can create a dir with embedded spaces in it using a script. So could you post your script here so that it can be looked at.

~thanks
A. Clay Stephenson
Acclaimed Contributor

Re: create directory name (with blank spaces) using a script

It's actually trivially simple:

A="Clay Dir"
mkdir "${A}"

The key is to double quote everywhere.

If it ain't broke, I can fix that.
Jeff_Traigle
Honored Contributor

Re: create directory name (with blank spaces) using a script

First off, it'd probably be a lot easier to use rsync for this than write some complicated script to handle the task.

However, going along with your script attempt, it would be a little easier to troubleshoot your script if you showed the actual code you're trying to use. However, based on your description and the mention of a FOR loop, I'll guess you're doing something like:

for I in $(find /dir -type d)
do
mkdir "${I}"
done

Your problem would be that the find command is going to generate a list of directories and the FOR statement is going to take all those spaces in the directory names as separators for unique values to use for I. No matter how you quote or escape within the loop, the results will always be the same.

One option offhand would be dump the find output to a file and use the file as input to a while loop:

find /dir -type d > mkdir.tmp
while read I
do
mkdir "${I}"
done < mkdir.tmp
--
Jeff Traigle
Dario_1
Trusted Contributor

Re: create directory name (with blank spaces) using a script

Wow! The script was not ready, I was using pieces to see if I was going to be able to make it work. I created the preliminary part of the script, which I will modified later but you will be able to get a better idea of what's happening.

Rsync and rdist are not going to be used because there are close to 4,000 files and we won't need those files there. The application will only need directory structure.

I will be replying with both file outputs shortly.

thank you for your help.
Dario_1
Trusted Contributor

Re: create directory name (with blank spaces) using a script

File containing directories to be deleted from DR.
Dario_1
Trusted Contributor

Re: create directory name (with blank spaces) using a script

File containing directories to be created in DR server.
Dario_1
Trusted Contributor

Re: create directory name (with blank spaces) using a script

I also tried using mkdir "${i}" within the script with the same results.

thx,
Jeff_Traigle
Honored Contributor

Re: create directory name (with blank spaces) using a script

Looks like I was right on my guess how you were trying to implement it. Skip the quoting in the input file (dr_samba.dir). Use the construct I mentioned previously with a WHILE loop instead of FOR loop.
--
Jeff Traigle
A. Clay Stephenson
Acclaimed Contributor

Re: create directory name (with blank spaces) using a script

Ok, this really couldn't be simpler:

#!/usr/bin/sh

typeset INFILE="/your/file/listing/the directories"

typeset DIRNAME=""
cat ${INFILE} | while read DIRNAME
do
mkdir "${DIRNAME}"
done

If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: create directory name (with blank spaces) using a script

Ok, this really couldn't be simpler:

#!/usr/bin/sh

typeset INFILE="/your/file/listing/the directories"

typeset DIRNAME=""
cat "${INFILE}" | while read DIRNAME
do
mkdir "${DIRNAME}"
done

#NOTE that quotes are need even in the cat "${INFILE}" because in my example the file name contains whitespace
If it ain't broke, I can fix that.
Dario_1
Trusted Contributor

Re: create directory name (with blank spaces) using a script

thank you all. Problem has been resolved. I appreciate every answer.
Dario_1
Trusted Contributor

Re: create directory name (with blank spaces) using a script

thank you again.