![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
|||
|
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 |
|
|||
|
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 Code:
find /home/"$dir1" -type d -print | sed "s;^/home/$dir1\(/\|$\);/home/$dir2\1;'" | while read newdir |
|
||||
|
[quote=convenientstore]that did not work
Quote:
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;" |
|||
| Google UNIX.COM |