IN the book below example showed
find /home/tolstoy -type d -print | Find all directories
sed 's;/home/tolstoy/;/home/lt/;' | Change name, note use of semicolon delimiter
while read newdir Read new directory name
do
mkdir $newdir Make new directory
done
what is the right syntax in order to be able to take positional parameter as below?
how come $dir2 does not
[root@rleeserver tmp]# cat yaho11
#! /bin/sh
dir1=$1
dir2=$2
echo $dir1
echo $dir2
find /home/"$dir1" -type d -print |
sed 's;/home/"$dir1"/;/home/"$dir2"/;' |
while read newdir
do
mkdir $newdir
done
[root@rleeserver tmp]# ./yaho11 xmen3 xmen4
xmen3
xmen4
mkdir: cannot create directory `/home/xmen3': File exists
mkdir: cannot create directory `/home/xmen3/one': File exists
mkdir: cannot create directory `/home/xmen3/three': File exists
mkdir: cannot create directory `/home/xmen3/two': File exists
I got it..
can someone place explain why in sed, using single quote ' ' makes this work versus double quote " " ??
[root@rleeserver tmp]# cat yaho11
#! /bin/sh
dir1=$1
dir2=$2
echo $dir1
echo $dir2
find /home/"$dir1" -type d -print |
sed -e 's;/home/'$dir1'/;/home/'$dir2'/;' |
while read newdir
do
mkdir $newdir
done
Your use of quotes in the sed expression is incorrect. Remove the inner double quotes and replace the outer single quotes with double quotes.
Expansion of variables does not happen inside single quotes, the method you have used works because the variables are not quoted at all now.
that did not work
[root@rleeserver tmp]# cat yaho11
#! /bin/sh
dir1=$1
dir2=$2
echo $dir1
echo $dir2
find /home/"$dir1" -type d -print |
sed -e "s;/home/$dir1 /;/home/$dir2/;" |
while read newdir
do
mkdir $newdir
done
[root@rleeserver tmp]# sh -x ./yaho11 xmen3 xmen4
+ dir1=xmen3
+ dir2=xmen4
+ echo xmen3
xmen3
+ echo xmen4
xmen4
+ find /home/xmen3 -type d -print
+ sed -e 's;/home/xmen3 /;/home/xmen4/;'
+ read newdir
+ mkdir /home/xmen3
mkdir: cannot create directory `/home/xmen3': File exists
+ read newdir
+ mkdir /home/xmen3/one
mkdir: cannot create directory `/home/xmen3/one': File exists
+ read newdir
+ mkdir /home/xmen3/three
mkdir: cannot create directory `/home/xmen3/three': File exists
+ read newdir
+ mkdir /home/xmen3/two
mkdir: cannot create directory `/home/xmen3/two': File exists
+ read newdir
[root@rleeserver tmp]#
find doesn't print directoriy name with a trailing /, so your sed command works only for subdirectories.
Modifify your command
find /home/"$dir1" -type d -print |
sed "s;^/home/$dir1\(/\|$\);/home/$dir2\1;'" |
while read newdir
Jean-Pierre.
[quote="convenientstore"]
that did not work
It could never work with a space between $dir1 and /, becasue the string would never be replaced.
Also as pointed out by aigles, either you need to change the sed command or the xmen4 directory must already exist.
thanks guys ; below was my mistake.. (have question though at the bottom)
[root@rleeserver tmp]# cat yaho11.b
#! /bin/sh
dir1=$1
dir2=$2
echo $dir1
echo $dir2
find /home/"$dir1" -type d -print |
sed -e "s;/home/$dir1/;/home/$dir2/;" |
while read newdir
do
mkdir $newdir
done
[root@rleeserver tmp]# ls -l /home/xmen4/*
ls: /home/xmen4/*: No such file or directory
[root@rleeserver tmp]# ls -l /home/xmen4
total 0
[root@rleeserver tmp]# sh -x ./yaho11.b xmen3 xmen4
+ dir1=xmen3
+ dir2=xmen4
+ echo xmen3
xmen3
+ echo xmen4
xmen4
+ find /home/xmen3 -type d -print
+ sed -e 's;/home/xmen3/;/home/xmen4/;'
+ read newdir
+ mkdir /home/xmen3
mkdir: cannot create directory `/home/xmen3': File exists
+ read newdir
+ mkdir /home/xmen4/one
+ read newdir
+ mkdir /home/xmen4/three
+ read newdir
+ mkdir /home/xmen4/two
+ read newdir
I also see below working to better solution.. but what does \(/\|$\) do??
\( \) is backreferenceing 1 that I understand but what is /\|$ do??
sed -e "s;^/home/$dir1\(/\|$\);/home/$dir2\1;"
Forget this solution, it is not supported by all the versions of sed.
The same thing:
sed -e "s;^/home/$dir1/;/home/$dir2/;;s^/home/$dir1\$;/home/$dir2;"
Jean-Pierre.