LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Basic shell scripting help (https://www.linuxquestions.org/questions/linux-newbie-8/basic-shell-scripting-help-424166/)

zipper777 03-12-2006 01:35 PM

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

perfect_circle 03-12-2006 03:42 PM

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 \;

zipper777 03-12-2006 08:37 PM

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 \;
what are the {} for? I tried to google for them, but google wouldn't take those as a search parameter. Also with is the backslash for? Thanks for any help.

gilead 03-12-2006 09:09 PM

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.

perfect_circle 03-12-2006 10:05 PM

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

timmeke 03-13-2006 02:22 AM

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
would select "def" from the string "abc/def/ghi".

Maybe shell knows backreferences too. I haven't checked that.

zipper777 03-13-2006 12:58 PM

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.