what is the right syntax ??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting what is the right syntax ??
# 1  
Old 06-02-2007
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
# 2  
Old 06-02-2007
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
# 3  
Old 06-02-2007
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.
# 4  
Old 06-02-2007
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]#
# 5  
Old 06-03-2007
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.
# 6  
Old 06-03-2007
[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.
# 7  
Old 06-03-2007
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;"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Perl syntax

Query with perl syntax Aim: is to change a perl script to use a new file I was required to replace - entries \"$entries\" with -lib <full_path_to_filename> So in the code detector.pm sub rundetector { my $class = shift; mkdir($resultDirectory); my... (3 Replies)
Discussion started by: sa@@
3 Replies

2. Shell Programming and Scripting

GnuPG Syntax Help

Hi all, I'm trying to decrypt a GnuPG file but not having much luck. I'm new to using it and have tried 4 different ways to do it but nothing works. Here are examples of the attempts I have made: gpg -o ./file_name.tar.Z --passphrase-fd 0 ./file_name.tar.Z.gpg 0<./password.txt cat... (5 Replies)
Discussion started by: Korn0474
5 Replies

3. Shell Programming and Scripting

for ... do - syntax

hi! pls help me :) cd folder for f in *; do ... done this circle takes all files from folder1 i need only .pdf files but it may be like a.pdf or a.PDF what syntax must i use? smth like it: cd folder for f in *.(pdf|PDF); do (2 Replies)
Discussion started by: optik77
2 Replies

4. UNIX for Dummies Questions & Answers

Syntax Help

Hi everyone, I got some code here that I've been working on, but I've gotten stuck. I cant figure out what is wrong with the syntax. sub cksum{ my $uint32_t = \buffer; my $word_count; my $bias; ( #uint32_t checksum = $bias $word_count = $bias while ($word_count>=0) ... (2 Replies)
Discussion started by: TeamUSA
2 Replies

5. Shell Programming and Scripting

What syntax to use with sed c\

I know that I want to entirely replace line 3 in my file filename.txt. I have tried all sorts of variations of sed 3,3,c\replacement stuff\ filename.txt with no success. about the only thing that causes any reaction is sed 3,3c\\ filename.txt but it just prints out the whole file. ... (13 Replies)
Discussion started by: SusanDAC
13 Replies

6. Shell Programming and Scripting

Regarding syntax

Hi All, What does this mean ${#var} (2 Replies)
Discussion started by: krishna_gnv
2 Replies

7. UNIX for Dummies Questions & Answers

csh syntax

I am trying to list output in columns in csh. What would be the syntax for this shell if in ksh it is: ls -d !(*SNMP*) ? (0 Replies)
Discussion started by: charlie11k
0 Replies

8. Shell Programming and Scripting

Help with the syntax

can anyone explain the code for me... i am new to shell programming while getopts ":S:D:U:" OPTION "$@" do case $OPTION in S) SRVR=$OPTARG;; D) DB="$OPTARG"; USEDB="use $OPTARG";; U) UID=$OPTARG;; :) MISSINGOPTARG="$MISSINGOPTARG -$OPTARG";; ?) if then ... (2 Replies)
Discussion started by: chandhar
2 Replies

9. Shell Programming and Scripting

Help with the syntax

export check=$(expandname $(dirname $(which $0))) (2 Replies)
Discussion started by: chandhar
2 Replies

10. UNIX for Dummies Questions & Answers

What does this syntax mean...

OK, the title is a little vague, but basically i was gonig through some files and ran into some strange syntax... heres what it looks like: ... 1&>~/<file extension> where ... is a chain of commands (the 1&>~ is part of the arguments) and the file extension is just a pointer to a file... (1 Reply)
Discussion started by: DrAwesomePhD
1 Replies
Login or Register to Ask a Question