LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-12-2006, 02:35 PM   #1
zipper777
LQ Newbie
 
Registered: Mar 2006
Posts: 3

Rep: Reputation: 0
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
 
Old 03-12-2006, 04:42 PM   #2
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
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 \;
 
Old 03-12-2006, 09:37 PM   #3
zipper777
LQ Newbie
 
Registered: Mar 2006
Posts: 3

Original Poster
Rep: Reputation: 0
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.
 
Old 03-12-2006, 10:09 PM   #4
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,125

Rep: Reputation: 165Reputation: 165
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.
 
Old 03-12-2006, 11:05 PM   #5
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
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
 
Old 03-13-2006, 03:22 AM   #6
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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.
 
Old 03-13-2006, 01:58 PM   #7
zipper777
LQ Newbie
 
Registered: Mar 2006
Posts: 3

Original Poster
Rep: Reputation: 0
Ahh thank you all very much, this is exactly what I need to know.

-Zipper
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 03:14 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 04:32 PM
Basic bach scripting BruceC Linux - Newbie 4 02-25-2005 10:31 AM
very basic scripting question spatter9999 Programming 3 09-18-2003 02:36 AM
Basic BASH scripting question Dark_Helmet Programming 3 06-26-2003 06:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration