LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 02-23-2007, 09:52 AM   #1
SpLaSh212
Member
 
Registered: Mar 2006
Posts: 42

Rep: Reputation: 15
Shell scripting help


Hello guys, I need some help ..
I'm new to shell scripting, and I have a several questions
so lets begin
emmm how can I dived my script into parts ?
I mean something like
main:
exit:

and how I can manipulate the interpeter to go there ?
something like "goto" command in dos ? :\
thnx alot
 
Old 02-23-2007, 10:11 AM   #2
tuxrules
Senior Member
 
Registered: Jun 2004
Location: Chicago
Distribution: Slackware64 -current
Posts: 1,158

Rep: Reputation: 62
Quote:
Originally Posted by SpLaSh212
Hello guys, I need some help ..
I'm new to shell scripting, and I have a several questions
so lets begin
emmm how can I dived my script into parts ?
I mean something like
main:
exit:

and how I can manipulate the interpeter to go there ?
something like "goto" command in dos ? :\
thnx alot
Have a google search...there are a lot of links out there for shell scripting. Good one to start is TLDP. Grab the docs and give it a spin.

Tux,
 
Old 02-23-2007, 10:42 AM   #3
SpLaSh212
Member
 
Registered: Mar 2006
Posts: 42

Original Poster
Rep: Reputation: 15
I need to write something that deletes all the files in the directory except the files that were created within this week.

I'm not sure how I can do this, I mean, my plan is to take the date the script is executed in the date +%F format and divide it to $day,$month,$year
My question is how I can delete a file based on his date ?

Does anyone have any idea ?
 
Old 02-23-2007, 11:06 AM   #4
SpLaSh212
Member
 
Registered: Mar 2006
Posts: 42

Original Poster
Rep: Reputation: 15
I found this command
Code:
find <PATH> -type f -newer <PATH> -exec rm{}/;
The problem is this will show me all the files newer than a file and I need older than a file :\
and the
Code:
!-newer
gives me:
Code:
-bash: !-newer /home/ilia/scripts/DEL/have: event not found
Does anybody knows what can I do ?

EDIT:
Never mind, I dound IT
the command is
find <PATH> -type f -mtime <N>

thnx

Last edited by SpLaSh212; 02-23-2007 at 11:12 AM.
 
Old 02-23-2007, 11:58 AM   #5
otheus
LQ Newbie
 
Registered: Jun 2006
Location: Austria
Distribution: RHEL AS 4
Posts: 26

Rep: Reputation: 16
Quote:
Originally Posted by SpLaSh212
I need to write something that deletes all the files in the directory except the files that were created within this week.

I'm not sure how I can do this, I mean, my plan is to take the date the script is executed in the date +%F format and divide it to $day,$month,$year
My question is how I can delete a file based on his date ?

Does anyone have any idea ?
For deleting files before or after an arbitrary date, your -newer solution is pretty close. One can (in Linux) create a reference file using the touch -t <date>. You then find files that are not newer using this:
Code:
 touch -t 02190800
 find dir/path \! -newer reffile -exec rm -f "{}" ";"
With bash, make sure you get the backslash \ or it will do strange things to you.

But if you want to delete all files within a specific number of ''days'', you can use find's -mtime n feature, which will find files that were changed n days ago. Preceeding n with a + means before thist time, while preceeding it with a - will mean since that time. The GNU version accepts the parameter daystart in case you want the days to count from the start of today.
Code:
 find dir/path -mtime +7 -exec rm -f "{}" ";"
will remove files more than 7 days old.

Oh and do be careful of using find and rm together!!
 
Old 02-23-2007, 12:18 PM   #6
otheus
LQ Newbie
 
Registered: Jun 2006
Location: Austria
Distribution: RHEL AS 4
Posts: 26

Rep: Reputation: 16
Angry

Quote:
Originally Posted by SpLaSh212
Hello guys, I need some help ..
I'm new to shell scripting, and I have a several questions
so lets begin
emmm how can I dived my script into parts ?
I mean something like
main:
exit:

and how I can manipulate the interpeter to go there ?
something like "goto" command in dos ? :\
thnx alot
None of the UNIX shells I know make use of goto or anything like it. The closest thing that does this is sed but you don't want to go there.

But let's assume you are NOT yet addicted to csh or tcsh (always a good thing!). In that case, you will likely use the original Bourne shell (/bin/sh) or the Bourne Again shell (bash). (You might also be inclined to use zsh and ksh, and they are very similar.) Basically, these allow you to create shell functions. These are pretty powerful: they let you pass parameters, they return results, and they can even accept pipes. So using the examples you gave, here's some sample code:

Code:
#!/bin/sh

# invoke main function
main "$@"
# exit with return code of main
exit $?

#####
onerror() { 
  echo "Oh shucks, there was an error!" >&2 
  exit 1
}

do_stuff() {
  echo "Doing stuff"
}

do_stuff2() { 
  # send input from pipe to a file called stuff.txt
  cat >/tmp/stuff.txt

  # if we were passed a parameter, mv the stuff.txt file to the file specified by it ($1)
  test -n "$1" && mv /tmp/stuff.txt $1

  # so if stuff.txt doesn't exist, exit (the pipeline) with an error
  test -f /tmp/stuff.txt  || exit 1
}

main() {
  echo "Entering main function."
  echo "Argument list: " $*
  echo "Argument list: " "$@"

  trap onerror 15 2 1
  echo "Error handler installed."

  # pipe (send) the output of the function do_stuff into the input of function do_stuff2 
  do_stuff | do_stuff2 /tmp/file2

  # was there an error?
  if [ "$?" != 0 ]; then 
     echo 'pipeline failed! killing my self!'
     kill -15 $$
  fi
}
 
  


Reply



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 02:14 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 03:32 PM
Shell Scripting Help jester_69 Linux - General 15 11-05-2003 03:52 PM
shell scripting mindcry Linux - Software 2 11-26-2002 06:25 AM

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

All times are GMT -5. The time now is 02:22 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
Open Source Consulting | Domain Registration