The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
csh syntax charlie11k UNIX for Dummies Questions & Answers 0 05-31-2007 11:29 PM
vim and syntax max os kezzol OS X (Apple) 1 04-30-2007 11:22 AM
Help with the syntax chandhar Shell Programming and Scripting 2 03-26-2007 04:38 AM
Help with the syntax chandhar Shell Programming and Scripting 2 03-25-2007 10:48 PM
What does this syntax mean... DrAwesomePhD UNIX for Dummies Questions & Answers 1 07-31-2006 08:54 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-02-2007
Registered User
 

Join Date: Apr 2007
Posts: 42
what is the right syntax ??

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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-02-2007
Registered User
 

Join Date: Apr 2007
Posts: 42
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
Reply With Quote
  #3 (permalink)  
Old 06-02-2007
reborg's Avatar
Administrator
 
Join Date: Mar 2005
Location: Ireland
Posts: 3,495
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.
Reply With Quote
  #4 (permalink)  
Old 06-02-2007
Registered User
 

Join Date: Apr 2007
Posts: 42
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]#
Reply With Quote
  #5 (permalink)  
Old 06-03-2007
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,158
find doesn't print directoriy name with a trailing /, so your sed command works only for subdirectories.
Modifify your command

Code:
find /home/"$dir1" -type d -print |
sed "s;^/home/$dir1\(/\|$\);/home/$dir2\1;'" |
while read newdir
Jean-Pierre.
Reply With Quote
  #6 (permalink)  
Old 06-03-2007
reborg's Avatar
Administrator
 
Join Date: Mar 2005
Location: Ireland
Posts: 3,495
[quote=convenientstore]that did not work

Quote:
sed -e "s;/home/$dir1 /;/home/$dir2/;" |
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.
Reply With Quote
  #7 (permalink)  
Old 06-03-2007
Registered User
 

Join Date: Apr 2007
Posts: 42
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;"
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0