LinuxQuestions.org
Help answer threads with 0 replies.
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 10-26-2012, 02:17 AM   #1
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
How to re-direct an error in script


I have a script, something like this:
Code:
#!/bin/bash
filelist="/tmp/fileslist"
month=$(date +%m)
....
....
monthslist="01 02 03 04 05 06... 12"
for mymonth in $monthlist
do

ls -la $HOME/logfile.$mymonth* | awk -F" " '{print $9}' > $filelist

    for file in `cat $filelist`
      do
      mv $file /path/to/directory
      echo "Movement done for $file"  
      done
done
Now suppose a file don't exist for some month pattern, let's say logfile.07 doesn't exist, then script is displaying an error of "File logfile.07: No such file or dir.." on my screen. I just want to redirect such error, so it diplay only movement done echo message, not the errors. How can I redirect such error while i am already redirecting output of ls -la... command.

Last edited by shivaa; 10-26-2012 at 02:48 AM.
 
Old 10-26-2012, 02:22 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Is this what you are looking for:
Code:
ls -la $HOME/logfile.$mymonth* 2>/dev/null | awk -F" " '{print $9}' > $/tmp/fileslist
The above redirects to /dev/null, you can also redirect to a file.
 
1 members found this post helpful.
Old 10-26-2012, 06:52 AM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Original Poster
Rep: Reputation: 286Reputation: 286Reputation: 286
Great Druuna! It worked!!
Cheers!!
 
Old 10-26-2012, 07:23 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
You're welcome

BTW: Can you put up the [SOLVED] tag.
first post -> Thread Tools -> Mark this thread as solved
 
Old 10-28-2012, 05:17 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
I have some comments on the above script:

1) It is not recommended to parse ls for data. If you need to extract the metadata of a file to test against, use stat.

2) Do not Read Lines With For! You should always use a while+read loop when reading lines of text input.

3) $(..) is highly recommended over `..`.


But actually, you seem to be going to a lot of roundabout trouble just to move a few files. Saving the output of ls -l to a file, then using awk to parse out the filenames again, and finally a for loop to move them one at a time. Seriously?

Is there any reason you can't simply do this?

Code:
echo 'Now moving the following files:'
printf '%s\n' $HOME/logfile.*

mv -t /path/to/directory $HOME/logfile.*
You can usually match the filenames you want with the proper globbing patterns. And the nice thing is you won't get any error messages unless there are no files that match at all (and even that can be controlled with the nullglob and failglob shell options).

If you need to be more specific and only match files with actual month extensions, you can use bash's extended globbing:

Code:
shopt -s extglob
mv -t /path/to/directory $HOME/logfile.@(0[1-9]|1[0-2])

Finally, if this is some kind of logfile maintenance script, have you looked into using logrotate?
 
1 members found this post helpful.
Old 10-29-2012, 01:13 AM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Original Poster
Rep: Reputation: 286Reputation: 286Reputation: 286
David, thanks for sharing such valuable info.
I am beginner in shell scripting, thus using simple commands, but knowledge is always limitless and your Wiki helped me a lot! Thanks agian.
 
Old 10-30-2012, 09:14 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Glad to help out, and I hope you didn't think I'm being too hard on you. It's natural for beginners to use "spaghetti logic" in their scripts, and I've been guilty of it myself. But I tend to be a bit blunt in my criticisms.

Just keep working at it, listen to advice, and follow the links I gave. That whole site is a treasure trove of advice. I particularly recommend working through their BashGuide.

One thing that particularly helps when posting here is to focus on explaining your final goal, as opposed to just the immediate problem at hand. There are often better ways to accomplish what you want.
 
1 members found this post helpful.
  


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
prelink configuration error :: fatal error: ac_nonexistent.h: No such file or direct jubitaneja Linux - Software 4 04-13-2012 09:55 AM
Detect bash script source vs. direct execution Jessard Programming 11 11-30-2010 07:43 AM
Direct Rending Error canuck_barlow Linux - Hardware 4 12-23-2004 02:28 AM
direct connect : dcgui-qt -> error jrembold Linux - Software 1 07-21-2004 05:42 PM
re-direct script GT I.N.C General 1 08-14-2002 02:09 PM


All times are GMT -5. The time now is 07:32 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