1843473 Members
3239 Online
110216 Solutions
New Discussion

Shell problem

 
Catherine_3
Occasional Advisor

Shell problem

I met a problem.
I have a file named ss
ss:
consulting
Global Exotica - DEVADM
Global Exotica - DEVSTD
NSD

My question:
for dirname in `cat ss`
do
echo "$dirname"
mkdir "$dirname"
done

I cannot get the dirname "Global Exotica - DEVADM" and Global Exotica - DEVSTD" because of the space

Hope I can get feedback
very appreciated advance
10 REPLIES 10
S.K. Chan
Honored Contributor

Re: Shell problem

I can suggest getting rid of the spaces in file "ss". For example ..

# sed 's/ //g' ss > ss.new

The "ss.new" file would then looked like ..

consulting
GlobalExotica-DEVADM
GlobalExotica-DEVSTD
NSD

Now run it through the for loop.

harry d brown jr
Honored Contributor

Re: Shell problem

You can do spaces, but it's not a good idea because of parsing rules.


# mkdir "this is stupid"
# ls -l | grep this
drwxrwxrwx 2 root sys 96 Apr 2 14:05 this is stupid
#

put filename in QUOTES


live free or die
harry
Live Free or Die
Francois Bariselle_3
Regular Advisor

Re: Shell problem

Hi,

You can change all space by a _.

cat ss | sed s/" "/_/g >! ss.new

Frank.
Fais la ...
Mark Greene_1
Honored Contributor

Re: Shell problem

change the FS (field separator) variable to just use linefeeds:

OLD_FS=$FS
FS=`echo "\n"`
for...
...
done
FS=$OLD_FS

HTH
mark
the future will be a lot like now, only later
Sachin Patel
Honored Contributor

Re: Shell problem

You can do this

for dirname in `cat ss`
do
echo "$dirname"
mkdir "${dirname}"
done

Sachin
Is photography a hobby or another way to spend $
Darrell Allen
Honored Contributor

Re: Shell problem

Use read instead of for. Since you have multiple variables on an input line, read will place the entire line in dirname.

while read dirname
do
echo $dirname
mkdir "$dirname"
done
Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Catherine_3
Occasional Advisor

Re: Shell problem

Hi guys,
Thanks a lot
my question is I can't get dirname from ss
Deshpande Prashant
Honored Contributor

Re: Shell problem

Hi
Try this
ss:
"consulting "
"Global Exotica - DEVADM "
"Global Exotica - DEVSTD "
"NSD "

Add "" in file ss and then

#cat ss |xargs mkdir

Thanks
Prashant.
Take it as it comes.
Sachin Patel
Honored Contributor

Re: Shell problem

This will work I have try it

cat ss | while read line;
do
echo $line
mkdir ${line}
done

Sachin Patel
Is photography a hobby or another way to spend $
Darrell Allen
Honored Contributor

Re: Shell problem

Hi Catherine,

Did you try the "while read dirname" loop? For each line of ss, $dirname will contain the entire line, spaces and all and it will create the directories with names like "Global Exotica - DEVADM".

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)