Basic shell scripting help
Hello all, I'm fairly new to linux, and I've been playing around with shell scripting and I had a few questions. FYI I'm using Red Hat Enterprise Linux Advanced Server 3 Update 6.
1) How do you set and change your working path inside of the script. I tried using CD, but that doesn't seem to work. 2) I have been trying to get: expr "$string1" : '\/.*\/' to return everything inside of string1 between / and /. So far I have not been able to get it to return anything other than 0. I am familiar with Perl REs, but I have not been able to get these to work in a way that I understand. 3) I tried the following command to get all files from the current directory and all subdirectories: "cp -rR *.xml ../temp/" and it just keeps on saying that *.xml is not a valid file or directory. I tried varients of it like ./*.xml ./*/ etc.... If I specify the exact directory then the cp commands works, but I do not seem to be able to get the recursive fuction to work correctly. I am sure I'm just doing something stupid, but so far I have been unable to figure out what. Any help would be appreciated. Thanks -Zipper |
1)cd should work fine but only inside the script. If the script terminates then you'll have your old working-directory back
3) Code:
find . -name "*.xml" -exec cp {} /temp \; |
Thanks alot that did the trick. I do have a question though, I am trying to understand everything that I am doing, and I am not entirely sure why the script works.
Code:
find . -name "*.log" -exec cp {} $ISSPATH/tempPSLInfo \; |
Have a look at man find for more info, but briefly:
The string {} gets replaced by the current filename. The ';' terminates the find command - it is escaped with the '\' to prevent the shell from doing anything with it. |
find . -name "*.log"
means print all the files that are under . and have and end in .log {} is for file you find from find So this actually means for every file you find do: cp <filename> $ISSPATH/tempPSLInfo |
2. In Perl, you can use () around the parts of the RE you want to refer to after the RE matching.
See "man perlre" for details. ie: $string1 =~ /\/(.*)\//; $1 now contains wathever was matched in the first pair of () ("backreference"). $2, $3, ... can be used if you use more than one pair of (). You'll need to use \1, \2, ... instead of $1, $2, ... if you want to refer to the matched items from within the statement that contains the RE. $1, $2, ... are only intended to be used after that statement (ie when following statements refer back to the last RE match - hence the term "backreference"). In shell, you may want to use a utility like cut, sed, grep or awk. cut is probably the easiest (but you can specify only 1 delimiter character): Code:
echo "$string1" | cut -d'/' -f2 Maybe shell knows backreferences too. I haven't checked that. |
Ahh thank you all very much, this is exactly what I need to know.
-Zipper |
All times are GMT -5. The time now is 06:54 PM. |